diff options
Diffstat (limited to 'src/test')
6 files changed, 376 insertions, 1 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 new file mode 100644 index 0000000..0c3fb84 --- /dev/null +++ b/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java @@ -0,0 +1,192 @@ +/*-
+ * ============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.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.when;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.UnknownHostException;
+
+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.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.MapperConfig;
+import org.onap.dcaegen2.services.pmmapper.utils.RequestSender;
+
+import com.google.gson.Gson;
+
+@ExtendWith(MockitoExtension.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 Gson gson = new Gson();
+ @Mock
+ private RequestSender sender;
+
+ @BeforeAll()
+ public static void beforeAll() throws IOException {
+ 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"));
+ }
+
+ @Test
+ public void getMapperConfig_success() throws Exception {
+ when(sender.send(anyString())).then(invocation -> {
+ String url = (String) invocation.getArguments()[0];
+ return url.equals(consulURL) ? cbsConfig : validMapperConfig;
+ });
+
+ MapperConfig actualConfig = getMapperConfig();
+ MapperConfig expectedConfig = gson.fromJson(validMapperConfig, MapperConfig.class);
+
+ assertEquals(expectedConfig, actualConfig);
+ }
+
+ @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;
+ });
+
+ assertThrows(CBSServerError.class, this::getMapperConfig);
+ }
+
+ @Test
+ public void consul_port_invalid() throws Exception {
+ System.setProperty("CONSUL_PORT", "19d93hjuji");
+ 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";
+ });
+
+ 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;
+ });
+
+ assertThrows(MapperConfigException.class, this::getMapperConfig);
+ }
+
+ private MapperConfig getMapperConfig()
+ throws UnknownHostException, EnvironmentConfigException, CBSConfigException, Exception {
+ return new ConfigHandler(sender).getMapperConfig();
+ }
+
+ private static String getFileContents(String fileName) throws IOException {
+ ClassLoader classLoader = ConfigHandlerTests.class.getClassLoader();
+ String fileAsString = "";
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(classLoader.getResourceAsStream(fileName)))) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ fileAsString += line;
+ }
+ }
+ return fileAsString;
+ }
+
+}
diff --git a/src/test/java/org/onap/dcaegen2/pmmapper/config/util/RequestSenderTests.java b/src/test/java/org/onap/dcaegen2/pmmapper/config/util/RequestSenderTests.java new file mode 100644 index 0000000..470f146 --- /dev/null +++ b/src/test/java/org/onap/dcaegen2/pmmapper/config/util/RequestSenderTests.java @@ -0,0 +1,116 @@ +/*-
+ * ============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.util;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockserver.integration.ClientAndServer.startClientAndServer;
+import static org.mockserver.model.HttpRequest.request;
+import static org.mockserver.model.HttpResponse.response;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.UnknownHostException;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mockserver.client.server.MockServerClient;
+import org.mockserver.integration.ClientAndServer;
+import org.mockserver.model.HttpStatusCode;
+import org.mockserver.verify.VerificationTimes;
+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;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(RequestSender.class)
+
+public class RequestSenderTests {
+ private static ClientAndServer mockServer;
+ private MockServerClient client = mockClient();
+
+ @BeforeClass
+ public static void setup() {
+ mockServer = startClientAndServer(1080);
+ }
+
+ @AfterClass
+ public static void teardown() {
+ mockServer.stop();
+ }
+
+ @BeforeClass
+ public static void setEnvironmentVariable() {
+ System.setProperty("CONSUL_HOST", "my_consult_host");
+ System.setProperty("CONFIG_BINDING_SERVICE", "config-binding-service");
+ System.setProperty("HOSTNAME", "hostname");
+ }
+
+ @Test
+ public void send_success() throws Exception {
+
+ client.when(request())
+ .respond(response().withStatusCode(HttpStatusCode.OK_200.code()));
+
+ new RequestSender().send("http://127.0.0.1:1080/once");
+
+ client.verify(request(), VerificationTimes.exactly(1));
+ client.clear(request());
+ }
+
+ @Test
+ public void host_unavailable_retry_mechanism() throws Exception {
+ PowerMockito.mockStatic(Thread.class);
+
+ client.when(request())
+ .respond(response().withStatusCode(HttpStatusCode.SERVICE_UNAVAILABLE_503.code()));
+
+ assertThrows(Exception.class, () -> {
+ new RequestSender().send("http://127.0.0.1:1080/anypath");
+ });
+
+ client.verify(request(), VerificationTimes.exactly(5));
+ client.clear(request());
+ }
+
+ @Test
+ public void host_unknown() throws IOException {
+ PowerMockito.mockStatic(Thread.class);
+ URL url = PowerMockito.mock(URL.class);
+ PowerMockito.when(url.openConnection())
+ .thenThrow(UnknownHostException.class);
+
+ assertThrows(Exception.class, () -> {
+ new RequestSender().send("http://127.0.0.1:1080/host-is-unknown");
+ });
+
+ client.verify(request(), VerificationTimes.exactly(5));
+ client.clear(request());
+ }
+
+ private MockServerClient mockClient() {
+ return new MockServerClient("127.0.0.1", 1080);
+ }
+
+}
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 25fb8ae..645d1be 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 @@ -49,8 +49,8 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import org.onap.dcaegen2.services.pmmapper.config.BusControllerConfig; import org.onap.dcaegen2.services.pmmapper.exceptions.TooManyTriesException; +import org.onap.dcaegen2.services.pmmapper.model.BusControllerConfig; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.dcaegen2.services.pmmapper.model.EventMetadata; import org.powermock.api.mockito.PowerMockito; diff --git a/src/test/resources/incomplete_mapper_config.json b/src/test/resources/incomplete_mapper_config.json new file mode 100644 index 0000000..aec0aca --- /dev/null +++ b/src/test/resources/incomplete_mapper_config.json @@ -0,0 +1,24 @@ +{
+ "_comment": "This mapper config is missing the messagerouter feed name",
+ "pm-mapper-filter": {
+ "filters": "{[]}"
+ },
+ "3GPP.schema.file": "{\"3GPP_Schema\":\"./etc/3GPP_relaxed_schema.xsd\"}",
+ "streams_subscribes": {},
+ "streams_publishes": {
+ "pm_mapper_handle_out": {
+ "type": "message_router",
+ "aaf_password": null,
+ "dmaap_info": {
+ "topic_url": "https://we-are-message-router.us:3905/events/some-topic",
+ "client_role": null,
+ "location": null,
+ "client_id": null
+ },
+ "aaf_username": null
+ }
+ },
+ "some parameter": "unauthenticated.PM_VES_OUTPUT",
+ "some field": "1",
+ "services_calls": {}
+}
\ No newline at end of file diff --git a/src/test/resources/valid_cbs_config.json b/src/test/resources/valid_cbs_config.json new file mode 100644 index 0000000..e2fc650 --- /dev/null +++ b/src/test/resources/valid_cbs_config.json @@ -0,0 +1,23 @@ +[
+ {
+ "ID": "6e331b82-6563-3bf7-4acc-d02d1e042c9b",
+ "Node": "dcae-bootstrap",
+ "Address": "10.42.249.191",
+ "Datacenter": "dc1",
+ "TaggedAddresses": {
+ "lan": "10.42.249.191",
+ "wan": "10.42.249.191"
+ },
+ "NodeMeta": {
+ "consul-network-segment": ""
+ },
+ "ServiceID": "dcae-cbs0",
+ "ServiceName": "pm-mapper-service-name",
+ "ServiceTags": [],
+ "ServiceAddress": "config-binding-service",
+ "ServicePort": 10000,
+ "ServiceEnableTagOverride": false,
+ "CreateIndex": 124,
+ "ModifyIndex": 124
+ }
+]
\ No newline at end of file diff --git a/src/test/resources/valid_mapper_config.json b/src/test/resources/valid_mapper_config.json new file mode 100644 index 0000000..7106141 --- /dev/null +++ b/src/test/resources/valid_mapper_config.json @@ -0,0 +1,20 @@ +{
+ "pm-mapper-filter": "{ \"filters\":[]}",
+ "3GPP.schema.file": "{\"3GPP_Schema\":\"./etc/3GPP_relaxed_schema.xsd\"}",
+ "streams_subscribes": {},
+ "streams_publishes": {
+ "pm_mapper_handle_out": {
+ "type": "message_router",
+ "aaf_password": null,
+ "dmaap_info": {
+ "topic_url": "https://we-are-message-router.us:3905/events/some-topic",
+ "client_role": null,
+ "location": null,
+ "client_id": null
+ },
+ "aaf_username": null
+ }
+ },
+ "streams_subscribes.pm_mapper_handle_out.message_router_topic": "unauthenticated.PM_VES_OUTPUT",
+ "services_calls": {}
+}
\ No newline at end of file |