efd89837ff14bd21e421c986bef9943a7199521a
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.service;
20
21
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24
25 import com.google.gson.Gson;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonObject;
28 import org.junit.jupiter.api.Test;
29 import org.onap.dcaegen2.collectors.datafile.model.EnvProperties;
30 import org.onap.dcaegen2.collectors.datafile.model.ImmutableEnvProperties;
31 import reactor.core.publisher.Mono;
32 import reactor.test.StepVerifier;
33
34 class DatafileConfigurationProviderTest {
35     private static final Gson gson = new Gson();
36     private static final String configBindingService = "[{\"ID\":\"9c8dd674-34ce-7049-d318-e98d93a64303\",\"Node\""
37         + ":\"dcae-bootstrap\",\"Address\":\"10.42.52.82\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":"
38         + "{\"lan\":\"10.42.52.82\",\"wan\":\"10.42.52.82\"},\"NodeMeta\":{\"consul-network-segment\":\"\"},"
39         + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[],"
40         + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false,"
41         + "\"CreateIndex\":14352,\"ModifyIndex\":14352},{\"ID\":\"35c6f540-a29c-1a92-23b0-1305bd8c81f5\",\"Node\":"
42         + "\"dev-consul-server-1\",\"Address\":\"10.42.165.51\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":"
43         + "{\"lan\":\"10.42.165.51\",\"wan\":\"10.42.165.51\"},\"NodeMeta\":{\"consul-network-segment\":\"\"},"
44         + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[],"
45         + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false,"
46         + "\"CreateIndex\":803,\"ModifyIndex\":803}]";
47     private static final JsonArray configBindingServiceJson = gson.fromJson(configBindingService, JsonArray.class);
48     private static final JsonArray emptyConfigBindingServiceJson = gson.fromJson("[]", JsonArray.class);
49     private static final String datafileCollectorMockConfiguration = "{\"test\":1}";
50     private static final JsonObject datafileCollectorMockConfigurationJson = gson.fromJson(datafileCollectorMockConfiguration, JsonObject.class);
51
52     private EnvProperties envProperties = ImmutableEnvProperties.builder()
53         .appName("dcae-datafile-collector")
54         .cbsName("config-binding-service")
55         .consulHost("consul")
56         .consulPort(8500)
57         .build();
58
59     @Test
60     void shouldReturnDatafileCollectorConfiguration() {
61         // given
62         HttpGetClient webClient = mock(HttpGetClient.class);
63         when(
64             webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
65             .thenReturn(Mono.just(configBindingServiceJson));
66         when(webClient.callHttpGet("http://config-binding-service:10000/service_component/dcae-datafile-collector", JsonObject.class))
67             .thenReturn(Mono.just(datafileCollectorMockConfigurationJson));
68
69         DatafileConfigurationProvider provider = new DatafileConfigurationProvider(webClient);
70
71         //when/then
72         StepVerifier.create(provider.callForDataFileCollectorConfiguration(envProperties)).expectSubscription()
73             .expectNext(datafileCollectorMockConfigurationJson).verifyComplete();
74     }
75
76     @Test
77     void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() {
78         // given
79         HttpGetClient webClient = mock(HttpGetClient.class);
80         when(
81             webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
82             .thenReturn(Mono.just(emptyConfigBindingServiceJson));
83
84         DatafileConfigurationProvider provider = new DatafileConfigurationProvider(webClient);
85
86         //when/then
87         StepVerifier.create(provider.callForDataFileCollectorConfiguration(envProperties)).expectSubscription()
88             .expectError(IllegalStateException.class).verify();
89     }
90 }