Omit CBS lookup in Consul
[dcaegen2/services/sdk.git] / rest-services / cbs-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / cbs / client / providers / ReactiveCloudConfigurationProviderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018-2019 NOKIA Intellectual Property. 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.providers;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import com.google.gson.Gson;
30 import com.google.gson.JsonArray;
31 import com.google.gson.JsonObject;
32 import java.io.IOException;
33 import java.util.List;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest;
38 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
39 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
40 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.GsonUtils;
41 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
42 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration;
43 import reactor.core.publisher.Mono;
44 import reactor.test.StepVerifier;
45
46 /**
47  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
48  */
49 class ReactiveCloudConfigurationProviderTest {
50
51     private static final Gson gson = new Gson();
52     private static final String CONFIGURATION_MOCK = "{\"test\":1}";
53     private static final JsonObject CONFIGURATION_JSON_MOCK = gson
54             .fromJson(CONFIGURATION_MOCK, JsonObject.class);
55
56     private final RxHttpClient httpClient = mock(RxHttpClient.class);
57     private final JsonArray configBindingService = GsonUtils.readObjectArrayFromResource("/sample_config_binding_service.json");
58
59     private CbsClientConfiguration cbsClientConfiguration = ImmutableCbsClientConfiguration.builder()
60             .appName("dcae-prh")
61             .cbsName("config-binding-service")
62             .consulHost("consul")
63             .consulPort(8500)
64             .build();
65
66     private HttpResponse response;
67     private ReactiveCloudConfigurationProvider provider;
68
69     ReactiveCloudConfigurationProviderTest() throws IOException {
70     }
71
72
73     @BeforeEach
74     void setUp() {
75         response = mock(HttpResponse.class);
76         provider = new ReactiveCloudConfigurationProvider(httpClient);
77     }
78
79     @Test
80     void shouldReturnPrhConfiguration(){
81         //when
82         when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
83         when(response.bodyAsJson(JsonArray.class)).thenReturn(configBindingService);
84         when(response.bodyAsJson(JsonObject.class)).thenReturn(CONFIGURATION_JSON_MOCK);
85
86
87         //then
88         StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration))
89                 .expectSubscription()
90                 .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete();
91     }
92
93     @Test
94     void shouldRequestCorrectUrl(){
95         // given
96         String consulRequestUrl = "http://consul:8500/v1/catalog/service/config-binding-service";
97         String configRequestUrl = "http://config-binding-service:10000/service_component/dcae-prh";
98
99         //when
100         when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
101         when(response.bodyAsJson(JsonArray.class)).thenReturn(configBindingService);
102         when(response.bodyAsJson(JsonObject.class)).thenReturn(CONFIGURATION_JSON_MOCK);
103
104
105         //then
106         StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration))
107                 .expectSubscription()
108                 .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete();
109
110
111         ArgumentCaptor<HttpRequest> httpReq = ArgumentCaptor
112                 .forClass(HttpRequest.class);
113         verify(httpClient, times(2)).call(httpReq.capture());
114
115         List<HttpRequest> allRequests = httpReq.getAllValues();
116         assertThat(allRequests.get(0).url()).isEqualTo(consulRequestUrl);
117         assertThat(allRequests.get(1).url()).isEqualTo(configRequestUrl);
118     }
119
120     @Test
121     void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() {
122         // given
123         JsonArray emptyArray = gson.fromJson("[]", JsonArray.class);
124
125         //when
126         when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
127         when(response.bodyAsJson(JsonArray.class)).thenReturn(emptyArray);
128
129
130         //then
131         StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration))
132                 .expectSubscription()
133                 .expectError(IllegalStateException.class).verify();
134     }
135 }