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.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
28 import ch.qos.logback.classic.spi.ILoggingEvent;
29 import ch.qos.logback.core.read.ListAppender;
31 import com.google.common.base.Charsets;
32 import com.google.common.io.Resources;
33 import com.google.gson.JsonIOException;
34 import com.google.gson.JsonObject;
35 import com.google.gson.JsonParser;
36 import com.google.gson.JsonSyntaxException;
38 import java.io.ByteArrayInputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.InputStreamReader;
43 import java.nio.charset.StandardCharsets;
44 import java.util.Properties;
46 import org.junit.Test;
47 import org.junit.jupiter.api.Assertions;
48 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
49 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
50 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
51 import org.oransc.policyagent.utils.LoggingUtils;
53 import reactor.core.publisher.Flux;
54 import reactor.core.publisher.Mono;
55 import reactor.test.StepVerifier;
57 public class ApplicationConfigTest {
59 private ApplicationConfig appConfigUnderTest;
60 CbsClient cbsClient = mock(CbsClient.class);
62 private static EnvProperties properties() {
63 return ImmutableEnvProperties.builder() //
64 .consulHost("host") //
66 .cbsName("cbsName") //
67 .appName("appName") //
72 public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
74 appConfigUnderTest = spy(ApplicationConfig.class);
75 appConfigUnderTest.systemEnvironment = new Properties();
77 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
78 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
80 StepVerifier.create(task).expectSubscription().verifyComplete();
82 assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
86 public void whenPeriodicConfigRefreshNoConsul() {
87 appConfigUnderTest = spy(ApplicationConfig.class);
88 appConfigUnderTest.systemEnvironment = new Properties();
90 EnvProperties props = properties();
91 doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
93 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
94 Flux<JsonObject> err = Flux.error(new IOException());
95 doReturn(err).when(cbsClient).updates(any(), any(), any());
97 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
98 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
102 .expectSubscription() //
106 logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
110 public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
111 appConfigUnderTest = spy(ApplicationConfig.class);
112 appConfigUnderTest.systemEnvironment = new Properties();
114 EnvProperties props = properties();
115 doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
116 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
118 Flux<JsonObject> json = Flux.just(getJsonRootObject());
119 doReturn(json).when(cbsClient).updates(any(), any(), any());
121 Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
125 .expectSubscription() //
126 .expectNext(appConfigUnderTest) //
129 Assertions.assertNotNull(appConfigUnderTest.getRicConfigs());
132 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
133 JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
137 private static InputStream getCorrectJson() throws IOException {
138 URL url = ApplicationConfigParser.class.getClassLoader().getResource("test_application_configuration.json");
139 String string = Resources.toString(url, Charsets.UTF_8);
140 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));