2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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.oransc.policyagent.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.oransc.policyagent.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");
74 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
75 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
79 private static InputStream getCorrectJson() throws IOException {
80 URL url = ApplicationConfigParser.class.getClassLoader()
81 .getResource("test_application_configuration_with_dmaap_config.json");
82 String string = Resources.toString(url, Charsets.UTF_8);
83 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
87 void whenDmaapConfigHasSeveralStreamsPublishing() throws Exception {
88 JsonObject jsonRootObject = getJsonRootObject();
89 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_publishes");
90 JsonObject fake_info_object = new JsonObject();
91 fake_info_object.addProperty("fake_info", "fake");
92 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
93 DataPublishing data = new Gson().fromJson(json.toString(), DataPublishing.class);
94 final String expectedMessage =
95 "Invalid configuration. Number of streams must be one, config: " + data.toString();
97 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
99 assertEquals(expectedMessage, actualException.getMessage(),
100 "Wrong error message when the DMaaP config has several streams publishing");
103 class DataPublishing {
104 private JsonObject dmaap_publisher;
105 private JsonObject fake_info_object;
108 public String toString() {
109 return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
110 fake_info_object.toString());
115 void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
116 JsonObject jsonRootObject = getJsonRootObject();
117 JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
118 JsonObject fake_info_object = new JsonObject();
119 fake_info_object.addProperty("fake_info", "fake");
120 json.add("fake_info_object", new Gson().toJsonTree(fake_info_object));
121 DataSubscribing data = new Gson().fromJson(json.toString(), DataSubscribing.class);
122 final String expectedMessage =
123 "Invalid configuration. Number of streams must be one, config: " + data.toString();
125 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
127 assertEquals(expectedMessage, actualException.getMessage(),
128 "Wrong error message when the DMaaP config has several streams subscribing");
131 private class DataSubscribing {
132 private JsonObject dmaap_subscriber;
133 private JsonObject fake_info_object;
136 public String toString() {
137 return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
138 fake_info_object.toString());
143 void whenWrongMemberNameInObject() throws Exception {
144 JsonObject jsonRootObject = getJsonRootObject();
145 JsonObject json = jsonRootObject.getAsJsonObject("config");
147 final String message = "Could not find member: 'ric' in: " + json;
149 Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
151 assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
154 JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
155 String dmaapPublisherOrSubscriber) throws Exception {
156 return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
157 .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");