changeset 1947:2c8adb63d39a

Make setup end early and inform user if thermostat is already configured Running cli setup (both -s and -c) will print a message to console informing the user that thermostat is already configured and then quit. The gui setup will show the same message but in a dialog window. PR3045 Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-June/019794.html
author Anirudhan Mukundan <amukunda@redhat.com>
date Fri, 24 Jun 2016 12:17:11 -0400
parents f2baa291edde
children 131195abd5fc
files setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupCommand.java setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupWindow.java setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/StructureInformation.java setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/ThermostatSetup.java setup/command/src/main/java/com/redhat/thermostat/setup/command/locale/LocaleResources.java setup/command/src/main/resources/com/redhat/thermostat/setup/locale/strings.properties setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/SetupCommandTest.java setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/model/StructureInformationTest.java setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/model/ThermostatSetupTest.java
diffstat 9 files changed, 138 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupCommand.java	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupCommand.java	Fri Jun 24 12:17:11 2016 -0400
@@ -39,6 +39,7 @@
 import java.awt.EventQueue;
 import java.awt.GraphicsEnvironment;
 import java.io.IOException;
+import java.io.PrintStream;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -80,9 +81,11 @@
     private Launcher launcher;
     private Keyring keyring;
     private String[] origArgsList;
+    private PrintStream out;
 
     @Override
     public void run(CommandContext ctx) throws CommandException {
+        out = ctx.getConsole().getOutput();
         Arguments args = ctx.getArguments();
         if (args.hasArgument(ORIG_CMD_ARGUMENT_NAME)) {
             String origArgs = args.getArgument(ORIG_CMD_ARGUMENT_NAME);
@@ -132,6 +135,16 @@
 
     // package-private for testing
     void runCLISetup(ThermostatSetup setup, Console console) throws CommandException {
+        try {
+            if (setup.isThermostatConfigured()) {
+                out.println(t.localize(LocaleResources.THERMOSTAT_ALREADY_CONFIGURED_TITLE).getContents());
+                out.println(t.localize(LocaleResources.THERMOSTAT_ALREADY_CONFIGURED_MESSAGE,
+                    paths.getUserThermostatHome().getCanonicalPath()).getContents());
+                return;
+            }
+        } catch (IOException e) {
+            throw new CommandException(t.localize(LocaleResources.SETUP_FAILED), e);
+        }
         CLISetup cliSetup = new CLISetup(setup, console);
         cliSetup.run();
     }
@@ -139,6 +152,12 @@
     // package-private for testing
     void runSilentSetup(ThermostatSetup setup) throws CommandException {
         try {
+            if (setup.isThermostatConfigured()) {
+                out.println(t.localize(LocaleResources.THERMOSTAT_ALREADY_CONFIGURED_TITLE).getContents());
+                out.println(t.localize(LocaleResources.THERMOSTAT_ALREADY_CONFIGURED_MESSAGE,
+                    paths.getUserThermostatHome().getCanonicalPath()).getContents());
+                return;
+            }
             new ThermostatQuickSetup(setup).run();
         } catch (IOException e) {
             throw new CommandException(t.localize(LocaleResources.SETUP_FAILED), e);
@@ -205,7 +224,7 @@
 
     // package-private for testing
     void createMainWindowAndRun(ThermostatSetup setup) throws CommandException {
-        mainWindow = new SetupWindow(setup);
+        mainWindow = new SetupWindow(setup, paths);
         mainWindow.run();
     }
 
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupWindow.java	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupWindow.java	Fri Jun 24 12:17:11 2016 -0400
@@ -48,6 +48,7 @@
 import java.awt.event.ActionListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
+import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
@@ -85,6 +86,7 @@
 import com.redhat.thermostat.setup.command.internal.model.CredentialGenerator;
 import com.redhat.thermostat.setup.command.internal.model.ThermostatQuickSetup;
 import com.redhat.thermostat.setup.command.internal.model.ThermostatSetup;
+import com.redhat.thermostat.shared.config.CommonPaths;
 import com.redhat.thermostat.setup.command.locale.LocaleResources;
 import com.redhat.thermostat.shared.locale.Translate;
 
@@ -109,6 +111,7 @@
     private boolean setupCancelled = false;
     private final ThermostatSetup thermostatSetup;
     private SwingWorker<IOException, Void> finishAction;
+    private final File userThermostatHome;
 
     private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer();
     private static final Logger logger = LoggingUtils.getLogger(SetupWindow.class);
@@ -117,12 +120,22 @@
     private static final int FRAME_HEIGHT = 400;
     private static final int FRAME_LARGE_HEIGHT = 600;
 
-    public SetupWindow(ThermostatSetup thermostatSetup) {
+    public SetupWindow(ThermostatSetup thermostatSetup, CommonPaths paths) {
         this.thermostatSetup = thermostatSetup;
         this.shutdown = new CountDownLatch(1);
+        this.userThermostatHome = paths.getUserThermostatHome();
     }
 
     public void run() throws CommandException {
+        try {
+            if (thermostatSetup.isThermostatConfigured()) {
+                showThermostatAlreadyConfiguredDialog();
+                return;
+            }
+        } catch (IOException e) {
+            throw new CommandException(translator.localize(LocaleResources.SETUP_FAILED), e);
+        }
+
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
@@ -394,6 +407,26 @@
         shutdown.countDown();
     }
 
+    private void showThermostatAlreadyConfiguredDialog() throws IOException {
+        final String userThermostatHomePath = userThermostatHome.getCanonicalPath();
+        try {
+            doSynchronouslyOnEdt(new Runnable() {
+                @Override
+                public void run() {
+                    JOptionPane.showMessageDialog(
+                        null,
+                        translator.localize(LocaleResources.THERMOSTAT_ALREADY_CONFIGURED_MESSAGE,
+                            userThermostatHomePath).getContents(),
+                        translator.localize(LocaleResources.THERMOSTAT_ALREADY_CONFIGURED_TITLE).getContents(),
+                        JOptionPane.ERROR_MESSAGE);
+                }
+            });
+        } catch (InvocationTargetException | InterruptedException e) {
+            logger.log(Level.WARNING, "Error while waiting for user to dismiss dialog", e);
+        }
+    }
+
+
     private abstract class SetupSwingWorker extends SwingWorker<IOException, Void> {
         @Override
         public IOException doInBackground() {
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/StructureInformation.java	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/StructureInformation.java	Fri Jun 24 12:17:11 2016 -0400
@@ -45,9 +45,11 @@
 class StructureInformation {
     
     private final String webApp;
+    private final String userSetupCompleteStamp;
     
     StructureInformation(CommonPaths paths) {
         webApp = paths.getSystemThermostatHome() + "/webapp";
+        userSetupCompleteStamp = paths.getUserSetupCompleteStampFile().toString();
     }
 
     boolean isWebAppInstalled() {
@@ -58,4 +60,9 @@
             return false;
         }
     }
+
+    boolean isThermostatConfigured() {
+        Path userSetupCompleteStampPath = Paths.get(userSetupCompleteStamp);
+        return Files.exists(userSetupCompleteStampPath);
+    }
 }
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/ThermostatSetup.java	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/ThermostatSetup.java	Fri Jun 24 12:17:11 2016 -0400
@@ -127,6 +127,10 @@
         return structureInfo.isWebAppInstalled();
     }
 
+    public boolean isThermostatConfigured() {
+        return structureInfo.isThermostatConfigured();
+    }
+
     public String determineReasonFromException(Throwable e) {
         if (e.getCause() instanceof MongodbUserSetup.StorageAlreadyRunningException) {
             return translator.localize(LocaleResources.STORAGE_RUNNING).getContents();
--- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/locale/LocaleResources.java	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/locale/LocaleResources.java	Fri Jun 24 12:17:11 2016 -0400
@@ -106,6 +106,8 @@
     SHOW_LESS_ERROR_INFO,
     STEPS_TO_RESOLVE_ERROR_LABEL_TEXT,
     SETUP_COMPLETE_NOTE_CREDENTIALS_TEXT,
+    THERMOSTAT_ALREADY_CONFIGURED_TITLE,
+    THERMOSTAT_ALREADY_CONFIGURED_MESSAGE,
     ;
 
     static final String RESOURCE_BUNDLE =
--- a/setup/command/src/main/resources/com/redhat/thermostat/setup/locale/strings.properties	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/main/resources/com/redhat/thermostat/setup/locale/strings.properties	Fri Jun 24 12:17:11 2016 -0400
@@ -168,3 +168,7 @@
 SETUP_COMPLETE_NOTE_CREDENTIALS_TEXT=<html>Please take note of the credentials listed here \
   as they will be required in order to use certain components of Thermostat. \
   You can hover your mouse over the info icons for more information.</html>
+THERMOSTAT_ALREADY_CONFIGURED_TITLE=Thermostat is already configured.
+THERMOSTAT_ALREADY_CONFIGURED_MESSAGE=If you would like to reconfigure Thermostat please remove the USER_THERMOSTAT_HOME\n\
+  directory, ''{0}'', and run setup again. WARNING: Please be aware\n\
+  that removing the USER_THERMOSTAT_HOME directory will result in data loss.
\ No newline at end of file
--- a/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/SetupCommandTest.java	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/SetupCommandTest.java	Fri Jun 24 12:17:11 2016 -0400
@@ -51,6 +51,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
 import java.io.PrintStream;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
@@ -129,6 +130,11 @@
             void createMainWindowAndRun(ThermostatSetup setup) {
                 //do nothing
             }
+
+            @Override
+            ThermostatSetup createSetup() {
+                return thermostatSetup;
+            }
         };
 
         setServices();
@@ -150,6 +156,11 @@
             void createMainWindowAndRun(ThermostatSetup setup) {
                 isSet[0] = true;
             }
+
+            @Override
+            ThermostatSetup createSetup() {
+                return thermostatSetup;
+            }
         };
 
         setServices();
@@ -328,7 +339,6 @@
     private void testExitStatus(Arguments setupArgs, int exitVal) {
         setServices();
 
-        CommandContext ctxt = mock(CommandContext.class);
         when(ctxt.getArguments()).thenReturn(setupArgs);
 
         try {
@@ -340,6 +350,37 @@
     }
 
     @Test
+    public void testSilentSetupAlreadyConfigured() throws CommandException, IOException {
+        Arguments args = mock(Arguments.class);
+        when(args.hasArgument("silent")).thenReturn(true);
+        testSetupAlreadyConfigured(args);
+    }
+
+    @Test
+    public void testCliSetupAlreadyConfigured() throws CommandException, IOException {
+        Arguments args = mock(Arguments.class);
+        when(args.hasArgument("nonGui")).thenReturn(true);
+        testSetupAlreadyConfigured(args);
+    }
+
+    private void testSetupAlreadyConfigured(Arguments args) throws CommandException, IOException {
+        cmd = createSetupCommand();
+        setServices();
+
+        when(ctxt.getArguments()).thenReturn(args);
+        when(thermostatSetup.isThermostatConfigured()).thenReturn(true);
+        File userHome = mock(File.class);
+        when(userHome.getCanonicalPath()).thenReturn("user-home");
+        when(paths.getUserThermostatHome()).thenReturn(userHome);
+
+        cmd.run(ctxt);
+
+        String output = new String(outputBaos.toByteArray());
+        assertTrue(output.contains("Thermostat is already configured."));
+        verify(exitStatus).setExitStatus(ExitStatus.EXIT_SUCCESS);
+    }
+
+    @Test
     public void verifyOriginalCommandRunsAfterSetup() throws CommandException {
         doTestOriginalCmdRunsAfterSetup("web-storage-service", new String[] {
            "web-storage-service"     
@@ -359,7 +400,6 @@
         setServices();
         
         Arguments args = mock(Arguments.class);
-        CommandContext ctxt = mock(CommandContext.class);
         when(ctxt.getArguments()).thenReturn(args);
         when(args.hasArgument("origArgs")).thenReturn(true);
         when(args.getArgument("origArgs")).thenReturn("setup");
@@ -433,7 +473,6 @@
         setServices();
 
         Arguments args = mock(Arguments.class);
-        CommandContext ctxt = mock(CommandContext.class);
         when(ctxt.getArguments()).thenReturn(args);
         when(args.hasArgument("origArgs")).thenReturn(true);
         when(args.getArgument("origArgs")).thenReturn("local");
@@ -455,7 +494,6 @@
         setServices();
         
         Arguments args = mock(Arguments.class);
-        CommandContext ctxt = mock(CommandContext.class);
         when(ctxt.getArguments()).thenReturn(args);
         when(args.hasArgument("origArgs")).thenReturn(true);
         when(args.getArgument("origArgs")).thenReturn(origArgs);
--- a/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/model/StructureInformationTest.java	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/model/StructureInformationTest.java	Fri Jun 24 12:17:11 2016 -0400
@@ -55,6 +55,7 @@
     
     private Path testRoot;
     private Path thermostatSysHome;
+    private Path userSetupCompleteStamp;
     private StructureInformation structureInfo;
     
     @Before
@@ -62,9 +63,11 @@
         testRoot = TestRootHelper.createTestRootDirectory(getClass().getName());
 
         thermostatSysHome = testRoot.resolve("system");
+        userSetupCompleteStamp = testRoot.resolve("setup-complete.stamp");
         Files.createDirectory(thermostatSysHome);
         CommonPaths mockPaths = mock(CommonPaths.class);
         when(mockPaths.getSystemThermostatHome()).thenReturn(thermostatSysHome.toFile());
+        when(mockPaths.getUserSetupCompleteStampFile()).thenReturn(userSetupCompleteStamp.toFile());
         structureInfo = new StructureInformation(mockPaths);
     }
     
@@ -86,4 +89,17 @@
         //a THERMOSTAT_SYS_HOME/webapp directory
         assertFalse(structureInfo.isWebAppInstalled());
     }
+
+    @Test
+    public void testThermostatConfiguredSuccess() throws IOException {
+        Files.createFile(userSetupCompleteStamp);
+        assertTrue(structureInfo.isThermostatConfigured());
+    }
+
+    @Test
+    public void testThermostatConfiguredFail() throws IOException {
+        //Call isThermostatConfigured() without creating
+        //a setup-complete.stamp file
+        assertFalse(structureInfo.isThermostatConfigured());
+    }
 }
--- a/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/model/ThermostatSetupTest.java	Fri Jun 24 12:15:09 2016 -0400
+++ b/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/model/ThermostatSetupTest.java	Fri Jun 24 12:17:11 2016 -0400
@@ -75,6 +75,15 @@
         assertTrue(setup.isWebAppInstalled());
         verify(structureInfo).isWebAppInstalled();
     }
+
+    @Test
+    public void testIsThermostatConfiguredDelegates() {
+        StructureInformation structureInfo = mock(StructureInformation.class);
+        ThermostatSetup setup = new ThermostatSetup(userSetup, mongoUserSetup, structureInfo, mock(AuthFileWriter.class), mock(KeyringWriter.class));
+        when(structureInfo.isThermostatConfigured()).thenReturn(true);
+        assertTrue(setup.isThermostatConfigured());
+        verify(structureInfo).isThermostatConfigured();
+    }
     
     @Test
     public void testCreateAgentUser() {