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===================================
20 package org.oransc.policyagent.configuration;
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;
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;
52 public class ApplicationConfigTest {
54 private ApplicationConfig appConfigUnderTest;
55 CbsClient cbsClient = mock(CbsClient.class);
58 private static EnvProperties properties() {
59 return ImmutableEnvProperties.builder() //
60 .consulHost("host") //
62 .cbsName("cbsName") //
63 .appName("appName") //
68 public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
70 appConfigUnderTest = spy(ApplicationConfig.class);
71 appConfigUnderTest.systemEnvironment = new Properties();
73 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
74 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
76 StepVerifier.create(task)
80 assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
84 public void whenPeriodicConfigRefreshNoConsul() {
85 appConfigUnderTest = spy(ApplicationConfig.class);
86 appConfigUnderTest.systemEnvironment = new Properties();
88 EnvProperties props = properties();
89 doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
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());
95 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
96 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
100 .expectSubscription() //
104 logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
108 public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
109 appConfigUnderTest = spy(ApplicationConfig.class);
110 appConfigUnderTest.systemEnvironment = new Properties();
112 EnvProperties props = properties();
113 doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
114 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
116 Flux<JsonObject> json = Flux.just(getJsonRootObject());
117 doReturn(json).when(cbsClient).updates(any(), any(), any());
119 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
123 .expectSubscription() //
124 .expectNext(appConfigUnderTest) //
127 Assertions.assertNotNull(appConfigUnderTest.getRicConfigs());
130 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
131 JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
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)));