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