2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.configuration;
23 import static org.junit.jupiter.api.Assertions.assertAll;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import com.google.common.base.Charsets;
28 import com.google.common.io.Resources;
29 import com.google.gson.Gson;
30 import com.google.gson.JsonIOException;
31 import com.google.gson.JsonObject;
32 import com.google.gson.JsonParser;
33 import com.google.gson.JsonSyntaxException;
35 import java.io.ByteArrayInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.net.MalformedURLException;
41 import java.nio.charset.StandardCharsets;
43 import java.util.Properties;
45 import org.junit.jupiter.api.Test;
46 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
47 import org.oransc.policyagent.exceptions.ServiceException;
48 import org.springframework.http.MediaType;
50 public class ApplicationConfigParserTest {
52 ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
55 public void whenCorrectConfig() throws Exception {
56 JsonObject jsonRootObject = getJsonRootObject();
58 ApplicationConfigParser.ConfigParserResult result = parserUnderTest.parse(jsonRootObject);
60 Properties actualPublisherConfig = result.dmaapPublisherConfig();
61 assertAll("publisherConfig",
62 () -> assertEquals("localhost:6845/events", actualPublisherConfig.get("ServiceName"), "Wrong ServiceName"),
63 () -> assertEquals("A1-POLICY-AGENT-WRITE", actualPublisherConfig.get("topic"), "Wrong topic"),
64 () -> assertEquals("localhost:6845", actualPublisherConfig.get("host"), "Wrong host"),
65 () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualPublisherConfig.get("contenttype"),
67 () -> assertEquals("admin", actualPublisherConfig.get("userName"), "Wrong userName"),
68 () -> assertEquals("admin", actualPublisherConfig.get("password"), "Wrong password"),
69 () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualPublisherConfig.get("TransportType"),
70 "Wrong TransportType"),
71 () -> assertEquals(15000, actualPublisherConfig.get("timeout"), "Wrong timeout"),
72 () -> assertEquals(100, actualPublisherConfig.get("limit"), "Wrong limit"));
74 Properties actualConsumerConfig = result.dmaapConsumerConfig();
75 assertAll("consumerConfig",
76 () -> assertEquals("localhost:6845/events", actualConsumerConfig.get("ServiceName"), "Wrong ServiceName"),
77 () -> assertEquals("A1-POLICY-AGENT-READ", actualConsumerConfig.get("topic"), "Wrong topic"),
78 () -> assertEquals("localhost:6845", actualConsumerConfig.get("host"), "Wrong host"),
79 () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualConsumerConfig.get("contenttype"),
81 () -> assertEquals("admin", actualConsumerConfig.get("userName"), "Wrong userName"),
82 () -> assertEquals("admin", actualConsumerConfig.get("password"), "Wrong password"),
83 () -> assertEquals("users", actualConsumerConfig.get("group"), "Wrong group"),
84 () -> assertEquals("policy-agent", actualConsumerConfig.get("id"), "Wrong id"),
85 () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualConsumerConfig.get("TransportType"),
86 "Wrong TransportType"),
87 () -> assertEquals(15000, actualConsumerConfig.get("timeout"), "Wrong timeout"),
88 () -> assertEquals(100, actualConsumerConfig.get("limit"), "Wrong limit"));
90 Map<String, ControllerConfig> controllers = result.controllerConfigs();
91 assertEquals(1, controllers.size(), "size");
92 ImmutableControllerConfig expectedControllerConfig = ImmutableControllerConfig.builder() //
93 .baseUrl("http://localhost:8083/") //
94 .name("controller1") //
96 .password("password") //
98 assertEquals(expectedControllerConfig, controllers.get("controller1"), "controller contents");
101 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
102 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
106 private static InputStream getCorrectJson() throws IOException {
107 URL url = ApplicationConfigParser.class.getClassLoader()
108 .getResource("test_application_configuration_with_dmaap_config.json");
109 String string = Resources.toString(url, Charsets.UTF_8);
110 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
114 public void whenDmaapConfigHasSeveralStreamsPublishing() throws Exception {
115 JsonObject jsonRootObject = getJsonRootObject();
116 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_publishes");
117 JsonObject fake_info_object = new JsonObject();
118 fake_info_object.addProperty("fake_info", "fake");
119 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
120 DataPublishing data = new Gson().fromJson(json.toString(), DataPublishing.class);
121 final String expectedMessage =
122 "Invalid configuration. Number of streams must be one, config: " + data.toString();
124 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
126 assertEquals(expectedMessage, actualException.getMessage(),
127 "Wrong error message when the DMaaP config has several streams publishing");
130 class DataPublishing {
131 private JsonObject dmaap_publisher;
132 private JsonObject fake_info_object;
134 public String toString() {
135 return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
136 fake_info_object.toString());
141 public void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
142 JsonObject jsonRootObject = getJsonRootObject();
143 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
144 JsonObject fake_info_object = new JsonObject();
145 fake_info_object.addProperty("fake_info", "fake");
146 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
147 DataSubscribing data = new Gson().fromJson(json.toString(), DataSubscribing.class);
148 final String expectedMessage =
149 "Invalid configuration. Number of streams must be one, config: " + data.toString();
151 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
153 assertEquals(expectedMessage, actualException.getMessage(),
154 "Wrong error message when the DMaaP config has several streams subscribing");
157 private class DataSubscribing {
158 private JsonObject dmaap_subscriber;
159 private JsonObject fake_info_object;
161 public String toString() {
162 return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
163 fake_info_object.toString());
168 public void whenMalformedUrlStreamsSubscribing() throws Exception {
169 JsonObject jsonRootObject = getJsonRootObject();
170 final String wrongTopicUrl = "WrongTopicUrl";
171 JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
172 json.addProperty("topic_url", wrongTopicUrl);
173 final String expectedMessage = "Could not parse the URL";
175 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
177 assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
178 "Wrong error message when the streams subscribes' URL is malformed");
179 assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
180 "The exception is not a MalformedURLException");
184 public void whenMalformedUrlStreamsPublishing() throws Exception {
185 JsonObject jsonRootObject = getJsonRootObject();
186 final String wrongTopicUrl = "WrongTopicUrl";
187 JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
188 json.addProperty("topic_url", wrongTopicUrl);
189 final String expectedMessage = "Could not parse the URL";
191 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
193 assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
194 "Wrong error message when the streams publishes' URL is malformed");
195 assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
196 "The exception is not a MalformedURLException");
200 public void whenWrongMemberNameInObject() throws Exception {
201 JsonObject jsonRootObject = getJsonRootObject();
202 JsonObject json = jsonRootObject.getAsJsonObject("config");
204 final String message = "Could not find member: ric in: " + json;
206 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
208 assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
212 public void whenWrongUrlPathStreamsSubscribing() throws Exception {
213 JsonObject jsonRootObject = getJsonRootObject();
214 final String wrongTopicUrlString =
215 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent/wrong-topic-url";
216 final URL wrongTopicUrl = new URL(wrongTopicUrlString);
217 JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
218 json.addProperty("topic_url", wrongTopicUrlString);
219 final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
221 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
223 assertEquals(expectedMessage, actualException.getMessage(),
224 "Wrong error message when the streams subscribes' URL has incorrect syntax");
228 public void whenWrongUrlPathStreamsPublishing() throws Exception {
229 JsonObject jsonRootObject = getJsonRootObject();
230 final String wrongTopicUrlString =
231 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE/wrong-topic-url";
232 final URL wrongTopicUrl = new URL(wrongTopicUrlString);
233 JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
234 json.addProperty("topic_url", wrongTopicUrlString);
235 final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
237 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
239 assertEquals(expectedMessage, actualException.getMessage(),
240 "Wrong error message when the streams publishes' URL has incorrect syntax");
243 public JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
244 String dmaapPublisherOrSubscriber) throws Exception {
245 return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
246 .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");