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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.dcae.controller;
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;
32 import io.vavr.control.Try;
33 import org.json.JSONObject;
34 import org.junit.Test;
35 import org.onap.dcae.WiremockBasedTest;
38 public class ConfigCBSSourceTest extends WiremockBasedTest {
41 public void shouldReturnValidAppConfiguration() {
43 String sampleConfigForVES = "{\"collector.port\": 8080}";
45 stubConsulToReturnLocalAddressOfCBS();
46 stubCBSToReturnAppConfig(sampleConfigForVES);
49 Try<JSONObject> actual = tryToGetConfig();
52 assertThat(actual.get().toString()).isEqualTo(new JSONObject(sampleConfigForVES).toString());
56 public void shouldReturnFailureOnFailureToCommunicateWithConsul() {
58 stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
59 .willReturn(aResponse().withStatus(400)));
62 Try<JSONObject> actual = tryToGetConfig();
65 assertFailureHasInfo(actual, "HTTP", "Consul", "400",
66 "http://localhost:" + wireMockRule.port() + "/v1/catalog/service/CBSName");
70 public void shouldReturnFailureOnBadJsonFromConsul() {
72 stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
73 .willReturn(aResponse().withStatus(200).withBody("[{")));
76 Try<JSONObject> actual = tryToGetConfig();
79 assertFailureHasInfo(actual, "JSON", "array", "[{");
83 public void shouldReturnFailureOnInvalidCatalogFormat() {
85 String notAListCatalog = ""
87 + "\"ServiceAddress\":\"localhost\","
88 + "\"ServicePort\":" + wireMockRule.port()
91 stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
92 .willReturn(aResponse().withStatus(200).withBody(notAListCatalog)));
95 Try<JSONObject> actual = tryToGetConfig();
98 assertFailureHasInfo(actual, "JSON", "array", notAListCatalog);
103 public void shouldReturnFailureIfConfigIsMissingRequiredProperties() {
105 String actualConf = "{\"ServicePort\":" + wireMockRule.port() + "}";
106 String asCatalog = "[" + actualConf + "]";
108 stubFor(get(urlEqualTo("/v1/catalog/service/CBSName"))
109 .willReturn(aResponse().withStatus(200).withBody(asCatalog)));
112 Try<JSONObject> actual = tryToGetConfig();
115 assertFailureHasInfo(actual, "ServiceAddress", "ServicePort", "missing", actualConf);
120 public void shouldReturnFailureOnFailureToCommunicateWithCBS() {
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)));
128 Try<JSONObject> actual = tryToGetConfig();
131 assertFailureHasInfo(actual, "HTTP", "CBS", "400",
132 "http://localhost:" + wireMockRule.port() + "/service_component/VESCollector");
136 public void shouldReturnFailureIfAppIsInvalidJsonDocument() {
138 String invalidAppConf = "[$";
139 stubConsulToReturnLocalAddressOfCBS();
140 stubCBSToReturnAppConfig(invalidAppConf);
143 Try<JSONObject> actual = tryToGetConfig();
146 assertFailureHasInfo(actual, "JSON", "document", invalidAppConf);
149 private Try<JSONObject> tryToGetConfig() {
150 return getAppConfig(new EnvProps("http", "localhost", wireMockRule.port(), "http", "CBSName", "VESCollector"));