e99389f5337ef1a1b25fc2a4d4ed6c24859d4f3e
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
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.prh.service;
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.prh.model.EnvProperties;
31 import org.onap.dcaegen2.services.prh.model.ImmutableEnvProperties;
32 import reactor.core.publisher.Mono;
33 import reactor.test.StepVerifier;
34
35
36 class PrhConfigurationProviderTest {
37
38     private static final Gson gson = new Gson();
39     private static final String configBindingService = "[{\"ID\":\"9c8dd674-34ce-7049-d318-e98d93a64303\",\"Node\""
40         + ":\"dcae-bootstrap\",\"Address\":\"10.42.52.82\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":"
41         + "{\"lan\":\"10.42.52.82\",\"wan\":\"10.42.52.82\"},\"NodeMeta\":{\"consul-network-segment\":\"\"},"
42         + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[],"
43         + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false,"
44         + "\"CreateIndex\":14352,\"ModifyIndex\":14352},{\"ID\":\"35c6f540-a29c-1a92-23b0-1305bd8c81f5\",\"Node\":"
45         + "\"dev-consul-server-1\",\"Address\":\"10.42.165.51\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":"
46         + "{\"lan\":\"10.42.165.51\",\"wan\":\"10.42.165.51\"},\"NodeMeta\":{\"consul-network-segment\":\"\"},"
47         + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[],"
48         + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false,"
49         + "\"CreateIndex\":803,\"ModifyIndex\":803}]";
50     private static final JsonArray configBindingServiceJson = gson.fromJson(configBindingService, JsonArray.class);
51     private static final JsonArray emptyConfigBindingServiceJson = gson.fromJson("[]", JsonArray.class);
52     private static final String prhMockConfiguration = "{\"test\":1}";
53     private static final JsonObject prhMockConfigurationJson = gson.fromJson(prhMockConfiguration, JsonObject.class);
54
55     private EnvProperties envProperties = ImmutableEnvProperties.builder()
56         .appName("dcae-prh")
57         .cbsName("config-binding-service")
58         .consulHost("consul")
59         .consulPort(8500)
60         .build();
61
62     @Test
63     void shouldReturnPrhConfiguration() {
64         // given
65         HttpGetClient webClient = mock(HttpGetClient.class);
66         when(
67             webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
68             .thenReturn(Mono.just(configBindingServiceJson));
69         when(webClient.callHttpGet("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class))
70             .thenReturn(Mono.just(prhMockConfigurationJson));
71
72         PrhConfigurationProvider provider = new PrhConfigurationProvider(webClient);
73
74         //when/then
75         StepVerifier.create(provider.callForPrhConfiguration(envProperties)).expectSubscription()
76             .expectNext(prhMockConfigurationJson).verifyComplete();
77     }
78
79     @Test
80     void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() {
81         // given
82         HttpGetClient webClient = mock(HttpGetClient.class);
83         when(
84             webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
85             .thenReturn(Mono.just(emptyConfigBindingServiceJson));
86
87         PrhConfigurationProvider provider = new PrhConfigurationProvider(webClient);
88
89         //when/then
90         StepVerifier.create(provider.callForPrhConfiguration(envProperties)).expectSubscription()
91             .expectError(IllegalStateException.class).verify();
92     }
93 }