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;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonParser;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.nio.charset.StandardCharsets;
34 import java.util.Objects;
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
40 * Tests the AppConfig.
42 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/9/18
43 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
47 private static final String DATAFILE_ENDPOINTS = "datafile_endpoints.json";
48 private static final boolean CORRECT_JSON = true;
49 private static final boolean INCORRECT_JSON = false;
51 private static AppConfig appConfigUnderTest;
53 private static String filePath =
54 Objects.requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile();
58 appConfigUnderTest = spy(AppConfig.class);
62 public void whenApplicationWasStarted_FilePathIsSet() {
64 appConfigUnderTest.setFilepath(filePath);
67 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
68 verify(appConfigUnderTest, times(0)).loadConfigurationFromFile();
69 Assertions.assertEquals(filePath, appConfigUnderTest.getFilepath());
73 public void whenTheConfigurationFits_GetFtpsAndDmaapObjectRepresentationConfiguration() throws IOException {
75 InputStream inputStream =
76 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
79 appConfigUnderTest.setFilepath(filePath);
80 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
81 appConfigUnderTest.loadConfigurationFromFile();
84 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
85 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
86 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
87 Assertions.assertNotNull(appConfigUnderTest.getDmaapPublisherConfiguration());
88 Assertions.assertEquals(appConfigUnderTest.getDmaapPublisherConfiguration(),
89 appConfigUnderTest.getDmaapPublisherConfiguration());
90 Assertions.assertEquals(appConfigUnderTest.getDmaapConsumerConfiguration(),
91 appConfigUnderTest.getDmaapConsumerConfiguration());
92 Assertions.assertEquals(appConfigUnderTest.getFtpesConfiguration(), appConfigUnderTest.getFtpesConfiguration());
96 public void whenFileIsNotExist_ThrowIoException() {
98 filePath = "/temp.json";
99 appConfigUnderTest.setFilepath(filePath);
102 appConfigUnderTest.loadConfigurationFromFile();
105 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
106 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
107 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
108 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
109 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
114 public void whenFileIsExistsButJsonIsIncorrect() throws IOException {
116 InputStream inputStream =
117 new ByteArrayInputStream((getJsonConfig(INCORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
120 appConfigUnderTest.setFilepath(filePath);
121 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
122 appConfigUnderTest.loadConfigurationFromFile();
125 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
126 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
127 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
128 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
129 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
135 public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException {
137 InputStream inputStream =
138 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
140 appConfigUnderTest.setFilepath(filePath);
141 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
142 JsonElement jsonElement = mock(JsonElement.class);
143 when(jsonElement.isJsonObject()).thenReturn(false);
144 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(JsonParser.class), any(InputStream.class));
145 appConfigUnderTest.loadConfigurationFromFile();
148 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
149 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
150 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
151 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
152 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
155 private String getJsonConfig(boolean correct) {
156 JsonObject dmaapConsumerConfigData = new JsonObject();
157 dmaapConsumerConfigData.addProperty("dmaapHostName", "localhost");
158 dmaapConsumerConfigData.addProperty("dmaapPortNumber", 2222);
159 dmaapConsumerConfigData.addProperty("dmaapTopicName", "/events/unauthenticated.VES_NOTIFICATION_OUTPUT");
160 dmaapConsumerConfigData.addProperty("dmaapProtocol", "http");
161 dmaapConsumerConfigData.addProperty("dmaapUserName", "admin");
162 dmaapConsumerConfigData.addProperty("dmaapUserPassword", "admin");
163 dmaapConsumerConfigData.addProperty("dmaapContentType", "application/json");
164 dmaapConsumerConfigData.addProperty("consumerId", "C12");
165 dmaapConsumerConfigData.addProperty("consumerGroup", "OpenDcae-c12");
166 dmaapConsumerConfigData.addProperty("timeoutMs", -1);
167 dmaapConsumerConfigData.addProperty("messageLimit", 1);
169 JsonObject dmaapProducerConfigData = new JsonObject();
170 dmaapProducerConfigData.addProperty("dmaapHostName", "localhost");
171 dmaapProducerConfigData.addProperty("dmaapPortNumber", 3907);
172 dmaapProducerConfigData.addProperty("dmaapTopicName", "publish");
173 dmaapProducerConfigData.addProperty("dmaapProtocol", "https");
175 dmaapProducerConfigData.addProperty("dmaapUserName", "dradmin");
176 dmaapProducerConfigData.addProperty("dmaapUserPassword", "dradmin");
177 dmaapProducerConfigData.addProperty("dmaapContentType", "application/octet-stream");
180 JsonObject dmaapConfigs = new JsonObject();
181 dmaapConfigs.add("dmaapConsumerConfiguration", dmaapConsumerConfigData);
182 dmaapConfigs.add("dmaapProducerConfiguration", dmaapProducerConfigData);
184 JsonObject ftpesConfigData = new JsonObject();
185 ftpesConfigData.addProperty("keyCert", "config/dfc.jks");
186 ftpesConfigData.addProperty("keyPassword", "secret");
187 ftpesConfigData.addProperty("trustedCa", "config/ftp.jks");
188 ftpesConfigData.addProperty("trustedCaPassword", "secret");
190 JsonObject security = new JsonObject();
191 security.addProperty("trustStorePath", "trustStorePath");
192 security.addProperty("trustStorePasswordPath", "trustStorePasswordPath");
193 security.addProperty("keyStorePath", "keyStorePath");
194 security.addProperty("keyStorePasswordPath", "keyStorePasswordPath");
195 security.addProperty("enableDmaapCertAuth", "enableDmaapCertAuth");
197 JsonObject ftpesConfiguration = new JsonObject();
198 ftpesConfiguration.add("ftpesConfiguration", ftpesConfigData);
200 JsonObject configs = new JsonObject();
201 configs.add("dmaap", dmaapConfigs);
202 configs.add("ftp", ftpesConfiguration);
203 configs.add("security", security);
205 JsonObject completeJson = new JsonObject();
206 completeJson.add("configs", configs);
208 return completeJson.toString();