Added cbs API
[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 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.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import com.google.gson.Gson;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonObject;
29 import org.junit.jupiter.api.Test;
30 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.CloudHttpClient;
31 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties;
33 import reactor.core.publisher.Mono;
34 import reactor.test.StepVerifier;
35
36 /**
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
38  */
39 class ReactiveCloudConfigurationProviderTest {
40
41     private static final Gson gson = new Gson();
42     private static final String configBindingService = "[{\"ID\":\"9c8dd674-34ce-7049-d318-e98d93a64303\",\"Node\""
43         + ":\"dcae-bootstrap\",\"Address\":\"10.42.52.82\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":"
44         + "{\"lan\":\"10.42.52.82\",\"wan\":\"10.42.52.82\"},\"NodeMeta\":{\"consul-network-segment\":\"\"},"
45         + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[],"
46         + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false,"
47         + "\"CreateIndex\":14352,\"ModifyIndex\":14352},{\"ID\":\"35c6f540-a29c-1a92-23b0-1305bd8c81f5\",\"Node\":"
48         + "\"dev-consul-server-1\",\"Address\":\"10.42.165.51\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":"
49         + "{\"lan\":\"10.42.165.51\",\"wan\":\"10.42.165.51\"},\"NodeMeta\":{\"consul-network-segment\":\"\"},"
50         + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[],"
51         + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false,"
52         + "\"CreateIndex\":803,\"ModifyIndex\":803}]";
53     private static final JsonArray configBindingServiceJson = gson.fromJson(configBindingService, JsonArray.class);
54     private static final JsonArray emptyConfigBindingServiceJson = gson.fromJson("[]", JsonArray.class);
55     private static final String prhMockConfiguration = "{\"test\":1}";
56     private static final JsonObject prhMockConfigurationJson = gson.fromJson(prhMockConfiguration, JsonObject.class);
57
58     private EnvProperties envProperties = ImmutableEnvProperties.builder()
59         .appName("dcae-prh")
60         .cbsName("config-binding-service")
61         .consulHost("consul")
62         .consulPort(8500)
63         .build();
64
65     @Test
66     void shouldReturnPrhConfiguration() {
67         // given
68         CloudHttpClient webClient = mock(CloudHttpClient.class);
69         when(
70             webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
71             .thenReturn(Mono.just(configBindingServiceJson));
72         when(webClient.callHttpGet("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class))
73             .thenReturn(Mono.just(prhMockConfigurationJson));
74
75         ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient);
76
77         //when/then
78         StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription()
79             .expectNext(prhMockConfigurationJson).verifyComplete();
80     }
81
82     @Test
83     void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() {
84         // given
85         CloudHttpClient webClient = mock(CloudHttpClient.class);
86         when(
87             webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
88             .thenReturn(Mono.just(emptyConfigBindingServiceJson));
89
90         ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient);
91
92         //when/then
93         StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription()
94             .expectError(IllegalStateException.class).verify();
95     }
96 }