Merge "Updated function test env with new tests and features"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / configuration / ApplicationConfigParserTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.configuration;
22
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;
26
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;
34
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;
40 import java.net.URL;
41 import java.nio.charset.StandardCharsets;
42 import java.util.Map;
43 import java.util.Properties;
44
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;
49
50 class ApplicationConfigParserTest {
51
52     ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
53
54     @Test
55     void whenCorrectConfig() throws Exception {
56         JsonObject jsonRootObject = getJsonRootObject();
57
58         ApplicationConfigParser.ConfigParserResult result = parserUnderTest.parse(jsonRootObject);
59
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"),
66                 "Wrong 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"));
73
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"),
80                 "Wrong 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"));
89
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") //
95             .userName("user") //
96             .password("password") //
97             .build(); //
98         assertEquals(expectedControllerConfig, controllers.get("controller1"), "controller contents");
99     }
100
101     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
102         JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
103         return rootObject;
104     }
105
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)));
111     }
112
113     @Test
114     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();
123
124         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
125
126         assertEquals(expectedMessage, actualException.getMessage(),
127             "Wrong error message when the DMaaP config has several streams publishing");
128     }
129
130     class DataPublishing {
131         private JsonObject dmaap_publisher;
132         private JsonObject fake_info_object;
133
134         @Override
135         public String toString() {
136             return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
137                 fake_info_object.toString());
138         }
139     }
140
141     @Test
142     void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
143         JsonObject jsonRootObject = getJsonRootObject();
144         JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
145         JsonObject fake_info_object = new JsonObject();
146         fake_info_object.addProperty("fake_info", "fake");
147         json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
148         DataSubscribing data = new Gson().fromJson(json.toString(), DataSubscribing.class);
149         final String expectedMessage =
150             "Invalid configuration. Number of streams must be one, config: " + data.toString();
151
152         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
153
154         assertEquals(expectedMessage, actualException.getMessage(),
155             "Wrong error message when the DMaaP config has several streams subscribing");
156     }
157
158     private class DataSubscribing {
159         private JsonObject dmaap_subscriber;
160         private JsonObject fake_info_object;
161
162         @Override
163         public String toString() {
164             return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
165                 fake_info_object.toString());
166         }
167     }
168
169     @Test
170     void whenMalformedUrlStreamsSubscribing() throws Exception {
171         JsonObject jsonRootObject = getJsonRootObject();
172         final String wrongTopicUrl = "WrongTopicUrl";
173         JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
174         json.addProperty("topic_url", wrongTopicUrl);
175         final String expectedMessage = "Could not parse the URL";
176
177         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
178
179         assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
180             "Wrong error message when the streams subscribes' URL is malformed");
181         assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
182             "The exception is not a MalformedURLException");
183     }
184
185     @Test
186     void whenMalformedUrlStreamsPublishing() throws Exception {
187         JsonObject jsonRootObject = getJsonRootObject();
188         final String wrongTopicUrl = "WrongTopicUrl";
189         JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
190         json.addProperty("topic_url", wrongTopicUrl);
191         final String expectedMessage = "Could not parse the URL";
192
193         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
194
195         assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
196             "Wrong error message when the streams publishes' URL is malformed");
197         assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
198             "The exception is not a MalformedURLException");
199     }
200
201     @Test
202     void whenWrongMemberNameInObject() throws Exception {
203         JsonObject jsonRootObject = getJsonRootObject();
204         JsonObject json = jsonRootObject.getAsJsonObject("config");
205         json.remove("ric");
206         final String message = "Could not find member: 'ric' in: " + json;
207
208         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
209
210         assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
211     }
212
213     @Test
214     void whenWrongUrlPathStreamsSubscribing() throws Exception {
215         JsonObject jsonRootObject = getJsonRootObject();
216         final String wrongTopicUrlString =
217             "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent/wrong-topic-url";
218         final URL wrongTopicUrl = new URL(wrongTopicUrlString);
219         JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
220         json.addProperty("topic_url", wrongTopicUrlString);
221         final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
222
223         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
224
225         assertEquals(expectedMessage, actualException.getMessage(),
226             "Wrong error message when the streams subscribes' URL has incorrect syntax");
227     }
228
229     @Test
230     void whenWrongUrlPathStreamsPublishing() throws Exception {
231         JsonObject jsonRootObject = getJsonRootObject();
232         final String wrongTopicUrlString =
233             "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE/wrong-topic-url";
234         final URL wrongTopicUrl = new URL(wrongTopicUrlString);
235         JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
236         json.addProperty("topic_url", wrongTopicUrlString);
237         final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
238
239         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
240
241         assertEquals(expectedMessage, actualException.getMessage(),
242             "Wrong error message when the streams publishes' URL has incorrect syntax");
243     }
244
245     JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
246         String dmaapPublisherOrSubscriber) throws Exception {
247         return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
248             .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");
249     }
250 }