Release 1.7.3 DCAEGEN2 VESCollector container
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / controller / ConfigCBSSourceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2018 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcae.controller;
23
24 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
28 import static org.assertj.core.api.Java6Assertions.assertThat;
29 import static org.onap.dcae.TestingUtilities.assertFailureHasInfo;
30 import static org.onap.dcae.controller.ConfigSource.getAppConfig;
31
32 import io.vavr.control.Try;
33 import org.json.JSONObject;
34 import org.junit.Test;
35 import org.onap.dcae.WiremockBasedTest;
36
37
38 public class ConfigCBSSourceTest extends WiremockBasedTest {
39
40     @Test
41     public void shouldReturnValidAppConfiguration() {
42         // given
43         String sampleConfigForVES = "{\"collector.port\": 8080}";
44
45         stubConsulToReturnLocalAddressOfCBS();
46         stubCBSToReturnAppConfig(sampleConfigForVES);
47
48         // when
49         Try<JSONObject> actual = tryToGetConfig();
50
51         // then
52         assertThat(actual.get().toString()).isEqualTo(new JSONObject(sampleConfigForVES).toString());
53     }
54
55     @Test
56     public void shouldReturnFailureOnFailureToCommunicateWithConsul() {
57         // given
58         stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
59             .willReturn(aResponse().withStatus(400)));
60
61         // when
62         Try<JSONObject> actual = tryToGetConfig();
63
64         // then
65         assertFailureHasInfo(actual, "HTTP", "Consul", "400",
66             "http://localhost:" + wireMockRule.port() + "/v1/catalog/service/CBSName");
67     }
68
69     @Test
70     public void shouldReturnFailureOnBadJsonFromConsul() {
71         // given
72         stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
73             .willReturn(aResponse().withStatus(200).withBody("[{")));
74
75         // when
76         Try<JSONObject> actual = tryToGetConfig();
77
78         // then
79         assertFailureHasInfo(actual, "JSON", "array", "[{");
80     }
81
82     @Test
83     public void shouldReturnFailureOnInvalidCatalogFormat() {
84         // given
85         String notAListCatalog = ""
86             + "{"
87             + "\"ServiceAddress\":\"localhost\","
88             + "\"ServicePort\":" + wireMockRule.port()
89             + "}";
90
91         stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
92             .willReturn(aResponse().withStatus(200).withBody(notAListCatalog)));
93
94         // when
95         Try<JSONObject> actual = tryToGetConfig();
96
97         // then
98         assertFailureHasInfo(actual, "JSON", "array", notAListCatalog);
99     }
100
101
102     @Test
103     public void shouldReturnFailureIfConfigIsMissingRequiredProperties() {
104         // given
105         String actualConf = "{\"ServicePort\":" + wireMockRule.port() + "}";
106         String asCatalog = "[" + actualConf + "]";
107
108         stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
109             .willReturn(aResponse().withStatus(200).withBody(asCatalog)));
110
111         // when
112         Try<JSONObject> actual = tryToGetConfig();
113
114         // then
115         assertFailureHasInfo(actual, "ServiceAddress", "ServicePort", "missing", actualConf);
116     }
117
118
119     @Test
120     public void shouldReturnFailureOnFailureToCommunicateWithCBS() {
121         // given
122         stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
123             .willReturn(aResponse().withStatus(200).withBody(validLocalCBSConf())));
124         stubFor(get(urlEqualTo("/service_component/VESCollector"))
125             .willReturn(aResponse().withStatus(400)));
126
127         // when
128         Try<JSONObject> actual = tryToGetConfig();
129
130         // then
131         assertFailureHasInfo(actual, "HTTP", "CBS", "400",
132             "http://localhost:" + wireMockRule.port() + "/service_component/VESCollector");
133     }
134
135     @Test
136     public void shouldReturnFailureIfAppIsInvalidJsonDocument() {
137         // given
138         String invalidAppConf = "[$";
139         stubConsulToReturnLocalAddressOfCBS();
140         stubCBSToReturnAppConfig(invalidAppConf);
141
142         // when
143         Try<JSONObject> actual = tryToGetConfig();
144
145         // then
146         assertFailureHasInfo(actual, "JSON", "document", invalidAppConf);
147     }
148
149     private Try<JSONObject> tryToGetConfig() {
150         return getAppConfig(new EnvProps("http", "localhost", wireMockRule.port(), "http", "CBSName", "VESCollector"));
151     }
152 }
153