From a424173f03b236958d4a454c95a1a7597073cea6 Mon Sep 17 00:00:00 2001 From: Tomasz Pietruszkiewicz Date: Mon, 26 Apr 2021 10:02:00 +0200 Subject: CBS-Client to support config fetch from a configMap Change-Id: I2ad57844d142ece3275ed6d8586d8689b296bbbe Issue-ID: DCAEGEN2-2716 Signed-off-by: Tomasz Pietruszkiewicz Signed-off-by: Michal Banka --- .../services/cbs/client/api/CbsClientFactory.java | 16 ++-- .../cbs/client/impl/CbsClientConfigMap.java | 78 +++++++++++++++++++ .../services/cbs/client/impl/CbsClientImpl.java | 89 ---------------------- .../services/cbs/client/impl/CbsClientRest.java | 87 +++++++++++++++++++++ .../cbs/client/model/CbsClientConfiguration.java | 6 +- .../cbs/client/impl/CbsClientConfigMapTest.java | 57 ++++++++++++++ .../services/cbs/client/impl/CbsClientImplIT.java | 68 ++++++++++++----- .../cbs/client/impl/CbsClientImplTest.java | 79 ------------------- .../cbs/client/impl/CbsClientRestTest.java | 77 +++++++++++++++++++ .../src/test/resources/application_config.yaml | 10 +++ .../resources/sample_local_service_config.json | 33 ++++++++ 11 files changed, 405 insertions(+), 195 deletions(-) create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMap.java delete mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRest.java create mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMapTest.java delete mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java create mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRestTest.java create mode 100644 rest-services/cbs-client/src/test/resources/application_config.yaml create mode 100644 rest-services/cbs-client/src/test/resources/sample_local_service_config.json (limited to 'rest-services/cbs-client/src') diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java index 7e625182..f1e49bb7 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * DCAEGEN2-SERVICES-SDK * ================================================================================ - * Copyright (C) 2019-2020 Nokia. All rights reserved. + * Copyright (C) 2019-2021 Nokia. 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. @@ -22,7 +22,8 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api; import org.jetbrains.annotations.NotNull; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClientFactory; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientImpl; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientConfigMap; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientRest; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsLookup; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys; @@ -36,7 +37,6 @@ import reactor.core.publisher.Mono; * @since 1.1.2 */ public class CbsClientFactory { - /** *

Creates Mono which will emit instance of {@link CbsClient} when service discovery is complete.

* @@ -67,7 +67,13 @@ public class CbsClientFactory { private static Mono createCbsClientMono(RxHttpClient httpClient, CbsClientConfiguration configuration) { + CbsClientConfigMap cbsClientConfigMap = new CbsClientConfigMap(configuration.configMapFilePath()); + return cbsClientConfigMap.verifyConfigMapFile() ? Mono.just(cbsClientConfigMap) : + getConfigFromCBS(httpClient, configuration); + } + + private static Mono getConfigFromCBS(RxHttpClient httpClient, CbsClientConfiguration configuration) { return new CbsLookup().lookup(configuration) - .map(addr -> new CbsClientImpl(httpClient, configuration.appName(), addr, configuration.protocol())); + .map(addr ->new CbsClientRest(httpClient, configuration.appName(), addr, configuration.protocol())); } -} +} \ No newline at end of file diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMap.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMap.java new file mode 100644 index 00000000..875e20ed --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMap.java @@ -0,0 +1,78 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2021 Nokia. 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.sdk.rest.services.cbs.client.impl; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import org.jetbrains.annotations.NotNull; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest; +import org.onap.dcaegen2.services.sdk.services.common.FileReader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; +import reactor.core.publisher.Mono; +import java.util.LinkedHashMap; + +public class CbsClientConfigMap implements CbsClient { + + private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientConfigMap.class); + private final String configMapFilePath; + + public CbsClientConfigMap (String configMapFilePath) { + this.configMapFilePath = configMapFilePath; + } + + @Override + public @NotNull Mono get(CbsRequest request) { + return Mono.just(this.loadConfigMapFile()) + .doOnNext(this::logConfigMapOutput); + } + + public boolean verifyConfigMapFile() { + try { + LOGGER.info("Trying to load configuration from configMap file: {}", configMapFilePath); + this.loadConfigMapFile().isJsonObject(); + return true; + } catch(Exception ex) { + this.logConfigMapError(ex); + return false; + } + } + + private JsonObject loadConfigMapFile() { + Gson gson = new GsonBuilder().create(); + return gson.fromJson(gson.toJson(this.loadYamlConfigMapFile(), LinkedHashMap.class), JsonObject.class); + } + + private Object loadYamlConfigMapFile() { + return new Yaml().load(new FileReader(configMapFilePath).getContent()); + } + + private void logConfigMapOutput(JsonObject jsonObject) { + LOGGER.info("Got successful output from ConfigMap file"); + LOGGER.debug("ConfigMap output: {}", jsonObject); + } + + private void logConfigMapError(Exception ex) { + LOGGER.error("Error loading configuration from configMap file: {}", ex.getMessage()); + } +} \ No newline at end of file diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java deleted file mode 100644 index a895f3a1..00000000 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * DCAEGEN2-SERVICES-SDK - * ================================================================================ - * Copyright (C) 2019 Nokia. 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.sdk.rest.services.cbs.client.impl; - -import com.google.gson.JsonObject; -import org.jetbrains.annotations.NotNull; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import reactor.core.publisher.Mono; - -import java.net.InetSocketAddress; -import java.net.MalformedURLException; -import java.net.URL; - -public class CbsClientImpl implements CbsClient { - - private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientImpl.class); - private final RxHttpClient httpClient; - private final String serviceName; - private final InetSocketAddress cbsAddress; - private final String protocol; - - public CbsClientImpl(RxHttpClient httpClient, String serviceName, InetSocketAddress cbsAddress, String protocol) { - this.httpClient = httpClient; - this.serviceName = serviceName; - this.cbsAddress = cbsAddress; - this.protocol = protocol; - } - - @Override - public @NotNull Mono get(CbsRequest request) { - return Mono.fromCallable(() -> constructUrl(request).toString()) - .doOnNext(this::logRequestUrl) - .map(url -> ImmutableHttpRequest.builder() - .method(HttpMethod.GET) - .url(url) - .diagnosticContext(request.diagnosticContext()) - .build()) - .flatMap(httpClient::call) - .doOnNext(HttpResponse::throwIfUnsuccessful) - .map(resp -> resp.bodyAsJson(JsonObject.class)) - .doOnNext(this::logCbsResponse); - } - - - private URL constructUrl(CbsRequest request) { - try { - return new URL( - this.protocol, - cbsAddress.getHostString(), - cbsAddress.getPort(), - request.requestPath().getForService(serviceName)); - } catch (MalformedURLException e) { - throw new IllegalArgumentException("Invalid CBS URL", e); - } - } - - private void logRequestUrl(String url) { - LOGGER.debug("Calling {} for configuration", url); - } - - private void logCbsResponse(JsonObject json) { - LOGGER.info("Got successful response from Config Binding Service"); - LOGGER.debug("CBS response: {}", json); - } -} diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRest.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRest.java new file mode 100644 index 00000000..4559a902 --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRest.java @@ -0,0 +1,87 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2019-2021 Nokia. 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.sdk.rest.services.cbs.client.impl; + +import com.google.gson.JsonObject; +import org.jetbrains.annotations.NotNull; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Mono; + +import java.net.InetSocketAddress; +import java.net.MalformedURLException; +import java.net.URL; + +public class CbsClientRest implements CbsClient { + private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientRest.class); + private final RxHttpClient httpClient; + private final String serviceName; + private final InetSocketAddress cbsAddress; + private final String protocol; + + public CbsClientRest(RxHttpClient httpClient, String serviceName, InetSocketAddress cbsAddress, String protocol) { + this.httpClient = httpClient; + this.serviceName = serviceName; + this.cbsAddress = cbsAddress; + this.protocol = protocol; + } + + @Override + public @NotNull Mono get(CbsRequest request) { + return Mono.fromCallable(() -> constructUrl(request).toString()) + .doOnNext(this::logRequestUrl) + .map(url -> ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(url) + .diagnosticContext(request.diagnosticContext()) + .build()) + .flatMap(httpClient::call) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(resp -> resp.bodyAsJson(JsonObject.class)) + .doOnNext(this::logCbsResponse); + } + + private void logRequestUrl(String url) { + LOGGER.debug("Calling {} for configuration", url); + } + + private void logCbsResponse(JsonObject json) { + LOGGER.info("Got successful response from Config Binding Service"); + LOGGER.debug("CBS response: {}", json); + } + + private URL constructUrl(CbsRequest request) { + try { + return new URL( + this.protocol, + cbsAddress.getHostString(), + cbsAddress.getPort(), + request.requestPath().getForService(serviceName)); + } catch (MalformedURLException e) { + throw new IllegalArgumentException("Invalid CBS URL", e); + } + } +} \ No newline at end of file diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java index 36555c2b..0a3b9657 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java @@ -2,7 +2,7 @@ * ============LICENSE_START==================================== * DCAEGEN2-SERVICES-SDK * ========================================================= - * Copyright (C) 2019 Nokia. All rights reserved. + * Copyright (C) 2019-2021 Nokia. 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. @@ -126,6 +126,10 @@ public interface CbsClientConfiguration { default String cbsName() { return "config-binding-service"; } + @Value.Default + default String configMapFilePath() { + return "/app-config/application_config.yaml"; + } /** * Creates CbsClientConfiguration from system environment variables. diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMapTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMapTest.java new file mode 100644 index 00000000..07b44e9e --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMapTest.java @@ -0,0 +1,57 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2021 Nokia. 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.sdk.rest.services.cbs.client.impl; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; +import org.onap.dcaegen2.services.sdk.services.common.FileReader; +import org.yaml.snakeyaml.Yaml; +import java.util.LinkedHashMap; +import static org.assertj.core.api.Assertions.assertThat; + +public class CbsClientConfigMapTest { + + @Test + void shouldFetchUsingProperConfigMapFile() { + // given + String configMapFilePath = "src/test/resources/application_config.yaml"; + final CbsClient cut = new CbsClientConfigMap(configMapFilePath); + + RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create(); + + // when + final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block(); + + // then + assertThat(result).isNotNull(); + assertThat(result).isEqualTo(covertYamlToJson(configMapFilePath)); + } + + private JsonObject covertYamlToJson(String configMapFilePath) { + Gson gson = new GsonBuilder().create(); + return gson.fromJson(gson.toJson(new Yaml().load(new FileReader(configMapFilePath).getContent()), + LinkedHashMap.class), JsonObject.class); + } +} diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java index 5804c165..83743a6c 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java @@ -2,7 +2,7 @@ * ============LICENSE_START==================================== * DCAEGEN2-SERVICES-SDK * ========================================================= - * Copyright (C) 2019 Nokia. All rights reserved. + * Copyright (C) 2019-2021 Nokia. 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. @@ -22,6 +22,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; import com.google.gson.JsonObject; import io.vavr.collection.Stream; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -64,8 +65,11 @@ class CbsClientImplIT { private static final String SAMPLE_ALL = "/sample_all.json"; private static final String SAMPLE_KEY = "/sample_key.json"; private static final String SAMPLE_CONFIG_KEY = "keystore.path"; - private static final String EXPECTED_CONFIG_VALUE = "/var/run/security/keystore.p12"; - private static CbsClientConfiguration sampleConfiguration; + private static final String EXPECTED_CONFIG_VALUE_FROM_CBS = "/var/run/security/keystore.p12"; + private static final String EXPECTED_CONFIG_VALUE_FROM_FILE = "/var/run/security/keystore_file.p12"; + private static final String CONFIG_MAP_FILE_PATH = "src/test/resources/sample_local_service_config.json"; + private static CbsClientConfiguration sampleConfigurationCbsSource; + private static CbsClientConfiguration sampleConfigurationFileSource; private static DummyHttpServer server; @BeforeAll @@ -75,12 +79,9 @@ class CbsClientImplIT { .get("/service_component_all/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_ALL)) .get("/sampleKey/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_KEY)) ); - sampleConfiguration = ImmutableCbsClientConfiguration.builder() - .protocol("http") - .appName("dcae-component") - .hostname(server.host()) - .port(server.port()) - .build(); + ImmutableCbsClientConfiguration.Builder configBuilder = getConfigBuilder(); + sampleConfigurationCbsSource = configBuilder.build(); + sampleConfigurationFileSource = configBuilder.configMapFilePath(CONFIG_MAP_FILE_PATH).build(); } @AfterAll @@ -91,7 +92,7 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleCall() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // when @@ -99,7 +100,7 @@ class CbsClientImplIT { // then StepVerifier.create(result.map(this::sampleConfigValue)) - .expectNext(EXPECTED_CONFIG_VALUE) + .expectNext(EXPECTED_CONFIG_VALUE_FROM_CBS) .expectComplete() .verify(Duration.ofSeconds(5)); } @@ -107,7 +108,7 @@ class CbsClientImplIT { @Test void testCbsClientWithPeriodicCall() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // when @@ -117,7 +118,7 @@ class CbsClientImplIT { // then final int itemsToTake = 5; StepVerifier.create(result.take(itemsToTake).map(this::sampleConfigValue)) - .expectNextSequence(Stream.of(EXPECTED_CONFIG_VALUE).cycle(itemsToTake)) + .expectNextSequence(Stream.of(EXPECTED_CONFIG_VALUE_FROM_CBS).cycle(itemsToTake)) .expectComplete() .verify(Duration.ofSeconds(5)); } @@ -125,7 +126,7 @@ class CbsClientImplIT { @Test void testCbsClientWithUpdatesCall() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); final Duration period = Duration.ofMillis(10); @@ -136,7 +137,23 @@ class CbsClientImplIT { // then final Duration timeToCollectItemsFor = period.multipliedBy(50); StepVerifier.create(result.take(timeToCollectItemsFor).map(this::sampleConfigValue)) - .expectNext(EXPECTED_CONFIG_VALUE) + .expectNext(EXPECTED_CONFIG_VALUE_FROM_CBS) + .expectComplete() + .verify(Duration.ofSeconds(5)); + } + + @Test + void testCbsClientWithConfigRetrievedFromFile() { + // given + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationFileSource); + final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); + + // when + final Mono result = sut.flatMap(cbsClient -> cbsClient.get(request)); + + // then + StepVerifier.create(result.map(this::sampleConfigValue)) + .expectNext(EXPECTED_CONFIG_VALUE_FROM_FILE) .expectComplete() .verify(Duration.ofSeconds(5)); } @@ -144,7 +161,7 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsing() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final StreamFromGsonParser kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser(); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -168,7 +185,7 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsingUsingSwitch() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // TODO: Use these parsers below final StreamFromGsonParser kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser(); @@ -204,7 +221,7 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsingWhenUsingInvalidParser() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final StreamFromGsonParser kafkaSourceParser = StreamFromGsonParsers.kafkaSourceParser(); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -228,7 +245,7 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleAllRequest() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getAll(RequestDiagnosticContext.create()); // when @@ -249,7 +266,7 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleKeyRequest() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getByKey(RequestDiagnosticContext.create(), "sampleKey"); // when @@ -268,7 +285,7 @@ class CbsClientImplIT { @Test void testCbsClientWhenTheConfigurationWasNotFound() { // given - final CbsClientConfiguration unknownAppEnv = ImmutableCbsClientConfiguration.copyOf(sampleConfiguration).withAppName("unknown_app"); + final CbsClientConfiguration unknownAppEnv = ImmutableCbsClientConfiguration.copyOf(sampleConfigurationCbsSource).withAppName("unknown_app"); final Mono sut = CbsClientFactory.createCbsClient(unknownAppEnv); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -281,6 +298,15 @@ class CbsClientImplIT { .verify(Duration.ofSeconds(5)); } + @NotNull + private static ImmutableCbsClientConfiguration.Builder getConfigBuilder() { + return ImmutableCbsClientConfiguration.builder() + .protocol("http") + .appName("dcae-component") + .hostname(server.host()) + .port(server.port()); + } + private String sampleConfigValue(JsonObject obj) { return obj.get(SAMPLE_CONFIG_KEY).getAsString(); } diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java deleted file mode 100644 index c9f92717..00000000 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * ============LICENSE_START==================================== - * DCAEGEN2-SERVICES-SDK - * ========================================================= - * Copyright (C) 2019-2021 Nokia. 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.sdk.rest.services.cbs.client.impl; - -import com.google.gson.JsonObject; -import io.vavr.collection.HashMultimap; -import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpResponse; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests; -import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; -import reactor.core.publisher.Mono; - -import java.net.InetSocketAddress; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -/** - * @author Piotr Jaszczyk - * @since February 2019 - */ -class CbsClientImplTest { - private final RxHttpClient httpClient = mock(RxHttpClient.class); - - @Test - void shouldFetchUsingProperUrl() { - // given - InetSocketAddress cbsAddress = InetSocketAddress.createUnresolved("cbshost", 6969); - String serviceName = "dcaegen2-ves-collector"; - final CbsClient cut = new CbsClientImpl(httpClient, serviceName, cbsAddress, "http"); - final HttpResponse httpResponse = ImmutableHttpResponse.builder() - .url("http://xxx") - .statusCode(200) - .rawBody("{}".getBytes()) - .headers(HashMultimap.withSeq().empty()) - .build(); - given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse)); - RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create(); - - // when - final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block(); - - // then - final String expectedUrl = "http://cbshost:6969/service_component/dcaegen2-ves-collector"; - verify(httpClient).call(ImmutableHttpRequest.builder() - .method(HttpMethod.GET) - .url(expectedUrl) - .diagnosticContext(diagnosticContext) - .build()); - assertThat(result.toString()).isEqualTo(httpResponse.bodyAsString()); - } -} diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRestTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRestTest.java new file mode 100644 index 00000000..6368fbac --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientRestTest.java @@ -0,0 +1,77 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019-2021 Nokia. 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.sdk.rest.services.cbs.client.impl; + +import com.google.gson.JsonObject; +import io.vavr.collection.HashMultimap; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; +import reactor.core.publisher.Mono; +import java.net.InetSocketAddress; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +/** + * @author Piotr Jaszczyk + * @since February 2019 + */ +class CbsClientRestTest { + private final RxHttpClient httpClient = mock(RxHttpClient.class); + + @Test + void shouldFetchUsingProperUrl() { + // given + InetSocketAddress cbsAddress = InetSocketAddress.createUnresolved("cbshost", 6969); + String serviceName = "dcaegen2-ves-collector"; + final CbsClient cut = new CbsClientRest(httpClient, serviceName, cbsAddress, "http"); + final HttpResponse httpResponse = ImmutableHttpResponse.builder() + .url("http://xxx") + .statusCode(200) + .rawBody("{}".getBytes()) + .headers(HashMultimap.withSeq().empty()) + .build(); + given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse)); + RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create(); + + // when + final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block(); + + // then + final String expectedUrl = "http://cbshost:6969/service_component/dcaegen2-ves-collector"; + verify(httpClient).call(ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(expectedUrl) + .diagnosticContext(diagnosticContext) + .build()); + assertThat(result.toString()).isEqualTo(httpResponse.bodyAsString()); + } +} diff --git a/rest-services/cbs-client/src/test/resources/application_config.yaml b/rest-services/cbs-client/src/test/resources/application_config.yaml new file mode 100644 index 00000000..c43b9733 --- /dev/null +++ b/rest-services/cbs-client/src/test/resources/application_config.yaml @@ -0,0 +1,10 @@ +--- +streams_publishes: + perf3gpp: + type: kafka + aaf_credentials: + username: admin + password: admin_secret + kafka_info: + bootstrap_servers: message-router-kafka-0:9093 + topic_name: HV_VES_PERF3GPP diff --git a/rest-services/cbs-client/src/test/resources/sample_local_service_config.json b/rest-services/cbs-client/src/test/resources/sample_local_service_config.json new file mode 100644 index 00000000..af0ce6d5 --- /dev/null +++ b/rest-services/cbs-client/src/test/resources/sample_local_service_config.json @@ -0,0 +1,33 @@ +{ + "keystore.path": "/var/run/security/keystore_file.p12", + "streams_publishes": { + "perf3gpp": { + "type": "kafka", + "kafka_info": { + "bootstrap_servers": "dmaap-mr-kafka:6060", + "topic_name": "HVVES_PERF3GPP" + } + }, + "pnf_ready": { + "type": "message_router", + "dmaap_info": { + "topic_url": "http://message-router:3904/events/VES_PNF_READY" + } + }, + "call_trace": { + "type": "kafka", + "kafka_info": { + "bootstrap_servers": "dmaap-mr-kafka:6060", + "topic_name": "HVVES_TRACE" + } + } + }, + "streams_subscribes": { + "measurements": { + "type": "message_router", + "dmaap_info": { + "topic_url": "http://message-router:3904/events/VES_MEASUREMENT" + } + } + } +} -- cgit 1.2.3-korg