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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.tasks;
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;
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.FileMetaData;
41 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
42 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
43 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileMetaData;
45 import reactor.test.StepVerifier;
48 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
51 public class XnfCollectorTaskImplTest {
52 private static final String PRODUCT_NAME = "NrRadio";
53 private static final String VENDOR_NAME = "Ericsson";
54 private static final String LAST_EPOCH_MICROSEC = "8745745764578";
55 private static final String SOURCE_NAME = "oteNB5309";
56 private static final String START_EPOCH_MICROSEC = "8745745764578";
57 private static final String TIME_ZONE_OFFSET = "UTC+05:00";
58 private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
59 private static final String FILE_READY_CHANGE_TYPE = "FileReady";
60 private static final String FTPES_SCHEME = "ftpes://";
61 private static final String SFTP_SCHEME = "sftp://";
62 private static final String SERVER_ADDRESS = "192.168.0.101";
63 private static final int PORT_22 = 22;
64 private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
65 private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
66 private static final String LOCAL_FILE_LOCATION = "target" + File.separator + PM_FILE_NAME;
67 private static final String USER = "usr";
68 private static final String PWD = "pwd";
69 private static final String FTPES_LOCATION =
70 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
71 private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
72 private static final String GZIP_COMPRESSION = "gzip";
73 private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
74 private static final String FILE_FORMAT_VERSION = "V10";
76 private static final String FTP_KEY_PATH = "ftpKeyPath";
77 private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
78 private static final String TRUSTED_CA_PATH = "trustedCAPath";
79 private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
81 private static AppConfig appConfigMock = mock(AppConfig.class);
82 private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
84 private FtpsClient ftpsClientMock = mock(FtpsClient.class);
86 private SftpClient sftpClientMock = mock(SftpClient.class);
87 private RetryTimer retryTimerMock = mock(RetryTimer.class);
89 private FileMetaData fileMetaData = ImmutableFileMetaData.builder()
90 .productName(PRODUCT_NAME)
91 .vendorName(VENDOR_NAME)
92 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
93 .sourceName(SOURCE_NAME)
94 .startEpochMicrosec(START_EPOCH_MICROSEC)
95 .timeZoneOffset(TIME_ZONE_OFFSET)
96 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER)
97 .changeType(FILE_READY_CHANGE_TYPE)
102 public static void setUpConfiguration() {
103 when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
104 when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
105 when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
106 when(ftpesConfigMock.trustedCA()).thenReturn(TRUSTED_CA_PATH);
107 when(ftpesConfigMock.trustedCAPassword()).thenReturn(TRUSTED_CA_PASSWORD);
111 public void whenFtpesFile_returnCorrectResponse() {
112 XnfCollectorTaskImpl collectorUndetTest =
113 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
116 FileData fileData = ImmutableFileData.builder()
117 .fileMetaData(fileMetaData)
119 .location(FTPES_LOCATION)
120 .compression(GZIP_COMPRESSION)
121 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
122 .fileFormatVersion(FILE_FORMAT_VERSION)
125 FileServerData fileServerData = ImmutableFileServerData.builder()
126 .serverAddress(SERVER_ADDRESS)
132 when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
133 .thenReturn(new FileCollectResult());
136 ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
137 .productName(PRODUCT_NAME)
138 .vendorName(VENDOR_NAME)
139 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
140 .sourceName(SOURCE_NAME)
141 .startEpochMicrosec(START_EPOCH_MICROSEC)
142 .timeZoneOffset(TIME_ZONE_OFFSET)
144 .location(FTPES_LOCATION)
145 .internalLocation(LOCAL_FILE_LOCATION)
146 .compression(GZIP_COMPRESSION)
147 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
148 .fileFormatVersion(FILE_FORMAT_VERSION)
152 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
155 verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
156 verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH);
157 verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD);
158 verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH);
159 verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD);
160 verifyNoMoreInteractions(ftpsClientMock);
164 public void whenSftpFile_returnCorrectResponse() {
165 XnfCollectorTaskImpl collectorUndetTest =
166 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
168 FileData fileData = ImmutableFileData.builder()
169 .fileMetaData(fileMetaData)
171 .location(SFTP_LOCATION)
172 .compression(GZIP_COMPRESSION)
173 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
174 .fileFormatVersion(FILE_FORMAT_VERSION)
178 FileServerData fileServerData = ImmutableFileServerData.builder()
179 .serverAddress(SERVER_ADDRESS)
185 when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
186 .thenReturn(new FileCollectResult());
188 ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
189 .productName(PRODUCT_NAME)
190 .vendorName(VENDOR_NAME)
191 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
192 .sourceName(SOURCE_NAME)
193 .startEpochMicrosec(START_EPOCH_MICROSEC)
194 .timeZoneOffset(TIME_ZONE_OFFSET)
196 .location(SFTP_LOCATION)
197 .internalLocation(LOCAL_FILE_LOCATION)
198 .compression(GZIP_COMPRESSION)
199 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
200 .fileFormatVersion(FILE_FORMAT_VERSION)
203 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
206 verify(sftpClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
207 verifyNoMoreInteractions(sftpClientMock);
211 public void whenFtpesFileAlwaysFail_retryAndReturnEmpty() {
212 XnfCollectorTaskImpl collectorUndetTest =
213 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
214 collectorUndetTest.setRetryTimer(retryTimerMock);
216 FileData fileData = ImmutableFileData.builder()
217 .fileMetaData(fileMetaData)
219 .location(FTPES_LOCATION)
220 .compression(GZIP_COMPRESSION)
221 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
222 .fileFormatVersion(FILE_FORMAT_VERSION)
225 FileServerData fileServerData = ImmutableFileServerData.builder()
226 .serverAddress(SERVER_ADDRESS)
232 ErrorData errorData = new ErrorData();
233 errorData.addError("Unable to collect file.", new Exception());
234 when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
235 .thenReturn(new FileCollectResult(errorData));
236 doReturn(new FileCollectResult(errorData)).when(ftpsClientMock).retryCollectFile();
238 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
240 verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
241 verify(ftpsClientMock, times(2)).retryCollectFile();
245 public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() {
246 XnfCollectorTaskImpl collectorUndetTest =
247 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
248 collectorUndetTest.setRetryTimer(retryTimerMock);
250 FileData fileData = ImmutableFileData.builder()
251 .fileMetaData(fileMetaData)
253 .location(FTPES_LOCATION)
254 .compression(GZIP_COMPRESSION)
255 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
256 .fileFormatVersion(FILE_FORMAT_VERSION)
259 FileServerData fileServerData = ImmutableFileServerData.builder()
260 .serverAddress(SERVER_ADDRESS)
266 ErrorData errorData = new ErrorData();
267 errorData.addError("Unable to collect file.", new Exception());
268 when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
269 .thenReturn(new FileCollectResult(errorData));
270 doReturn(new FileCollectResult()).when(ftpsClientMock).retryCollectFile();
272 ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
273 .productName(PRODUCT_NAME)
274 .vendorName(VENDOR_NAME)
275 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
276 .sourceName(SOURCE_NAME)
277 .startEpochMicrosec(START_EPOCH_MICROSEC)
278 .timeZoneOffset(TIME_ZONE_OFFSET)
280 .location(FTPES_LOCATION)
281 .internalLocation(LOCAL_FILE_LOCATION)
282 .compression(GZIP_COMPRESSION)
283 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
284 .fileFormatVersion(FILE_FORMAT_VERSION)
287 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
291 verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
292 verify(ftpsClientMock, times(1)).retryCollectFile();
296 public void whenWrongScheme_returnEmpty() {
297 XnfCollectorTaskImpl collectorUndetTest =
298 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
300 FileData fileData = ImmutableFileData.builder()
301 .fileMetaData(fileMetaData)
303 .location("http://host.com/file.zip")
304 .compression(GZIP_COMPRESSION)
305 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
306 .fileFormatVersion(FILE_FORMAT_VERSION)
309 FileServerData fileServerData = ImmutableFileServerData.builder()
310 .serverAddress(SERVER_ADDRESS)
316 when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
317 .thenReturn(new FileCollectResult());
319 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
321 verifyNoMoreInteractions(sftpClientMock);