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