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