5e218582b1703d2e6e15696549a1335bbb952983
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.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.onap.ccsdk.oran.a1policymanagementservice.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         assertEquals(2, result.ricConfigs().size());
74     }
75
76     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
77         JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
78         return rootObject;
79     }
80
81     private static InputStream getCorrectJson() throws IOException {
82         URL url = ApplicationConfigParser.class.getClassLoader()
83                 .getResource("test_application_configuration_with_dmaap_config.json");
84         String string = Resources.toString(url, Charsets.UTF_8);
85         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
86     }
87
88     @Test
89     void whenDmaapConfigHasSeveralStreamsPublishing() throws Exception {
90         JsonObject jsonRootObject = getJsonRootObject();
91         JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_publishes");
92         JsonObject fake_info_object = new JsonObject();
93         fake_info_object.addProperty("fake_info", "fake");
94         json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
95         DataPublishing data = new Gson().fromJson(json.toString(), DataPublishing.class);
96         final String expectedMessage =
97                 "Invalid configuration. Number of streams must be one, config: " + data.toString();
98
99         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
100
101         assertEquals(expectedMessage, actualException.getMessage(),
102                 "Wrong error message when the DMaaP config has several streams publishing");
103     }
104
105     class DataPublishing {
106         private JsonObject dmaap_publisher;
107         private JsonObject fake_info_object;
108
109         @Override
110         public String toString() {
111             return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
112                     fake_info_object.toString());
113         }
114     }
115
116     @Test
117     void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
118         JsonObject jsonRootObject = getJsonRootObject();
119         JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
120         JsonObject fake_info_object = new JsonObject();
121         fake_info_object.addProperty("fake_info", "fake");
122         json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
123         DataSubscribing data = new Gson().fromJson(json.toString(), DataSubscribing.class);
124         final String expectedMessage =
125                 "Invalid configuration. Number of streams must be one, config: " + data.toString();
126
127         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
128
129         assertEquals(expectedMessage, actualException.getMessage(),
130                 "Wrong error message when the DMaaP config has several streams subscribing");
131     }
132
133     private class DataSubscribing {
134         private JsonObject dmaap_subscriber;
135         private JsonObject fake_info_object;
136
137         @Override
138         public String toString() {
139             return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
140                     fake_info_object.toString());
141         }
142     }
143
144     @Test
145     void whenWrongMemberNameInObject() throws Exception {
146         JsonObject jsonRootObject = getJsonRootObject();
147         JsonObject json = jsonRootObject.getAsJsonObject("config");
148         json.remove("ric");
149         final String message = "Could not find member: [ric] in: " + json;
150
151         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
152
153         assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
154     }
155
156     JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
157             String dmaapPublisherOrSubscriber) throws Exception {
158         return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
159                 .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");
160     }
161 }