2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Copyright (C) 2022 Nokia. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.dcaegen2.services.pmmapper.config;
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import java.io.IOException;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.List;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.extension.ExtendWith;
41 import org.junit.jupiter.params.ParameterizedTest;
42 import org.junit.jupiter.params.provider.MethodSource;
43 import org.mockito.junit.jupiter.MockitoExtension;
44 import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException;
45 import org.onap.dcaegen2.services.pmmapper.exceptions.MapperConfigException;
46 import org.onap.dcaegen2.services.pmmapper.model.MapperConfig;
48 import com.google.gson.Gson;
49 import com.google.gson.JsonObject;
51 import ch.qos.logback.classic.spi.ILoggingEvent;
52 import ch.qos.logback.core.read.ListAppender;
53 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
54 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
55 import reactor.core.publisher.Mono;
56 import utils.FileUtils;
57 import utils.LoggingUtils;
60 @ExtendWith(MockitoExtension.class)
61 class ConfigHandlerTests {
62 private static String validMapperConfig;
63 private static String validMapperConfigChanged;
65 private static final Path INVALID_CONFIGS_DIRECTORY = Paths.get("src/test/resources/invalid_configs/");
66 private static final Path MISSING_OPTIONAL_FIELDS_CONFIGS_DIRECTORY =
67 Paths.get("src/test/resources/missing_optional_fields/");
68 private static final String EXPECTED_ERROR_MESSAGE_IN_LOG = "Error parsing configuration";
69 private static final String EXPECTED_CHANGED_VALUE = "https://dmaap-dr-node:8443/delete_changed";
71 private final Gson gson = new Gson();
72 private final CbsClient cbsClient = mock(CbsClient.class);
73 private final CbsRequest cbsRequest = mock(CbsRequest.class);
77 static void beforeAll() {
78 validMapperConfig = FileUtils.getFileContents("valid_mapper_config.json");
79 validMapperConfigChanged = FileUtils.getFileContents("valid_mapper_config_after_change.json");
84 Mono<JsonObject> just = createMonoJsonObject(validMapperConfig);
85 when(cbsClient.get(any())).thenReturn(just);
89 void getMapperConfig_success() throws Exception {
90 MapperConfig expectedConfig = gson.fromJson(validMapperConfig, MapperConfig.class);
92 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigHandler.class);
93 MapperConfig actualConfig = new ConfigHandler(cbsClient, cbsRequest).getMapperConfig();
95 assertEquals(expectedConfig.getPublisherTopicUrl(), actualConfig.getPublisherTopicUrl());
96 assertEquals(expectedConfig.getPublisherUserName(), actualConfig.getPublisherUserName());
97 assertEquals(expectedConfig.getPublisherPassword(), actualConfig.getPublisherPassword());
98 assertEquals(expectedConfig, actualConfig);
104 void configuration_should_can_be_changed() throws EnvironmentConfigException {
105 MapperConfig expectedConfig = gson.fromJson(validMapperConfig, MapperConfig.class);
106 Mono<JsonObject> just = createMonoJsonObject(validMapperConfig);
108 when(cbsClient.get(any())).thenReturn(just);
109 ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
111 MapperConfig actualConfig = configHandler.getInitialConfiguration();
112 assertEquals(expectedConfig.getDmaapDRDeleteEndpoint(), actualConfig.getDmaapDRDeleteEndpoint());
114 Mono<JsonObject> justChanged = createMonoJsonObject(validMapperConfigChanged);
115 when(cbsClient.get(any())).thenReturn(justChanged);
116 MapperConfig changedConfig = configHandler.getMapperConfig();
118 System.out.println(changedConfig.getDmaapDRDeleteEndpoint());
119 assertEquals(EXPECTED_CHANGED_VALUE, changedConfig.getDmaapDRDeleteEndpoint());
123 void should_throw_exception_when_configuration_is_not_initialized() {
124 String wrongConfigJson = "{\"test\": \"test\"}";
125 Mono<JsonObject> just = createMonoJsonObject(wrongConfigJson);
127 when(cbsClient.get(any())).thenReturn(just);
129 ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
130 assertThrows(EnvironmentConfigException.class, configHandler::getMapperConfig);
134 void mapper_parse_invalid_json_mapper_config() {
135 String wrongConfigJson = "{\"test\": \"test\"}";
136 Mono<JsonObject> just = createMonoJsonObject(wrongConfigJson);
138 when(cbsClient.get(any())).thenReturn(just);
139 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigHandler.class);
140 ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
142 assertThrows(MapperConfigException.class, configHandler::getInitialConfiguration);
143 assertConfigurationErrorIsLogged(logAppender);
148 @MethodSource("getConfigsWithMissingOptionalFields")
149 void should_parse_json_with_missing_optional_fields(String mapperConfig) {
150 Mono<JsonObject> just = createMonoJsonObject(mapperConfig);
152 when(cbsClient.get(any())).thenReturn(just);
153 ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
155 assertDoesNotThrow(configHandler::getInitialConfiguration);
159 @MethodSource("getInvalidConfigs")
160 void parse_valid_json_bad_values_mapper_config(String mapperConfig) throws Exception {
161 Mono<JsonObject> just = createMonoJsonObject(mapperConfig);
163 when(cbsClient.get(any())).thenReturn(just);
164 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigHandler.class);
165 ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
167 assertThrows(MapperConfigException.class, configHandler::getInitialConfiguration);
168 assertConfigurationErrorIsLogged(logAppender);
172 private void assertConfigurationErrorIsLogged(ListAppender<ILoggingEvent> logAppender) {
173 boolean containMessage = logAppender.list.stream()
174 .anyMatch(iLoggingEvent -> iLoggingEvent.getFormattedMessage().contains(EXPECTED_ERROR_MESSAGE_IN_LOG));
175 assertTrue(containMessage);
178 private Mono<JsonObject> createMonoJsonObject(String stringJson) {
179 JsonObject configJson = new Gson().fromJson(stringJson, JsonObject.class);
180 return Mono.just(configJson);
183 private static List<String> getInvalidConfigs() throws IOException {
184 return FileUtils.getFilesFromDirectory(INVALID_CONFIGS_DIRECTORY);
187 private static List<String> getConfigsWithMissingOptionalFields() throws IOException {
188 return FileUtils.getFilesFromDirectory(MISSING_OPTIONAL_FIELDS_CONFIGS_DIRECTORY);