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;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.onap.dcaegen2.collectors.datafile.integration.junit5.mockito.MockitoExtension;
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>
45 @ExtendWith({MockitoExtension.class})
48 private static final String DATAFILE_ENDPOINTS = "datafile_endpoints.json";
49 private static final boolean CORRECT_JSON = true;
50 private static final boolean INCORRECT_JSON = false;
52 private static AppConfig appConfigUnderTest;
54 private static String filePath =
55 Objects.requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile();
59 appConfigUnderTest = spy(AppConfig.class);
63 public void whenApplicationWasStarted_FilePathIsSet() {
65 appConfigUnderTest.setFilepath(filePath);
68 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
69 verify(appConfigUnderTest, times(0)).loadConfigurationFromFile();
70 Assertions.assertEquals(filePath, appConfigUnderTest.getFilepath());
74 public void whenTheConfigurationFits_GetFtpsAndDmaapObjectRepresentationConfiguration() throws IOException {
76 InputStream inputStream =
77 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
80 appConfigUnderTest.setFilepath(filePath);
81 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
82 appConfigUnderTest.loadConfigurationFromFile();
85 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
86 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
87 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
88 Assertions.assertNotNull(appConfigUnderTest.getDmaapPublisherConfiguration());
89 Assertions.assertEquals(appConfigUnderTest.getDmaapPublisherConfiguration(),
90 appConfigUnderTest.getDmaapPublisherConfiguration());
91 Assertions.assertEquals(appConfigUnderTest.getDmaapConsumerConfiguration(),
92 appConfigUnderTest.getDmaapConsumerConfiguration());
93 Assertions.assertEquals(appConfigUnderTest.getFtpesConfiguration(), appConfigUnderTest.getFtpesConfiguration());
97 public void whenFileIsNotExist_ThrowIoException() {
99 filePath = "/temp.json";
100 appConfigUnderTest.setFilepath(filePath);
103 appConfigUnderTest.loadConfigurationFromFile();
106 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
107 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
108 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
109 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
110 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
115 public void whenFileIsExistsButJsonIsIncorrect() throws IOException {
117 InputStream inputStream =
118 new ByteArrayInputStream((getJsonConfig(INCORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
121 appConfigUnderTest.setFilepath(filePath);
122 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
123 appConfigUnderTest.loadConfigurationFromFile();
126 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
127 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
128 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
129 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
130 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
136 public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException {
138 InputStream inputStream =
139 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
141 appConfigUnderTest.setFilepath(filePath);
142 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
143 JsonElement jsonElement = mock(JsonElement.class);
144 when(jsonElement.isJsonObject()).thenReturn(false);
145 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(JsonParser.class), any(InputStream.class));
146 appConfigUnderTest.loadConfigurationFromFile();
149 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
150 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
151 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
152 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
153 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
156 private String getJsonConfig(boolean correct) {
157 JsonObject dmaapConsumerConfigData = new JsonObject();
158 dmaapConsumerConfigData.addProperty("dmaapHostName", "localhost");
159 dmaapConsumerConfigData.addProperty("dmaapPortNumber", 2222);
160 dmaapConsumerConfigData.addProperty("dmaapTopicName", "/events/unauthenticated.VES_NOTIFICATION_OUTPUT");
161 dmaapConsumerConfigData.addProperty("dmaapProtocol", "http");
162 dmaapConsumerConfigData.addProperty("dmaapUserName", "admin");
163 dmaapConsumerConfigData.addProperty("dmaapUserPassword", "admin");
164 dmaapConsumerConfigData.addProperty("dmaapContentType", "application/json");
165 dmaapConsumerConfigData.addProperty("consumerId", "C12");
166 dmaapConsumerConfigData.addProperty("consumerGroup", "OpenDcae-c12");
167 dmaapConsumerConfigData.addProperty("timeoutMs", -1);
168 dmaapConsumerConfigData.addProperty("messageLimit", 1);
170 JsonObject dmaapProducerConfigData = new JsonObject();
171 dmaapProducerConfigData.addProperty("dmaapHostName", "localhost");
172 dmaapProducerConfigData.addProperty("dmaapPortNumber", 3907);
173 dmaapProducerConfigData.addProperty("dmaapTopicName", "publish");
174 dmaapProducerConfigData.addProperty("dmaapProtocol", "https");
176 dmaapProducerConfigData.addProperty("dmaapUserName", "dradmin");
177 dmaapProducerConfigData.addProperty("dmaapUserPassword", "dradmin");
178 dmaapProducerConfigData.addProperty("dmaapContentType", "application/octet-stream");
181 JsonObject dmaapConfigs = new JsonObject();
182 dmaapConfigs.add("dmaapConsumerConfiguration", dmaapConsumerConfigData);
183 dmaapConfigs.add("dmaapProducerConfiguration", dmaapProducerConfigData);
185 JsonObject ftpesConfigData = new JsonObject();
186 ftpesConfigData.addProperty("keyCert", "config/dfc.jks");
187 ftpesConfigData.addProperty("keyPassword", "secret");
188 ftpesConfigData.addProperty("trustedCA", "config/ftp.jks");
189 ftpesConfigData.addProperty("trustedCAPassword", "secret");
191 JsonObject security = new JsonObject();
192 security.addProperty("trustStorePath", "trustStorePath");
193 security.addProperty("trustStorePasswordPath", "trustStorePasswordPath");
194 security.addProperty("keyStorePath", "keyStorePath");
195 security.addProperty("keyStorePasswordPath", "keyStorePasswordPath");
196 security.addProperty("enableDmaapCertAuth", "enableDmaapCertAuth");
198 JsonObject ftpesConfiguration = new JsonObject();
199 ftpesConfiguration.add("ftpesConfiguration", ftpesConfigData);
201 JsonObject configs = new JsonObject();
202 configs.add("dmaap", dmaapConfigs);
203 configs.add("ftp", ftpesConfiguration);
204 configs.add("security", security);
206 JsonObject completeJson = new JsonObject();
207 completeJson.add("configs", configs);
209 return completeJson.toString();