Fix loading SSL Context when certpaths not exist in configuration
[dcaegen2/services/pm-mapper.git] / src / test / java / org / onap / dcaegen2 / services / pmmapper / config / ConfigHandlerTests.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
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;
28
29 import java.io.IOException;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
33
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;
47
48 import com.google.gson.Gson;
49 import com.google.gson.JsonObject;
50
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;
58
59
60 @ExtendWith(MockitoExtension.class)
61 class ConfigHandlerTests {
62     private static String validMapperConfig;
63     private static String validMapperConfigChanged;
64
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";
70
71     private final Gson gson = new Gson();
72     private final CbsClient cbsClient = mock(CbsClient.class);
73     private final CbsRequest cbsRequest = mock(CbsRequest.class);
74
75
76     @BeforeAll
77     static void beforeAll() {
78         validMapperConfig = FileUtils.getFileContents("valid_mapper_config.json");
79         validMapperConfigChanged = FileUtils.getFileContents("valid_mapper_config_after_change.json");
80     }
81
82     @BeforeEach
83     void setup() {
84         Mono<JsonObject> just = createMonoJsonObject(validMapperConfig);
85         when(cbsClient.get(any())).thenReturn(just);
86     }
87
88     @Test
89     void getMapperConfig_success() throws Exception {
90         MapperConfig expectedConfig = gson.fromJson(validMapperConfig, MapperConfig.class);
91
92         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigHandler.class);
93         MapperConfig actualConfig = new ConfigHandler(cbsClient, cbsRequest).getMapperConfig();
94
95         assertEquals(expectedConfig.getPublisherTopicUrl(), actualConfig.getPublisherTopicUrl());
96         assertEquals(expectedConfig.getPublisherUserName(), actualConfig.getPublisherUserName());
97         assertEquals(expectedConfig.getPublisherPassword(), actualConfig.getPublisherPassword());
98         assertEquals(expectedConfig, actualConfig);
99         logAppender.stop();
100
101     }
102
103     @Test
104     void configuration_should_can_be_changed() throws EnvironmentConfigException {
105         MapperConfig expectedConfig = gson.fromJson(validMapperConfig, MapperConfig.class);
106         Mono<JsonObject> just = createMonoJsonObject(validMapperConfig);
107
108         when(cbsClient.get(any())).thenReturn(just);
109         ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
110
111         MapperConfig actualConfig = configHandler.getInitialConfiguration();
112         assertEquals(expectedConfig.getDmaapDRDeleteEndpoint(), actualConfig.getDmaapDRDeleteEndpoint());
113
114         Mono<JsonObject> justChanged = createMonoJsonObject(validMapperConfigChanged);
115         when(cbsClient.get(any())).thenReturn(justChanged);
116         MapperConfig changedConfig = configHandler.getMapperConfig();
117
118         System.out.println(changedConfig.getDmaapDRDeleteEndpoint());
119         assertEquals(EXPECTED_CHANGED_VALUE, changedConfig.getDmaapDRDeleteEndpoint());
120     }
121
122     @Test
123     void should_throw_exception_when_configuration_is_not_initialized() {
124         String wrongConfigJson = "{\"test\": \"test\"}";
125         Mono<JsonObject> just = createMonoJsonObject(wrongConfigJson);
126
127         when(cbsClient.get(any())).thenReturn(just);
128
129         ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
130         assertThrows(EnvironmentConfigException.class, configHandler::getMapperConfig);
131     }
132
133     @Test
134     void mapper_parse_invalid_json_mapper_config() {
135         String wrongConfigJson = "{\"test\": \"test\"}";
136         Mono<JsonObject> just = createMonoJsonObject(wrongConfigJson);
137
138         when(cbsClient.get(any())).thenReturn(just);
139         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigHandler.class);
140         ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
141
142         assertThrows(MapperConfigException.class, configHandler::getInitialConfiguration);
143         assertConfigurationErrorIsLogged(logAppender);
144         logAppender.stop();
145     }
146
147     @ParameterizedTest
148     @MethodSource("getConfigsWithMissingOptionalFields")
149     void should_parse_json_with_missing_optional_fields(String mapperConfig) {
150         Mono<JsonObject> just = createMonoJsonObject(mapperConfig);
151
152         when(cbsClient.get(any())).thenReturn(just);
153         ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
154
155         assertDoesNotThrow(configHandler::getInitialConfiguration);
156     }
157
158     @ParameterizedTest
159     @MethodSource("getInvalidConfigs")
160     void parse_valid_json_bad_values_mapper_config(String mapperConfig) throws Exception {
161         Mono<JsonObject> just = createMonoJsonObject(mapperConfig);
162
163         when(cbsClient.get(any())).thenReturn(just);
164         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ConfigHandler.class);
165         ConfigHandler configHandler = new ConfigHandler(cbsClient, cbsRequest);
166
167         assertThrows(MapperConfigException.class, configHandler::getInitialConfiguration);
168         assertConfigurationErrorIsLogged(logAppender);
169         logAppender.stop();
170     }
171
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);
176     }
177
178     private Mono<JsonObject> createMonoJsonObject(String stringJson) {
179         JsonObject configJson = new Gson().fromJson(stringJson, JsonObject.class);
180         return Mono.just(configJson);
181     }
182
183     private static List<String> getInvalidConfigs() throws IOException {
184         return FileUtils.getFilesFromDirectory(INVALID_CONFIGS_DIRECTORY);
185     }
186
187     private static List<String> getConfigsWithMissingOptionalFields() throws IOException {
188         return FileUtils.getFilesFromDirectory(MISSING_OPTIONAL_FIELDS_CONFIGS_DIRECTORY);
189     }
190 }