Integrate Config Binding Service client
[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.junit.Assert.assertTrue;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.doReturn;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.net.URL;
32 import java.nio.charset.StandardCharsets;
33 import java.util.Properties;
34 import ch.qos.logback.classic.spi.ILoggingEvent;
35 import ch.qos.logback.core.read.ListAppender;
36 import com.google.common.base.Charsets;
37 import com.google.common.io.Resources;
38 import com.google.gson.JsonIOException;
39 import com.google.gson.JsonObject;
40 import com.google.gson.JsonParser;
41 import com.google.gson.JsonSyntaxException;
42 import org.junit.Test;
43 import org.junit.jupiter.api.Assertions;
44 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
45 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
46 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
47 import org.oransc.policyagent.utils.LoggingUtils;
48 import reactor.core.publisher.Flux;
49 import reactor.core.publisher.Mono;
50 import reactor.test.StepVerifier;
51
52 public class ApplicationConfigTest {
53
54     private ApplicationConfig appConfigUnderTest;
55     CbsClient cbsClient = mock(CbsClient.class);
56
57
58     private static EnvProperties properties() {
59         return ImmutableEnvProperties.builder() //
60                 .consulHost("host") //
61                 .consulPort(123) //
62                 .cbsName("cbsName") //
63                 .appName("appName") //
64                 .build();
65     }
66
67     @Test
68     public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
69
70         appConfigUnderTest = spy(ApplicationConfig.class);
71         appConfigUnderTest.systemEnvironment = new Properties();
72
73         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
74         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
75
76         StepVerifier.create(task)
77                 .expectSubscription()
78                 .verifyComplete();
79
80         assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
81     }
82
83     @Test
84     public void whenPeriodicConfigRefreshNoConsul() {
85         appConfigUnderTest = spy(ApplicationConfig.class);
86         appConfigUnderTest.systemEnvironment = new Properties();
87
88         EnvProperties props = properties();
89         doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
90
91         doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
92         Flux<JsonObject> err = Flux.error(new IOException());
93         doReturn(err).when(cbsClient).updates(any(), any(), any());
94
95         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
96         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
97
98         StepVerifier //
99                 .create(task) //
100                 .expectSubscription() //
101                 .verifyComplete();
102
103         assertTrue(
104                 logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
105     }
106
107     @Test
108     public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
109         appConfigUnderTest = spy(ApplicationConfig.class);
110         appConfigUnderTest.systemEnvironment = new Properties();
111
112         EnvProperties props = properties();
113         doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
114         doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
115
116         Flux<JsonObject> json = Flux.just(getJsonRootObject());
117         doReturn(json).when(cbsClient).updates(any(), any(), any());
118
119         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
120
121         StepVerifier //
122                 .create(task) //
123                 .expectSubscription() //
124                 .expectNext(appConfigUnderTest) //
125                 .verifyComplete();
126
127         Assertions.assertNotNull(appConfigUnderTest.getRicConfigs());
128     }
129
130     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
131         JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
132         return rootObject;
133     }
134
135     private static InputStream getCorrectJson() throws IOException {
136         URL url = ApplicationConfigParser.class.getClassLoader().getResource("test_application_configuration.json");
137         String string = Resources.toString(url, Charsets.UTF_8);
138         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
139     }
140
141 }