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.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyString;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonParser;
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Objects;
38 import org.junit.jupiter.api.Assertions;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.onap.dcaegen2.collectors.datafile.integration.junit5.mockito.MockitoExtension;
45 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
46 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
48 @ExtendWith({MockitoExtension.class})
51 private static final String DATAFILE_ENDPOINTS = "datafile_endpoints.json";
52 private static final boolean CORRECT_JSON = true;
53 private static final boolean INCORRECT_JSON = false;
55 private static AppConfig appConfigUnderTest;
58 private static String filePath = Objects
59 .requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile();
63 appConfigUnderTest = spy(AppConfig.class);
67 public void whenApplicationWasStarted_FilePathIsSet() {
69 appConfigUnderTest.setFilepath(filePath);
72 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
73 verify(appConfigUnderTest, times(0)).initFileStreamReader();
74 Assertions.assertEquals(filePath, appConfigUnderTest.getFilepath());
78 public void whenTheConfigurationFits_GetFtpsAndDmaapObjectRepresentationConfiguration() throws IOException {
80 InputStream inputStream =
81 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
84 appConfigUnderTest.setFilepath(filePath);
85 doReturn(inputStream).when(appConfigUnderTest).getInputStream(any());
86 appConfigUnderTest.initFileStreamReader();
87 appConfigUnderTest.dmaapConsumerConfiguration = appConfigUnderTest.getDmaapConsumerConfiguration();
88 appConfigUnderTest.dmaapPublisherConfiguration = appConfigUnderTest.getDmaapPublisherConfiguration();
89 appConfigUnderTest.ftpesConfig = appConfigUnderTest.getFtpesConfiguration();
92 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
93 verify(appConfigUnderTest, times(1)).initFileStreamReader();
94 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
95 Assertions.assertNotNull(appConfigUnderTest.getDmaapPublisherConfiguration());
96 Assertions.assertEquals(appConfigUnderTest.getDmaapPublisherConfiguration(),
97 appConfigUnderTest.getDmaapPublisherConfiguration());
98 Assertions.assertEquals(appConfigUnderTest.getDmaapConsumerConfiguration(),
99 appConfigUnderTest.getDmaapConsumerConfiguration());
100 Assertions.assertEquals(appConfigUnderTest.getFtpesConfiguration(), appConfigUnderTest.getFtpesConfiguration());
105 public void whenFileIsNotExist_ThrowIoException() {
107 filePath = "/temp.json";
108 appConfigUnderTest.setFilepath(filePath);
111 appConfigUnderTest.initFileStreamReader();
114 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
115 verify(appConfigUnderTest, times(1)).initFileStreamReader();
116 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
117 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
118 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
123 public void whenFileIsExistsButJsonIsIncorrect() throws IOException {
125 InputStream inputStream =
126 new ByteArrayInputStream((getJsonConfig(INCORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
129 appConfigUnderTest.setFilepath(filePath);
130 doReturn(inputStream).when(appConfigUnderTest).getInputStream(any());
131 appConfigUnderTest.initFileStreamReader();
134 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
135 verify(appConfigUnderTest, times(1)).initFileStreamReader();
136 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
137 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
138 Assertions.assertNotNull(appConfigUnderTest.getFtpesConfiguration());
144 public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException {
146 InputStream inputStream =
147 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
149 appConfigUnderTest.setFilepath(filePath);
150 doReturn(inputStream).when(appConfigUnderTest).getInputStream(any());
151 JsonElement jsonElement = mock(JsonElement.class);
152 when(jsonElement.isJsonObject()).thenReturn(false);
153 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(JsonParser.class), any(InputStream.class));
154 appConfigUnderTest.initFileStreamReader();
155 appConfigUnderTest.dmaapConsumerConfiguration = appConfigUnderTest.getDmaapConsumerConfiguration();
156 appConfigUnderTest.dmaapPublisherConfiguration = appConfigUnderTest.getDmaapPublisherConfiguration();
157 appConfigUnderTest.ftpesConfig = appConfigUnderTest.getFtpesConfiguration();
160 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
161 verify(appConfigUnderTest, times(1)).initFileStreamReader();
162 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
163 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
164 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
167 private String getJsonConfig(boolean correct) {
168 JsonObject dmaapConsumerConfigData = new JsonObject();
169 dmaapConsumerConfigData.addProperty("dmaapHostName", "localhost");
170 dmaapConsumerConfigData.addProperty("dmaapPortNumber", 2222);
171 dmaapConsumerConfigData.addProperty("dmaapTopicName", "/events/unauthenticated.VES_NOTIFICATION_OUTPUT");
172 dmaapConsumerConfigData.addProperty("dmaapProtocol", "http");
173 dmaapConsumerConfigData.addProperty("dmaapUserName", "admin");
174 dmaapConsumerConfigData.addProperty("dmaapUserPassword", "admin");
175 dmaapConsumerConfigData.addProperty("dmaapContentType", "application/json");
176 dmaapConsumerConfigData.addProperty("consumerId", "C12");
177 dmaapConsumerConfigData.addProperty("consumerGroup", "OpenDcae-c12");
178 dmaapConsumerConfigData.addProperty("timeoutMs", -1);
179 dmaapConsumerConfigData.addProperty("messageLimit", 1);
181 JsonObject dmaapProducerConfigData = new JsonObject();
182 dmaapProducerConfigData.addProperty("dmaapHostName", "localhost");
183 dmaapProducerConfigData.addProperty("dmaapPortNumber", 3907);
184 dmaapProducerConfigData.addProperty("dmaapTopicName", "publish");
185 dmaapProducerConfigData.addProperty("dmaapProtocol", "https");
187 dmaapProducerConfigData.addProperty("dmaapUserName", "dradmin");
188 dmaapProducerConfigData.addProperty("dmaapUserPassword", "dradmin");
189 dmaapProducerConfigData.addProperty("dmaapContentType", "application/octet-stream");
192 JsonObject dmaapConfigs = new JsonObject();
193 dmaapConfigs.add("dmaapConsumerConfiguration", dmaapConsumerConfigData);
194 dmaapConfigs.add("dmaapProducerConfiguration", dmaapProducerConfigData);
196 JsonObject ftpesConfigData = new JsonObject();
197 ftpesConfigData.addProperty("keyCert", "config/ftpKey.jks");
198 ftpesConfigData.addProperty("keyPassword", "secret");
199 ftpesConfigData.addProperty("trustedCA", "config/cacerts");
200 ftpesConfigData.addProperty("trustedCAPassword", "secret");
202 JsonObject security = new JsonObject();
203 security.addProperty("trustStorePath", "trustStorePath");
204 security.addProperty("trustStorePasswordPath", "trustStorePasswordPath");
205 security.addProperty("keyStorePath", "keyStorePath");
206 security.addProperty("keyStorePasswordPath", "keyStorePasswordPath");
207 security.addProperty("enableDmaapCertAuth", "enableDmaapCertAuth");
209 JsonObject ftpesConfiguration = new JsonObject();
210 ftpesConfiguration.add("ftpesConfiguration", ftpesConfigData);
212 JsonObject configs = new JsonObject();
213 configs.add("dmaap", dmaapConfigs);
214 configs.add("ftp", ftpesConfiguration);
215 configs.add("security", security);
217 JsonObject completeJson = new JsonObject();
218 completeJson.add("configs", configs);
220 return completeJson.toString();