761cc5c1bbda6856d71cc1f909d037ce215a0b3e
[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 java.time.Duration;
28 import org.junit.jupiter.api.AfterAll;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.EnvProperties;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.ImmutableEnvProperties;
35 import reactor.core.publisher.Mono;
36 import reactor.test.StepVerifier;
37
38 /**
39  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
40  * @since February 2019
41  */
42 class CbsClientImplIT {
43
44     private static final String CONSUL_RESP = "[\n"
45             + "    {\n"
46             + "        \"ServiceAddress\": \"HOST\",\n"
47             + "        \"ServiceName\": \"the_cbs\",\n"
48             + "        \"ServicePort\": PORT\n"
49             + "    }\n"
50             + "]\n";
51     private static final String RES_CONFIG = "/sample_config.json";
52     private static DummyHttpServer server;
53
54     @BeforeAll
55     static void setUp() {
56         server = DummyHttpServer.start(routes ->
57                 routes.get("/v1/catalog/service/the_cbs", (req, resp) -> sendString(resp, lazyConsulResponse()))
58                         .get("/service_component/dcae-component", (req, resp) -> sendResource(resp, RES_CONFIG)));
59     }
60
61     @AfterAll
62     static void tearDown() {
63         server.close();
64     }
65
66     @Test
67     void testCbsClient() {
68         // given
69         final EnvProperties env = ImmutableEnvProperties.builder()
70                 .appName("dcae-component")
71                 .cbsName("the_cbs")
72                 .consulHost(server.host())
73                 .consulPort(server.port())
74                 .build();
75         final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(env);
76
77         // when
78         final Mono<JsonObject> result = sut.flatMap(CbsClient::get);
79
80         // then
81         StepVerifier.create(result.map(obj -> obj.get("keystore.path").getAsString()))
82                 .expectNext("/var/run/security/keystore.p12")
83                 .expectComplete()
84                 .verify(Duration.ofSeconds(5));
85     }
86
87     private static Mono<String> lazyConsulResponse() {
88         return Mono.just(CONSUL_RESP)
89                 .map(CbsClientImplIT::processConsulResponseTemplate);
90     }
91
92     private static String processConsulResponseTemplate(String resp) {
93         return resp.replaceAll("HOST", server.host())
94                 .replaceAll("PORT", Integer.toString(server.port()));
95     }
96 }