5be75ab3b4b7c2eed76252e026f392bd6a4787f0
[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 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
39 /**
40  * Tests the AppConfig.
41  *
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>
44  */
45 class AppConfigTest {
46
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;
50
51     private static AppConfig appConfigUnderTest;
52
53     private static String filePath =
54             Objects.requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile();
55
56     @BeforeEach
57     public void setUp() {
58         appConfigUnderTest = spy(AppConfig.class);
59     }
60
61     @Test
62     public void whenApplicationWasStarted_FilePathIsSet() {
63         // When
64         appConfigUnderTest.setFilepath(filePath);
65
66         // Then
67         verify(appConfigUnderTest, times(1)).setFilepath(anyString());
68         verify(appConfigUnderTest, times(0)).loadConfigurationFromFile();
69         Assertions.assertEquals(filePath, appConfigUnderTest.getFilepath());
70     }
71
72     @Test
73     public void whenTheConfigurationFits_GetFtpsAndDmaapObjectRepresentationConfiguration() throws IOException {
74         // Given
75         InputStream inputStream =
76                 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
77
78         // When
79         appConfigUnderTest.setFilepath(filePath);
80         doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
81         appConfigUnderTest.loadConfigurationFromFile();
82
83         // Then
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());
93     }
94
95     @Test
96     public void whenFileIsNotExist_ThrowIoException() {
97         // Given
98         filePath = "/temp.json";
99         appConfigUnderTest.setFilepath(filePath);
100
101         // When
102         appConfigUnderTest.loadConfigurationFromFile();
103
104         // Then
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());
110
111     }
112
113     @Test
114     public void whenFileIsExistsButJsonIsIncorrect() throws IOException {
115         // Given
116         InputStream inputStream =
117                 new ByteArrayInputStream((getJsonConfig(INCORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
118
119         // When
120         appConfigUnderTest.setFilepath(filePath);
121         doReturn(inputStream).when(appConfigUnderTest).createInputStream(any());
122         appConfigUnderTest.loadConfigurationFromFile();
123
124         // Then
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());
130
131     }
132
133
134     @Test
135     public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() throws IOException {
136         // Given
137         InputStream inputStream =
138                 new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8)));
139         // When
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();
146
147         // Then
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());
153     }
154
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);
168
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");
174         if (correct) {
175             dmaapProducerConfigData.addProperty("dmaapUserName", "dradmin");
176             dmaapProducerConfigData.addProperty("dmaapUserPassword", "dradmin");
177             dmaapProducerConfigData.addProperty("dmaapContentType", "application/octet-stream");
178         }
179
180         JsonObject dmaapConfigs = new JsonObject();
181         dmaapConfigs.add("dmaapConsumerConfiguration", dmaapConsumerConfigData);
182         dmaapConfigs.add("dmaapProducerConfiguration", dmaapProducerConfigData);
183
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");
189
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");
196
197         JsonObject ftpesConfiguration = new JsonObject();
198         ftpesConfiguration.add("ftpesConfiguration", ftpesConfigData);
199
200         JsonObject configs = new JsonObject();
201         configs.add("dmaap", dmaapConfigs);
202         configs.add("ftp", ftpesConfiguration);
203         configs.add("security", security);
204
205         JsonObject completeJson = new JsonObject();
206         completeJson.add("configs", configs);
207
208         return completeJson.toString();
209     }
210 }