2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 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 static org.assertj.core.api.Assertions.assertThat;
20 import static org.assertj.core.api.Assertions.assertThatThrownBy;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import com.google.common.base.Charsets;
31 import com.google.common.io.Resources;
32 import com.google.gson.JsonElement;
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;
45 import java.util.Properties;
47 import org.junit.jupiter.api.Assertions;
48 import org.junit.jupiter.api.BeforeEach;
49 import org.junit.jupiter.api.Test;
50 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
51 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
52 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
53 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
54 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties;
55 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider;
56 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
57 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapPublisherConfiguration;
59 import ch.qos.logback.classic.spi.ILoggingEvent;
60 import ch.qos.logback.core.read.ListAppender;
61 import reactor.core.publisher.Flux;
62 import reactor.core.publisher.Mono;
63 import reactor.test.StepVerifier;
66 * Tests the AppConfig.
68 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
69 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
73 private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
76 private static final ImmutableDmaapConsumerConfiguration CORRECT_DMAAP_CONSUMER_CONFIG = //
77 new ImmutableDmaapConsumerConfiguration.Builder() //
79 .dmaapHostName("message-router.onap.svc.cluster.local") //
80 .dmaapUserName("admin") //
81 .dmaapUserPassword("admin") //
82 .dmaapTopicName("events/unauthenticated.VES_NOTIFICATION_OUTPUT") //
83 .dmaapPortNumber(2222) //
84 .dmaapContentType("application/json") //
86 .dmaapProtocol("http") //
88 .consumerGroup("OpenDcae-c12") //
89 .trustStorePath("trustStorePath") //
90 .trustStorePasswordPath("trustStorePasswordPath") //
91 .keyStorePath("keyStorePath") //
92 .keyStorePasswordPath("keyStorePasswordPath") //
93 .enableDmaapCertAuth(true) //
96 private static final ConsumerConfiguration CORRECT_CONSUMER_CONFIG = ImmutableConsumerConfiguration.builder() //
98 "http://admin:admin@message-router.onap.svc.cluster.local:2222/events/unauthenticated.VES_NOTIFICATION_OUTPUT/OpenDcae-c12/C12")
99 .trustStorePath("trustStorePath") //
100 .trustStorePasswordPath("trustStorePasswordPath") //
101 .keyStorePath("keyStorePath") //
102 .keyStorePasswordPath("keyStorePasswordPath") //
103 .enableDmaapCertAuth(true) //
106 private static final PublisherConfiguration CORRECT_PUBLISHER_CONFIG = //
107 ImmutablePublisherConfiguration.builder() //
108 .publishUrl("https://message-router.onap.svc.cluster.local:3907/publish/1") //
109 .logUrl("https://dmaap.example.com/feedlog/972").trustStorePath("trustStorePath") //
110 .trustStorePasswordPath("trustStorePasswordPath") //
111 .keyStorePath("keyStorePath") //
112 .keyStorePasswordPath("keyStorePasswordPath") //
113 .enableDmaapCertAuth(true) //
114 .changeIdentifier("PM_MEAS_FILES") //
116 .passWord("password") //
119 private static final ImmutableFtpesConfig CORRECT_FTPES_CONFIGURATION = //
120 new ImmutableFtpesConfig.Builder() //
121 .keyCert("/config/dfc.jks") //
122 .keyPassword("secret") //
123 .trustedCa("config/ftp.jks") //
124 .trustedCaPassword("secret") //
127 private static final ImmutableDmaapPublisherConfiguration CORRECT_DMAAP_PUBLISHER_CONFIG = //
128 new ImmutableDmaapPublisherConfiguration.Builder() //
129 .dmaapTopicName("/publish/1") //
130 .dmaapUserPassword("password") //
131 .dmaapPortNumber(3907) //
132 .dmaapProtocol("https") //
133 .dmaapContentType("application/octet-stream") //
134 .dmaapHostName("message-router.onap.svc.cluster.local") //
135 .dmaapUserName("user") //
136 .trustStorePath("trustStorePath") //
137 .trustStorePasswordPath("trustStorePasswordPath") //
138 .keyStorePath("keyStorePath") //
139 .keyStorePasswordPath("keyStorePasswordPath") //
140 .enableDmaapCertAuth(true) //
143 private static EnvProperties properties() {
144 return ImmutableEnvProperties.builder() //
145 .consulHost("host") //
147 .cbsName("cbsName") //
148 .appName("appName") //
152 private AppConfig appConfigUnderTest;
153 private CloudConfigurationProvider cloudConfigurationProvider = mock(CloudConfigurationProvider.class);
154 private final Map<String, String> context = MappedDiagnosticContext.initializeTraceContext();
158 public void setUp() {
159 appConfigUnderTest = spy(AppConfig.class);
160 appConfigUnderTest.setCloudConfigurationProvider(cloudConfigurationProvider);
161 appConfigUnderTest.systemEnvironment = new Properties();
165 public void whenTheConfigurationFits() throws IOException, DatafileTaskException {
167 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
168 appConfigUnderTest.initialize();
171 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
173 ConsumerConfiguration consumerCfg = appConfigUnderTest.getDmaapConsumerConfiguration();
174 Assertions.assertNotNull(consumerCfg);
175 assertThat(consumerCfg.toDmaap()).isEqualToComparingFieldByField(CORRECT_DMAAP_CONSUMER_CONFIG);
176 assertThat(consumerCfg).isEqualToComparingFieldByField(CORRECT_CONSUMER_CONFIG);
178 PublisherConfiguration publisherCfg = appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER);
179 Assertions.assertNotNull(publisherCfg);
180 assertThat(publisherCfg).isEqualToComparingFieldByField(CORRECT_PUBLISHER_CONFIG);
181 assertThat(publisherCfg.toDmaap()).isEqualToComparingFieldByField(CORRECT_DMAAP_PUBLISHER_CONFIG);
183 FtpesConfig ftpesConfig = appConfigUnderTest.getFtpesConfiguration();
184 assertThat(ftpesConfig).isNotNull();
185 assertThat(ftpesConfig).isEqualToComparingFieldByField(CORRECT_FTPES_CONFIGURATION);
189 public void whenTheConfigurationFits_twoProducers() throws IOException, DatafileTaskException {
191 doReturn(getCorrectJsonTwoProducers()).when(appConfigUnderTest).createInputStream(any());
192 appConfigUnderTest.loadConfigurationFromFile();
195 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
196 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
197 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER));
198 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("XX_FILES"));
199 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("YY_FILES"));
201 assertThat(appConfigUnderTest.getPublisherConfiguration("XX_FILES").publishUrl())
202 .isEqualTo("feed01::publish_url");
203 assertThat(appConfigUnderTest.getPublisherConfiguration("YY_FILES").publishUrl())
204 .isEqualTo("feed01::publish_url");
208 public void whenFileIsNotExist_ThrowException() throws DatafileTaskException {
210 appConfigUnderTest.setFilepath("/temp.json");
213 appConfigUnderTest.loadConfigurationFromFile();
216 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
217 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
218 .hasMessageContaining("No PublishingConfiguration loaded, changeIdentifier: PM_MEAS_FILES");
220 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
224 public void whenFileIsExistsButJsonIsIncorrect() throws IOException, DatafileTaskException {
227 doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any());
228 appConfigUnderTest.loadConfigurationFromFile();
231 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
232 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
233 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
234 .hasMessageContaining(CHANGE_IDENTIFIER);
235 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
239 public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException, DatafileTaskException {
242 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
243 JsonElement jsonElement = mock(JsonElement.class);
244 when(jsonElement.isJsonObject()).thenReturn(false);
245 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(JsonParser.class), any(InputStream.class));
246 appConfigUnderTest.loadConfigurationFromFile();
249 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
250 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
251 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
252 .hasMessageContaining(CHANGE_IDENTIFIER);
253 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
257 public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
258 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
260 Flux<AppConfig> task = appConfigUnderTest.createRefreshConfigurationTask(1L, context);
264 .expectSubscription() //
265 .expectNextCount(0) //
268 assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
272 public void whenPeriodicConfigRefreshNoConsul() {
273 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
275 doReturn(Mono.just(properties())).when(appConfigUnderTest).readEnvironmentVariables(any(), any());
276 Mono<JsonObject> err = Mono.error(new IOException());
277 doReturn(err).when(cloudConfigurationProvider).callForServiceConfigurationReactive(any());
279 Flux<AppConfig> task = appConfigUnderTest.createRefreshConfigurationTask(1L, context);
283 .expectSubscription() //
284 .expectNextCount(0) //
287 assertTrue(logAppender.list.toString()
288 .contains("Could not refresh application configuration java.io.IOException"));
292 public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
293 doReturn(Mono.just(properties())).when(appConfigUnderTest).readEnvironmentVariables(any(), any());
295 Mono<JsonObject> json = Mono.just(getJsonRootObject());
297 doReturn(json, json).when(cloudConfigurationProvider).callForServiceConfigurationReactive(any());
299 Flux<AppConfig> task = appConfigUnderTest.createRefreshConfigurationTask(1L, context);
303 .expectSubscription() //
304 .expectNext(appConfigUnderTest) //
307 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
311 public void whenPeriodicConfigRefreshSuccess2() throws JsonIOException, JsonSyntaxException, IOException {
312 doReturn(Mono.just(properties())).when(appConfigUnderTest).readEnvironmentVariables(any(), any());
314 Mono<JsonObject> json = Mono.just(getJsonRootObject());
315 Mono<JsonObject> err = Mono.error(new IOException()); // no config entry created by the
318 doReturn(json, err).when(cloudConfigurationProvider).callForServiceConfigurationReactive(any());
320 Flux<AppConfig> task = appConfigUnderTest.createRefreshConfigurationTask(1L, context);
324 .expectSubscription() //
325 .expectNext(appConfigUnderTest) //
328 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
331 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
332 JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
336 private static InputStream getCorrectJson() throws IOException {
337 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test.json");
338 String string = Resources.toString(url, Charsets.UTF_8);
339 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
342 private static InputStream getCorrectJsonTwoProducers() throws IOException {
343 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test_2producers.json");
344 String string = Resources.toString(url, Charsets.UTF_8);
345 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
348 private static InputStream getIncorrectJson() {
349 String string = "{" + //
350 " \"configs\": {" + //
352 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));