changeset 2750:f39efe6bebd7

Fix bug where no startup configuration was read. Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024884.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 07 Sep 2017 18:41:40 +0200
parents ed2aca3cd482
children f31a81430347
files agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java agent/core/src/main/java/com/redhat/thermostat/agent/internal/AgentApplication.java agent/core/src/test/java/com/redhat/thermostat/agent/config/AgentConfigsUtilsTest.java agent/core/src/test/java/com/redhat/thermostat/agent/internal/AgentApplicationTest.java
diffstat 4 files changed, 53 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java	Thu Sep 07 14:04:15 2017 -0400
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java	Thu Sep 07 18:41:40 2017 +0200
@@ -60,8 +60,10 @@
     }
     
     private static void readAndSetProperties(File systemConfigFile, File userConfigFile, AgentStartupConfiguration configuration)
-            throws InvalidConfigurationException
-    {
+            throws InvalidConfigurationException {
+        if (systemConfigFile == null && userConfigFile == null) {
+            throw new InvalidConfigurationException("No agent configuration files set!");
+        }
         Properties systemConfig = new Properties();
         if (systemConfigFile != null) {
             try {
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/internal/AgentApplication.java	Thu Sep 07 14:04:15 2017 -0400
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/internal/AgentApplication.java	Thu Sep 07 18:41:40 2017 +0200
@@ -67,6 +67,7 @@
 import com.redhat.thermostat.common.cli.CommandRegistryImpl;
 import com.redhat.thermostat.common.tools.ApplicationState;
 import com.redhat.thermostat.common.utils.LoggingUtils;
+import com.redhat.thermostat.shared.config.CommonPaths;
 import com.redhat.thermostat.shared.config.InvalidConfigurationException;
 import com.redhat.thermostat.storage.core.WriterID;
 import sun.misc.Signal;
@@ -107,7 +108,9 @@
     private AgentInfoDAO agentInfoDAO;
     @Reference
     private BackendInfoDAO backendInfoDAO;
-
+    @Reference
+    private CommonPaths commonPaths;
+    
     private CommandRegistry reg;
 
     private CountDownLatch shutdownLatch;
@@ -170,7 +173,7 @@
 
     @Override
     public void run(CommandContext ctx) throws CommandException {
-        configuration = configurationCreator.create();
+        configuration = configurationCreator.create(commonPaths);
 
         parseArguments(ctx.getArguments());
         if (!parser.isHelp()) {
@@ -258,7 +261,9 @@
     }
 
     static class ConfigurationCreator {
-        public AgentStartupConfiguration create() throws InvalidConfigurationException {
+        public AgentStartupConfiguration create(CommonPaths commonPaths) throws InvalidConfigurationException {
+            AgentConfigsUtils.setConfigFiles(commonPaths.getSystemAgentConfigurationFile(),
+                                             commonPaths.getUserAgentConfigurationFile());
             return AgentConfigsUtils.createAgentConfigs();
         }
     }
--- a/agent/core/src/test/java/com/redhat/thermostat/agent/config/AgentConfigsUtilsTest.java	Thu Sep 07 14:04:15 2017 -0400
+++ b/agent/core/src/test/java/com/redhat/thermostat/agent/config/AgentConfigsUtilsTest.java	Thu Sep 07 18:41:40 2017 +0200
@@ -72,6 +72,11 @@
         Assert.assertFalse(config.purge());
     }
     
+    @Test(expected = InvalidConfigurationException.class)
+    public void testNoConfiSet() throws InvalidConfigurationException {
+        AgentConfigsUtils.createAgentConfigs();
+    }
+    
     @Test
     public void testUserPurgeProp() throws InvalidConfigurationException, IOException {
         Properties sysProps = createSystemProperties();
--- a/agent/core/src/test/java/com/redhat/thermostat/agent/internal/AgentApplicationTest.java	Thu Sep 07 14:04:15 2017 -0400
+++ b/agent/core/src/test/java/com/redhat/thermostat/agent/internal/AgentApplicationTest.java	Thu Sep 07 18:41:40 2017 +0200
@@ -37,13 +37,20 @@
 package com.redhat.thermostat.agent.internal;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLDecoder;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -74,6 +81,7 @@
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.CommandContext;
 import com.redhat.thermostat.common.cli.CommandException;
+import com.redhat.thermostat.shared.config.CommonPaths;
 import com.redhat.thermostat.shared.config.InvalidConfigurationException;
 import com.redhat.thermostat.storage.core.WriterID;
 import com.redhat.thermostat.testutils.StubBundleContext;
@@ -86,6 +94,7 @@
     private ConfigurationCreator configCreator;
     private ExitStatus exitStatus;
     private WriterID writerId;
+    private CommonPaths commonPaths;
 
     @Before
     public void setUp() throws InvalidConfigurationException {
@@ -95,7 +104,8 @@
         AgentStartupConfiguration config = mock(AgentStartupConfiguration.class);
 
         configCreator = mock(ConfigurationCreator.class);
-        when(configCreator.create()).thenReturn(config);
+        commonPaths = mock(CommonPaths.class);
+        when(configCreator.create(eq(commonPaths))).thenReturn(config);
 
         AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class);
         context.registerService(AgentInfoDAO.class.getName(), agentInfoDAO, null);
@@ -139,6 +149,31 @@
             fail("Timeout expired!");
         }
     }
+    
+    @Test
+    public void verifyConfigCreator() {
+        ConfigurationCreator configCreator = new ConfigurationCreator();
+        File testFile = getTestFile("/test_agent.properties");
+        assertNotNull("Precondition", testFile);
+        when(commonPaths.getSystemAgentConfigurationFile()).thenReturn(testFile);
+        AgentStartupConfiguration config = configCreator.create(commonPaths);
+        assertTrue(config.isBasicAuthEnabled());
+    }
+
+    private File getTestFile(String string) {
+        URL fileUrl = getClass().getResource(string);
+        return new File(decodeFilePath(fileUrl));
+    }
+    
+    private static String decodeFilePath(URL url) {
+        try {
+            // Spaces are encoded as %20 in URLs. Use URLDecoder.decode() so
+            // as to handle cases like that.
+            return URLDecoder.decode(url.getFile(), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new AssertionError("UTF-8 not supported, huh?");
+        }
+    }
 
     private Bundle createBundle() {
         String qualifier = "201207241700";