Added ServiceController and Service supervision
[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.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27
28 import ch.qos.logback.classic.spi.ILoggingEvent;
29 import ch.qos.logback.core.read.ListAppender;
30
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;
37
38 import java.io.ByteArrayInputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.InputStreamReader;
42 import java.net.URL;
43 import java.nio.charset.StandardCharsets;
44 import java.util.Properties;
45
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;
52
53 import reactor.core.publisher.Flux;
54 import reactor.core.publisher.Mono;
55 import reactor.test.StepVerifier;
56
57 public class ApplicationConfigTest {
58
59     private ApplicationConfig appConfigUnderTest;
60     CbsClient cbsClient = mock(CbsClient.class);
61
62     private static EnvProperties properties() {
63         return ImmutableEnvProperties.builder() //
64             .consulHost("host") //
65             .consulPort(123) //
66             .cbsName("cbsName") //
67             .appName("appName") //
68             .build();
69     }
70
71     @Test
72     public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
73
74         appConfigUnderTest = spy(ApplicationConfig.class);
75         appConfigUnderTest.systemEnvironment = new Properties();
76
77         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
78         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
79
80         StepVerifier.create(task).expectSubscription().verifyComplete();
81
82         assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
83     }
84
85     @Test
86     public void whenPeriodicConfigRefreshNoConsul() {
87         appConfigUnderTest = spy(ApplicationConfig.class);
88         appConfigUnderTest.systemEnvironment = new Properties();
89
90         EnvProperties props = properties();
91         doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
92
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());
96
97         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class);
98         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
99
100         StepVerifier //
101             .create(task) //
102             .expectSubscription() //
103             .verifyComplete();
104
105         assertTrue(
106             logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
107     }
108
109     @Test
110     public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
111         appConfigUnderTest = spy(ApplicationConfig.class);
112         appConfigUnderTest.systemEnvironment = new Properties();
113
114         EnvProperties props = properties();
115         doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any());
116         doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props);
117
118         Flux<JsonObject> json = Flux.just(getJsonRootObject());
119         doReturn(json).when(cbsClient).updates(any(), any(), any());
120
121         Flux<ApplicationConfig> task = appConfigUnderTest.createRefreshTask();
122
123         StepVerifier //
124             .create(task) //
125             .expectSubscription() //
126             .expectNext(appConfigUnderTest) //
127             .verifyComplete();
128
129         Assertions.assertNotNull(appConfigUnderTest.getRicConfigs());
130     }
131
132     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
133         JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
134         return rootObject;
135     }
136
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)));
141     }
142
143 }