2 * ========================LICENSE_START=================================
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
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.onap.ccsdk.oran.a1policymanagementservice.configuration;
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;
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;
37 import java.io.ByteArrayInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.InputStreamReader;
42 import java.nio.charset.StandardCharsets;
45 import org.junit.jupiter.api.Test;
46 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
48 class ApplicationConfigParserTest {
50 ApplicationConfig applicationConfigMock = spy(new ApplicationConfig());
51 ApplicationConfigParser parserUnderTest = new ApplicationConfigParser(applicationConfigMock);
54 void whenCorrectConfig() throws Exception {
55 JsonObject jsonRootObject = getJsonRootObject();
57 when(applicationConfigMock.getConfigurationFileSchemaPath())
58 .thenReturn("/application_configuration_schema.json");
60 ApplicationConfigParser.ConfigParserResult result = parserUnderTest.parse(jsonRootObject);
62 String topicUrl = result.getDmaapProducerTopicUrl();
63 assertEquals("http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE", topicUrl, "controller contents");
65 topicUrl = result.getDmaapConsumerTopicUrl();
67 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=15000&limit=100",
68 topicUrl, "controller contents");
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") //
76 .password("password") //
79 ControllerConfig actual = controllers.get("controller1");
80 assertEquals(expectedControllerConfig, actual, "controller contents");
82 assertEquals(2, result.getRicConfigs().size());
85 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
86 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
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)));
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();
108 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
110 assertEquals(expectedMessage, actualException.getMessage(),
111 "Wrong error message when the DMaaP config has several streams publishing");
114 class DataPublishing {
115 private JsonObject dmaap_publisher;
116 private JsonObject fake_info_object;
119 public String toString() {
120 return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
121 fake_info_object.toString());
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();
136 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
138 assertEquals(expectedMessage, actualException.getMessage(),
139 "Wrong error message when the DMaaP config has several streams subscribing");
142 private class DataSubscribing {
143 private JsonObject dmaap_subscriber;
144 private JsonObject fake_info_object;
147 public String toString() {
148 return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
149 fake_info_object.toString());
154 void whenWrongMemberNameInObject() throws Exception {
155 JsonObject jsonRootObject = getJsonRootObject();
156 JsonObject json = jsonRootObject.getAsJsonObject("config");
158 final String message = "Could not find member: [ric] in: " + json;
160 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
162 assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
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");
173 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
175 assertThat(actualException.getMessage()).contains("Json schema validation failure");
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");