6a4f8b3681bc69b3d4223d8346f9900f10e54223
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / configuration / ApplicationConfigTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.configuration;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30
31 import ch.qos.logback.classic.spi.ILoggingEvent;
32 import ch.qos.logback.core.read.ListAppender;
33
34 import com.google.common.base.Charsets;
35 import com.google.common.io.Resources;
36 import com.google.gson.JsonIOException;
37 import com.google.gson.JsonObject;
38 import com.google.gson.JsonParser;
39 import com.google.gson.JsonSyntaxException;
40
41 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
45 import java.net.URL;
46 import java.nio.charset.StandardCharsets;
47 import java.util.Arrays;
48 import java.util.Properties;
49 import java.util.Vector;
50
51 import org.junit.jupiter.api.Assertions;
52 import org.junit.jupiter.api.Test;
53 import org.junit.jupiter.api.extension.ExtendWith;
54 import org.junit.runner.RunWith;
55 import org.mockito.Mock;
56 import org.mockito.junit.MockitoJUnitRunner;
57 import org.mockito.junit.jupiter.MockitoExtension;
58 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
59 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
60 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
61 import org.oransc.policyagent.exceptions.ServiceException;
62 import org.oransc.policyagent.utils.LoggingUtils;
63
64 import reactor.core.publisher.Flux;
65 import reactor.core.publisher.Mono;
66 import reactor.test.StepVerifier;
67
68 @ExtendWith(MockitoExtension.class)
69 @RunWith(MockitoJUnitRunner.class)
70 public class ApplicationConfigTest {
71
72     private ApplicationConfig appConfigUnderTest;
73
74     @Mock
75     CbsClient cbsClient;
76
77     public static final ImmutableRicConfig CORRECT_RIC_CONIFG = ImmutableRicConfig.builder() //
78         .name("ric1") //
79         .baseUrl("http://localhost:8080/") //
80         .managedElementIds(new Vector<String>(Arrays.asList("kista_1", "kista_2"))) //
81         .build();
82
83     private static EnvProperties properties() {
84         return ImmutableEnvProperties.builder() //
85             .consulHost("host") //
86             .consulPort(123) //
87             .cbsName("cbsName") //
88             .appName("appName") //
89             .build();
90     }
91
92     @Test
93     public void whenTheConfigurationFits() throws IOException, ServiceException {
94
95         appConfigUnderTest = spy(ApplicationConfig.class);
96         appConfigUnderTest.systemEnvironment = new Properties();
97         // When
98         doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
99         doReturn("fileName").when(appConfigUnderTest).getLocalConfigurationFilePath();
100         appConfigUnderTest.initialize();
101
102         // Then
103         verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
104
105         Iterable<RicConfig> ricConfigs = appConfigUnderTest.getRicConfigs();
106         RicConfig ricConfig = ricConfigs.iterator().next();
107         assertThat(ricConfigs).isNotNull();
108         assertThat(ricConfig).isEqualTo(CORRECT_RIC_CONIFG);
109     }
110
111     @Test
112     public void whenFileIsExistsButJsonIsIncorrect() throws IOException, ServiceException {
113
114         appConfigUnderTest = spy(ApplicationConfig.class);
115         appConfigUnderTest.systemEnvironment = new Properties();
116
117         // When
118         doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any());
119         doReturn("fileName").when(appConfigUnderTest).getLocalConfigurationFilePath();
120         appConfigUnderTest.loadConfigurationFromFile();
121
122         // Then
123         verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
124         Assertions.assertEquals(0, appConfigUnderTest.getRicConfigs().size());
125     }
126
127     @Test
128     public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
129
130         appConfigUnderTest = spy(ApplicationConfig.class);
131         appConfigUnderTest.systemEnvironment = new Properties();
132
133         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
134         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
135
136         StepVerifier.create(task).expectSubscription().verifyComplete();
137
138         assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
139     }
140
141     @Test
142     public void whenPeriodicConfigRefreshNoConsul() {
143         appConfigUnderTest = spy(ApplicationConfig.class);
144         appConfigUnderTest.systemEnvironment = new Properties();
145
146         EnvProperties props = properties();
147         doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
148
149         doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
150         Flux<JsonObject> err = Flux.error(new IOException());
151         doReturn(err).when(cbsClient).updates(any(), any(), any());
152
153         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
154         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
155
156         StepVerifier //
157             .create(task) //
158             .expectSubscription() //
159             .verifyComplete();
160
161         assertTrue(
162             logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
163     }
164
165     @Test
166     public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
167         appConfigUnderTest = spy(ApplicationConfig.class);
168         appConfigUnderTest.systemEnvironment = new Properties();
169
170         EnvProperties props = properties();
171         doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
172         doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
173
174         Flux<JsonObject> json = Flux.just(getJsonRootObject());
175         doReturn(json).when(cbsClient).updates(any(), any(), any());
176
177         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
178
179         StepVerifier //
180             .create(task) //
181             .expectSubscription() //
182             .expectNext(appConfigUnderTest) //
183             .verifyComplete();
184
185         Assertions.assertNotNull(appConfigUnderTest.getRicConfigs());
186     }
187
188     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
189         JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
190         return rootObject;
191     }
192
193     private static InputStream getCorrectJson() throws IOException {
194         URL url = ApplicationConfigParser.class.getClassLoader().getResource("test_application_configuration.json");
195         String string = Resources.toString(url, Charsets.UTF_8);
196         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
197     }
198
199     private static InputStream getIncorrectJson() {
200         String string = "{" + //
201             "    \"config\": {" + //
202             "        \"ric\": {"; //
203         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
204     }
205
206 }