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