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