2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018, 2020-2022 Nokia. All rights reserved.
4 * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved.
5 * ===============================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License
12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing permissions and limitations under
15 * ============LICENSE_END========================================================================
18 package org.onap.dcaegen2.collectors.datafile.configuration;
20 import ch.qos.logback.classic.spi.ILoggingEvent;
21 import ch.qos.logback.core.read.ListAppender;
22 import com.google.common.base.Charsets;
23 import com.google.common.io.Resources;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonIOException;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28 import com.google.gson.JsonSyntaxException;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
33 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
34 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
38 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys;
39 import reactor.core.publisher.Flux;
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
43 import java.io.ByteArrayInputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.InputStreamReader;
48 import java.nio.charset.StandardCharsets;
50 import java.util.Properties;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.assertj.core.api.Assertions.assertThatThrownBy;
54 import static org.junit.jupiter.api.Assertions.assertTrue;
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.Mockito.doReturn;
57 import static org.mockito.Mockito.mock;
58 import static org.mockito.Mockito.spy;
59 import static org.mockito.Mockito.times;
60 import static org.mockito.Mockito.verify;
61 import static org.mockito.Mockito.when;
64 * Tests the AppConfig.
66 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
67 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
71 public static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
73 private static final PublisherConfiguration CORRECT_PUBLISHER_CONFIG = //
74 ImmutablePublisherConfiguration.builder() //
75 .publishUrl("https://localhost:3907/publish/1") //
76 .logUrl("https://localhost:3907/feedlog/1") //
77 .trustStorePath("src/test/resources/trust.jks") //
78 .trustStorePasswordPath("src/test/resources/trust.pass") //
79 .keyStorePath("src/test/resources/cert.jks") //
80 .keyStorePasswordPath("src/test/resources/jks.pass") //
81 .enableDmaapCertAuth(true) //
82 .changeIdentifier("PM_MEAS_FILES") //
83 .userName("CYE9fl40") //
84 .password("izBJD8nLjawq0HMG") //
87 private static final ImmutableCertificateConfig CORRECT_CERTIFICATE_CONFIGURATION = //
88 new ImmutableCertificateConfig.Builder() //
89 .keyCert("/src/test/resources/dfc.jks") //
90 .keyPasswordPath("/src/test/resources/dfc.jks.pass") //
91 .trustedCa("/src/test/resources/cert.jks") //
92 .trustedCaPasswordPath("/src/test/resources/cert.jks.pass") //
93 .httpsHostnameVerify(true)
97 private AppConfig appConfigUnderTest;
98 private final Map<String, String> context = MappedDiagnosticContext.initializeTraceContext();
99 CbsClient cbsClient = mock(CbsClient.class);
100 CbsClientConfiguration cbsClientConfiguration = mock(CbsClientConfiguration.class);
104 appConfigUnderTest = spy(AppConfig.class);
105 appConfigUnderTest.systemEnvironment = new Properties();
110 void whenTheConfigurationFits() throws IOException, DatafileTaskException {
112 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
113 appConfigUnderTest.initialize();
116 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
118 ConsumerConfiguration consumerCfg = appConfigUnderTest.getDmaapConsumerConfiguration();
119 Assertions.assertNotNull(consumerCfg);
120 assertThat(consumerCfg).satisfies(this::checkCorrectConsumerConfiguration);
122 PublisherConfiguration publisherCfg = appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER);
123 Assertions.assertNotNull(publisherCfg);
124 assertThat(publisherCfg).isEqualToComparingFieldByField(CORRECT_PUBLISHER_CONFIG);
126 CertificateConfig certificateConfig = appConfigUnderTest.getCertificateConfiguration();
127 assertThat(certificateConfig)
129 .isEqualToComparingFieldByField(CORRECT_CERTIFICATE_CONFIGURATION);
133 void shouldInitializeApplicationWithoutCertificates() throws IOException {
135 doReturn(getCorrectConfigWithoutTLS()).when(appConfigUnderTest).createInputStream(any());
136 appConfigUnderTest.initialize();
139 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
141 CertificateConfig certificateConfig = appConfigUnderTest.getCertificateConfiguration();
142 assertThat(certificateConfig).isNotNull();
146 void whenTheConfigurationFits_twoProducers() throws IOException, DatafileTaskException {
148 doReturn(getCorrectJsonTwoProducers()).when(appConfigUnderTest).createInputStream(any());
149 appConfigUnderTest.loadConfigurationFromFile();
152 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
153 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
154 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER));
155 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("XX_FILES"));
156 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("YY_FILES"));
158 assertThat(appConfigUnderTest.getPublisherConfiguration("XX_FILES").publishUrl())
159 .isEqualTo("feed01::publish_url");
160 assertThat(appConfigUnderTest.getPublisherConfiguration("YY_FILES").publishUrl())
161 .isEqualTo("feed01::publish_url");
165 void whenFileIsNotExist_ThrowException() throws DatafileTaskException {
167 appConfigUnderTest.setFilepath("/temp.json");
170 appConfigUnderTest.loadConfigurationFromFile();
173 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
174 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
175 .hasMessageContaining("No PublishingConfiguration loaded, changeIdentifier: PM_MEAS_FILES");
177 Assertions.assertNull(appConfigUnderTest.getCertificateConfiguration());
181 void whenFileIsExistsButJsonIsIncorrect() throws IOException, DatafileTaskException {
184 doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any());
185 appConfigUnderTest.loadConfigurationFromFile();
188 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
189 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
190 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
191 .hasMessageContaining(CHANGE_IDENTIFIER);
192 Assertions.assertNull(appConfigUnderTest.getCertificateConfiguration());
196 void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException, DatafileTaskException {
199 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
200 JsonElement jsonElement = mock(JsonElement.class);
201 when(jsonElement.isJsonObject()).thenReturn(false);
202 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(InputStream.class));
203 appConfigUnderTest.loadConfigurationFromFile();
206 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
207 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
208 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
209 .hasMessageContaining(CHANGE_IDENTIFIER);
210 Assertions.assertNull(appConfigUnderTest.getCertificateConfiguration());
214 void whenPeriodicConfigRefreshNoEnvironmentVariables() {
215 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
216 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask();
220 .expectSubscription() //
221 .verifyComplete(); //
223 assertTrue(logAppender.list.toString().contains("CbsClientConfigurationException"));
227 void whenPeriodicConfigRefreshNoConsul() {
228 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
229 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
230 Flux<JsonObject> err = Flux.error(new IOException());
231 doReturn(err).when(cbsClient).updates(any(), any(), any());
233 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
234 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask();
238 .expectSubscription() //
242 logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
246 void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
247 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
248 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
250 Flux<JsonObject> json = Flux.just(getJsonRootObject());
251 doReturn(json).when(cbsClient).updates(any(), any(), any());
253 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask();
257 .expectSubscription() //
258 .expectNext(appConfigUnderTest) //
261 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
265 void whenPeriodicConfigRefreshSuccess2() throws JsonIOException, JsonSyntaxException, IOException {
266 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
267 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
269 Flux<JsonObject> json = Flux.just(getJsonRootObject());
270 Flux<JsonObject> err = Flux.error(new IOException()); // no config entry created by the
273 doReturn(json, err).when(cbsClient).updates(any(), any(), any());
275 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask();
279 .expectSubscription() //
280 .expectNext(appConfigUnderTest) //
283 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
286 private void checkCorrectConsumerConfiguration(ConsumerConfiguration consumerConfiguration) {
287 MessageRouterSubscribeRequest messageRouterSubscribeRequest =
288 consumerConfiguration.getMessageRouterSubscribeRequest();
289 assertThat(messageRouterSubscribeRequest.consumerGroup()).isEqualTo("OpenDcae-c12");
290 assertThat(messageRouterSubscribeRequest.consumerId()).isEqualTo("C12");
291 assertThat(messageRouterSubscribeRequest.sourceDefinition().topicUrl())
292 .isEqualTo("http://localhost:2222/events/unauthenticated.VES_NOTIFICATION_OUTPUT");
293 SecurityKeys securityKeys = consumerConfiguration.getMessageRouterSubscriberConfig().securityKeys();
294 assertThat(securityKeys.keyStore().path().toString()).hasToString("src/test/resources/cert.jks");
295 assertThat(securityKeys.trustStore().path().toString()).hasToString("src/test/resources/trust.jks");
296 assertThat(consumerConfiguration.getMessageRouterSubscriber()).isNotNull();
299 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
300 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
304 private static InputStream getCorrectJson() throws IOException {
305 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test.json");
306 String string = Resources.toString(url, Charsets.UTF_8);
307 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
310 private static InputStream getCorrectConfigWithoutTLS() throws IOException {
311 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_test_config_no_tls.json");
312 String string = Resources.toString(url, Charsets.UTF_8);
313 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
316 private static InputStream getCorrectJsonTwoProducers() throws IOException {
317 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test_2producers.json");
318 String string = Resources.toString(url, Charsets.UTF_8);
319 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
322 private static InputStream getIncorrectJson() {
323 String string = "{" + //
324 " \"configs\": {" + //
326 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));