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.dmaapProducerTopicUrl();
63 assertEquals("http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE", topicUrl, "controller contents");
65 topicUrl = result.dmaapConsumerTopicUrl();
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.controllerConfigs();
71 assertEquals(1, controllers.size(), "size");
72 ImmutableControllerConfig expectedControllerConfig = ImmutableControllerConfig.builder() //
73 .baseUrl("http://localhost:8083/") //
74 .name("controller1") //
76 .password("password") //
78 assertEquals(expectedControllerConfig, controllers.get("controller1"), "controller contents");
80 assertEquals(2, result.ricConfigs().size());
83 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
84 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
88 private static InputStream getCorrectJson() throws IOException {
89 URL url = ApplicationConfigParser.class.getClassLoader()
90 .getResource("test_application_configuration_with_dmaap_config.json");
91 String string = Resources.toString(url, Charsets.UTF_8);
92 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
96 void whenDmaapConfigHasSeveralStreamsPublishing() throws Exception {
97 JsonObject jsonRootObject = getJsonRootObject();
98 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_publishes");
99 JsonObject fake_info_object = new JsonObject();
100 fake_info_object.addProperty("fake_info", "fake");
101 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
102 DataPublishing data = new Gson().fromJson(json.toString(), DataPublishing.class);
103 final String expectedMessage =
104 "Invalid configuration. Number of streams must be one, config: " + data.toString();
106 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
108 assertEquals(expectedMessage, actualException.getMessage(),
109 "Wrong error message when the DMaaP config has several streams publishing");
112 class DataPublishing {
113 private JsonObject dmaap_publisher;
114 private JsonObject fake_info_object;
117 public String toString() {
118 return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
119 fake_info_object.toString());
124 void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
125 JsonObject jsonRootObject = getJsonRootObject();
126 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
127 JsonObject fake_info_object = new JsonObject();
128 fake_info_object.addProperty("fake_info", "fake");
129 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
130 DataSubscribing data = new Gson().fromJson(json.toString(), DataSubscribing.class);
131 final String expectedMessage =
132 "Invalid configuration. Number of streams must be one, config: " + data.toString();
134 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
136 assertEquals(expectedMessage, actualException.getMessage(),
137 "Wrong error message when the DMaaP config has several streams subscribing");
140 private class DataSubscribing {
141 private JsonObject dmaap_subscriber;
142 private JsonObject fake_info_object;
145 public String toString() {
146 return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
147 fake_info_object.toString());
152 void whenWrongMemberNameInObject() throws Exception {
153 JsonObject jsonRootObject = getJsonRootObject();
154 JsonObject json = jsonRootObject.getAsJsonObject("config");
156 final String message = "Could not find member: [ric] in: " + json;
158 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
160 assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
164 void schemaValidationError() throws Exception {
165 when(applicationConfigMock.getConfigurationFileSchemaPath())
166 .thenReturn("application_configuration_schema.json");
167 JsonObject jsonRootObject = getJsonRootObject();
168 JsonObject json = jsonRootObject.getAsJsonObject("config");
171 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
173 assertThat(actualException.getMessage()).contains("Json schema validation failure");
176 JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
177 String dmaapPublisherOrSubscriber) throws Exception {
178 return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
179 .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");