5a6b0232a3e27a16fa952c0ca3674bbd9237bd49
[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.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import com.google.common.base.Charsets;
27 import com.google.common.io.Resources;
28 import com.google.gson.Gson;
29 import com.google.gson.JsonIOException;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
32 import com.google.gson.JsonSyntaxException;
33
34 import java.io.ByteArrayInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.net.URL;
39 import java.nio.charset.StandardCharsets;
40 import java.util.Map;
41
42 import org.junit.jupiter.api.Test;
43 import org.oransc.policyagent.exceptions.ServiceException;
44
45 class ApplicationConfigParserTest {
46
47     ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
48
49     @Test
50     void whenCorrectConfig() throws Exception {
51         JsonObject jsonRootObject = getJsonRootObject();
52
53         ApplicationConfigParser.ConfigParserResult result = parserUnderTest.parse(jsonRootObject);
54
55         String topicUrl = result.dmaapProducerTopicUrl();
56         assertEquals("http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE", topicUrl, "controller contents");
57
58         topicUrl = result.dmaapConsumerTopicUrl();
59         assertEquals(
60             "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=15000&limit=100",
61             topicUrl, "controller contents");
62
63         Map<String, ControllerConfig> controllers = result.controllerConfigs();
64         assertEquals(1, controllers.size(), "size");
65         ImmutableControllerConfig expectedControllerConfig = ImmutableControllerConfig.builder() //
66             .baseUrl("http://localhost:8083/") //
67             .name("controller1") //
68             .userName("user") //
69             .password("password") //
70             .build(); //
71         assertEquals(expectedControllerConfig, controllers.get("controller1"), "controller contents");
72     }
73
74     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
75         JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
76         return rootObject;
77     }
78
79     private static InputStream getCorrectJson() throws IOException {
80         URL url = ApplicationConfigParser.class.getClassLoader()
81             .getResource("test_application_configuration_with_dmaap_config.json");
82         String string = Resources.toString(url, Charsets.UTF_8);
83         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
84     }
85
86     @Test
87     void whenDmaapConfigHasSeveralStreamsPublishing() throws Exception {
88         JsonObject jsonRootObject = getJsonRootObject();
89         JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_publishes");
90         JsonObject fake_info_object = new JsonObject();
91         fake_info_object.addProperty("fake_info", "fake");
92         json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
93         DataPublishing data = new Gson().fromJson(json.toString(), DataPublishing.class);
94         final String expectedMessage =
95             "Invalid configuration. Number of streams must be one, config: " + data.toString();
96
97         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
98
99         assertEquals(expectedMessage, actualException.getMessage(),
100             "Wrong error message when the DMaaP config has several streams publishing");
101     }
102
103     class DataPublishing {
104         private JsonObject dmaap_publisher;
105         private JsonObject fake_info_object;
106
107         @Override
108         public String toString() {
109             return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
110                 fake_info_object.toString());
111         }
112     }
113
114     @Test
115     void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
116         JsonObject jsonRootObject = getJsonRootObject();
117         JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
118         JsonObject fake_info_object = new JsonObject();
119         fake_info_object.addProperty("fake_info", "fake");
120         json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
121         DataSubscribing data = new Gson().fromJson(json.toString(), DataSubscribing.class);
122         final String expectedMessage =
123             "Invalid configuration. Number of streams must be one, config: " + data.toString();
124
125         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
126
127         assertEquals(expectedMessage, actualException.getMessage(),
128             "Wrong error message when the DMaaP config has several streams subscribing");
129     }
130
131     private class DataSubscribing {
132         private JsonObject dmaap_subscriber;
133         private JsonObject fake_info_object;
134
135         @Override
136         public String toString() {
137             return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
138                 fake_info_object.toString());
139         }
140     }
141
142     @Test
143     void whenWrongMemberNameInObject() throws Exception {
144         JsonObject jsonRootObject = getJsonRootObject();
145         JsonObject json = jsonRootObject.getAsJsonObject("config");
146         json.remove("ric");
147         final String message = "Could not find member: 'ric' in: " + json;
148
149         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
150
151         assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
152     }
153
154     JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
155         String dmaapPublisherOrSubscriber) throws Exception {
156         return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
157             .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");
158     }
159 }