Fix configuration in JSon
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / tasks / RefreshConfigTaskTest.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.tasks;
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.mockito.Mock;
55 import org.mockito.Spy;
56 import org.mockito.junit.jupiter.MockitoExtension;
57 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
58 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
59 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
60 import org.oransc.policyagent.configuration.ApplicationConfig;
61 import org.oransc.policyagent.configuration.ApplicationConfigParser;
62 import org.oransc.policyagent.configuration.ImmutableRicConfig;
63 import org.oransc.policyagent.configuration.RicConfig;
64 import org.oransc.policyagent.exceptions.ServiceException;
65 import org.oransc.policyagent.utils.LoggingUtils;
66
67 import reactor.core.publisher.Flux;
68 import reactor.core.publisher.Mono;
69 import reactor.test.StepVerifier;
70
71 @ExtendWith(MockitoExtension.class)
72 public class RefreshConfigTaskTest {
73
74     private RefreshConfigTask refreshTaskUnderTest;
75
76     @Spy
77     ApplicationConfig appConfig;
78
79     @Mock
80     CbsClient cbsClient;
81
82     public static final ImmutableRicConfig CORRECT_RIC_CONIFG = ImmutableRicConfig.builder() //
83         .name("ric1") //
84         .baseUrl("http://localhost:8080/") //
85         .managedElementIds(new Vector<String>(Arrays.asList("kista_1", "kista_2"))) //
86         .build();
87
88     private static EnvProperties properties() {
89         return ImmutableEnvProperties.builder() //
90             .consulHost("host") //
91             .consulPort(123) //
92             .cbsName("cbsName") //
93             .appName("appName") //
94             .build();
95     }
96
97     @Test
98     public void whenTheConfigurationFits() throws IOException, ServiceException {
99         refreshTaskUnderTest = spy(new RefreshConfigTask(appConfig));
100         refreshTaskUnderTest.systemEnvironment = new Properties();
101         // When
102         doReturn(getCorrectJson()).when(refreshTaskUnderTest).createInputStream(any());
103         doReturn("fileName").when(appConfig).getLocalConfigurationFilePath();
104         refreshTaskUnderTest.start();
105
106         // Then
107         verify(refreshTaskUnderTest, times(1)).loadConfigurationFromFile();
108
109         Iterable<RicConfig> ricConfigs = appConfig.getRicConfigs();
110         RicConfig ricConfig = ricConfigs.iterator().next();
111         assertThat(ricConfigs).isNotNull();
112         assertThat(ricConfig).isEqualTo(CORRECT_RIC_CONIFG);
113     }
114
115     @Test
116     public void whenFileIsExistsButJsonIsIncorrect() throws IOException, ServiceException {
117         refreshTaskUnderTest = spy(new RefreshConfigTask(appConfig));
118         refreshTaskUnderTest.systemEnvironment = new Properties();
119
120         // When
121         doReturn(getIncorrectJson()).when(refreshTaskUnderTest).createInputStream(any());
122         doReturn("fileName").when(appConfig).getLocalConfigurationFilePath();
123         refreshTaskUnderTest.loadConfigurationFromFile();
124
125         // Then
126         verify(refreshTaskUnderTest, times(1)).loadConfigurationFromFile();
127         Assertions.assertEquals(0, appConfig.getRicConfigs().size());
128     }
129
130     @Test
131     public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
132         refreshTaskUnderTest = spy(new RefreshConfigTask(appConfig));
133         refreshTaskUnderTest.systemEnvironment = new Properties();
134
135         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(RefreshConfigTask.class);
136         Flux<ApplicationConfig> task = refreshTaskUnderTest.createRefreshTask();
137
138         StepVerifier.create(task).expectSubscription().verifyComplete();
139
140         assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
141     }
142
143     @Test
144     public void whenPeriodicConfigRefreshNoConsul() {
145         refreshTaskUnderTest = spy(new RefreshConfigTask(appConfig));
146         refreshTaskUnderTest.systemEnvironment = new Properties();
147
148         EnvProperties props = properties();
149         doReturn(Mono.just(props)).when(refreshTaskUnderTest).getEnvironment(any());
150
151         doReturn(Mono.just(cbsClient)).when(refreshTaskUnderTest).createCbsClient(props);
152         Flux<JsonObject> err = Flux.error(new IOException());
153         doReturn(err).when(cbsClient).updates(any(), any(), any());
154
155         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(RefreshConfigTask.class);
156         Flux<ApplicationConfig> task = refreshTaskUnderTest.createRefreshTask();
157
158         StepVerifier //
159             .create(task) //
160             .expectSubscription() //
161             .verifyComplete();
162
163         assertTrue(
164             logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
165     }
166
167     @Test
168     public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
169         refreshTaskUnderTest = spy(new RefreshConfigTask(appConfig));
170         refreshTaskUnderTest.systemEnvironment = new Properties();
171
172         EnvProperties props = properties();
173         doReturn(Mono.just(props)).when(refreshTaskUnderTest).getEnvironment(any());
174         doReturn(Mono.just(cbsClient)).when(refreshTaskUnderTest).createCbsClient(props);
175
176         Flux<JsonObject> json = Flux.just(getJsonRootObject());
177         doReturn(json).when(cbsClient).updates(any(), any(), any());
178
179         Flux<ApplicationConfig> task = refreshTaskUnderTest.createRefreshTask();
180
181         StepVerifier //
182             .create(task) //
183             .expectSubscription() //
184             .expectNext(appConfig) //
185             .verifyComplete();
186
187         Assertions.assertNotNull(appConfig.getRicConfigs());
188     }
189
190     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
191         JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
192         return rootObject;
193     }
194
195     private static InputStream getCorrectJson() throws IOException {
196         URL url = ApplicationConfigParser.class.getClassLoader().getResource("test_application_configuration.json");
197         String string = Resources.toString(url, Charsets.UTF_8);
198         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
199     }
200
201     private static InputStream getIncorrectJson() {
202         String string = "{" + //
203             "    \"config\": {" + //
204             "        \"ric\": {"; //
205         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
206     }
207 }