e16605de0d13287420f9b5f8c56289a92c710059
[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.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.isA;
27 import static org.mockito.BDDMockito.given;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30
31 import com.google.gson.JsonArray;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonParser;
34 import java.io.InputStreamReader;
35 import java.net.InetSocketAddress;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod;
39 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest;
40 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpResponse;
41 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
42 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.ServiceLookupException;
43 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
44 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
45 import reactor.core.publisher.Mono;
46 import reactor.test.StepVerifier;
47
48 /**
49  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
50  * @since February 2019
51  */
52 class CbsLookupTest {
53
54     private final EnvProperties env = ImmutableEnvProperties.builder()
55             .cbsName("cbs-service")
56             .consulHost("consul.local")
57             .consulPort(8050)
58             .appName("whatever").build();
59     private final RxHttpClient httpClient = mock(RxHttpClient.class);
60     private final CbsLookup cut = new CbsLookup(httpClient);
61
62     @Test
63     void lookupShouldReturnValidConfiguration() {
64         // given
65         givenConsulResponse(parseResource("/consul_cbs_service.json").getAsJsonArray());
66
67         // when
68         final InetSocketAddress result = cut.lookup(env).block();
69
70         // then
71         assertThat(result.getHostString()).isEqualTo("config-binding-service");
72         assertThat(result.getPort()).isEqualTo(10000);
73
74         final String url = "http://"
75                 + env.consulHost()
76                 + ":"
77                 + env.consulPort()
78                 + "/v1/catalog/service/"
79                 + env.cbsName();
80         verifyHttpGetHasBeenCalled(url);
81     }
82
83     @Test
84     void lookupShouldEmitErrorWhenServiceArrayIsEmpty() {
85         // given
86         givenConsulResponse(new JsonArray());
87
88         // when
89         final Mono<InetSocketAddress> result = cut.lookup(env);
90
91         // then
92         StepVerifier.create(result).verifyError(ServiceLookupException.class);
93     }
94
95     private JsonElement parseResource(String resource) {
96         return new JsonParser().parse(new InputStreamReader(CbsLookupTest.class.getResourceAsStream(resource)));
97     }
98
99     private void givenConsulResponse(JsonArray jsonArray) {
100         given(httpClient.call(any(HttpRequest.class)))
101                 .willReturn(Mono.just(ImmutableHttpResponse.builder()
102                         .url("http://xxx")
103                         .statusCode(200)
104                         .rawBody(jsonArray.toString().getBytes())
105                         .build()));
106     }
107
108     private void verifyHttpGetHasBeenCalled(String url) {
109         final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
110         verify(httpClient).call(httpRequestArgumentCaptor.capture());
111         assertThat(httpRequestArgumentCaptor.getValue().url())
112                 .describedAs("HTTP request URL")
113                 .isEqualTo(url);
114         assertThat(httpRequestArgumentCaptor.getValue().method())
115                 .describedAs("HTTP request method")
116                 .isEqualTo(HttpMethod.GET);
117     }
118
119
120 }