From 9779ca13ed180fc69cdad5336b30a26edfd7d0fa Mon Sep 17 00:00:00 2001 From: Tomasz Pietruszkiewicz Date: Thu, 6 May 2021 10:27:19 +0200 Subject: Add to Java CBS-Client ability to resolve evns in app-config.yaml loaded from ConfigMap Change-Id: Ic85c1511133594500bdf31b9d491cc70d34ff5d5 Issue-ID: DCAEGEN2-2716 Signed-off-by: Tomasz Pietruszkiewicz --- .../cbs/client/impl/CbsClientConfigMapTest.java | 23 ++++++--- .../impl/CbsClientEnvironmentParsingTest.java | 57 ++++++++++++++++++++++ .../services/cbs/client/impl/CbsClientImplIT.java | 45 +++++++++++++++-- .../src/test/resources/application_config.yaml | 36 ++++++++++++-- .../resources/sample_expected_service_config.json | 53 ++++++++++++++++++++ .../src/test/resources/sample_service_config.json | 16 ++++++ 6 files changed, 215 insertions(+), 15 deletions(-) create mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsingTest.java create mode 100644 rest-services/cbs-client/src/test/resources/sample_expected_service_config.json (limited to 'rest-services/cbs-client/src/test') 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 index 07b44e9e..a9d8407b 100644 --- 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 @@ -22,20 +22,28 @@ 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 com.google.gson.stream.JsonReader; +import org.junit.Rule; +import org.junit.contrib.java.lang.system.EnvironmentVariables; 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 java.io.FileNotFoundException; +import java.io.FileReader; import static org.assertj.core.api.Assertions.assertThat; public class CbsClientConfigMapTest { + private static final String SAMPLE_EXPECTED_CONFIG = "src/test/resources/sample_expected_service_config.json"; + @Rule + public final EnvironmentVariables envs = new EnvironmentVariables(); + @Test - void shouldFetchUsingProperConfigMapFile() { + void shouldFetchUsingProperConfigMapFile() throws FileNotFoundException { // given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); String configMapFilePath = "src/test/resources/application_config.yaml"; final CbsClient cut = new CbsClientConfigMap(configMapFilePath); @@ -46,12 +54,11 @@ public class CbsClientConfigMapTest { // then assertThat(result).isNotNull(); - assertThat(result).isEqualTo(covertYamlToJson(configMapFilePath)); + assertThat(result).isEqualTo(convertToJson(new JsonReader(new FileReader(SAMPLE_EXPECTED_CONFIG)))); } - private JsonObject covertYamlToJson(String configMapFilePath) { + private JsonObject convertToJson(JsonReader jsonReader) { Gson gson = new GsonBuilder().create(); - return gson.fromJson(gson.toJson(new Yaml().load(new FileReader(configMapFilePath).getContent()), - LinkedHashMap.class), JsonObject.class); + return gson.fromJson(jsonReader, JsonObject.class); } } diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsingTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsingTest.java new file mode 100644 index 00000000..41d757fd --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientEnvironmentParsingTest.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 com.google.gson.stream.JsonReader; +import org.junit.Rule; +import org.junit.contrib.java.lang.system.EnvironmentVariables; +import org.junit.jupiter.api.Test; +import java.io.FileNotFoundException; +import java.io.FileReader; +import static org.assertj.core.api.Assertions.assertThat; + +public class CbsClientEnvironmentParsingTest { + + private static final String SAMPLE_CONFIG = "src/test/resources/sample_service_config.json"; + private static final String SAMPLE_EXPECTED_CONFIG = "src/test/resources/sample_expected_service_config.json"; + @Rule + public final EnvironmentVariables envs = new EnvironmentVariables(); + + @Test + void shouldProcessEnvironmentVariables() throws FileNotFoundException { + //given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); + JsonObject jsonObject = getSampleJsonObject(SAMPLE_CONFIG); + //when + JsonObject result = CbsClientEnvironmentParsing.processEnvironmentVariables(jsonObject); + //then + assertThat(result).isEqualTo(getSampleJsonObject(SAMPLE_EXPECTED_CONFIG)); + } + + private JsonObject getSampleJsonObject(String file) throws FileNotFoundException { + Gson gson = new GsonBuilder().create(); + JsonReader reader = new JsonReader(new FileReader(file)); + return gson.fromJson(reader, 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 83743a6c..db881a2e 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 @@ -23,6 +23,8 @@ 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.Rule; +import org.junit.contrib.java.lang.system.EnvironmentVariables; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -35,6 +37,7 @@ import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttp import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.EnvironmentParsingException; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.StreamParsingException; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.DataStreams; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParser; @@ -66,12 +69,15 @@ class CbsClientImplIT { 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_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 final String CONFIG_MAP_FILE_PATH = "src/test/resources/application_config.yaml"; private static CbsClientConfiguration sampleConfigurationCbsSource; private static CbsClientConfiguration sampleConfigurationFileSource; private static DummyHttpServer server; + + @Rule + public final EnvironmentVariables envs = new EnvironmentVariables(); + @BeforeAll static void setUp() { server = DummyHttpServer.start(routes -> @@ -92,6 +98,8 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleCall() { // given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -108,6 +116,8 @@ class CbsClientImplIT { @Test void testCbsClientWithPeriodicCall() { // given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -126,6 +136,8 @@ class CbsClientImplIT { @Test void testCbsClientWithUpdatesCall() { // given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); final Duration period = Duration.ofMillis(10); @@ -142,9 +154,30 @@ class CbsClientImplIT { .verify(Duration.ofSeconds(5)); } + @Test + void testCbsClientWithConfigRetrievedFromFileMissingEnv() { + // given + envs.set("AAF_USER", ""); + 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) + .expectErrorSatisfies(ex -> { + assertThat(ex).isInstanceOf(EnvironmentParsingException.class); + assertThat(ex).hasMessageContaining("Cannot read AAF_USER from environment."); + }) + .verify(Duration.ofSeconds(5)); + } + @Test void testCbsClientWithConfigRetrievedFromFile() { // given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationFileSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -153,7 +186,7 @@ class CbsClientImplIT { // then StepVerifier.create(result.map(this::sampleConfigValue)) - .expectNext(EXPECTED_CONFIG_VALUE_FROM_FILE) + .expectNext(EXPECTED_CONFIG_VALUE_FROM_CBS) .expectComplete() .verify(Duration.ofSeconds(5)); } @@ -161,6 +194,8 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsing() { // given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final StreamFromGsonParser kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser(); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -185,6 +220,8 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsingUsingSwitch() { // given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // TODO: Use these parsers below @@ -221,6 +258,8 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsingWhenUsingInvalidParser() { // given + envs.set("AAF_USER", "admin"); + envs.set("AAF_PASSWORD", "admin_secret"); final Mono sut = CbsClientFactory.createCbsClient(sampleConfigurationCbsSource); final StreamFromGsonParser kafkaSourceParser = StreamFromGsonParsers.kafkaSourceParser(); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); diff --git a/rest-services/cbs-client/src/test/resources/application_config.yaml b/rest-services/cbs-client/src/test/resources/application_config.yaml index c43b9733..a2466802 100644 --- a/rest-services/cbs-client/src/test/resources/application_config.yaml +++ b/rest-services/cbs-client/src/test/resources/application_config.yaml @@ -1,10 +1,38 @@ --- +keystore.path: "/var/run/security/keystore.p12" streams_publishes: perf3gpp: + testArray: + - testPrimitiveArray: + - "${AAF_USER}" + - "${AAF_PASSWORD}" + - nestedArray: + - "${AAF_USER}" + testPrimitive: "${AAF_USER}" + aaf_credentials: + username: "${AAF_USER}" + password: "${AAF_PASSWORD}" type: kafka + kafka_info: + bootstrap_servers: dmaap-mr-kafka:6060 + topic_name: HVVES_PERF3GPP + pnf_ready: + aaf_credentials: + username: "${AAF_USER}" + password: "${AAF_PASSWORD}" + type: message_router + dmaap_info: + topic_url: http://message-router:3904/events/VES_PNF_READY + call_trace: aaf_credentials: - username: admin - password: admin_secret + username: "${AAF_USER}" + password: "${AAF_PASSWORD}" + type: kafka kafka_info: - bootstrap_servers: message-router-kafka-0:9093 - topic_name: HV_VES_PERF3GPP + 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 diff --git a/rest-services/cbs-client/src/test/resources/sample_expected_service_config.json b/rest-services/cbs-client/src/test/resources/sample_expected_service_config.json new file mode 100644 index 00000000..2ceda99a --- /dev/null +++ b/rest-services/cbs-client/src/test/resources/sample_expected_service_config.json @@ -0,0 +1,53 @@ +{ + "keystore.path": "/var/run/security/keystore.p12", + "streams_publishes": { + "perf3gpp": { + "testArray": [{ + "testPrimitiveArray": ["admin", "admin_secret", { + "nestedArray": ["admin"] + } + ], + "testPrimitive": "admin", + "aaf_credentials": { + "username": "admin", + "password": "admin_secret" + } + } + ], + "type": "kafka", + "kafka_info": { + "bootstrap_servers": "dmaap-mr-kafka:6060", + "topic_name": "HVVES_PERF3GPP" + } + }, + "pnf_ready": { + "aaf_credentials": { + "username": "admin", + "password": "admin_secret" + }, + "type": "message_router", + "dmaap_info": { + "topic_url": "http://message-router:3904/events/VES_PNF_READY" + } + }, + "call_trace": { + "aaf_credentials": { + "username": "admin", + "password": "admin_secret" + }, + "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" + } + } + } +} diff --git a/rest-services/cbs-client/src/test/resources/sample_service_config.json b/rest-services/cbs-client/src/test/resources/sample_service_config.json index 266326f4..e5798597 100644 --- a/rest-services/cbs-client/src/test/resources/sample_service_config.json +++ b/rest-services/cbs-client/src/test/resources/sample_service_config.json @@ -2,6 +2,14 @@ "keystore.path": "/var/run/security/keystore.p12", "streams_publishes": { "perf3gpp": { + "testArray": [{ + "testPrimitiveArray": ["${AAF_USER}", "${AAF_PASSWORD}", {"nestedArray": ["${AAF_USER}"]}], + "testPrimitive": "${AAF_USER}", + "aaf_credentials": { + "username": "${AAF_USER}", + "password": "${AAF_PASSWORD}" + } + }], "type": "kafka", "kafka_info": { "bootstrap_servers": "dmaap-mr-kafka:6060", @@ -9,12 +17,20 @@ } }, "pnf_ready": { + "aaf_credentials": { + "username": "${AAF_USER}", + "password": "${AAF_PASSWORD}" + }, "type": "message_router", "dmaap_info": { "topic_url": "http://message-router:3904/events/VES_PNF_READY" } }, "call_trace": { + "aaf_credentials": { + "username": "${AAF_USER}", + "password": "${AAF_PASSWORD}" + }, "type": "kafka", "kafka_info": { "bootstrap_servers": "dmaap-mr-kafka:6060", -- cgit 1.2.3-korg