2 * ============LICENSE_START======================================================================
3 * Copyright (C) 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
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.junit.Assert.assertEquals;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.doThrow;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.verifyNoMoreInteractions;
28 import static org.mockito.Mockito.when;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.time.Duration;
33 import java.util.HashMap;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
40 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
41 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
42 import org.onap.dcaegen2.collectors.datafile.exceptions.NonRetryableDatafileTaskException;
43 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
44 import org.onap.dcaegen2.collectors.datafile.ftp.Scheme;
45 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
46 import org.onap.dcaegen2.collectors.datafile.model.Counters;
47 import org.onap.dcaegen2.collectors.datafile.model.FileData;
48 import org.onap.dcaegen2.collectors.datafile.model.FilePublishInformation;
49 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
50 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFilePublishInformation;
51 import org.onap.dcaegen2.collectors.datafile.model.ImmutableMessageMetaData;
52 import org.onap.dcaegen2.collectors.datafile.model.MessageMetaData;
53 import reactor.test.StepVerifier;
55 public class FileCollectorTest {
56 private static final String PRODUCT_NAME = "NrRadio";
57 private static final String VENDOR_NAME = "Ericsson";
58 private static final String LAST_EPOCH_MICROSEC = "8745745764578";
59 private static final String SOURCE_NAME = "oteNB5309";
60 private static final String START_EPOCH_MICROSEC = "8745745764578";
61 private static final String TIME_ZONE_OFFSET = "UTC+05:00";
62 private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
63 private static final String FILE_READY_CHANGE_TYPE = "FileReady";
64 private static final String FTPES_SCHEME = "ftpes://";
65 private static final String SFTP_SCHEME = "sftp://";
66 private static final String SERVER_ADDRESS = "192.168.0.101";
67 private static final int PORT_22 = 22;
68 private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
69 private static final Path LOCAL_FILE_LOCATION = Paths.get(FileData.DATAFILE_TMPDIR, PM_FILE_NAME);
70 private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
71 private static final String USER = "usr";
72 private static final String PWD = "pwd";
73 private static final String FTPES_LOCATION =
74 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
76 private static final String FTPES_LOCATION_NO_PORT =
77 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
78 private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
79 private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
81 private static final String GZIP_COMPRESSION = "gzip";
82 private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
83 private static final String FILE_FORMAT_VERSION = "V10";
85 private static final String FTP_KEY_PATH = "ftpKeyPath";
86 private static final String FTP_KEY_PASSWORD_PATH = "ftpKeyPassword";
87 private static final String TRUSTED_CA_PATH = "trustedCAPath";
88 private static final String TRUSTED_CA_PASSWORD_PATH = "trustedCAPassword";
89 private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
91 private static AppConfig appConfigMock = mock(AppConfig.class);
92 private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
94 private FtpsClient ftpsClientMock = mock(FtpsClient.class);
96 private SftpClient sftpClientMock = mock(SftpClient.class);
97 private final Map<String, String> contextMap = new HashMap<>();
99 private Counters counters;
101 private MessageMetaData createMessageMetaData() {
102 return ImmutableMessageMetaData.builder() //
103 .productName(PRODUCT_NAME) //
104 .vendorName(VENDOR_NAME) //
105 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
106 .sourceName(SOURCE_NAME) //
107 .startEpochMicrosec(START_EPOCH_MICROSEC) //
108 .timeZoneOffset(TIME_ZONE_OFFSET) //
109 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER) //
110 .changeType(FILE_READY_CHANGE_TYPE) //
114 private FileData createFileData(String location, Scheme scheme) {
115 return ImmutableFileData.builder() //
116 .name(PM_FILE_NAME) //
117 .location(location) //
118 .compression(GZIP_COMPRESSION) //
119 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
120 .fileFormatVersion(FILE_FORMAT_VERSION) //
122 .messageMetaData(createMessageMetaData()) //
126 private FilePublishInformation createExpectedFilePublishInformation(String location) {
127 return ImmutableFilePublishInformation.builder() //
128 .productName(PRODUCT_NAME) //
129 .vendorName(VENDOR_NAME) //
130 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
131 .sourceName(SOURCE_NAME) //
132 .startEpochMicrosec(START_EPOCH_MICROSEC) //
133 .timeZoneOffset(TIME_ZONE_OFFSET) //
134 .name(PM_FILE_NAME) //
135 .location(location) //
136 .internalLocation(LOCAL_FILE_LOCATION) //
137 .compression(GZIP_COMPRESSION) //
138 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
139 .fileFormatVersion(FILE_FORMAT_VERSION) //
140 .context(new HashMap<String, String>()) //
141 .changeIdentifier(CHANGE_IDENTIFIER) //
146 static void setUpConfiguration() {
147 when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
148 when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
149 when(ftpesConfigMock.keyPasswordPath()).thenReturn(FTP_KEY_PASSWORD_PATH);
150 when(ftpesConfigMock.trustedCa()).thenReturn(TRUSTED_CA_PATH);
151 when(ftpesConfigMock.trustedCaPasswordPath()).thenReturn(TRUSTED_CA_PASSWORD_PATH);
156 counters = new Counters();
160 public void whenFtpesFile_returnCorrectResponse() throws Exception {
161 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
162 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
164 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
166 FilePublishInformation expectedfilePublishInformation =
167 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
169 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
170 .expectNext(expectedfilePublishInformation) //
173 verify(ftpsClientMock, times(1)).open();
174 verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
175 verify(ftpsClientMock, times(1)).close();
176 verifyNoMoreInteractions(ftpsClientMock);
178 assertEquals("collectedFiles should have been 1", 1, counters.getNoOfCollectedFiles());
179 assertEquals("failedFtpAttempts should have been 0", 0, counters.getNoOfFailedFtpAttempts());
183 public void whenSftpFile_returnCorrectResponse() throws Exception {
184 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
185 doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
187 FileData fileData = createFileData(SFTP_LOCATION_NO_PORT, Scheme.SFTP);
188 FilePublishInformation expectedfilePublishInformation =
189 createExpectedFilePublishInformation(SFTP_LOCATION_NO_PORT);
191 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
192 .expectNext(expectedfilePublishInformation) //
195 // The same again, but with port
196 fileData = createFileData(SFTP_LOCATION, Scheme.SFTP);
197 expectedfilePublishInformation = createExpectedFilePublishInformation(SFTP_LOCATION);
199 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
200 .expectNext(expectedfilePublishInformation) //
203 verify(sftpClientMock, times(2)).open();
204 verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
205 verify(sftpClientMock, times(2)).close();
206 verifyNoMoreInteractions(sftpClientMock);
208 assertEquals("collectedFiles should have been 2", 2, counters.getNoOfCollectedFiles());
212 public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
213 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
214 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
216 FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
217 doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
218 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
220 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
221 .expectErrorMessage("Retries exhausted: 3/3") //
224 verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
226 assertEquals("collectedFiles should have been 0", 0, counters.getNoOfCollectedFiles());
227 assertEquals("failedFtpAttempts should have been 4", 4, counters.getNoOfFailedFtpAttempts());
231 public void whenFtpesFileAlwaysFail_failWithoutRetry() throws Exception {
232 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
233 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
235 FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
236 doThrow(new NonRetryableDatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
237 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
239 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
240 .expectErrorMessage("Non retryable file transfer failure") //
243 verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
245 assertEquals("collectedFiles should have been 0", 0, counters.getNoOfCollectedFiles());
246 assertEquals("failedFtpAttempts should have been 1", 1, counters.getNoOfFailedFtpAttempts());
250 public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
251 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
252 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
253 doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock)
254 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
256 FilePublishInformation expectedfilePublishInformation =
257 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
259 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
261 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
262 .expectNext(expectedfilePublishInformation) //
265 verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
267 assertEquals("collectedFiles should have been 1", 1, counters.getNoOfCollectedFiles());
268 assertEquals("failedFtpAttempts should have been 1", 1, counters.getNoOfFailedFtpAttempts());