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;
57 private static String filePath =
58 Objects.requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile();
62 appConfigUnderTest = spy(AppConfig.class);
66 public void whenApplicationWasStarted_FilePathIsSet() {
68 appConfigUnderTest.setFilepath(filePath);
71 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
72 verify(appConfigUnderTest, times(0)).loadConfigurationFromFile();
73 Assertions.assertEquals(filePath, appConfigUnderTest.getFilepath());
77 public void whenTheConfigurationFits_GetFtpsAndDmaapObjectRepresentationConfiguration() throws IOException {
79 InputStream inputStream =
80 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
83 appConfigUnderTest.setFilepath(filePath);
84 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
85 appConfigUnderTest.loadConfigurationFromFile();
88 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
89 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
90 Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration());
91 Assertions.assertNotNull(appConfigUnderTest.getDmaapPublisherConfiguration());
92 Assertions.assertEquals(appConfigUnderTest.getDmaapPublisherConfiguration(),
93 appConfigUnderTest.getDmaapPublisherConfiguration());
94 Assertions.assertEquals(appConfigUnderTest.getDmaapConsumerConfiguration(),
95 appConfigUnderTest.getDmaapConsumerConfiguration());
96 Assertions.assertEquals(appConfigUnderTest.getFtpesConfiguration(), appConfigUnderTest.getFtpesConfiguration());
100 public void whenFileIsNotExist_ThrowIoException() {
102 filePath = "/temp.json";
103 appConfigUnderTest.setFilepath(filePath);
106 appConfigUnderTest.loadConfigurationFromFile();
109 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
110 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
111 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
112 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
113 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
118 public void whenFileIsExistsButJsonIsIncorrect() throws IOException {
120 InputStream inputStream =
121 new ByteArrayInputStream((getJsonConfig(INCORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
124 appConfigUnderTest.setFilepath(filePath);
125 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
126 appConfigUnderTest.loadConfigurationFromFile();
129 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
130 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
131 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
132 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
133 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
139 public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException {
141 InputStream inputStream =
142 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
144 appConfigUnderTest.setFilepath(filePath);
145 doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
146 JsonElement jsonElement = mock(JsonElement.class);
147 when(jsonElement.isJsonObject()).thenReturn(false);
148 doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(JsonParser.class), any(InputStream.class));
149 appConfigUnderTest.loadConfigurationFromFile();
152 verify(appConfigUnderTest, times(1)).setFilepath(anyString());
153 verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
154 Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration());
155 Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration());
156 Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration());
159 private String getJsonConfig(boolean correct) {
160 JsonObject dmaapConsumerConfigData = new JsonObject();
161 dmaapConsumerConfigData.addProperty("dmaapHostName", "localhost");
162 dmaapConsumerConfigData.addProperty("dmaapPortNumber", 2222);
163 dmaapConsumerConfigData.addProperty("dmaapTopicName", "/events/unauthenticated.VES_NOTIFICATION_OUTPUT");
164 dmaapConsumerConfigData.addProperty("dmaapProtocol", "http");
165 dmaapConsumerConfigData.addProperty("dmaapUserName", "admin");
166 dmaapConsumerConfigData.addProperty("dmaapUserPassword", "admin");
167 dmaapConsumerConfigData.addProperty("dmaapContentType", "application/json");
168 dmaapConsumerConfigData.addProperty("consumerId", "C12");
169 dmaapConsumerConfigData.addProperty("consumerGroup", "OpenDcae-c12");
170 dmaapConsumerConfigData.addProperty("timeoutMs", -1);
171 dmaapConsumerConfigData.addProperty("messageLimit", 1);
173 JsonObject dmaapProducerConfigData = new JsonObject();
174 dmaapProducerConfigData.addProperty("dmaapHostName", "localhost");
175 dmaapProducerConfigData.addProperty("dmaapPortNumber", 3907);
176 dmaapProducerConfigData.addProperty("dmaapTopicName", "publish");
177 dmaapProducerConfigData.addProperty("dmaapProtocol", "https");
179 dmaapProducerConfigData.addProperty("dmaapUserName", "dradmin");
180 dmaapProducerConfigData.addProperty("dmaapUserPassword", "dradmin");
181 dmaapProducerConfigData.addProperty("dmaapContentType", "application/octet-stream");
184 JsonObject dmaapConfigs = new JsonObject();
185 dmaapConfigs.add("dmaapConsumerConfiguration", dmaapConsumerConfigData);
186 dmaapConfigs.add("dmaapProducerConfiguration", dmaapProducerConfigData);
188 JsonObject ftpesConfigData = new JsonObject();
189 ftpesConfigData.addProperty("keyCert", "config/ftpKey.jks");
190 ftpesConfigData.addProperty("keyPassword", "secret");
191 ftpesConfigData.addProperty("trustedCA", "config/cacerts");
192 ftpesConfigData.addProperty("trustedCAPassword", "secret");
194 JsonObject security = new JsonObject();
195 security.addProperty("trustStorePath", "trustStorePath");
196 security.addProperty("trustStorePasswordPath", "trustStorePasswordPath");
197 security.addProperty("keyStorePath", "keyStorePath");
198 security.addProperty("keyStorePasswordPath", "keyStorePasswordPath");
199 security.addProperty("enableDmaapCertAuth", "enableDmaapCertAuth");
201 JsonObject ftpesConfiguration = new JsonObject();
202 ftpesConfiguration.add("ftpesConfiguration", ftpesConfigData);
204 JsonObject configs = new JsonObject();
205 configs.add("dmaap", dmaapConfigs);
206 configs.add("ftp", ftpesConfiguration);
207 configs.add("security", security);
209 JsonObject completeJson = new JsonObject();
210 completeJson.add("configs", configs);
212 return completeJson.toString();