diff options
author | emartin <ephraim.martin@est.tech> | 2019-02-12 17:12:08 +0000 |
---|---|---|
committer | emartin <ephraim.martin@est.tech> | 2019-02-12 17:12:08 +0000 |
commit | 85d315a40fa48255e1104a0c2d6c5ad984952c9c (patch) | |
tree | 8ba6342009b1020750e02f2dbbc863aaab720763 /src/test/java/org/onap | |
parent | a8e48cfb480f325f0d359428672cc7f5cc20d13e (diff) |
Fix mapper configuration to use CBS
Change-Id: I17bb9577ac2dd9267eeade948e5bf05cdadbac57
Issue-ID: DCAEGEN2-1186
Signed-off-by: emartin <ephraim.martin@est.tech>
Diffstat (limited to 'src/test/java/org/onap')
3 files changed, 114 insertions, 105 deletions
diff --git a/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java b/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java index b4a2870..f6aa2a8 100644 --- a/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java +++ b/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java @@ -18,8 +18,7 @@ * ============LICENSE_END=========================================================
*/
package org.onap.dcaegen2.pmmapper.config;
-
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
import java.io.BufferedReader;
@@ -27,21 +26,26 @@ import java.io.IOException; import java.io.InputStreamReader;
import java.net.UnknownHostException;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.dcaegen2.services.pmmapper.config.ConfigHandler;
import org.onap.dcaegen2.services.pmmapper.exceptions.CBSConfigException;
import org.onap.dcaegen2.services.pmmapper.exceptions.CBSServerError;
-import org.onap.dcaegen2.services.pmmapper.exceptions.ConsulServerError;
import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException;
import org.onap.dcaegen2.services.pmmapper.exceptions.MapperConfigException;
+import org.onap.dcaegen2.services.pmmapper.model.EnvironmentConfig;
import org.onap.dcaegen2.services.pmmapper.model.MapperConfig;
import org.onap.dcaegen2.services.pmmapper.utils.RequestSender;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
import com.google.gson.Gson;
@@ -49,134 +53,68 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender;
import utils.LoggingUtils;
-@ExtendWith(MockitoExtension.class)
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(EnvironmentConfig.class)
public class ConfigHandlerTests {
- private static String cbsConfig;
private static String validMapperConfig;
- private String consulURL = "http://my_consult_host:8500/v1/catalog/service/config-binding-service";
- private String cbsURL = "http://config-binding-service:10000/service_component/pm-mapper-service-name";
+ private static String HOSTNAME = "pm-mapper-service-name";
+ private static String CBS_HOST = "cbs_host";
+ private static int CBS_PORT = 10000;
private Gson gson = new Gson();
@Mock
private RequestSender sender;
- @BeforeAll()
- public static void beforeAll() throws IOException {
+ @BeforeClass()
+ public static void beforeClass() throws Exception {
validMapperConfig = getFileContents("valid_mapper_config.json");
- cbsConfig = getFileContents("valid_cbs_config.json");
}
- @BeforeEach
- public void beforeEach() throws Exception {
- System.setProperty("CONSUL_HOST", "my_consult_host");
- System.setProperty("CONSUL_PORT", "8500");
- System.setProperty("CONFIG_BINDING_SERVICE", "config-binding-service");
- System.setProperty("HOSTNAME", "hotstname");
- }
- @Test
- public void environmentConfig_missing_consulHost() throws EnvironmentConfigException {
- System.clearProperty("CONSUL_HOST");
- System.clearProperty("CONFIG_BINDING_SERVICE");
-
- Exception exception = assertThrows(EnvironmentConfigException.class, () -> {
- ConfigHandler configHandler = new ConfigHandler(sender);
- configHandler.getMapperConfig();
- });
-
- assertTrue(exception.getMessage()
- .contains("$CONSUL_HOST environment variable must be defined"));
+ @Before
+ public void before() throws Exception {
+ PowerMockito.mockStatic(EnvironmentConfig.class);
+ PowerMockito.when(EnvironmentConfig.getCBSHostName()).thenReturn(CBS_HOST);
+ PowerMockito.when(EnvironmentConfig.getCBSPort()).thenReturn(CBS_PORT);
+ PowerMockito.when(EnvironmentConfig.getServiceName()).thenReturn(HOSTNAME);
}
@Test
public void getMapperConfig_success() throws Exception {
ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigHandler.class);
- when(sender.send(anyString())).then(invocation -> {
- String url = (String) invocation.getArguments()[0];
- return url.equals(consulURL) ? cbsConfig : validMapperConfig;
- });
+ String validCbsUrl = "http://" + CBS_HOST + ":" + CBS_PORT +"/service_component/" + HOSTNAME;
+ when(sender.send(validCbsUrl)).thenReturn(validMapperConfig);
MapperConfig actualConfig = getMapperConfig();
MapperConfig expectedConfig = gson.fromJson(validMapperConfig, MapperConfig.class);
assertEquals(expectedConfig, actualConfig);
assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY");
- assertTrue(logAppender.list.get(1).getMessage().contains("Received ConfigBinding Service parameters"));
+ assertTrue(logAppender.list.get(1).getMessage().contains("Received pm-mapper configuration from ConfigBinding Service"));
assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT");
- assertTrue(logAppender.list.get(4).getMessage().contains("Received pm-mapper configuration from ConfigBinding Service"));
logAppender.stop();
}
@Test
- public void configbinding_config_format_error() throws Exception {
- when(sender.send(consulURL)).then((invocationMock) -> {
- return "some string that is not cbs config";
- });
-
- assertThrows(CBSConfigException.class, this::getMapperConfig);
- }
-
- @Test
- public void consul_host_is_unknown() throws Exception {
- when(sender.send(consulURL)).thenThrow(new UnknownHostException());
- assertThrows(ConsulServerError.class, this::getMapperConfig);
- }
-
- @Test
- public void configbinding_host_is_unknown() throws Exception {
- when(sender.send(anyString())).then(invocation -> {
- boolean isCBS = invocation.getArguments()[0].equals(cbsURL);
- if (isCBS) {
- throw new UnknownHostException("unknown cbs");
- }
- return cbsConfig;
- });
-
+ public void configbinding_server_error() throws Exception {
+ when(sender.send(anyString())).thenThrow(CBSServerError.class);
assertThrows(CBSServerError.class, this::getMapperConfig);
}
@Test
- public void consul_port_invalid() throws Exception {
- System.setProperty("CONSUL_PORT", "19d93hjuji");
+ public void configbinding_server_host_missing() throws Exception {
+ PowerMockito.when(EnvironmentConfig.getCBSHostName()).thenThrow(EnvironmentConfigException.class);
assertThrows(EnvironmentConfigException.class, this::getMapperConfig);
}
@Test
- public void consul_server_error() throws Exception {
- when(sender.send(consulURL)).thenThrow(new ConsulServerError("consul server error", new Throwable()));
- assertThrows(ConsulServerError.class, this::getMapperConfig);
- }
-
- @Test
- public void configbinding_server_error() throws Exception {
- when(sender.send(anyString())).then(invocation -> {
- boolean isCBS = invocation.getArguments()[0].equals(cbsURL);
- if (isCBS) {
- throw new CBSServerError("unknown cbs", new Throwable());
- }
- return cbsConfig;
- });
-
- assertThrows(CBSServerError.class, this::getMapperConfig);
- }
-
- @Test
public void mapper_parse_invalid_json() throws Exception {
- when(sender.send(anyString())).then(invocation -> {
- String url = (String) invocation.getArguments()[0];
- return url.equals(consulURL) ? cbsConfig : "mapper config with incorrect format";
- });
-
+ when(sender.send(anyString())).thenReturn("mapper config with incorrect format");
assertThrows(MapperConfigException.class, this::getMapperConfig);
}
@Test
public void mapper_parse_valid_json_missing_attributes() throws Exception {
- when(sender.send(anyString())).then(invocation -> {
- String incompleteConfig = getFileContents("incomplete_mapper_config.json");
- String url = (String) invocation.getArguments()[0];
- return url.equals(consulURL) ? cbsConfig : incompleteConfig;
- });
-
+ when(sender.send(anyString())).thenReturn(getFileContents("incomplete_mapper_config.json"));
assertThrows(MapperConfigException.class, this::getMapperConfig);
}
diff --git a/src/test/java/org/onap/dcaegen2/pmmapper/config/EnvironmentConfigTest.java b/src/test/java/org/onap/dcaegen2/pmmapper/config/EnvironmentConfigTest.java new file mode 100644 index 0000000..95a51f7 --- /dev/null +++ b/src/test/java/org/onap/dcaegen2/pmmapper/config/EnvironmentConfigTest.java @@ -0,0 +1,71 @@ +/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dcaegen2.pmmapper.config;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.Assert.assertEquals;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException;
+import org.onap.dcaegen2.services.pmmapper.model.EnvironmentConfig;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(EnvironmentConfig.class)
+public class EnvironmentConfigTest {
+
+ @Before
+ public void before() throws Exception {
+ PowerMockito.mockStatic(System.class);
+ }
+
+ @Test
+ public void environmentConfig_is_present_success() throws EnvironmentConfigException {
+ String CBS_HOST = "cbs_host";
+ PowerMockito.when(System.getenv(EnvironmentConfig.ENV_CBS_HOST_KEY)).thenReturn(CBS_HOST);
+ assertEquals(CBS_HOST,EnvironmentConfig.getCBSHostName() );
+ }
+
+ @Test
+ public void environmentConfig_host_not_present() throws EnvironmentConfigException {
+ PowerMockito.when(System.getenv(EnvironmentConfig.ENV_CBS_HOST_KEY)).thenCallRealMethod();
+ assertThrows(EnvironmentConfigException.class,EnvironmentConfig::getCBSHostName);
+ }
+
+ @Test
+ public void environmentConfig_hostname_present() throws EnvironmentConfigException {
+ PowerMockito.when(System.getenv(EnvironmentConfig.ENV_SERVICE_NAME_KEY)).thenCallRealMethod();
+ assertThrows(EnvironmentConfigException.class,EnvironmentConfig::getCBSHostName);
+ }
+
+ @Test
+ public void environmentConfig_default_port_is_used() throws EnvironmentConfigException {
+ PowerMockito.when(System.getenv(EnvironmentConfig.ENV_CBS_PORT_KEY)).thenReturn(null);
+ assertEquals(Integer.valueOf(EnvironmentConfig.DEFAULT_CBS_PORT),EnvironmentConfig.getCBSPort());
+ }
+
+ @Test
+ public void environmentConfig_port_invalid() throws EnvironmentConfigException {
+ PowerMockito.when(System.getenv(EnvironmentConfig.ENV_CBS_PORT_KEY)).thenReturn("Invalid_port number");
+ assertThrows(EnvironmentConfigException.class,EnvironmentConfig::getCBSHostName);
+ }
+}
diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java index 3239e93..00dc040 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java @@ -57,7 +57,7 @@ import org.mockito.Mock; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.onap.dcaegen2.services.pmmapper.exceptions.TooManyTriesException; -import org.onap.dcaegen2.services.pmmapper.model.BusControllerConfig; +import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.dcaegen2.services.pmmapper.model.EventMetadata; import org.onap.dcaegen2.services.pmmapper.utils.HttpServerExchangeAdapter; @@ -86,8 +86,8 @@ public class DataRouterSubscriberTest { PowerMockito.mockStatic(Thread.class); URL subEndpoint = mock(URL.class); - BusControllerConfig config = new BusControllerConfig(); - config.setDataRouterSubscribeEndpoint(subEndpoint); + MapperConfig config = mock(MapperConfig.class); + when(config.getBusControllerSubscriptionUrl()).thenReturn(subEndpoint); HttpURLConnection huc = mock(HttpURLConnection.class, RETURNS_DEEP_STUBS); when(subEndpoint.openConnection()).thenReturn(huc); when(huc.getResponseCode()).thenReturn(300); @@ -97,8 +97,8 @@ public class DataRouterSubscriberTest { @Test public void testStartImmediateSuccess() throws IOException, TooManyTriesException, InterruptedException { URL subEndpoint = mock(URL.class); - BusControllerConfig config = new BusControllerConfig(); - config.setDataRouterSubscribeEndpoint(subEndpoint); + MapperConfig config = mock(MapperConfig.class); + when(config.getBusControllerSubscriptionUrl()).thenReturn(subEndpoint); HttpURLConnection huc = mock(HttpURLConnection.class, RETURNS_DEEP_STUBS); when(subEndpoint.openConnection()).thenReturn(huc); when(huc.getResponseCode()).thenReturn(200); @@ -111,8 +111,8 @@ public class DataRouterSubscriberTest { PowerMockito.mockStatic(Thread.class); URL subEndpoint = mock(URL.class); - BusControllerConfig config = new BusControllerConfig(); - config.setDataRouterSubscribeEndpoint(subEndpoint); + MapperConfig config = mock(MapperConfig.class); + when(config.getBusControllerSubscriptionUrl()).thenReturn(subEndpoint); HttpURLConnection huc = mock(HttpURLConnection.class, RETURNS_DEEP_STUBS); when(subEndpoint.openConnection()).thenReturn(huc); doAnswer(new Answer() { @@ -136,8 +136,8 @@ public class DataRouterSubscriberTest { PowerMockito.mockStatic(Thread.class); URL subEndpoint = mock(URL.class); - BusControllerConfig config = new BusControllerConfig(); - config.setDataRouterSubscribeEndpoint(subEndpoint); + MapperConfig config = mock(MapperConfig.class); + when(config.getBusControllerSubscriptionUrl()).thenReturn(subEndpoint); HttpURLConnection huc = mock(HttpURLConnection.class, RETURNS_DEEP_STUBS); when(subEndpoint.openConnection()).thenReturn(huc); doThrow(new IOException()).when(huc).getResponseCode(); |