acd0c0bd4bfc30937343f58b5c60ff5547045870
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 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.tasks;
18
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.verifyNoMoreInteractions;
24 import static org.mockito.Mockito.when;
25
26 import java.io.File;
27
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
31 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
32 import org.onap.dcaegen2.collectors.datafile.ftp.ErrorData;
33 import org.onap.dcaegen2.collectors.datafile.ftp.FileCollectResult;
34 import org.onap.dcaegen2.collectors.datafile.ftp.FileServerData;
35 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
36 import org.onap.dcaegen2.collectors.datafile.ftp.ImmutableFileServerData;
37 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
38 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
39 import org.onap.dcaegen2.collectors.datafile.model.FileData;
40 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
41 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
42
43 import reactor.test.StepVerifier;
44
45 /**
46  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
47  *
48  */
49 public class XnfCollectorTaskImplTest {
50     private static final String PRODUCT_NAME = "NrRadio";
51     private static final String VENDOR_NAME = "Ericsson";
52     private static final String LAST_EPOCH_MICROSEC = "8745745764578";
53     private static final String SOURCE_NAME = "oteNB5309";
54     private static final String START_EPOCH_MICROSEC = "8745745764578";
55     private static final String TIME_ZONE_OFFSET = "UTC+05:00";
56     private static final String PM_MEAS_CHANGE_IDINTIFIER = "PM_MEAS_FILES";
57     private static final String FILE_READY_CHANGE_TYPE = "FileReady";
58     private static final String FTPES_SCHEME = "ftpes://";
59     private static final String SFTP_SCHEME = "sftp://";
60     private static final String SERVER_ADDRESS = "192.168.0.101";
61     private static final int PORT_22 = 22;
62     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
63     private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
64     private static final String LOCAL_FILE_LOCATION = "target" + File.separator + PM_FILE_NAME;
65     private static final String USER = "usr";
66     private static final String PWD = "pwd";
67     private static final String FTPES_LOCATION =
68             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
69     private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
70     private static final String GZIP_COMPRESSION = "gzip";
71     private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
72     private static final String FILE_FORMAT_VERSION = "V10";
73
74     private static final String FTP_KEY_PATH = "ftpKeyPath";
75     private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
76     private static final String TRUSTED_CA_PATH = "trustedCAPath";
77     private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
78
79     private static AppConfig appConfigMock = mock(AppConfig.class);
80     private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
81
82     private FtpsClient ftpsClientMock = mock(FtpsClient.class);
83
84     private SftpClient sftpClientMock = mock(SftpClient.class);
85     private RetryTimer retryTimerMock = mock(RetryTimer.class);
86
87
88     @BeforeAll
89     public static void setUpConfiguration() {
90         when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
91         when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
92         when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
93         when(ftpesConfigMock.trustedCA()).thenReturn(TRUSTED_CA_PATH);
94         when(ftpesConfigMock.trustedCAPassword()).thenReturn(TRUSTED_CA_PASSWORD);
95     }
96
97     @Test
98     public void whenFtpesFile_returnCorrectResponse() {
99         XnfCollectorTaskImpl collectorUndetTest =
100                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
101
102         // @formatter:off
103         FileData fileData = ImmutableFileData.builder()
104                 .productName(PRODUCT_NAME)
105                 .vendorName(VENDOR_NAME)
106                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
107                 .sourceName(SOURCE_NAME)
108                 .startEpochMicrosec(START_EPOCH_MICROSEC)
109                 .timeZoneOffset(TIME_ZONE_OFFSET)
110                 .changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
111                 .changeType(FILE_READY_CHANGE_TYPE)
112                 .name(PM_FILE_NAME)
113                 .location(FTPES_LOCATION)
114                 .compression(GZIP_COMPRESSION)
115                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
116                 .fileFormatVersion(FILE_FORMAT_VERSION)
117                 .build();
118
119         FileServerData fileServerData = ImmutableFileServerData.builder()
120                 .serverAddress(SERVER_ADDRESS)
121                 .userId(USER)
122                 .password(PWD)
123                 .port(PORT_22)
124                 .build();
125         // @formatter:on
126         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
127                 .thenReturn(new FileCollectResult());
128
129         // @formatter:off
130         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
131                 .productName(PRODUCT_NAME)
132                 .vendorName(VENDOR_NAME)
133                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
134                 .sourceName(SOURCE_NAME)
135                 .startEpochMicrosec(START_EPOCH_MICROSEC)
136                 .timeZoneOffset(TIME_ZONE_OFFSET)
137                 .name(PM_FILE_NAME)
138                 .location(FTPES_LOCATION)
139                 .internalLocation(LOCAL_FILE_LOCATION)
140                 .compression(GZIP_COMPRESSION)
141                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
142                 .fileFormatVersion(FILE_FORMAT_VERSION)
143                 .build();
144         // @formatter:on
145
146         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
147                 .verifyComplete();
148
149         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
150         verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH);
151         verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD);
152         verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH);
153         verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD);
154         verifyNoMoreInteractions(ftpsClientMock);
155     }
156
157     @Test
158     public void whenSftpFile_returnCorrectResponse() {
159         XnfCollectorTaskImpl collectorUndetTest =
160                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
161         // @formatter:off
162         FileData fileData = ImmutableFileData.builder()
163                 .productName(PRODUCT_NAME)
164                 .vendorName(VENDOR_NAME)
165                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
166                 .sourceName(SOURCE_NAME)
167                 .startEpochMicrosec(START_EPOCH_MICROSEC)
168                 .timeZoneOffset(TIME_ZONE_OFFSET)
169                 .changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
170                 .changeType(FILE_READY_CHANGE_TYPE)
171                 .name(PM_FILE_NAME)
172                 .location(SFTP_LOCATION)
173                 .compression(GZIP_COMPRESSION)
174                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
175                 .fileFormatVersion(FILE_FORMAT_VERSION)
176                 .build();
177
178
179         FileServerData fileServerData = ImmutableFileServerData.builder()
180                 .serverAddress(SERVER_ADDRESS)
181                 .userId("")
182                 .password("")
183                 .port(PORT_22)
184                 .build();
185         // @formatter:on
186         when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
187                 .thenReturn(new FileCollectResult());
188         // @formatter:off
189         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
190                 .productName(PRODUCT_NAME)
191                 .vendorName(VENDOR_NAME)
192                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
193                 .sourceName(SOURCE_NAME)
194                 .startEpochMicrosec(START_EPOCH_MICROSEC)
195                 .timeZoneOffset(TIME_ZONE_OFFSET)
196                 .name(PM_FILE_NAME)
197                 .location(SFTP_LOCATION)
198                 .internalLocation(LOCAL_FILE_LOCATION)
199                 .compression(GZIP_COMPRESSION)
200                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
201                 .fileFormatVersion(FILE_FORMAT_VERSION)
202                 .build();
203         // @formatter:on
204         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
205                 .verifyComplete();
206
207         verify(sftpClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
208         verifyNoMoreInteractions(sftpClientMock);
209     }
210
211     @Test
212     public void whenFtpesFileAlwaysFail_retryAndReturnEmpty() {
213         XnfCollectorTaskImpl collectorUndetTest =
214                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
215         collectorUndetTest.setRetryTimer(retryTimerMock);
216         // @formatter:off
217         FileData fileData = ImmutableFileData.builder()
218                 .productName(PRODUCT_NAME)
219                 .vendorName(VENDOR_NAME)
220                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
221                 .sourceName(SOURCE_NAME)
222                 .startEpochMicrosec(START_EPOCH_MICROSEC)
223                 .timeZoneOffset(TIME_ZONE_OFFSET)
224                 .changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
225                 .changeType(FILE_READY_CHANGE_TYPE)
226                 .name(PM_FILE_NAME)
227                 .location(FTPES_LOCATION)
228                 .compression(GZIP_COMPRESSION)
229                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
230                 .fileFormatVersion(FILE_FORMAT_VERSION)
231                 .build();
232
233         FileServerData fileServerData = ImmutableFileServerData.builder()
234                 .serverAddress(SERVER_ADDRESS)
235                 .userId(USER)
236                 .password(PWD)
237                 .port(PORT_22)
238                 .build();
239         // @formatter:on
240         ErrorData errorData = new ErrorData();
241         errorData.addError("Unable to collect file.", new Exception());
242         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
243                 .thenReturn(new FileCollectResult(errorData));
244         doReturn(new FileCollectResult(errorData)).when(ftpsClientMock).retryCollectFile();
245
246         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
247
248         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
249         verify(ftpsClientMock, times(2)).retryCollectFile();
250     }
251
252     @Test
253     public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() {
254         XnfCollectorTaskImpl collectorUndetTest =
255                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
256         collectorUndetTest.setRetryTimer(retryTimerMock);
257         // @formatter:off
258         FileData fileData = ImmutableFileData.builder()
259                 .productName(PRODUCT_NAME)
260                 .vendorName(VENDOR_NAME)
261                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
262                 .sourceName(SOURCE_NAME)
263                 .startEpochMicrosec(START_EPOCH_MICROSEC)
264                 .timeZoneOffset(TIME_ZONE_OFFSET)
265                 .changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
266                 .changeType(FILE_READY_CHANGE_TYPE)
267                 .name(PM_FILE_NAME)
268                 .location(FTPES_LOCATION)
269                 .compression(GZIP_COMPRESSION)
270                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
271                 .fileFormatVersion(FILE_FORMAT_VERSION)
272                 .build();
273
274         FileServerData fileServerData = ImmutableFileServerData.builder()
275                 .serverAddress(SERVER_ADDRESS)
276                 .userId(USER)
277                 .password(PWD)
278                 .port(PORT_22)
279                 .build();
280         // @formatter:on
281         ErrorData errorData = new ErrorData();
282         errorData.addError("Unable to collect file.", new Exception());
283         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
284                 .thenReturn(new FileCollectResult(errorData));
285         doReturn(new FileCollectResult()).when(ftpsClientMock).retryCollectFile();
286         // @formatter:off
287         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
288                 .productName(PRODUCT_NAME)
289                 .vendorName(VENDOR_NAME)
290                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
291                 .sourceName(SOURCE_NAME)
292                 .startEpochMicrosec(START_EPOCH_MICROSEC)
293                 .timeZoneOffset(TIME_ZONE_OFFSET)
294                 .name(PM_FILE_NAME)
295                 .location(FTPES_LOCATION)
296                 .internalLocation(LOCAL_FILE_LOCATION)
297                 .compression(GZIP_COMPRESSION)
298                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
299                 .fileFormatVersion(FILE_FORMAT_VERSION)
300                 .build();
301         // @formatter:on
302         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
303                 .verifyComplete();
304
305
306         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
307         verify(ftpsClientMock, times(1)).retryCollectFile();
308     }
309
310     @Test
311     public void whenWrongScheme_returnEmpty() {
312         XnfCollectorTaskImpl collectorUndetTest =
313                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
314         // @formatter:off
315         FileData fileData = ImmutableFileData.builder()
316                 .productName(PRODUCT_NAME)
317                 .vendorName(VENDOR_NAME)
318                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
319                 .sourceName(SOURCE_NAME)
320                 .startEpochMicrosec(START_EPOCH_MICROSEC)
321                 .timeZoneOffset(TIME_ZONE_OFFSET)
322                 .changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
323                 .changeType(FILE_READY_CHANGE_TYPE)
324                 .name(PM_FILE_NAME)
325                 .location("http://host.com/file.zip")
326                 .compression(GZIP_COMPRESSION)
327                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
328                 .fileFormatVersion(FILE_FORMAT_VERSION)
329                 .build();
330
331         FileServerData fileServerData = ImmutableFileServerData.builder()
332                 .serverAddress(SERVER_ADDRESS)
333                 .userId("")
334                 .password("")
335                 .port(PORT_22)
336                 .build();
337         // @formatter:on
338         when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
339                 .thenReturn(new FileCollectResult());
340
341         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
342
343         verifyNoMoreInteractions(sftpClientMock);
344     }
345 }