2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * =========================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.onap.dcaegen2.services.sdk.model.streams.StreamType.KAFKA;
25 import static org.onap.dcaegen2.services.sdk.model.streams.StreamType.MESSAGE_ROUTER;
26 import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendResource;
27 import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendString;
28 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamPredicates.streamOfType;
30 import com.google.gson.JsonObject;
31 import io.vavr.collection.Stream;
32 import java.time.Duration;
33 import org.junit.jupiter.api.AfterAll;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.onap.dcaegen2.services.sdk.model.streams.RawDataStream;
37 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.KafkaSink;
38 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.KafkaSource;
39 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSink;
40 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.exceptions.HttpException;
41 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer;
42 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
43 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
44 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
45 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.StreamParsingException;
46 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.DataStreams;
47 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParser;
48 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParsers;
49 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
50 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
51 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
52 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
53 import reactor.core.publisher.Flux;
54 import reactor.core.publisher.Mono;
55 import reactor.test.StepVerifier;
58 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
59 * @since February 2019
61 class CbsClientImplIT {
63 private static final String CONSUL_RESPONSE = "[\n"
65 + " \"ServiceAddress\": \"HOST\",\n"
66 + " \"ServiceName\": \"the_cbs\",\n"
67 + " \"ServicePort\": PORT\n"
70 private static final String SAMPLE_CONFIG = "/sample_service_config.json";
71 private static final String SAMPLE_ALL = "/sample_all.json";
72 private static final String SAMPLE_KEY = "/sample_key.json";
73 private static final String SAMPLE_CONFIG_KEY = "keystore.path";
74 private static final String EXPECTED_CONFIG_VALUE = "/var/run/security/keystore.p12";
75 private static EnvProperties sampleEnvironment;
76 private static DummyHttpServer server;
80 server = DummyHttpServer.start(routes ->
81 routes.get("/v1/catalog/service/the_cbs", (req, resp) -> sendString(resp, lazyConsulResponse()))
82 .get("/service_component/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_CONFIG))
83 .get("/service_component_all/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_ALL))
84 .get("/sampleKey/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_KEY))
86 sampleEnvironment = ImmutableEnvProperties.builder()
87 .appName("dcae-component")
89 .consulHost(server.host())
90 .consulPort(server.port())
95 static void tearDown() {
100 void testCbsClientWithSingleCall() {
102 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
103 final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
106 final Mono<JsonObject> result = sut.flatMap(cbsClient -> cbsClient.get(request));
109 StepVerifier.create(result.map(this::sampleConfigValue))
110 .expectNext(EXPECTED_CONFIG_VALUE)
112 .verify(Duration.ofSeconds(5));
116 void testCbsClientWithPeriodicCall() {
118 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
119 final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
122 final Flux<JsonObject> result = sut
123 .flatMapMany(cbsClient -> cbsClient.get(request, Duration.ZERO, Duration.ofMillis(10)));
126 final int itemsToTake = 5;
127 StepVerifier.create(result.take(itemsToTake).map(this::sampleConfigValue))
128 .expectNextSequence(Stream.of(EXPECTED_CONFIG_VALUE).cycle(itemsToTake))
130 .verify(Duration.ofSeconds(5));
134 void testCbsClientWithUpdatesCall() {
136 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
137 final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
138 final Duration period = Duration.ofMillis(10);
141 final Flux<JsonObject> result = sut
142 .flatMapMany(cbsClient -> cbsClient.updates(request, Duration.ZERO, period));
145 final Duration timeToCollectItemsFor = period.multipliedBy(50);
146 StepVerifier.create(result.take(timeToCollectItemsFor).map(this::sampleConfigValue))
147 .expectNext(EXPECTED_CONFIG_VALUE)
149 .verify(Duration.ofSeconds(5));
153 void testCbsClientWithStreamsParsing() {
155 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
156 final StreamFromGsonParser<KafkaSink> kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser();
157 final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
160 final Mono<KafkaSink> result = sut.flatMap(cbsClient -> cbsClient.get(request))
162 DataStreams.namedSinks(json).map(kafkaSinkParser::unsafeParse).head()
166 StepVerifier.create(result)
167 .consumeNextWith(kafkaSink -> {
168 assertThat(kafkaSink.name()).isEqualTo("perf3gpp");
169 assertThat(kafkaSink.bootstrapServers()).isEqualTo("dmaap-mr-kafka:6060");
170 assertThat(kafkaSink.topicName()).isEqualTo("HVVES_PERF3GPP");
173 .verify(Duration.ofSeconds(5));
177 void testCbsClientWithStreamsParsingUsingSwitch() {
179 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
180 final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
181 // TODO: Use these parsers below
182 final StreamFromGsonParser<KafkaSink> kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser();
183 final StreamFromGsonParser<MessageRouterSink> mrSinkParser = StreamFromGsonParsers.messageRouterSinkParser();
186 final Mono<Void> result = sut.flatMap(cbsClient -> cbsClient.get(request))
188 final Stream<RawDataStream<JsonObject>> sinks = DataStreams.namedSinks(json);
190 final Stream<KafkaSink> allKafkaSinks = sinks.filter(streamOfType(KAFKA))
191 .map(kafkaSinkParser::unsafeParse);
192 final Stream<MessageRouterSink> allMrSinks = sinks.filter(streamOfType(MESSAGE_ROUTER))
193 .map(mrSinkParser::unsafeParse);
195 assertThat(allKafkaSinks.size())
196 .describedAs("Number of kafka sinks")
198 assertThat(allMrSinks.size())
199 .describedAs("Number of DMAAP-MR sinks")
207 StepVerifier.create(result)
209 .verify(Duration.ofSeconds(5));
213 void testCbsClientWithStreamsParsingWhenUsingInvalidParser() {
215 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
216 final StreamFromGsonParser<KafkaSource> kafkaSourceParser = StreamFromGsonParsers.kafkaSourceParser();
217 final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
220 final Mono<KafkaSource> result = sut.flatMap(cbsClient -> cbsClient.get(request))
222 DataStreams.namedSources(json).map(kafkaSourceParser::unsafeParse).head()
226 StepVerifier.create(result)
227 .expectErrorSatisfies(ex -> {
228 assertThat(ex).isInstanceOf(StreamParsingException.class);
229 assertThat(ex).hasMessageContaining("Invalid stream type");
230 assertThat(ex).hasMessageContaining(MESSAGE_ROUTER.toString());
231 assertThat(ex).hasMessageContaining(KAFKA.toString());
233 .verify(Duration.ofSeconds(5));
237 void testCbsClientWithSingleAllRequest() {
239 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
240 final CbsRequest request = CbsRequests.getAll(RequestDiagnosticContext.create());
243 final Mono<JsonObject> result = sut.flatMap(cbsClient -> cbsClient.get(request));
246 StepVerifier.create(result)
247 .assertNext(json -> {
248 assertThat(json.get("config")).isNotNull();
249 assertThat(json.get("policies")).isNotNull();
250 assertThat(json.get("sampleKey")).isNotNull();
253 .verify(Duration.ofSeconds(5));
258 void testCbsClientWithSingleKeyRequest() {
260 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
261 final CbsRequest request = CbsRequests.getByKey(RequestDiagnosticContext.create(), "sampleKey");
264 final Mono<JsonObject> result = sut.flatMap(cbsClient -> cbsClient.get(request));
267 StepVerifier.create(result)
268 .assertNext(json -> {
269 assertThat(json.get("key")).isNotNull();
270 assertThat(json.get("key").getAsString()).isEqualTo("value");
273 .verify(Duration.ofSeconds(5));
277 void testCbsClientWhenTheConfigurationWasNotFound() {
279 final EnvProperties unknownAppEnv = ImmutableEnvProperties.copyOf(sampleEnvironment).withAppName("unknown_app");
280 final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(unknownAppEnv);
281 final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
284 final Mono<JsonObject> result = sut.flatMap(cbsClient -> cbsClient.get(request));
287 StepVerifier.create(result)
288 .expectError(HttpException.class)
289 .verify(Duration.ofSeconds(5));
292 private String sampleConfigValue(JsonObject obj) {
293 return obj.get(SAMPLE_CONFIG_KEY).getAsString();
296 private static Mono<String> lazyConsulResponse() {
297 return Mono.just(CONSUL_RESPONSE)
298 .map(CbsClientImplIT::processConsulResponseTemplate);
301 private static String processConsulResponseTemplate(String resp) {
302 return resp.replaceAll("HOST", server.host())
303 .replaceAll("PORT", Integer.toString(server.port()));