94ff53f982d3472021aed1eb1f456aa593cb88da
[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
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.BDDMockito.given;
26 import static org.mockito.Mockito.mock;
27
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonParser;
31 import java.io.InputStreamReader;
32 import java.net.InetSocketAddress;
33 import org.junit.jupiter.api.Test;
34 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.ServiceLookupException;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
37 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
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 CbsLookupTest {
46
47     private final EnvProperties env = ImmutableEnvProperties.builder()
48             .cbsName("cbs-service")
49             .consulHost("consul.local")
50             .consulPort(8050)
51             .appName("whatever").build();
52     private final CloudHttpClient httpClient = mock(CloudHttpClient.class);
53     private final CbsLookup cut = new CbsLookup(httpClient);
54
55     @Test
56     void lookupShouldReturnValidConfiguration() {
57         // given
58         givenConsulResponse(parseResource("/consul_cbs_service.json").getAsJsonArray());
59
60         // when
61         final InetSocketAddress result = cut.lookup(env).block();
62
63         // then
64         assertThat(result.getHostString()).isEqualTo("config-binding-service");
65         assertThat(result.getPort()).isEqualTo(10000);
66     }
67
68     @Test
69     void lookupShouldEmitErrorWhenServiceArrayIsEmpty() {
70         // given
71         givenConsulResponse(new JsonArray());
72
73         // when
74         final Mono<InetSocketAddress> result = cut.lookup(env);
75
76         // then
77         StepVerifier.create(result).verifyError(ServiceLookupException.class);
78     }
79
80     private JsonElement parseResource(String resource) {
81         return new JsonParser().parse(new InputStreamReader(CbsLookupTest.class.getResourceAsStream(resource)));
82     }
83
84     private void givenConsulResponse(JsonArray jsonArray) {
85         final String url = "http://"
86                 + env.consulHost()
87                 + ":"
88                 + env.consulPort()
89                 + "/v1/catalog/service/"
90                 + env.cbsName();
91         given(httpClient.get(url, JsonArray.class))
92                 .willReturn(Mono.just(jsonArray));
93     }
94
95 }