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;
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(),
125 fake_info_object.toString());
130 public void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
131 JsonObject jsonRootObject = getJsonRootObject();
132 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
133 JsonObject fake_info_object = new JsonObject();
134 fake_info_object.addProperty("fake_info", "fake");
135 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
136 DataSubscribing data = new Gson().fromJson(json.toString(), DataSubscribing.class);
137 final String expectedMessage =
138 "Invalid configuration. Number of streams must be one, config: " + data.toString();
140 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
142 assertEquals(expectedMessage, actualException.getMessage(),
143 "Wrong error message when the DMaaP config has several streams subscribing");
146 private class DataSubscribing {
147 private JsonObject dmaap_subscriber;
148 private JsonObject fake_info_object;
150 public String toString() {
151 return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
152 fake_info_object.toString());
157 public void whenMalformedUrlStreamsSubscribing() throws Exception {
158 JsonObject jsonRootObject = getJsonRootObject();
159 final String wrongTopicUrl = "WrongTopicUrl";
160 JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
161 json.addProperty("topic_url", wrongTopicUrl);
162 final String expectedMessage = "Could not parse the URL";
164 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
166 assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
167 "Wrong error message when the streams subscribes' URL is malformed");
168 assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
169 "The exception is not a MalformedURLException");
173 public void whenMalformedUrlStreamsPublishing() throws Exception {
174 JsonObject jsonRootObject = getJsonRootObject();
175 final String wrongTopicUrl = "WrongTopicUrl";
176 JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
177 json.addProperty("topic_url", wrongTopicUrl);
178 final String expectedMessage = "Could not parse the URL";
180 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
182 assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
183 "Wrong error message when the streams publishes' URL is malformed");
184 assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
185 "The exception is not a MalformedURLException");
189 public void whenWrongMemberNameInObject() throws Exception {
190 JsonObject jsonRootObject = getJsonRootObject();
191 JsonObject json = jsonRootObject.getAsJsonObject("config");
193 final String message = "Could not find member: ric in: " + json;
195 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
197 assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
201 public void whenWrongUrlPathStreamsSubscribing() throws Exception {
202 JsonObject jsonRootObject = getJsonRootObject();
203 final String wrongTopicUrlString =
204 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent/wrong-topic-url";
205 final URL wrongTopicUrl = new URL(wrongTopicUrlString);
206 JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
207 json.addProperty("topic_url", wrongTopicUrlString);
208 final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
210 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
212 assertEquals(expectedMessage, actualException.getMessage(),
213 "Wrong error message when the streams subscribes' URL has incorrect syntax");
217 public void whenWrongUrlPathStreamsPublishing() throws Exception {
218 JsonObject jsonRootObject = getJsonRootObject();
219 final String wrongTopicUrlString =
220 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE/wrong-topic-url";
221 final URL wrongTopicUrl = new URL(wrongTopicUrlString);
222 JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
223 json.addProperty("topic_url", wrongTopicUrlString);
224 final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
226 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
228 assertEquals(expectedMessage, actualException.getMessage(),
229 "Wrong error message when the streams publishes' URL has incorrect syntax");
232 public JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
233 String dmaapPublisherOrSubscriber) throws Exception {
234 return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
235 .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");