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