Additional tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/265562af Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/265562af Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/265562af Branch: refs/heads/master Commit: 265562af9942038d5bd29a6a1c5512161d1610ea Parents: 13024d0 Author: sblackmon <[email protected]> Authored: Mon Nov 17 14:16:57 2014 -0600 Committer: sblackmon <[email protected]> Committed: Mon Nov 17 14:16:57 2014 -0600 ---------------------------------------------------------------------- streams-config/pom.xml | 12 ++++ .../streams/config/ComponentConfigurator.java | 32 ++++++++- .../streams/config/StreamsConfigurator.java | 4 ++ .../config/test/ComponentConfiguratorTest.java | 68 +++++++++++++++++++- 4 files changed, 111 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/265562af/streams-config/pom.xml ---------------------------------------------------------------------- diff --git a/streams-config/pom.xml b/streams-config/pom.xml index 63a6857..bfc877a 100644 --- a/streams-config/pom.xml +++ b/streams-config/pom.xml @@ -63,6 +63,18 @@ <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-module-junit4</artifactId> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito</artifactId> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/265562af/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java ---------------------------------------------------------------------- diff --git a/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java b/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java index 0056a4a..7e3451d 100644 --- a/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java +++ b/streams-config/src/main/java/org/apache/streams/config/ComponentConfigurator.java @@ -1,3 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.streams.config; import com.fasterxml.jackson.databind.ObjectMapper; @@ -10,7 +28,14 @@ import org.slf4j.LoggerFactory; import java.io.Serializable; /** - * Created by sblackmon on 11/11/14. + * ComponentConfigurator supplies serializable configuration beans derived from a specified typesafe path or object. + * + * Typically a component will select a 'default' typesafe path to be used if no other path or object is provided. + * + * For example, streams-persist-elasticsearch will use 'elasticsearch' by default, but an implementation + * such as github.com/w2ogroup/elasticsearch-reindex can resolve a reader from elasticsearch.source + * and a writer from elasticsearch.destination + * */ public class ComponentConfigurator<T extends Serializable> { @@ -38,10 +63,11 @@ public class ComponentConfigurator<T extends Serializable> { } public T detectConfiguration(String subConfig) { - return detectConfiguration( StreamsConfigurator.config.getString(subConfig)); + Config streamsConfig = StreamsConfigurator.config; + return detectConfiguration( streamsConfig.getConfig(subConfig)); } public T detectConfiguration(Config typesafeConfig, String subConfig) { - return detectConfiguration( typesafeConfig.getString(subConfig)); + return detectConfiguration( typesafeConfig.getConfig(subConfig)); } } http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/265562af/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java ---------------------------------------------------------------------- diff --git a/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java b/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java index 34e3844..4a69456 100644 --- a/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java +++ b/streams-config/src/main/java/org/apache/streams/config/StreamsConfigurator.java @@ -46,6 +46,10 @@ public class StreamsConfigurator { */ public static Config config = ConfigFactory.load(); + public static Config getConfig() { + return config; + } + public static StreamsConfiguration detectConfiguration() { return detectConfiguration(config); } http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/265562af/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java ---------------------------------------------------------------------- diff --git a/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java b/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java index e0acd6a..5b5c216 100644 --- a/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java +++ b/streams-config/src/test/java/org/apache/streams/config/test/ComponentConfiguratorTest.java @@ -3,11 +3,14 @@ package org.apache.streams.config.test; import com.fasterxml.jackson.databind.ObjectMapper; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigRenderOptions; import junit.framework.Assert; import org.apache.streams.config.ComponentConfiguration; import org.apache.streams.config.ComponentConfigurator; import org.apache.streams.config.StreamsConfigurator; import org.junit.Test; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; /** * Created by sblackmon on 10/20/14. @@ -17,7 +20,7 @@ public class ComponentConfiguratorTest { private final static ObjectMapper mapper = new ObjectMapper(); @Test - public void testDetectConfiguration() throws Exception { + public void testDetectDefaults() throws Exception { Config config = ConfigFactory.load("componentTest"); @@ -34,4 +37,65 @@ public class ComponentConfiguratorTest { Assert.assertEquals(configuredPojo,defaultPojo); } -} + + @Test + public void testDetectConfigurationConfig() throws Exception { + + Config config = ConfigFactory.load("componentTest").getConfig("configuredComponent"); + + ComponentConfigurator<ComponentConfiguration> configurator = new ComponentConfigurator<>(ComponentConfiguration.class); + + ComponentConfiguration testPojo = mapper.readValue(config.root().render(ConfigRenderOptions.concise()), ComponentConfiguration.class); + + assert(testPojo != null); + + ComponentConfiguration configuredPojo = configurator.detectConfiguration(config); + + assert(configuredPojo != null); + + Assert.assertEquals(configuredPojo,testPojo); + + } + + @Test + public void testDetectConfigurationString() throws Exception { + + Config config = ConfigFactory.load("componentTest"); + + StreamsConfigurator mockStreamsConfigurator = Mockito.mock(StreamsConfigurator.class); + + PowerMockito.when(mockStreamsConfigurator.getConfig()) + .thenReturn(config); + + ComponentConfigurator<ComponentConfiguration> configurator = new ComponentConfigurator<>(ComponentConfiguration.class); + + ComponentConfiguration testPojo = mapper.readValue(config.root().get("configuredComponent").render(ConfigRenderOptions.concise()), ComponentConfiguration.class); + + assert(testPojo != null); + + ComponentConfiguration configuredPojo = configurator.detectConfiguration("configuredComponent"); + + assert(configuredPojo != null); + + Assert.assertEquals(configuredPojo,testPojo); + } + + @Test + public void testDetectConfigurationConfigString() throws Exception { + + Config config = ConfigFactory.load("componentTest"); + + ComponentConfigurator<ComponentConfiguration> configurator = new ComponentConfigurator<>(ComponentConfiguration.class); + + ComponentConfiguration testPojo = mapper.readValue(config.root().get("configuredComponent").render(ConfigRenderOptions.concise()), ComponentConfiguration.class); + + + assert(testPojo != null); + + ComponentConfiguration configuredPojo = configurator.detectConfiguration(config, "configuredComponent"); + + assert(configuredPojo != null); + + Assert.assertEquals(configuredPojo,testPojo); + } +} \ No newline at end of file
