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.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
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;
34 import java.io.ByteArrayInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
39 import java.nio.charset.StandardCharsets;
42 import org.junit.jupiter.api.Test;
43 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
45 class ApplicationConfigParserTest {
47 ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
50 void whenCorrectConfig() throws Exception {
51 JsonObject jsonRootObject = getJsonRootObject();
53 ApplicationConfigParser.ConfigParserResult result = parserUnderTest.parse(jsonRootObject);
55 String topicUrl = result.dmaapProducerTopicUrl();
56 assertEquals("http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE", topicUrl, "controller contents");
58 topicUrl = result.dmaapConsumerTopicUrl();
60 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=15000&limit=100",
61 topicUrl, "controller contents");
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") //
69 .password("password") //
71 assertEquals(expectedControllerConfig, controllers.get("controller1"), "controller contents");
73 assertEquals(2, result.ricConfigs().size());
76 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
77 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
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)));
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();
99 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
101 assertEquals(expectedMessage, actualException.getMessage(),
102 "Wrong error message when the DMaaP config has several streams publishing");
105 class DataPublishing {
106 private JsonObject dmaap_publisher;
107 private JsonObject fake_info_object;
110 public String toString() {
111 return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
112 fake_info_object.toString());
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();
127 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
129 assertEquals(expectedMessage, actualException.getMessage(),
130 "Wrong error message when the DMaaP config has several streams subscribing");
133 private class DataSubscribing {
134 private JsonObject dmaap_subscriber;
135 private JsonObject fake_info_object;
138 public String toString() {
139 return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
140 fake_info_object.toString());
145 void whenWrongMemberNameInObject() throws Exception {
146 JsonObject jsonRootObject = getJsonRootObject();
147 JsonObject json = jsonRootObject.getAsJsonObject("config");
149 final String message = "Could not find member: [ric] in: " + json;
151 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
153 assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
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");