From: emartin Date: Tue, 12 Feb 2019 17:12:08 +0000 (+0000) Subject: Fix mapper configuration to use CBS X-Git-Tag: 1.0.0~28 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=85d315a40fa48255e1104a0c2d6c5ad984952c9c;p=dcaegen2%2Fservices%2Fpm-mapper.git Fix mapper configuration to use CBS Change-Id: I17bb9577ac2dd9267eeade948e5bf05cdadbac57 Issue-ID: DCAEGEN2-1186 Signed-off-by: emartin --- diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java index 784e0f1..3c075ba 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java @@ -28,11 +28,9 @@ import org.onap.dcaegen2.services.pmmapper.config.ConfigHandler; import org.onap.dcaegen2.services.pmmapper.datarouter.DataRouterSubscriber; 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.exceptions.TooManyTriesException; -import org.onap.dcaegen2.services.pmmapper.model.BusControllerConfig; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; import org.onap.dcaegen2.services.pmmapper.healthcheck.HealthCheckHandler; @@ -41,17 +39,14 @@ import java.net.URL; public class App { - public static void main(String[] args) throws MalformedURLException, InterruptedException, TooManyTriesException, CBSConfigException, ConsulServerError, EnvironmentConfigException, CBSServerError, MapperConfigException { + public static void main(String[] args) throws MalformedURLException, InterruptedException, TooManyTriesException, CBSConfigException, EnvironmentConfigException, CBSServerError, MapperConfigException { HealthCheckHandler healthCheckHandler = new HealthCheckHandler(); DataRouterSubscriber dataRouterSubscriber = new DataRouterSubscriber(event -> { event.getHttpServerExchange().unDispatch(); event.getHttpServerExchange().getResponseSender().send(StatusCodes.OK_STRING); - System.out.println(event.getMetadata().getProductName()); }); MapperConfig mapperConfig = new ConfigHandler().getMapperConfig(); - BusControllerConfig busConfig = mapperConfig.getBusControllerConfig(); - busConfig.setDataRouterSubscribeEndpoint(new URL("http://" + System.getenv("DMAAP_BC_SERVICE_HOST") + ":" + System.getenv("DMAAP_BC_SERVICE_PORT") + "/webapi/dr_subs")); - dataRouterSubscriber.start(busConfig); + dataRouterSubscriber.start(mapperConfig); Undertow.builder() .addHttpListener(8081, "0.0.0.0") diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandler.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandler.java index 0087826..fe2f247 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandler.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandler.java @@ -19,15 +19,10 @@ */ package org.onap.dcaegen2.services.pmmapper.config; -import java.util.Arrays; -import java.util.UUID; - 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.CBSConfig; import org.onap.dcaegen2.services.pmmapper.model.EnvironmentConfig; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; import org.onap.dcaegen2.services.pmmapper.utils.RequestSender; @@ -45,7 +40,6 @@ import com.google.gson.GsonBuilder; public class ConfigHandler { private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(ConfigHandler.class)); - private static final String EMPTY_MESSAGE = ""; private RequestSender sender; /** @@ -72,42 +66,23 @@ public class ConfigHandler { * @throws CBSServerError * @throws MapperConfigException */ - public MapperConfig getMapperConfig() throws CBSConfigException, ConsulServerError, EnvironmentConfigException, + public MapperConfig getMapperConfig() throws CBSConfigException, EnvironmentConfigException, CBSServerError, MapperConfigException { String mapperConfigJson = ""; - CBSConfig cbsConfig = convertCBSConfigToObject(getCBSConfigFromConsul()); - String cbsSocketAddress = cbsConfig.getServiceAddress() + ":" + cbsConfig.getServicePort(); - String requestURL = "http://" + cbsSocketAddress + "/service_component/" + cbsConfig.getServiceName(); + String cbsSocketAddress = EnvironmentConfig.getCBSHostName() + ":" + EnvironmentConfig.getCBSPort(); + String requestURL = "http://" + cbsSocketAddress + "/service_component/" + EnvironmentConfig.getServiceName(); try { logger.unwrap().info(ONAPLogConstants.Markers.ENTRY, "Fetching pm-mapper configuration from Configbinding Service"); mapperConfigJson = sender.send(requestURL); } catch (Exception exception) { throw new CBSServerError("Error connecting to Configbinding Service: ", exception); } finally { - logger.unwrap().info(ONAPLogConstants.Markers.EXIT, EMPTY_MESSAGE); + logger.unwrap().info(ONAPLogConstants.Markers.EXIT, "Received pm-mapper configuration from ConfigBinding Service:\n{}", mapperConfigJson); } - logger.unwrap().info("Received pm-mapper configuration from ConfigBinding Service:\n{}", mapperConfigJson); return convertMapperConfigToObject(mapperConfigJson); } - private String getCBSConfigFromConsul() throws ConsulServerError, EnvironmentConfigException { - String cbsParams=""; - String consulURL = "http://" + EnvironmentConfig.getConsulHost() + ":" + EnvironmentConfig.getConsultPort() - + "/v1/catalog/service/" + EnvironmentConfig.getCbsName(); - try { - logger.unwrap().info(ONAPLogConstants.Markers.ENTRY, - "Retrieving ConfigBinding Service parameters from this Consul URL: {}", consulURL); - cbsParams = sender.send(consulURL); - } catch (Exception exception) { - throw new ConsulServerError("Error connecting to Consul: ", exception); - } finally { - logger.unwrap().info(ONAPLogConstants.Markers.EXIT, "Received ConfigBinding Service parameters:\n{}", cbsParams); - } - - return cbsParams; - } - private MapperConfig convertMapperConfigToObject(String mapperConfigJson) throws MapperConfigException { MapperConfig mapperConfig; try { @@ -122,23 +97,4 @@ public class ConfigHandler { logger.unwrap().debug("Mapper configuration:\n{}", mapperConfig); return mapperConfig; } - - private CBSConfig convertCBSConfigToObject(String cbsParameters) throws CBSConfigException { - CBSConfig cbsConfig; - try { - cbsConfig = Arrays - .asList(new GsonBuilder() - .registerTypeAdapter(CBSConfig.class, new RequiredFieldDeserializer()) - .create() - .fromJson(cbsParameters, CBSConfig[].class)) - .get(0); - logger.unwrap().debug("ConfigBinding Service Configurations: " + cbsConfig); - } catch (Exception exception) { - throw new CBSConfigException( - "Error mapping the received ConfigBinding service configuration parameters: " + cbsParameters, - exception); - } - return cbsConfig; - } - } diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java index a8211e6..e262d42 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java @@ -32,7 +32,6 @@ import org.onap.dcaegen2.services.pmmapper.exceptions.NoMetadataException; import org.onap.dcaegen2.services.pmmapper.exceptions.TooManyTriesException; import org.onap.dcaegen2.services.pmmapper.model.EventMetadata; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; -import org.onap.dcaegen2.services.pmmapper.model.BusControllerConfig; import org.onap.dcaegen2.services.pmmapper.model.Event; import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; @@ -49,6 +48,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.nio.charset.StandardCharsets; +import java.time.Instant; import java.util.Optional; import java.util.Random; import java.util.UUID; @@ -91,7 +91,7 @@ public class DataRouterSubscriber implements HttpHandler { * all non constant configuration for subscription through this endpoint. * @throws TooManyTriesException in the event that timeout has occurred several times. */ - public void start(BusControllerConfig config) throws TooManyTriesException, InterruptedException { + public void start(MapperConfig config) throws TooManyTriesException, InterruptedException { try { logger.unwrap().info(ONAPLogConstants.Markers.ENTRY, "Starting subscription to DataRouter"); subscribe(NUMBER_OF_ATTEMPTS, DEFAULT_TIMEOUT, config); @@ -100,8 +100,8 @@ public class DataRouterSubscriber implements HttpHandler { } } - private HttpURLConnection getBusControllerConnection(BusControllerConfig config, int timeout) throws IOException { - HttpURLConnection connection = (HttpURLConnection) config.getDataRouterSubscribeEndpoint() + private HttpURLConnection getBusControllerConnection(MapperConfig config, int timeout) throws IOException { + HttpURLConnection connection = (HttpURLConnection) config.getBusControllerSubscriptionUrl() .openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(timeout); @@ -118,18 +118,18 @@ public class DataRouterSubscriber implements HttpHandler { return connection; } - private JsonObject getBusControllerSubscribeBody(BusControllerConfig config) { + private JsonObject getBusControllerSubscribeBody(MapperConfig config) { JsonObject subscriberObj = new JsonObject(); subscriberObj.addProperty("dcaeLocationName", config.getDcaeLocation()); - subscriberObj.addProperty("deliveryURL", config.getDeliveryURL()); - subscriberObj.addProperty("feedId", config.getFeedId()); - subscriberObj.addProperty("lastMod", config.getLastMod()); - subscriberObj.addProperty("username", config.getUsername()); - subscriberObj.addProperty("userpwd", config.getPassword()); + subscriberObj.addProperty("deliveryURL", config.getBusControllerDeliveryUrl()); + subscriberObj.addProperty("feedId", config.getBusControllerFeedId()); + subscriberObj.addProperty("lastMod", Instant.now().toString()); + subscriberObj.addProperty("username", config.getBusControllerUserName()); + subscriberObj.addProperty("userpwd", config.getBusControllerPassword()); return subscriberObj; } - private void subscribe(int attempts, int timeout, BusControllerConfig config) throws TooManyTriesException, InterruptedException { + private void subscribe(int attempts, int timeout, MapperConfig config) throws TooManyTriesException, InterruptedException { int subResponse = 504; String subMessage = ""; try { diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/ConsulServerError.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/ConsulServerError.java deleted file mode 100644 index 4c2adab..0000000 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/ConsulServerError.java +++ /dev/null @@ -1,26 +0,0 @@ -/*- - * ============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.services.pmmapper.exceptions; - -public class ConsulServerError extends Exception { - public ConsulServerError(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/BusControllerConfig.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/BusControllerConfig.java deleted file mode 100644 index 3727099..0000000 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/BusControllerConfig.java +++ /dev/null @@ -1,40 +0,0 @@ -/*- - * ============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.services.pmmapper.model; - -import lombok.Data; - -import java.net.URL; - -/** - * Stub for BusControllerConfiguration object. - */ -@Data -public class BusControllerConfig { - - private String dcaeLocation = "dcaeLocation"; - private String deliveryURL = "deliveryURL"; - private int feedId = 2; - private String lastMod = "lastMod"; - private String username = "username"; - private String password = "password"; - private URL dataRouterSubscribeEndpoint; - -} diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/CBSConfig.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/CBSConfig.java deleted file mode 100644 index 66fbed4..0000000 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/CBSConfig.java +++ /dev/null @@ -1,46 +0,0 @@ -/*- - * ============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.services.pmmapper.model; - -import org.onap.dcaegen2.services.pmmapper.utils.GSONRequired; - -import com.google.gson.annotations.SerializedName; -import lombok.Data; - -@Data -public class CBSConfig { - - @GSONRequired - @SerializedName("ServiceID") - private String serviceID; - - @GSONRequired - @SerializedName("ServiceName") - private String serviceName; - - @GSONRequired - @SerializedName("ServiceAddress") - private String serviceAddress; - - @GSONRequired - @SerializedName("ServicePort") - private String servicePort; - -} diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/EnvironmentConfig.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/EnvironmentConfig.java index f9dd178..fe48bfc 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/EnvironmentConfig.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/EnvironmentConfig.java @@ -25,30 +25,32 @@ import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException public class EnvironmentConfig { - private static Integer consulPort = 8500; + public static final int DEFAULT_CBS_PORT = 10000; + public static final String ENV_CBS_HOST_KEY = "CONFIG_BINDING_SERVICE_SERVICE_HOST"; + public static final String ENV_CBS_PORT_KEY = "CONFIG_BINDING_SERVICE_SERVICE_PORT"; + public static final String ENV_SERVICE_NAME_KEY = "HOSTNAME"; - public static String getConsulHost() throws EnvironmentConfigException { - return Optional.ofNullable(System.getProperty("CONSUL_HOST")) + public static String getServiceName() throws EnvironmentConfigException { + return Optional.ofNullable(System.getenv("HOSTNAME")) .orElseThrow(() -> new EnvironmentConfigException( - "$CONSUL_HOST environment variable must be defined prior to pm-mapper initialization")); + ENV_SERVICE_NAME_KEY+ " environment variable must be defined prior to pm-mapper initialization.")); } - public static Integer getConsultPort() throws EnvironmentConfigException { - Integer port = consulPort; + public static String getCBSHostName() throws EnvironmentConfigException { + return Optional.ofNullable(System.getenv("CONFIG_BINDING_SERVICE_SERVICE_HOST")) + .orElseThrow(() -> new EnvironmentConfigException( + ENV_CBS_HOST_KEY+ " environment variable must be defined prior to pm-mapper initialization.")); + } + + public static Integer getCBSPort() throws EnvironmentConfigException { + Integer port = DEFAULT_CBS_PORT; try { - port = Optional.ofNullable(System.getProperty("CONSUL_PORT")) - .map(Integer::valueOf) - .orElse(consulPort); + port = Optional.ofNullable(System.getenv("CONFIG_BINDING_SERVICE_SERVICE_PORT")) + .map(Integer::valueOf).orElse(DEFAULT_CBS_PORT); } catch (NumberFormatException e) { - throw new EnvironmentConfigException("CONSUL_PORT must be valid: " + port); + throw new EnvironmentConfigException(ENV_CBS_PORT_KEY + " must be valid: " + port); } return port; } - - public static String getCbsName() throws EnvironmentConfigException { - return Optional.ofNullable(System.getProperty("CONFIG_BINDING_SERVICE")) - .orElseThrow(() -> new EnvironmentConfigException( - "$CONFIG_BINDING_SERVICE environment variable must be defined prior to pm-mapper initialization.")); - } } diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java index b4a9f01..1753c2c 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java @@ -19,20 +19,88 @@ */ package org.onap.dcaegen2.services.pmmapper.model; +import java.net.MalformedURLException; +import java.net.URL; + import org.onap.dcaegen2.services.pmmapper.utils.GSONRequired; import com.google.gson.annotations.SerializedName; -import lombok.Data; +import lombok.Getter; +import lombok.AccessLevel; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -@Data +@Getter +@EqualsAndHashCode @NoArgsConstructor public class MapperConfig { + public static final String CLIENT_NAME = "pm-mapper"; @GSONRequired - @SerializedName("streams_subscribes.pm_mapper_handle_out.message_router_topic") - private String messageRouterTopicName; + @Getter(AccessLevel.PRIVATE) + @SerializedName("streams_subscribes") + private StreamsSubscribes streamsSubscribes; + + @GSONRequired + @SerializedName("buscontroller_feed_subscription_endpoint") + private String busControllerSubscriptionEndpoint; + + @GSONRequired + @SerializedName("buscontroller_feed_id") + private String busControllerFeedId; + + public String getBusControllerDeliveryUrl() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getDeliveryUrl(); + } + + public String getDcaeLocation() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getLocation(); + } + + public String getBusControllerUserName() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getUsername(); + } + + public String getBusControllerPassword() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getPassword(); + } + + public URL getBusControllerSubscriptionUrl() throws MalformedURLException { + return new URL(this.getBusControllerSubscriptionEndpoint()); + } + + @Getter + @EqualsAndHashCode + private class StreamsSubscribes { + @GSONRequired + @SerializedName("dmaap_subscriber") + DmaapSubscriber dmaapSubscriber; + } + + @Getter + @EqualsAndHashCode + class DmaapSubscriber { + @GSONRequired + @SerializedName("dmaap_info") + DmaapInfo dmaapInfo; + } + + @Getter + @EqualsAndHashCode + class DmaapInfo { + @GSONRequired + private String location; + private String username; + private String password; + + @SerializedName("delivery_url") + private String deliveryUrl; + + @SerializedName("subscriber_id") + private String subscriberId; + } +} + + - BusControllerConfig busControllerConfig; -} \ No newline at end of file diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java index 04f3431..25519a0 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java @@ -76,6 +76,8 @@ public class RequestSender { } catch (Exception e) { if (retryLimitReached(i)) { + logger.unwrap() + .error("Execution error: "+connection.getResponseMessage(), e); throw new Exception(SERVER_ERROR_MESSAGE + ": " + connection.getResponseMessage(), e); } } 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 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(); diff --git a/src/test/resources/incomplete_mapper_config.json b/src/test/resources/incomplete_mapper_config.json index aec0aca..ed4ebd7 100644 --- a/src/test/resources/incomplete_mapper_config.json +++ b/src/test/resources/incomplete_mapper_config.json @@ -1,10 +1,10 @@ { - "_comment": "This mapper config is missing the messagerouter feed name", + "_comment": "This mapper config is missing streams_subscribes", "pm-mapper-filter": { "filters": "{[]}" }, "3GPP.schema.file": "{\"3GPP_Schema\":\"./etc/3GPP_relaxed_schema.xsd\"}", - "streams_subscribes": {}, + "streams_subscribes": null, "streams_publishes": { "pm_mapper_handle_out": { "type": "message_router", diff --git a/src/test/resources/valid_cbs_config.json b/src/test/resources/valid_cbs_config.json deleted file mode 100644 index e2fc650..0000000 --- a/src/test/resources/valid_cbs_config.json +++ /dev/null @@ -1,23 +0,0 @@ -[ - { - "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 index 7106141..27765ae 100644 --- a/src/test/resources/valid_mapper_config.json +++ b/src/test/resources/valid_mapper_config.json @@ -1,7 +1,20 @@ { "pm-mapper-filter": "{ \"filters\":[]}", "3GPP.schema.file": "{\"3GPP_Schema\":\"./etc/3GPP_relaxed_schema.xsd\"}", - "streams_subscribes": {}, + "streams_subscribes": { + "dmaap_subscriber": { + "type": "data_router", + "aaf_username": null, + "aaf_password": null, + "dmaap_info": { + "location": "location", + "delivery_url": "delivery_url", + "username": "username", + "password": "password", + "subscriber_id": "subsriber_id" + } + } + }, "streams_publishes": { "pm_mapper_handle_out": { "type": "message_router", @@ -15,6 +28,7 @@ "aaf_username": null } }, - "streams_subscribes.pm_mapper_handle_out.message_router_topic": "unauthenticated.PM_VES_OUTPUT", + "buscontroller_feed_id": "2", + "buscontroller_feed_subscription_endpoint": "http://dmaap-bc.onap.svc.cluster.local:8080/webapi/dr_subs", "services_calls": {} } \ No newline at end of file