2c136304de3b29cb1e354fa14c546a44c5fd4808
[dcaegen2/collectors/datafile.git] /
1 /*
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.configuration;
18
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
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonParser;
31
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;
37
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;
43
44 /**
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>
47  */
48 @ExtendWith({MockitoExtension.class})
49 class AppConfigTest {
50
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;
54
55     private static AppConfig appConfigUnderTest;
56
57     private static String filePath =
58             Objects.requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile();
59
60     @BeforeEach
61     public void setUp() {
62         appConfigUnderTest = spy(AppConfig.class);
63     }
64
65     @Test
66     public void whenApplicationWasStarted_FilePathIsSet() {
67         // When
68         appConfigUnderTest.setFilepath(filePath);
69
70         // Then
71         verify(appConfigUnderTest, times(1)).setFilepath(anyString());
72         verify(appConfigUnderTest, times(0)).loadConfigurationFromFile();
73         Assertions.assertEquals(filePath, appConfigUnderTest.getFilepath());
74     }
75
76     @Test
77     public void whenTheConfigurationFits_GetFtpsAndDmaapObjectRepresentationConfiguration() throws IOException {
78         // Given
79         InputStream inputStream =
80                 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
81
82         // When
83         appConfigUnderTest.setFilepath(filePath);
84         doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
85         appConfigUnderTest.loadConfigurationFromFile();
86
87         // Then
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());
97     }
98
99     @Test
100     public void whenFileIsNotExist_ThrowIoException() {
101         // Given
102         filePath = "/temp.json";
103         appConfigUnderTest.setFilepath(filePath);
104
105         // When
106         appConfigUnderTest.loadConfigurationFromFile();
107
108         // Then
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());
114
115     }
116
117     @Test
118     public void whenFileIsExistsButJsonIsIncorrect() throws IOException {
119         // Given
120         InputStream inputStream =
121                 new ByteArrayInputStream((getJsonConfig(INCORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
122
123         // When
124         appConfigUnderTest.setFilepath(filePath);
125         doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
126         appConfigUnderTest.loadConfigurationFromFile();
127
128         // Then
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());
134
135     }
136
137
138     @Test
139     public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException {
140         // Given
141         InputStream inputStream =
142                 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
143         // When
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();
150
151         // Then
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());
157     }
158
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);
172
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");
178         if (correct) {
179             dmaapProducerConfigData.addProperty("dmaapUserName", "dradmin");
180             dmaapProducerConfigData.addProperty("dmaapUserPassword", "dradmin");
181             dmaapProducerConfigData.addProperty("dmaapContentType", "application/octet-stream");
182         }
183
184         JsonObject dmaapConfigs = new JsonObject();
185         dmaapConfigs.add("dmaapConsumerConfiguration", dmaapConsumerConfigData);
186         dmaapConfigs.add("dmaapProducerConfiguration", dmaapProducerConfigData);
187
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");
193
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");
200
201         JsonObject ftpesConfiguration = new JsonObject();
202         ftpesConfiguration.add("ftpesConfiguration", ftpesConfigData);
203
204         JsonObject configs = new JsonObject();
205         configs.add("dmaap", dmaapConfigs);
206         configs.add("ftp", ftpesConfiguration);
207         configs.add("security", security);
208
209         JsonObject completeJson = new JsonObject();
210         completeJson.add("configs", configs);
211
212         return completeJson.toString();
213     }
214 }