2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018, 2020 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.configuration;
19 import ch.qos.logback.classic.spi.ILoggingEvent;
20 import ch.qos.logback.core.read.ListAppender;
21 import com.google.common.base.Charsets;
22 import com.google.common.io.Resources;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonIOException;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import com.google.gson.JsonSyntaxException;
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
32 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
33 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
37 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys;
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
40 import reactor.test.StepVerifier;
42 import java.io.ByteArrayInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.InputStreamReader;
47 import java.nio.charset.StandardCharsets;
49 import java.util.Properties;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.assertThatThrownBy;
53 import static org.junit.jupiter.api.Assertions.assertTrue;
54 import static org.mockito.ArgumentMatchers.any;
55 import static org.mockito.Mockito.doReturn;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.spy;
58 import static org.mockito.Mockito.times;
59 import static org.mockito.Mockito.verify;
60 import static org.mockito.Mockito.when;
63 * Tests the AppConfig.
65 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
66 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
68 public class AppConfigTest {
70 public static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
72 private static final PublisherConfiguration CORRECT_PUBLISHER_CONFIG = //
73 ImmutablePublisherConfiguration.builder() //
74 .publishUrl("https://localhost:3907/publish/1") //
75 .logUrl("https://localhost:3907/feedlog/1") //
76 .trustStorePath("src/test/resources/trust.jks") //
77 .trustStorePasswordPath("src/test/resources/trust.pass") //
78 .keyStorePath("src/test/resources/cert.jks") //
79 .keyStorePasswordPath("src/test/resources/jks.pass") //
80 .enableDmaapCertAuth(true) //
81 .changeIdentifier("PM_MEAS_FILES") //
82 .userName("CYE9fl40") //
83 .password("izBJD8nLjawq0HMG") //
86 private static final ImmutableFtpesConfig CORRECT_FTPES_CONFIGURATION = //
87 new ImmutableFtpesConfig.Builder() //
88 .keyCert("/src/test/resources/dfc.jks") //
89 .keyPasswordPath("/src/test/resources/dfc.jks.pass") //
90 .trustedCa("/src/test/resources/ftp.jks") //
91 .trustedCaPasswordPath("/src/test/resources/ftp.jks.pass") //
94 private AppConfig appConfigUnderTest;
95 private final Map<String, String> context = MappedDiagnosticContext.initializeTraceContext();
96 CbsClient cbsClient = mock(CbsClient.class);
97 CbsClientConfiguration cbsClientConfiguration = mock(CbsClientConfiguration.class);
101 appConfigUnderTest = spy(AppConfig.class);
102 appConfigUnderTest.systemEnvironment = new Properties();
107 public void whenTheConfigurationFits() throws IOException, DatafileTaskException {
109 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
110 appConfigUnderTest.initialize();
113 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
115 ConsumerConfiguration consumerCfg = appConfigUnderTest.getDmaapConsumerConfiguration();
116 Assertions.assertNotNull(consumerCfg);
117 assertThat(consumerCfg).satisfies(this::checkCorrectConsumerConfiguration);
119 PublisherConfiguration publisherCfg = appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER);
120 Assertions.assertNotNull(publisherCfg);
121 assertThat(publisherCfg).isEqualToComparingFieldByField(CORRECT_PUBLISHER_CONFIG);
123 FtpesConfig ftpesConfig = appConfigUnderTest.getFtpesConfiguration();
124 assertThat(ftpesConfig).isNotNull();
125 assertThat(ftpesConfig).isEqualToComparingFieldByField(CORRECT_FTPES_CONFIGURATION);
129 public void whenTheConfigurationFits_twoProducers() throws IOException, DatafileTaskException {
131 doReturn(getCorrectJsonTwoProducers()).when(appConfigUnderTest).createInputStream(any());
132 appConfigUnderTest.loadConfigurationFromFile();
135 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
136 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
137 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER));
138 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("XX_FILES"));
139 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("YY_FILES"));
141 assertThat(appConfigUnderTest.getPublisherConfiguration("XX_FILES").publishUrl())
142 .isEqualTo("feed01::publish_url");
143 assertThat(appConfigUnderTest.getPublisherConfiguration("YY_FILES").publishUrl())
144 .isEqualTo("feed01::publish_url");
148 public void whenFileIsNotExist_ThrowException() throws DatafileTaskException {
150 appConfigUnderTest.setFilepath("/temp.json");
153 appConfigUnderTest.loadConfigurationFromFile();
156 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
157 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
158 .hasMessageContaining("No PublishingConfiguration loaded, changeIdentifier: PM_MEAS_FILES");
160 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
164 public void whenFileIsExistsButJsonIsIncorrect() throws IOException, DatafileTaskException {
167 doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any());
168 appConfigUnderTest.loadConfigurationFromFile();
171 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
172 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
173 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
174 .hasMessageContaining(CHANGE_IDENTIFIER);
175 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
179 public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException, DatafileTaskException {
182 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
183 JsonElement jsonElement = mock(JsonElement.class);
184 when(jsonElement.isJsonObject()).thenReturn(false);
185 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(InputStream.class));
186 appConfigUnderTest.loadConfigurationFromFile();
189 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
190 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
191 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
192 .hasMessageContaining(CHANGE_IDENTIFIER);
193 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
197 public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
198 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
199 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask(context);
203 .expectSubscription() //
204 .verifyComplete(); //
206 assertTrue(logAppender.list.toString().contains("CbsClientConfigurationException"));
210 public void whenPeriodicConfigRefreshNoConsul() {
211 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
212 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
213 Flux<JsonObject> err = Flux.error(new IOException());
214 doReturn(err).when(cbsClient).updates(any(), any(), any());
216 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
217 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask(context);
221 .expectSubscription() //
225 logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
229 public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
230 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
231 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
233 Flux<JsonObject> json = Flux.just(getJsonRootObject());
234 doReturn(json).when(cbsClient).updates(any(), any(), any());
236 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask(context);
240 .expectSubscription() //
241 .expectNext(appConfigUnderTest) //
244 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
248 public void whenPeriodicConfigRefreshSuccess2() throws JsonIOException, JsonSyntaxException, IOException {
249 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
250 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
252 Flux<JsonObject> json = Flux.just(getJsonRootObject());
253 Flux<JsonObject> err = Flux.error(new IOException()); // no config entry created by the
256 doReturn(json, err).when(cbsClient).updates(any(), any(), any());
258 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask(context);
262 .expectSubscription() //
263 .expectNext(appConfigUnderTest) //
266 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
269 private void checkCorrectConsumerConfiguration(ConsumerConfiguration consumerConfiguration) {
270 MessageRouterSubscribeRequest messageRouterSubscribeRequest =
271 consumerConfiguration.getMessageRouterSubscribeRequest();
272 assertThat(messageRouterSubscribeRequest.consumerGroup()).isEqualTo("OpenDcae-c12");
273 assertThat(messageRouterSubscribeRequest.consumerId()).isEqualTo("C12");
274 assertThat(messageRouterSubscribeRequest.sourceDefinition().topicUrl())
275 .isEqualTo("http://localhost:2222/events/unauthenticated.VES_NOTIFICATION_OUTPUT");
276 SecurityKeys securityKeys = consumerConfiguration.getMessageRouterSubscriberConfig().securityKeys();
277 assertThat(securityKeys.keyStore().path().toString()).isEqualTo("src/test/resources/cert.jks");
278 assertThat(securityKeys.trustStore().path().toString()).isEqualTo("src/test/resources/trust.jks");
279 assertThat(consumerConfiguration.getMessageRouterSubscriber()).isNotNull();
282 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
283 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
287 private static InputStream getCorrectJson() throws IOException {
288 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test.json");
289 String string = Resources.toString(url, Charsets.UTF_8);
290 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
293 private static InputStream getCorrectJsonTwoProducers() throws IOException {
294 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test_2producers.json");
295 String string = Resources.toString(url, Charsets.UTF_8);
296 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
299 private static InputStream getIncorrectJson() {
300 String string = "{" + //
301 " \"configs\": {" + //
303 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));