309bb62f5adc141e4f855b1431761861eb8d29ed
[dcaegen2/services/sdk.git] /
1 /*
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
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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=====================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
22
23 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendResource;
24 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendString;
25
26 import com.google.gson.JsonObject;
27 import io.vavr.collection.Stream;
28 import java.time.Duration;
29 import org.junit.jupiter.api.AfterAll;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.EnvProperties;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.ImmutableEnvProperties;
37 import reactor.core.publisher.Flux;
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
40
41 /**
42  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
43  * @since February 2019
44  */
45 class CbsClientImplIT {
46
47     private static final String CONSUL_RESPONSE = "[\n"
48             + "    {\n"
49             + "        \"ServiceAddress\": \"HOST\",\n"
50             + "        \"ServiceName\": \"the_cbs\",\n"
51             + "        \"ServicePort\": PORT\n"
52             + "    }\n"
53             + "]\n";
54     private static final String SAMPLE_CONFIG = "/sample_config.json";
55     private static final String SAMPLE_CONFIG_KEY = "keystore.path";
56     private static final String EXPECTED_CONFIG_VALUE = "/var/run/security/keystore.p12";
57     private static EnvProperties sampleEnvironment;
58     private static DummyHttpServer server;
59
60     @BeforeAll
61     static void setUp() {
62         server = DummyHttpServer.start(routes ->
63                 routes.get("/v1/catalog/service/the_cbs", (req, resp) -> sendString(resp, lazyConsulResponse()))
64                         .get("/service_component/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_CONFIG)));
65         sampleEnvironment = ImmutableEnvProperties.builder()
66                 .appName("dcae-component")
67                 .cbsName("the_cbs")
68                 .consulHost(server.host())
69                 .consulPort(server.port())
70                 .build();
71     }
72
73     @AfterAll
74     static void tearDown() {
75         server.close();
76     }
77
78     @Test
79     void testCbsClientWithSingleCall() {
80         // given
81         final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
82         final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
83
84         // when
85         final Mono<JsonObject> result = sut.flatMap(cbsClient -> cbsClient.get(diagnosticContext));
86
87         // then
88         StepVerifier.create(result.map(this::sampleConfigValue))
89                 .expectNext(EXPECTED_CONFIG_VALUE)
90                 .expectComplete()
91                 .verify(Duration.ofSeconds(5));
92     }
93
94     @Test
95     void testCbsClientWithPeriodicCall() {
96         // given
97         final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment);
98         final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
99
100         // when
101         final Flux<JsonObject> result = sut.flatMapMany(cbsClient -> cbsClient.get(diagnosticContext, Duration.ZERO, Duration.ofMillis(10)));
102
103         // then
104         final int itemsToTake = 5;
105         StepVerifier.create(result.take(itemsToTake).map(this::sampleConfigValue))
106                 .expectNextSequence(Stream.of(EXPECTED_CONFIG_VALUE).cycle(itemsToTake))
107                 .expectComplete()
108                 .verify(Duration.ofSeconds(5));
109     }
110
111     private String sampleConfigValue(JsonObject obj) {
112         return obj.get(SAMPLE_CONFIG_KEY).getAsString();
113     }
114
115     private static Mono<String> lazyConsulResponse() {
116         return Mono.just(CONSUL_RESPONSE)
117                 .map(CbsClientImplIT::processConsulResponseTemplate);
118     }
119
120     private static String processConsulResponseTemplate(String resp) {
121         return resp.replaceAll("HOST", server.host())
122                 .replaceAll("PORT", Integer.toString(server.port()));
123     }
124 }