2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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;
42 import java.util.Properties;
44 import org.junit.jupiter.api.Test;
45 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
46 import org.oransc.policyagent.exceptions.ServiceException;
47 import org.springframework.http.MediaType;
49 public class ApplicationConfigParserTest {
51 ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
54 public void whenCorrectDmaapConfig() throws Exception {
55 JsonObject jsonRootObject = getJsonRootObject();
57 parserUnderTest.parse(jsonRootObject);
59 Properties actualPublisherConfig = parserUnderTest.getDmaapPublisherConfig();
60 assertAll("publisherConfig",
61 () -> assertEquals("localhost:6845/events", actualPublisherConfig.get("ServiceName"), "Wrong ServiceName"),
62 () -> assertEquals("A1-POLICY-AGENT-WRITE", actualPublisherConfig.get("topic"), "Wrong topic"),
63 () -> assertEquals("localhost:6845", actualPublisherConfig.get("host"), "Wrong host"),
64 () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualPublisherConfig.get("contenttype"),
66 () -> assertEquals("admin", actualPublisherConfig.get("userName"), "Wrong userName"),
67 () -> assertEquals("admin", actualPublisherConfig.get("password"), "Wrong password"),
68 () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualPublisherConfig.get("TransportType"),
69 "Wrong TransportType"),
70 () -> assertEquals(15000, actualPublisherConfig.get("timeout"), "Wrong timeout"),
71 () -> assertEquals(100, actualPublisherConfig.get("limit"), "Wrong limit"));
73 Properties actualConsumerConfig = parserUnderTest.getDmaapConsumerConfig();
74 assertAll("consumerConfig",
75 () -> assertEquals("localhost:6845/events", actualConsumerConfig.get("ServiceName"), "Wrong ServiceName"),
76 () -> assertEquals("A1-POLICY-AGENT-READ", actualConsumerConfig.get("topic"), "Wrong topic"),
77 () -> assertEquals("localhost:6845", actualConsumerConfig.get("host"), "Wrong host"),
78 () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualConsumerConfig.get("contenttype"),
80 () -> assertEquals("admin", actualConsumerConfig.get("userName"), "Wrong userName"),
81 () -> assertEquals("admin", actualConsumerConfig.get("password"), "Wrong password"),
82 () -> assertEquals("users", actualConsumerConfig.get("group"), "Wrong group"),
83 () -> assertEquals("policy-agent", actualConsumerConfig.get("id"), "Wrong id"),
84 () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualConsumerConfig.get("TransportType"),
85 "Wrong TransportType"),
86 () -> assertEquals(15000, actualConsumerConfig.get("timeout"), "Wrong timeout"),
87 () -> assertEquals(100, actualConsumerConfig.get("limit"), "Wrong limit"));
90 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
91 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
95 private static InputStream getCorrectJson() throws IOException {
96 URL url = ApplicationConfigParser.class.getClassLoader()
97 .getResource("test_application_configuration_with_dmaap_config.json");
98 String string = Resources.toString(url, Charsets.UTF_8);
99 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
103 public void whenDmaapConfigHasSeveralStreamsPublishing() throws Exception {
104 JsonObject jsonRootObject = getJsonRootObject();
105 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_publishes");
106 JsonObject fake_info_object = new JsonObject();
107 fake_info_object.addProperty("fake_info", "fake");
108 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
109 DataPublishing data = new Gson().fromJson(json.toString(), DataPublishing.class);
110 final String expectedMessage =
111 "Invalid configuration. Number of streams must be one, config: " + data.toString();
113 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
115 assertEquals(expectedMessage, actualException.getMessage(),
116 "Wrong error message when the DMaaP config has several streams publishing");
119 class DataPublishing {
120 private JsonObject dmaap_publisher;
121 private JsonObject fake_info_object;
123 public String toString() {
124 return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(), fake_info_object.toString());
129 public void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
130 JsonObject jsonRootObject = getJsonRootObject();
131 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
132 JsonObject fake_info_object = new JsonObject();
133 fake_info_object.addProperty("fake_info", "fake");
134 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
135 DataSubscribing data = new Gson().fromJson(json.toString(), DataSubscribing.class);
136 final String expectedMessage =
137 "Invalid configuration. Number of streams must be one, config: " + data.toString();
139 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
141 assertEquals(expectedMessage, actualException.getMessage(),
142 "Wrong error message when the DMaaP config has several streams subscribing");
145 private class DataSubscribing {
146 private JsonObject dmaap_subscriber;
147 private JsonObject fake_info_object;
149 public String toString() {
150 return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(), fake_info_object.toString());
155 public void whenMalformedUrlStreamsSubscribing() throws Exception {
156 JsonObject jsonRootObject = getJsonRootObject();
157 final String wrongTopicUrl = "WrongTopicUrl";
158 JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
159 json.addProperty("topic_url", wrongTopicUrl);
160 final String expectedMessage = "Could not parse the URL";
162 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
164 assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
165 "Wrong error message when the streams subscribes' URL is malformed");
166 assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
167 "The exception is not a MalformedURLException");
171 public void whenMalformedUrlStreamsPublishing() throws Exception {
172 JsonObject jsonRootObject = getJsonRootObject();
173 final String wrongTopicUrl = "WrongTopicUrl";
174 JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
175 json.addProperty("topic_url", wrongTopicUrl);
176 final String expectedMessage = "Could not parse the URL";
178 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
180 assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
181 "Wrong error message when the streams publishes' URL is malformed");
182 assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
183 "The exception is not a MalformedURLException");
187 public void whenWrongMemberNameInObject() throws Exception {
188 JsonObject jsonRootObject = getJsonRootObject();
189 JsonObject json = jsonRootObject.getAsJsonObject("config");
191 final String message = "Could not find member: ric in: " + json;
193 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
195 assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
199 public void whenWrongUrlPathStreamsSubscribing() throws Exception {
200 JsonObject jsonRootObject = getJsonRootObject();
201 final String wrongTopicUrlString =
202 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent/wrong-topic-url";
203 final URL wrongTopicUrl = new URL(wrongTopicUrlString);
204 JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
205 json.addProperty("topic_url", wrongTopicUrlString);
206 final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
208 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
210 assertEquals(expectedMessage, actualException.getMessage(),
211 "Wrong error message when the streams subscribes' URL has incorrect syntax");
215 public void whenWrongUrlPathStreamsPublishing() throws Exception {
216 JsonObject jsonRootObject = getJsonRootObject();
217 final String wrongTopicUrlString =
218 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE/wrong-topic-url";
219 final URL wrongTopicUrl = new URL(wrongTopicUrlString);
220 JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
221 json.addProperty("topic_url", wrongTopicUrlString);
222 final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
224 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
226 assertEquals(expectedMessage, actualException.getMessage(),
227 "Wrong error message when the streams publishes' URL has incorrect syntax");
230 public JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes, String dmaapPublisherOrSubscriber)
232 return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
233 .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");