d12fd6bc0748ccf3f1bc098d024152629f169d72
[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.Test;
46 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
47
48 class ApplicationConfigParserTest {
49
50     ApplicationConfig applicationConfigMock = spy(new ApplicationConfig());
51     ApplicationConfigParser parserUnderTest = new ApplicationConfigParser(applicationConfigMock);
52
53     @Test
54     void whenCorrectConfig() throws Exception {
55         JsonObject jsonRootObject = getJsonRootObject();
56
57         when(applicationConfigMock.getConfigurationFileSchemaPath())
58                 .thenReturn("application_configuration_schema.json");
59
60         ApplicationConfigParser.ConfigParserResult result = parserUnderTest.parse(jsonRootObject);
61
62         String topicUrl = result.dmaapProducerTopicUrl();
63         assertEquals("http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE", topicUrl, "controller contents");
64
65         topicUrl = result.dmaapConsumerTopicUrl();
66         assertEquals(
67                 "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=15000&limit=100",
68                 topicUrl, "controller contents");
69
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") //
75                 .userName("user") //
76                 .password("password") //
77                 .build(); //
78         assertEquals(expectedControllerConfig, controllers.get("controller1"), "controller contents");
79
80         assertEquals(2, result.ricConfigs().size());
81     }
82
83     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
84         JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
85         return rootObject;
86     }
87
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)));
93     }
94
95     @Test
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();
105
106         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
107
108         assertEquals(expectedMessage, actualException.getMessage(),
109                 "Wrong error message when the DMaaP config has several streams publishing");
110     }
111
112     class DataPublishing {
113         private JsonObject dmaap_publisher;
114         private JsonObject fake_info_object;
115
116         @Override
117         public String toString() {
118             return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
119                     fake_info_object.toString());
120         }
121     }
122
123     @Test
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();
133
134         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
135
136         assertEquals(expectedMessage, actualException.getMessage(),
137                 "Wrong error message when the DMaaP config has several streams subscribing");
138     }
139
140     private class DataSubscribing {
141         private JsonObject dmaap_subscriber;
142         private JsonObject fake_info_object;
143
144         @Override
145         public String toString() {
146             return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
147                     fake_info_object.toString());
148         }
149     }
150
151     @Test
152     void whenWrongMemberNameInObject() throws Exception {
153         JsonObject jsonRootObject = getJsonRootObject();
154         JsonObject json = jsonRootObject.getAsJsonObject("config");
155         json.remove("ric");
156         final String message = "Could not find member: [ric] in: " + json;
157
158         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
159
160         assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
161     }
162
163     @Test
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");
169         json.remove("ric");
170
171         Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
172
173         assertThat(actualException.getMessage()).contains("Json schema validation failure");
174     }
175
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");
180     }
181 }