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