443ddae7bb74e62c9cd4c80968ebf5629663a19d
[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
58     private static String filePath = Objects
59             .requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile();
60
61     @BeforeEach
62     public void setUp() {
63         appConfigUnderTest = spy(AppConfig.class);
64     }
65
66     @Test
67     public void whenApplicationWasStarted_FilePathIsSet() {
68         // When
69         appConfigUnderTest.setFilepath(filePath);
70
71         // Then
72         verify(appConfigUnderTest, times(1)).setFilepath(anyString());
73         verify(appConfigUnderTest, times(0)).initFileStreamReader();
74         Assertions.assertEquals(filePath, appConfigUnderTest.getFilepath());
75     }
76
77     @Test
78     public void whenTheConfigurationFits_GetFtpsAndDmaapObjectRepresentationConfiguration() throws IOException {
79         // Given
80         InputStream inputStream =
81                 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
82
83         // When
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();
90
91         // Then
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());
101
102     }
103
104     @Test
105     public void whenFileIsNotExist_ThrowIoException() {
106         // Given
107         filePath = "/temp.json";
108         appConfigUnderTest.setFilepath(filePath);
109
110         // When
111         appConfigUnderTest.initFileStreamReader();
112
113         // Then
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());
119
120     }
121
122     @Test
123     public void whenFileIsExistsButJsonIsIncorrect() throws IOException {
124         // Given
125         InputStream inputStream =
126                 new ByteArrayInputStream((getJsonConfig(INCORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
127
128         // When
129         appConfigUnderTest.setFilepath(filePath);
130         doReturn(inputStream).when(appConfigUnderTest).getInputStream(any());
131         appConfigUnderTest.initFileStreamReader();
132
133         // Then
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());
139
140     }
141
142
143     @Test
144     public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException {
145         // Given
146         InputStream inputStream =
147                 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
148         // When
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();
158
159         // Then
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());
165     }
166
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);
180
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");
186         if (correct) {
187             dmaapProducerConfigData.addProperty("dmaapUserName", "dradmin");
188             dmaapProducerConfigData.addProperty("dmaapUserPassword", "dradmin");
189             dmaapProducerConfigData.addProperty("dmaapContentType", "application/octet-stream");
190         }
191
192         JsonObject dmaapConfigs = new JsonObject();
193         dmaapConfigs.add("dmaapConsumerConfiguration", dmaapConsumerConfigData);
194         dmaapConfigs.add("dmaapProducerConfiguration", dmaapProducerConfigData);
195
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");
201
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");
208
209         JsonObject ftpesConfiguration = new JsonObject();
210         ftpesConfiguration.add("ftpesConfiguration", ftpesConfigData);
211
212         JsonObject configs = new JsonObject();
213         configs.add("dmaap", dmaapConfigs);
214         configs.add("ftp", ftpesConfiguration);
215         configs.add("security", security);
216
217         JsonObject completeJson = new JsonObject();
218         completeJson.add("configs", configs);
219
220         return completeJson.toString();
221     }
222 }