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