From 85e1d39b283ffe6c78f8d8175809f285059b92cb Mon Sep 17 00:00:00 2001 From: Piotr Bocheński Date: Fri, 30 Nov 2018 18:03:17 +0100 Subject: SDK integration: use 'cbs-client' in PRH MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ifd097c7d2305432f450bbcceb521cf2341f4cb6c Issue-ID: DCAEGEN2-1011 Signed-off-by: Piotr Bocheński --- pom.xml | 5 + prh-app-server/pom.xml | 5 + .../prh/configuration/CloudConfiguration.java | 32 +++---- .../prh/configuration/EnvironmentProcessor.java | 21 ++-- .../dcaegen2/services/prh/model/EnvProperties.java | 43 --------- .../services/prh/service/HttpGetClient.java | 92 ------------------ .../prh/service/PrhConfigurationProvider.java | 106 --------------------- .../prh/integration/ScheduledXmlContextITest.java | 7 +- .../services/prh/service/HttpGetClientTest.java | 79 --------------- .../prh/service/PrhConfigurationProviderTest.java | 93 ------------------ 10 files changed, 37 insertions(+), 446 deletions(-) delete mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/model/EnvProperties.java delete mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/HttpGetClient.java delete mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java delete mode 100644 prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/HttpGetClientTest.java delete mode 100644 prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProviderTest.java diff --git a/pom.xml b/pom.xml index b9e9f81c..322bf65b 100644 --- a/pom.xml +++ b/pom.xml @@ -200,6 +200,11 @@ tomcat-embed-websocket ${tomcat.version} + + org.onap.dcaegen2.services.sdk.rest.services + cbs-client + 1.0.0-SNAPSHOT + ch.qos.logback diff --git a/prh-app-server/pom.xml b/prh-app-server/pom.xml index 237dff1c..2ed127ea 100644 --- a/prh-app-server/pom.xml +++ b/prh-app-server/pom.xml @@ -174,6 +174,10 @@ prh-dmaap-client ${project.version} + + org.onap.dcaegen2.services.sdk.rest.services + cbs-client + ch.qos.logback @@ -231,6 +235,7 @@ org.assertj assertj-core + test diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java index c9667ed6..c862274e 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java @@ -21,36 +21,34 @@ package org.onap.dcaegen2.services.prh.configuration; import com.google.gson.JsonObject; -import java.util.Optional; -import java.util.Properties; -import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; -import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; -import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; -import org.onap.dcaegen2.services.prh.config.ImmutableAaiClientConfiguration; -import org.onap.dcaegen2.services.prh.model.EnvProperties; -import org.onap.dcaegen2.services.prh.service.PrhConfigurationProvider; +import org.onap.dcaegen2.services.prh.config.*; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.*; import org.springframework.scheduling.annotation.EnableScheduling; import reactor.core.publisher.Flux; import reactor.core.scheduler.Schedulers; +import java.util.Optional; +import java.util.Properties; + /** * @author Przemysław Wąsala on 8/9/18 */ @Configuration +@ComponentScan("org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers") @EnableConfigurationProperties @EnableScheduling @Primary public class CloudConfiguration extends AppConfig { private static final Logger LOGGER = LoggerFactory.getLogger(CloudConfiguration.class); - private PrhConfigurationProvider prhConfigurationProvider; + private CloudConfigurationClient prhConfigurationProvider; private AaiClientConfiguration aaiClientCloudConfiguration; private DmaapPublisherConfiguration dmaapPublisherCloudConfiguration; @@ -61,7 +59,7 @@ public class CloudConfiguration extends AppConfig { @Autowired - public void setThreadPoolTaskScheduler(PrhConfigurationProvider prhConfigurationProvider) { + public void setThreadPoolTaskScheduler(CloudConfigurationClient prhConfigurationProvider) { this.prhConfigurationProvider = prhConfigurationProvider; } @@ -72,16 +70,16 @@ public class CloudConfiguration extends AppConfig { } private void parsingConfigError(Throwable throwable) { - LOGGER.warn("Error in case of processing system environment, more details below: ", throwable); + LOGGER.warn("Failed to process system environments", throwable); } private void cloudConfigError(Throwable throwable) { - LOGGER.warn("Exception during getting configuration from CONSUL/CONFIG_BINDING_SERVICE ", throwable); + LOGGER.warn("Failed to gather configuration from ConfigBindingService/Consul", throwable); } private void parsingConfigSuccess(EnvProperties envProperties) { - LOGGER.info("Fetching PRH configuration from ConfigBindingService/Consul"); - prhConfigurationProvider.callForPrhConfiguration(envProperties) + LOGGER.debug("Fetching PRH configuration from ConfigBindingService/Consul"); + prhConfigurationProvider.callForServiceConfigurationReactive(envProperties) .subscribe(this::parseCloudConfig, this::cloudConfigError); } @@ -108,4 +106,4 @@ public class CloudConfiguration extends AppConfig { public DmaapConsumerConfiguration getDmaapConsumerConfiguration() { return Optional.ofNullable(dmaapConsumerCloudConfiguration).orElse(super.getDmaapConsumerConfiguration()); } -} \ No newline at end of file +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java index 114e9839..793fcc27 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java @@ -20,28 +20,29 @@ package org.onap.dcaegen2.services.prh.configuration; -import java.util.Optional; -import java.util.Properties; import org.onap.dcaegen2.services.prh.exceptions.EnvironmentLoaderException; -import org.onap.dcaegen2.services.prh.model.EnvProperties; -import org.onap.dcaegen2.services.prh.model.ImmutableEnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; +import java.util.Optional; +import java.util.Properties; + /** * @author Przemysław Wąsala on 8/10/18 */ -class EnvironmentProcessor { +final class EnvironmentProcessor { - private static final int DEFAULT_CONSUL_PORT = 8500; private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentProcessor.class); + private static final int DEFAULT_CONSUL_PORT = 8500; private EnvironmentProcessor() { } static Mono evaluate(Properties systemEnvironment) { - LOGGER.info("Loading configuration from system environment variables"); + LOGGER.debug("Loading configuration from system environment variables"); EnvProperties envProperties; try { envProperties = ImmutableEnvProperties.builder().consulHost(getConsulHost(systemEnvironment)) @@ -50,7 +51,7 @@ class EnvironmentProcessor { } catch (EnvironmentLoaderException e) { return Mono.error(e); } - LOGGER.info("Evaluated environment system variables {}", envProperties); + LOGGER.info("Evaluated system environment variables: {}", envProperties); return Mono.just(envProperties); } @@ -78,9 +79,7 @@ class EnvironmentProcessor { } private static Integer getDefaultPortOfConsul() { - LOGGER.warn("$CONSUL_PORT environment has not been defined"); - LOGGER.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT); + LOGGER.warn("$CONSUL_PORT environment has not been defined, using default port: {}", DEFAULT_CONSUL_PORT); return DEFAULT_CONSUL_PORT; } } - diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/model/EnvProperties.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/model/EnvProperties.java deleted file mode 100644 index c3895504..00000000 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/model/EnvProperties.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.prh.model; - -import org.immutables.value.Value; - -/** - * @author Przemysław Wąsala on 8/10/18 - */ -@Value.Immutable(prehash = true) -public interface EnvProperties { - - @Value.Parameter - String consulHost(); - - @Value.Parameter - Integer consulPort(); - - @Value.Parameter - String cbsName(); - - @Value.Parameter - String appName(); - -} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/HttpGetClient.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/HttpGetClient.java deleted file mode 100644 index e3a8dcf6..00000000 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/HttpGetClient.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.prh.service; - -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.web.reactive.function.client.ClientResponse; -import org.springframework.web.reactive.function.client.ExchangeFilterFunction; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Mono; - -class HttpGetClient { - - private static final Logger LOGGER = LoggerFactory.getLogger(HttpGetClient.class); - - private final WebClient webClient; - private final Gson gson; - - HttpGetClient() { - this(WebClient.builder().filter(logRequest()).filter(logResponse()).build()); - } - - HttpGetClient(WebClient webClient) { - this.webClient = webClient; - this.gson = new Gson(); - } - - Mono callHttpGet(String url, Class genericClassDeclaration) { - return webClient - .get() - .uri(url) - .retrieve() - .onStatus(HttpStatus::is4xxClientError, response -> Mono.error(getException(response))) - .onStatus(HttpStatus::is5xxServerError, response -> Mono.error(getException(response))) - .bodyToMono(String.class) - .flatMap(body -> getJsonFromRequest(body, genericClassDeclaration)); - } - - private RuntimeException getException(ClientResponse response) { - return new RuntimeException(String.format("Request for cloud config failed: HTTP %d", - response.statusCode().value())); - } - - private Mono getJsonFromRequest(String body, Class genericClassDeclaration) { - try { - return Mono.just(parseJson(body, genericClassDeclaration)); - } catch (JsonSyntaxException | IllegalStateException e) { - return Mono.error(e); - } - } - - private T parseJson(String body, Class genericClassDeclaration) { - return gson.fromJson(body, genericClassDeclaration); - } - - private static ExchangeFilterFunction logResponse() { - return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { - LOGGER.info("Response status {}", clientResponse.statusCode()); - return Mono.just(clientResponse); - }); - } - - private static ExchangeFilterFunction logRequest() { - return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { - LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url()); - clientRequest.headers() - .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value))); - return Mono.just(clientRequest); - }); - } -} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java deleted file mode 100644 index 38b060e9..00000000 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.prh.service; - -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import org.onap.dcaegen2.services.prh.model.EnvProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; -import org.springframework.web.util.DefaultUriBuilderFactory; -import reactor.core.publisher.Mono; - - -/** - * @author Przemysław Wąsala on 8/10/18 - */ - -@Service -public class PrhConfigurationProvider { - - private static final Logger LOGGER = LoggerFactory.getLogger(PrhConfigurationProvider.class); - - private final HttpGetClient httpGetClient; - - PrhConfigurationProvider() { - this(new HttpGetClient()); - } - - PrhConfigurationProvider(HttpGetClient httpGetClient) { - this.httpGetClient = httpGetClient; - } - - public Mono callForPrhConfiguration(EnvProperties envProperties) { - return callConsulForConfigBindingServiceEndpoint(envProperties) - .flatMap(this::callConfigBindingServiceForPrhConfiguration); - } - - private Mono callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { - LOGGER.info("Retrieving Config Binding Service endpoint from Consul"); - return httpGetClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class) - .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName())); - - } - - private String getConsulUrl(EnvProperties envProperties) { - return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service", - envProperties.cbsName()); - } - - private Mono callConfigBindingServiceForPrhConfiguration(String configBindingServiceUri) { - LOGGER.info("Retrieving PRH configuration"); - return httpGetClient.callHttpGet(configBindingServiceUri, JsonObject.class); - } - - - private Mono createConfigBindingServiceUrl(JsonArray jsonArray, String appName) { - return getConfigBindingObject(jsonArray) - .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName)); - } - - private Mono buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) { - return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(), - jsonObject.get("ServicePort").getAsInt(), "/service_component", appName)); - } - - private Mono getConfigBindingObject(JsonArray jsonArray) { - try { - if (jsonArray.size() > 0) { - return Mono.just(jsonArray.get(0).getAsJsonObject()); - } else { - throw new IllegalStateException("JSON Array was empty"); - } - } catch (IllegalStateException e) { - LOGGER.warn("Failed to retrieve JSON Object from array", e); - return Mono.error(e); - } - } - - private String getUri(String host, Integer port, String... paths) { - return new DefaultUriBuilderFactory().builder() - .scheme("http") - .host(host) - .port(port) - .path(String.join("/", paths)) - .build().toString(); - } -} diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/integration/ScheduledXmlContextITest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/integration/ScheduledXmlContextITest.java index 3d1d79fe..934f572f 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/integration/ScheduledXmlContextITest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/integration/ScheduledXmlContextITest.java @@ -40,11 +40,10 @@ import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; /** * @author Przemysław Wąsala on 3/27/18 */ - @Configuration -@ComponentScan +@ComponentScan("org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers") @ExtendWith({MockitoExtension.class, SpringExtension.class}) -@ContextConfiguration(locations = {"classpath:scheduled-context.xml"}) +@ContextConfiguration(locations = "classpath:scheduled-context.xml") class ScheduledXmlContextITest extends AbstractTestNGSpringContextTests { private static final int WAIT_FOR_SCHEDULING = 1; @@ -62,5 +61,3 @@ class ScheduledXmlContextITest extends AbstractTestNGSpringContextTests { verify(scheduledTask, atLeast(1)).scheduleMainPrhEventTask(); } } - - diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/HttpGetClientTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/HttpGetClientTest.java deleted file mode 100644 index ab789a00..00000000 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/HttpGetClientTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PROJECT - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - - -package org.onap.dcaegen2.services.prh.service; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.JsonSyntaxException; -import org.junit.jupiter.api.Test; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; - - -class HttpGetClientTest { - private static final String SOMEURL = "http://someurl"; - private static final String DATA = "{}"; - private Gson gson = new Gson(); - private WebClient webClient = mock(WebClient.class); - private WebClient.RequestHeadersUriSpec requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class); - private WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class); - - @Test - void shouldReturnJsonObjectOnGetCall() { - //given - mockWebClientDependantObject(); - HttpGetClient httpGetClient = new HttpGetClient(webClient); - when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(DATA)); - - //when/then - StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription() - .expectNext(gson.fromJson(DATA, JsonObject.class)).verifyComplete(); - } - - @Test - void shouldReturnMonoErrorOnGetCall() { - //given - mockWebClientDependantObject(); - HttpGetClient httpGetClient = new HttpGetClient(webClient); - when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("some wrong data")); - - //when/then - StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription() - .expectError(JsonSyntaxException.class).verify(); - } - - - private void mockWebClientDependantObject() { - doReturn(requestBodyUriSpec).when(webClient).get(); - when(requestBodyUriSpec.uri(SOMEURL)).thenReturn(requestBodyUriSpec); - doReturn(responseSpec).when(requestBodyUriSpec).retrieve(); - doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); - doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); - } - -} \ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProviderTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProviderTest.java deleted file mode 100644 index e99389f5..00000000 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProviderTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.prh.service; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.prh.model.EnvProperties; -import org.onap.dcaegen2.services.prh.model.ImmutableEnvProperties; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; - - -class PrhConfigurationProviderTest { - - private static final Gson gson = new Gson(); - private static final String configBindingService = "[{\"ID\":\"9c8dd674-34ce-7049-d318-e98d93a64303\",\"Node\"" - + ":\"dcae-bootstrap\",\"Address\":\"10.42.52.82\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":" - + "{\"lan\":\"10.42.52.82\",\"wan\":\"10.42.52.82\"},\"NodeMeta\":{\"consul-network-segment\":\"\"}," - + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[]," - + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false," - + "\"CreateIndex\":14352,\"ModifyIndex\":14352},{\"ID\":\"35c6f540-a29c-1a92-23b0-1305bd8c81f5\",\"Node\":" - + "\"dev-consul-server-1\",\"Address\":\"10.42.165.51\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":" - + "{\"lan\":\"10.42.165.51\",\"wan\":\"10.42.165.51\"},\"NodeMeta\":{\"consul-network-segment\":\"\"}," - + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[]," - + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false," - + "\"CreateIndex\":803,\"ModifyIndex\":803}]"; - private static final JsonArray configBindingServiceJson = gson.fromJson(configBindingService, JsonArray.class); - private static final JsonArray emptyConfigBindingServiceJson = gson.fromJson("[]", JsonArray.class); - private static final String prhMockConfiguration = "{\"test\":1}"; - private static final JsonObject prhMockConfigurationJson = gson.fromJson(prhMockConfiguration, JsonObject.class); - - private EnvProperties envProperties = ImmutableEnvProperties.builder() - .appName("dcae-prh") - .cbsName("config-binding-service") - .consulHost("consul") - .consulPort(8500) - .build(); - - @Test - void shouldReturnPrhConfiguration() { - // given - HttpGetClient webClient = mock(HttpGetClient.class); - when( - webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) - .thenReturn(Mono.just(configBindingServiceJson)); - when(webClient.callHttpGet("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class)) - .thenReturn(Mono.just(prhMockConfigurationJson)); - - PrhConfigurationProvider provider = new PrhConfigurationProvider(webClient); - - //when/then - StepVerifier.create(provider.callForPrhConfiguration(envProperties)).expectSubscription() - .expectNext(prhMockConfigurationJson).verifyComplete(); - } - - @Test - void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() { - // given - HttpGetClient webClient = mock(HttpGetClient.class); - when( - webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) - .thenReturn(Mono.just(emptyConfigBindingServiceJson)); - - PrhConfigurationProvider provider = new PrhConfigurationProvider(webClient); - - //when/then - StepVerifier.create(provider.callForPrhConfiguration(envProperties)).expectSubscription() - .expectError(IllegalStateException.class).verify(); - } -} \ No newline at end of file -- cgit 1.2.3-korg