2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.configuration;
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;
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;
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;
64 @ExtendWith(MockitoExtension.class)
65 @RunWith(MockitoJUnitRunner.class)
66 public class ApplicationConfigTest {
68 private ApplicationConfig appConfigUnderTest;
73 public static final ImmutableRicConfig CORRECT_RIC_CONIFG = ImmutableRicConfig.builder() //
75 .baseUrl("http://localhost:8080/") //
76 .managedElementIds(new Vector<String>(Arrays.asList("kista_1", "kista_2"))) //
79 private static EnvProperties properties() {
80 return ImmutableEnvProperties.builder() //
81 .consulHost("host") //
83 .cbsName("cbsName") //
84 .appName("appName") //
89 public void whenTheConfigurationFits() throws IOException, ServiceException {
91 appConfigUnderTest = spy(ApplicationConfig.class);
92 appConfigUnderTest.systemEnvironment = new Properties();
94 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
95 appConfigUnderTest.initialize();
98 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(any());
100 Vector<RicConfig> ricConfigs = appConfigUnderTest.getRicConfigs();
101 RicConfig ricConfig = ricConfigs.firstElement();
102 assertThat(ricConfigs).isNotNull();
103 assertThat(ricConfig).isEqualTo(CORRECT_RIC_CONIFG);
107 public void whenFileIsExistsButJsonIsIncorrect() throws IOException, ServiceException {
109 appConfigUnderTest = spy(ApplicationConfig.class);
110 appConfigUnderTest.systemEnvironment = new Properties();
113 doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any());
114 appConfigUnderTest.loadConfigurationFromFile(any());
117 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(any());
118 Assertions.assertNull(appConfigUnderTest.getRicConfigs());
122 public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
124 appConfigUnderTest = spy(ApplicationConfig.class);
125 appConfigUnderTest.systemEnvironment = new Properties();
127 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
128 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
130 StepVerifier.create(task).expectSubscription().verifyComplete();
132 assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
136 public void whenPeriodicConfigRefreshNoConsul() {
137 appConfigUnderTest = spy(ApplicationConfig.class);
138 appConfigUnderTest.systemEnvironment = new Properties();
140 EnvProperties props = properties();
141 doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
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());
147 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
148 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
152 .expectSubscription() //
156 logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
160 public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
161 appConfigUnderTest = spy(ApplicationConfig.class);
162 appConfigUnderTest.systemEnvironment = new Properties();
164 EnvProperties props = properties();
165 doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
166 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
168 Flux<JsonObject> json = Flux.just(getJsonRootObject());
169 doReturn(json).when(cbsClient).updates(any(), any(), any());
171 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
175 .expectSubscription() //
176 .expectNext(appConfigUnderTest) //
179 Assertions.assertNotNull(appConfigUnderTest.getRicConfigs());
182 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
183 JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
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)));
193 private static InputStream getIncorrectJson() {
194 String string = "{" + //
195 " \"config\": {" + //
197 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));