Mercurial > hg > thermostat-ng > web-gateway
changeset 254:ad7eb6bf2611
ConfigurationFactory refactoring.
Reviewed-by: jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024937.html
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/gateway/common/core/config/ConfigurationFactory.java Thu Sep 07 19:09:58 2017 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/gateway/common/core/config/ConfigurationFactory.java Mon Sep 11 15:40:33 2017 +0200 @@ -44,10 +44,16 @@ */ public class ConfigurationFactory { + private static final GatewayHomeRetriever RETRIEVER = new GatewayHomeRetriever(); private final String gatewayHome; private final GlobalConfiguration globalConfig; - public ConfigurationFactory(String gatewayHome) { + public ConfigurationFactory() { + this(RETRIEVER.getGatewayHome()); + } + + // package-private for testing + ConfigurationFactory(String gatewayHome) { this.gatewayHome = gatewayHome; this.globalConfig = new GlobalConfiguration(gatewayHome); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/main/java/com/redhat/thermostat/gateway/common/core/config/GatewayHomeRetriever.java Mon Sep 11 15:40:33 2017 +0200 @@ -0,0 +1,75 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.gateway.common.core.config; + +import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; + +public class GatewayHomeRetriever { + + private final EnvHelper envHelper; + private final PropertyHelper propertyHelper; + + public GatewayHomeRetriever() { + this(new EnvHelper(), new PropertyHelper()); + } + + // package-private for testing + GatewayHomeRetriever(EnvHelper envHelper, PropertyHelper propHelper) { + this.envHelper = envHelper; + this.propertyHelper = propHelper; + } + + public String getGatewayHome() { + String gatewayHome = propertyHelper.getProperty(GlobalConstants.GATEWAY_HOME_ENV, envHelper.getEnv(GlobalConstants.GATEWAY_HOME_ENV)); + if (gatewayHome == null) { + throw new RuntimeException("THERMOSTAT_GATEWAY_HOME not defined!"); + } + return gatewayHome; + } + + static class EnvHelper { + String getEnv(String name) { + return System.getenv(name); + } + } + + static class PropertyHelper { + String getProperty(String name, String defaultVal) { + return System.getProperty(name, defaultVal); + } + } +}
--- a/common/core/src/test/java/com/redhat/thermostat/gateway/common/core/config/ConfigurationFactoryTest.java Thu Sep 07 19:09:58 2017 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/gateway/common/core/config/ConfigurationFactoryTest.java Mon Sep 11 15:40:33 2017 +0200 @@ -44,9 +44,6 @@ import org.junit.Before; import org.junit.Test; -import com.redhat.thermostat.gateway.common.core.config.Configuration; -import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; - public class ConfigurationFactoryTest extends ConfigurationTest { private ConfigurationFactory factory;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/test/java/com/redhat/thermostat/gateway/common/core/config/GatewayHomeRetrieverTest.java Mon Sep 11 15:40:33 2017 +0200 @@ -0,0 +1,88 @@ +/* + * Copyright 2012-2017 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.gateway.common.core.config; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.gateway.common.core.config.GatewayHomeRetriever.EnvHelper; +import com.redhat.thermostat.gateway.common.core.config.GatewayHomeRetriever.PropertyHelper; +import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; + +public class GatewayHomeRetrieverTest { + + private static final String TEST_GW_HOME_ENV = "path/to/test/gw/home"; + private static final String TEST_GW_HOME_PROP = "path/to/test/gw/home/prop"; + private GatewayHomeRetriever retriever; + private PropertyHelper propHelper; + + + @Before + public void setup() { + EnvHelper envHelper = mock(EnvHelper.class); + when(envHelper.getEnv(eq(GlobalConstants.GATEWAY_HOME_ENV))).thenReturn(TEST_GW_HOME_ENV); + propHelper = mock(PropertyHelper.class); + retriever = new GatewayHomeRetriever(envHelper, propHelper); + } + + @Test + public void canGetGatewayHomeFromEnv() { + when(propHelper.getProperty(eq(GlobalConstants.GATEWAY_HOME_ENV), eq(TEST_GW_HOME_ENV))).thenReturn(TEST_GW_HOME_ENV); + String actual = retriever.getGatewayHome(); + assertEquals(TEST_GW_HOME_ENV, actual); + } + + @Test + public void canGetGatewayHomeFromProperty() { + when(propHelper.getProperty(eq(GlobalConstants.GATEWAY_HOME_ENV), any(String.class))).thenReturn(TEST_GW_HOME_PROP); + String actual = retriever.getGatewayHome(); + assertEquals(TEST_GW_HOME_PROP, actual); + } + + @Test(expected = RuntimeException.class) + public void noGwHomeThrowsException() { + GatewayHomeRetriever noEnvAndProp = new GatewayHomeRetriever(mock(EnvHelper.class), mock(PropertyHelper.class)); + noEnvAndProp.getGatewayHome(); + } + +}
--- a/common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/servlet/StorageConnectionSettingListenerTest.java Thu Sep 07 19:09:58 2017 +0200 +++ b/common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/servlet/StorageConnectionSettingListenerTest.java Mon Sep 11 15:40:33 2017 +0200 @@ -46,6 +46,7 @@ import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; +import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContext; @@ -55,7 +56,6 @@ import org.junit.Test; import com.redhat.thermostat.gateway.common.core.config.Configuration; -import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; import com.redhat.thermostat.gateway.common.mongodb.ThermostatMongoStorage; import com.redhat.thermostat.gateway.common.mongodb.configuration.MongoConfiguration; @@ -72,7 +72,8 @@ when(evt.getServletContext()).thenReturn(ctxt); when(ctxt.getInitParameter(eq(GlobalConstants.GATEWAY_HOME_KEY))).thenReturn(getTestGatewayRoot()); when(ctxt.getAttribute(eq(GlobalConstants.GATEWAY_HOME_KEY))).thenReturn(getTestGatewayRoot()); - when(ctxt.getAttribute(eq(GlobalConstants.SERVICE_CONFIG_KEY))).thenReturn(getTestGatewayConfiguration()); + Configuration testConfig = getTestGatewayConfiguration(); + when(ctxt.getAttribute(eq(GlobalConstants.SERVICE_CONFIG_KEY))).thenReturn(testConfig); } @Test @@ -100,7 +101,15 @@ } private Configuration getTestGatewayConfiguration() { - return new ConfigurationFactory(getTestGatewayRoot()).createServiceConfiguration("foo-service"); + Map<String, Object> testConfig = new HashMap<>(); + testConfig.put("MONGO_URL", "mongodb://localhost:21793"); + testConfig.put("MONGO_DB", "foo"); + testConfig.put("MONGO_USERNAME", "foo-user"); + testConfig.put("MONGO_PASSWORD", "foo-password"); + testConfig.put("MONGO_SERVER_TIMEOUT", "3"); + Configuration config = mock(Configuration.class); + when(config.asMap()).thenReturn(testConfig); + return config; } private String decodeFilePath(URL url) {
--- a/common/mongodb/src/test/resources/gateway-root/etc/foo-service/service-config.properties Thu Sep 07 19:09:58 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -MONGO_URL=mongodb://localhost:21793 -MONGO_DB=foo -MONGO_USERNAME=foo-user -MONGO_PASSWORD=foo-password -MONGO_SERVER_TIMEOUT=3 \ No newline at end of file
--- a/server/src/main/java/com/redhat/thermostat/gateway/server/CoreServerBuilder.java Thu Sep 07 19:09:58 2017 +0200 +++ b/server/src/main/java/com/redhat/thermostat/gateway/server/CoreServerBuilder.java Mon Sep 11 15:40:33 2017 +0200 @@ -58,6 +58,7 @@ import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; import com.redhat.thermostat.gateway.common.core.config.Configuration; +import com.redhat.thermostat.gateway.common.core.config.GatewayHomeRetriever; import com.redhat.thermostat.gateway.common.core.config.GlobalConfiguration; import com.redhat.thermostat.gateway.common.core.config.GlobalConfiguration.ConfigurationKey; import com.redhat.thermostat.gateway.server.apidoc.SwaggerUiHandler; @@ -71,18 +72,24 @@ private final SwaggerUiHandler swaggerHandler; private final StaticAssetsHandler staticAssetsHandler; private final Server server = new Server(); + private final GatewayHomeRetriever retriever; private CoreServiceBuilder coreServiceBuilder; private Configuration serverConfig; - private String gatewayHome; public CoreServerBuilder() { - this(new SwaggerUiHandler(), new StaticAssetsHandler()); + this(new SwaggerUiHandler(), new StaticAssetsHandler(), new GatewayHomeRetriever()); + } + + // package-private for test-overrides + CoreServerBuilder(SwaggerUiHandler swaggerHandler, StaticAssetsHandler staticHandler, GatewayHomeRetriever retriever) { + this.staticAssetsHandler = staticHandler; + this.swaggerHandler = swaggerHandler; + this.retriever = retriever; } // package-private for test-overrides CoreServerBuilder(SwaggerUiHandler swaggerHandler, StaticAssetsHandler staticHandler) { - this.staticAssetsHandler = staticHandler; - this.swaggerHandler = swaggerHandler; + this(swaggerHandler, staticHandler, new GatewayHomeRetriever()); } @@ -97,11 +104,6 @@ return this; } - public CoreServerBuilder setGatewayHome(String gatewayHome) { - this.gatewayHome = gatewayHome; - return this; - } - public Server build() { setupHandler(); setupConnector(); @@ -163,6 +165,7 @@ } private Connector getHttpsConnector(String listenAddress, int listenPort, Map<String, Object> serverConfigMap) { + String gatewayHome = retriever.getGatewayHome(); String keystoreCandidate = (String)serverConfigMap.get((GlobalConfiguration.ConfigurationKey.KEYSTORE_FILE.name())); Path keystoreFile = Paths.get(keystoreCandidate); if (!keystoreFile.isAbsolute()) {
--- a/server/src/main/java/com/redhat/thermostat/gateway/server/Start.java Thu Sep 07 19:09:58 2017 +0200 +++ b/server/src/main/java/com/redhat/thermostat/gateway/server/Start.java Mon Sep 11 15:40:33 2017 +0200 @@ -41,7 +41,6 @@ import com.redhat.thermostat.gateway.common.core.config.Configuration; import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; -import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; import com.redhat.thermostat.gateway.server.services.CoreServiceBuilder; import com.redhat.thermostat.gateway.server.services.CoreServiceBuilderFactory; import com.redhat.thermostat.gateway.server.services.CoreServiceBuilderFactory.CoreServiceType; @@ -59,16 +58,10 @@ } public void run() { - String gatewayHome = System.getProperty(GlobalConstants.GATEWAY_HOME_ENV, System.getenv(GlobalConstants.GATEWAY_HOME_ENV)); - if (gatewayHome == null) { - throw new RuntimeException("Environment variable THERMOSTAT_GATEWAY_HOME not defined!"); - } - - ConfigurationFactory factory = new ConfigurationFactory(gatewayHome); + ConfigurationFactory factory = new ConfigurationFactory(); CoreServerBuilder serverBuilder = new CoreServerBuilder(); setServerConfig(serverBuilder, factory); setServiceBuilder(serverBuilder, factory); - serverBuilder.setGatewayHome(gatewayHome); server = serverBuilder.build(); if (listener != null) {
--- a/server/src/test/java/com/redhat/thermostat/gateway/server/CoreServerBuilderTest.java Thu Sep 07 19:09:58 2017 +0200 +++ b/server/src/test/java/com/redhat/thermostat/gateway/server/CoreServerBuilderTest.java Mon Sep 11 15:40:33 2017 +0200 @@ -60,6 +60,7 @@ import org.junit.Test; import com.redhat.thermostat.gateway.common.core.config.Configuration; +import com.redhat.thermostat.gateway.common.core.config.GatewayHomeRetriever; import com.redhat.thermostat.gateway.common.core.config.GlobalConfiguration; import com.redhat.thermostat.gateway.server.apidoc.SwaggerUiHandler; import com.redhat.thermostat.gateway.server.services.CoreService; @@ -175,10 +176,11 @@ configMap.put(GlobalConfiguration.ConfigurationKey.WITH_TLS.name(), Boolean.TRUE.toString()); configMap.put(GlobalConfiguration.ConfigurationKey.KEYSTORE_FILE.name(), "test_me.jks"); Configuration configuration = getMockConfiguration(configMap); - CoreServerBuilder builder = new CoreServerBuilder(); + GatewayHomeRetriever homeRetriever = mock(GatewayHomeRetriever.class); + when(homeRetriever.getGatewayHome()).thenReturn(getTestRoot("/test_gw_home")); + CoreServerBuilder builder = new CoreServerBuilder(new SwaggerUiHandler(), new StaticAssetsHandler(), homeRetriever); builder.setServerConfiguration(configuration); builder.setServiceBuilder(serviceBuilder); - builder.setGatewayHome(getTestRoot("/test_gw_home")); Server server = builder.build(); Connector[] connectors = server.getConnectors();
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java Thu Sep 07 19:09:58 2017 +0200 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/tests/integration/IntegrationTest.java Mon Sep 11 15:40:33 2017 +0200 @@ -53,10 +53,9 @@ import com.redhat.thermostat.gateway.common.core.config.Configuration; import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; +import com.redhat.thermostat.gateway.common.core.config.GatewayHomeRetriever; import com.redhat.thermostat.gateway.common.core.config.GlobalConfiguration; -import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; import com.redhat.thermostat.gateway.server.Start; -import com.redhat.thermostat.gateway.tests.utils.MongodTestUtil; public class IntegrationTest { private static final ConfigurationFactory factory; @@ -67,12 +66,9 @@ protected static final Path distributionImage; static { - final String distDir = System.getProperty(GlobalConstants.GATEWAY_HOME_ENV, System.getenv(GlobalConstants.GATEWAY_HOME_ENV)); - distributionImage = distDir != null ? Paths.get(distDir) : null; - if (distributionImage == null) { - throw new RuntimeException("Environment variable THERMOSTAT_GATEWAY_HOME not defined!"); - } - factory = new ConfigurationFactory(distDir); + String distDir = new GatewayHomeRetriever().getGatewayHome(); + distributionImage = Paths.get(distDir); + factory = new ConfigurationFactory(); String scheme; if (isTLSEnabled()) { scheme = "https";