2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018, 2020-2021 NOKIA Intellectual Property, 2018-2019 Nordix Foundation.
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)
96 private AppConfig appConfigUnderTest;
97 private final Map<String, String> context = MappedDiagnosticContext.initializeTraceContext();
98 CbsClient cbsClient = mock(CbsClient.class);
99 CbsClientConfiguration cbsClientConfiguration = mock(CbsClientConfiguration.class);
103 appConfigUnderTest = spy(AppConfig.class);
104 appConfigUnderTest.systemEnvironment = new Properties();
109 void whenTheConfigurationFits() throws IOException, DatafileTaskException {
111 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
112 appConfigUnderTest.initialize();
115 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
117 ConsumerConfiguration consumerCfg = appConfigUnderTest.getDmaapConsumerConfiguration();
118 Assertions.assertNotNull(consumerCfg);
119 assertThat(consumerCfg).satisfies(this::checkCorrectConsumerConfiguration);
121 PublisherConfiguration publisherCfg = appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER);
122 Assertions.assertNotNull(publisherCfg);
123 assertThat(publisherCfg).isEqualToComparingFieldByField(CORRECT_PUBLISHER_CONFIG);
125 CertificateConfig certificateConfig = appConfigUnderTest.getCertificateConfiguration();
126 assertThat(certificateConfig)
128 .isEqualToComparingFieldByField(CORRECT_CERTIFICATE_CONFIGURATION);
132 void whenTheConfigurationFits_twoProducers() throws IOException, DatafileTaskException {
134 doReturn(getCorrectJsonTwoProducers()).when(appConfigUnderTest).createInputStream(any());
135 appConfigUnderTest.loadConfigurationFromFile();
138 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
139 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
140 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER));
141 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("XX_FILES"));
142 Assertions.assertNotNull(appConfigUnderTest.getPublisherConfiguration("YY_FILES"));
144 assertThat(appConfigUnderTest.getPublisherConfiguration("XX_FILES").publishUrl())
145 .isEqualTo("feed01::publish_url");
146 assertThat(appConfigUnderTest.getPublisherConfiguration("YY_FILES").publishUrl())
147 .isEqualTo("feed01::publish_url");
151 void whenFileIsNotExist_ThrowException() throws DatafileTaskException {
153 appConfigUnderTest.setFilepath("/temp.json");
156 appConfigUnderTest.loadConfigurationFromFile();
159 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
160 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
161 .hasMessageContaining("No PublishingConfiguration loaded, changeIdentifier: PM_MEAS_FILES");
163 Assertions.assertNull(appConfigUnderTest.getCertificateConfiguration());
167 void whenFileIsExistsButJsonIsIncorrect() throws IOException, DatafileTaskException {
170 doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any());
171 appConfigUnderTest.loadConfigurationFromFile();
174 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
175 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
176 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
177 .hasMessageContaining(CHANGE_IDENTIFIER);
178 Assertions.assertNull(appConfigUnderTest.getCertificateConfiguration());
182 void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException, DatafileTaskException {
185 doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
186 JsonElement jsonElement = mock(JsonElement.class);
187 when(jsonElement.isJsonObject()).thenReturn(false);
188 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(InputStream.class));
189 appConfigUnderTest.loadConfigurationFromFile();
192 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
193 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
194 assertThatThrownBy(() -> appConfigUnderTest.getPublisherConfiguration(CHANGE_IDENTIFIER))
195 .hasMessageContaining(CHANGE_IDENTIFIER);
196 Assertions.assertNull(appConfigUnderTest.getCertificateConfiguration());
200 void whenPeriodicConfigRefreshNoEnvironmentVariables() {
201 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
202 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask();
206 .expectSubscription() //
207 .verifyComplete(); //
209 assertTrue(logAppender.list.toString().contains("CbsClientConfigurationException"));
213 void whenPeriodicConfigRefreshNoConsul() {
214 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
215 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
216 Flux<JsonObject> err = Flux.error(new IOException());
217 doReturn(err).when(cbsClient).updates(any(), any(), any());
219 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(AppConfig.class);
220 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask();
224 .expectSubscription() //
228 logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException"));
232 void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException {
233 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
234 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
236 Flux<JsonObject> json = Flux.just(getJsonRootObject());
237 doReturn(json).when(cbsClient).updates(any(), any(), any());
239 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask();
243 .expectSubscription() //
244 .expectNext(appConfigUnderTest) //
247 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
251 void whenPeriodicConfigRefreshSuccess2() throws JsonIOException, JsonSyntaxException, IOException {
252 doReturn(Mono.just(cbsClientConfiguration)).when(appConfigUnderTest).createCbsClientConfiguration();
253 doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(cbsClientConfiguration);
255 Flux<JsonObject> json = Flux.just(getJsonRootObject());
256 Flux<JsonObject> err = Flux.error(new IOException()); // no config entry created by the
259 doReturn(json, err).when(cbsClient).updates(any(), any(), any());
261 Flux<AppConfig> task = appConfigUnderTest.createRefreshTask();
265 .expectSubscription() //
266 .expectNext(appConfigUnderTest) //
269 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
272 private void checkCorrectConsumerConfiguration(ConsumerConfiguration consumerConfiguration) {
273 MessageRouterSubscribeRequest messageRouterSubscribeRequest =
274 consumerConfiguration.getMessageRouterSubscribeRequest();
275 assertThat(messageRouterSubscribeRequest.consumerGroup()).isEqualTo("OpenDcae-c12");
276 assertThat(messageRouterSubscribeRequest.consumerId()).isEqualTo("C12");
277 assertThat(messageRouterSubscribeRequest.sourceDefinition().topicUrl())
278 .isEqualTo("http://localhost:2222/events/unauthenticated.VES_NOTIFICATION_OUTPUT");
279 SecurityKeys securityKeys = consumerConfiguration.getMessageRouterSubscriberConfig().securityKeys();
280 assertThat(securityKeys.keyStore().path().toString()).hasToString("src/test/resources/cert.jks");
281 assertThat(securityKeys.trustStore().path().toString()).hasToString("src/test/resources/trust.jks");
282 assertThat(consumerConfiguration.getMessageRouterSubscriber()).isNotNull();
285 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
286 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
290 private static InputStream getCorrectJson() throws IOException {
291 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test.json");
292 String string = Resources.toString(url, Charsets.UTF_8);
293 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
296 private static InputStream getCorrectJsonTwoProducers() throws IOException {
297 URL url = CloudConfigParser.class.getClassLoader().getResource("datafile_endpoints_test_2producers.json");
298 String string = Resources.toString(url, Charsets.UTF_8);
299 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
302 private static InputStream getIncorrectJson() {
303 String string = "{" + //
304 " \"configs\": {" + //
306 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));