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 ch.qos.logback.classic.spi.ILoggingEvent;
31 import ch.qos.logback.core.read.ListAppender;
33 import com.google.common.base.Charsets;
34 import com.google.common.io.Resources;
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonIOException;
37 import com.google.gson.JsonObject;
38 import com.google.gson.JsonParser;
39 import com.google.gson.JsonSyntaxException;
41 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
46 import java.nio.charset.StandardCharsets;
48 import java.util.Properties;
50 import org.junit.jupiter.api.Assertions;
51 import org.junit.jupiter.api.BeforeEach;
52 import org.junit.jupiter.api.Test;
53 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
54 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
55 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
56 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
57 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties;
58 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider;
59 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
60 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapPublisherConfiguration;
62 import reactor.core.publisher.Flux;
63 import reactor.core.publisher.Mono;
64 import reactor.test.StepVerifier;
67 * Tests the AppConfig.
69 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
70 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
74 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();
157 public void setUp() {
158 appConfigUnderTest = spy(AppConfig.class);
159 appConfigUnderTest.setCloudConfigurationProvider(cloudConfigurationProvider);
160 appConfigUnderTest.systemEnvironment = new Properties();
164 public void whenTheConfigurationFits() throws IOException, DatafileTaskException {
166 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
167 appConfigUnderTest.initialize();
170 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
172 ConsumerConfiguration consumerCfg = appConfigUnderTest.getDmaapConsumerConfiguration();
173 Assertions.assertNotNull(consumerCfg);
174 assertThat(consumerCfg.toDmaap()).isEqualToComparingFieldByField(CORRECT_DMAAP_CONSUMER_CONFIG);
175 assertThat(consumerCfg).isEqualToComparingFieldByField(CORRECT_CONSUMER_CONFIG);
177 PublisherConfiguration publisherCfg = appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER);
178 Assertions.assertNotNull(publisherCfg);
179 assertThat(publisherCfg).isEqualToComparingFieldByField(CORRECT_PUBLISHER_CONFIG);
180 assertThat(publisherCfg.toDmaap()).isEqualToComparingFieldByField(CORRECT_DMAAP_PUBLISHER_CONFIG);
182 FtpesConfig ftpesConfig = appConfigUnderTest.getFtpesConfiguration();
183 assertThat(ftpesConfig).isNotNull();
184 assertThat(ftpesConfig).isEqualToComparingFieldByField(CORRECT_FTPES_CONFIGURATION);
188 public void whenTheConfigurationFits_twoProducers() throws IOException, DatafileTaskException {
190 doReturn(getCorrectJsonTwoProducers()).when(appConfigUnderTest).createInputStream(any());
191 appConfigUnderTest.loadConfigurationFromFile();
194 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
195 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
196 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER));
197 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("XX_FILES"));
198 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("YY_FILES"));
200 assertThat(appConfigUnderTest.getPublisherConfiguration("XX_FILES").publishUrl())
201 .isEqualTo("feed01::publish_url");
202 assertThat(appConfigUnderTest.getPublisherConfiguration("YY_FILES").publishUrl())
203 .isEqualTo("feed01::publish_url");
207 public void whenFileIsNotExist_ThrowException() throws DatafileTaskException {
209 appConfigUnderTest.setFilepath("/temp.json");
211 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
214 appConfigUnderTest.loadConfigurationFromFile();
217 assertTrue("Error message missing in log.",
218 logAppender.list.toString().contains("[WARN] Local configuration file not loaded: /temp.json"));
221 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
222 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
223 .hasMessageContaining("No PublishingConfiguration loaded, changeIdentifier: PM_MEAS_FILES");
224 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
228 public void whenFileIsExistsButJsonIsIncorrect() throws IOException, DatafileTaskException {
231 doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any());
232 appConfigUnderTest.loadConfigurationFromFile();
235 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
236 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
237 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
238 .hasMessageContaining(CHANGE_IDENTIFIER);
239 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
243 public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException, DatafileTaskException {
246 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
247 JsonElement jsonElement = mock(JsonElement.class);
248 when(jsonElement.isJsonObject()).thenReturn(false);
249 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(JsonParser.class), any(InputStream.class));
250 appConfigUnderTest.loadConfigurationFromFile();
253 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
254 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
255 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
256 .hasMessageContaining(CHANGE_IDENTIFIER);
257 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
261 public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
262 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
264 Flux<AppConfig> task = appConfigUnderTest.createRefreshConfigurationTask(1L, context);
268 .expectSubscription() //
269 .expectNextCount(0) //
272 assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined"));
276 public void whenPeriodicConfigRefreshNoConsul() {
278 doReturn(Mono.just(properties())).when(appConfigUnderTest).readEnvironmentVariables(any(), any());
279 Mono<JsonObject> err = Mono.error(new IOException());
280 doReturn(err).when(cloudConfigurationProvider).callForServiceConfigurationReactive(any());
282 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
283 Flux<AppConfig> task = appConfigUnderTest.createRefreshConfigurationTask(1L, context);
287 .expectSubscription() //
288 .expectNextCount(0) //
292 logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
296 public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
297 doReturn(Mono.just(properties())).when(appConfigUnderTest).readEnvironmentVariables(any(), any());
299 Mono<JsonObject> json = Mono.just(getJsonRootObject());
301 doReturn(json, json).when(cloudConfigurationProvider).callForServiceConfigurationReactive(any());
303 Flux<AppConfig> task = appConfigUnderTest.createRefreshConfigurationTask(1L, context);
307 .expectSubscription() //
308 .expectNext(appConfigUnderTest) //
311 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
315 public void whenPeriodicConfigRefreshSuccess2() throws JsonIOException, JsonSyntaxException, IOException {
316 doReturn(Mono.just(properties())).when(appConfigUnderTest).readEnvironmentVariables(any(), any());
318 Mono<JsonObject> json = Mono.just(getJsonRootObject());
319 Mono<JsonObject> err = Mono.error(new IOException()); // no config entry created by the dmaap plugin
321 doReturn(json, err).when(cloudConfigurationProvider).callForServiceConfigurationReactive(any());
323 Flux<AppConfig> task = appConfigUnderTest.createRefreshConfigurationTask(1L, context);
327 .expectSubscription() //
328 .expectNext(appConfigUnderTest) //
331 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
334 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
335 JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
339 private static InputStream getCorrectJson() throws IOException {
340 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test.json");
341 String string = Resources.toString(url, Charsets.UTF_8);
342 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
345 private static InputStream getCorrectJsonTwoProducers() throws IOException {
346 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test_2producers.json");
347 String string = Resources.toString(url, Charsets.UTF_8);
348 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
351 private static InputStream getIncorrectJson() {
352 String string = "{" + //
353 " \"configs\": {" + //
355 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));