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;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
39 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
40 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
41 import org.onap.dcaegen2.collectors.datafile.exceptions.NonRetryableDatafileTaskException;
42 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
43 import org.onap.dcaegen2.collectors.datafile.ftp.Scheme;
44 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
45 import org.onap.dcaegen2.collectors.datafile.model.Counters;
46 import org.onap.dcaegen2.collectors.datafile.model.FileData;
47 import org.onap.dcaegen2.collectors.datafile.model.FilePublishInformation;
48 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
49 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFilePublishInformation;
50 import org.onap.dcaegen2.collectors.datafile.model.ImmutableMessageMetaData;
51 import org.onap.dcaegen2.collectors.datafile.model.MessageMetaData;
52 import reactor.test.StepVerifier;
54 public class FileCollectorTest {
55 private static final String PRODUCT_NAME = "NrRadio";
56 private static final String VENDOR_NAME = "Ericsson";
57 private static final String LAST_EPOCH_MICROSEC = "8745745764578";
58 private static final String SOURCE_NAME = "oteNB5309";
59 private static final String START_EPOCH_MICROSEC = "8745745764578";
60 private static final String TIME_ZONE_OFFSET = "UTC+05:00";
61 private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
62 private static final String FILE_READY_CHANGE_TYPE = "FileReady";
63 private static final String FTPES_SCHEME = "ftpes://";
64 private static final String SFTP_SCHEME = "sftp://";
65 private static final String SERVER_ADDRESS = "192.168.0.101";
66 private static final int PORT_22 = 22;
67 private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
68 private static final Path LOCAL_FILE_LOCATION = Paths.get(FileData.DATAFILE_TMPDIR, PM_FILE_NAME);
69 private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
70 private static final String USER = "usr";
71 private static final String PWD = "pwd";
72 private static final String FTPES_LOCATION =
73 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
75 private static final String FTPES_LOCATION_NO_PORT =
76 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
77 private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
78 private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
80 private static final String GZIP_COMPRESSION = "gzip";
81 private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
82 private static final String FILE_FORMAT_VERSION = "V10";
84 private static final String FTP_KEY_PATH = "ftpKeyPath";
85 private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
86 private static final String TRUSTED_CA_PATH = "trustedCAPath";
87 private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
88 private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
90 private static AppConfig appConfigMock = mock(AppConfig.class);
91 private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
93 private FtpsClient ftpsClientMock = mock(FtpsClient.class);
95 private SftpClient sftpClientMock = mock(SftpClient.class);
96 private final Map<String, String> contextMap = new HashMap<>();
98 private final Counters counters = new Counters();
100 private MessageMetaData createMessageMetaData() {
101 return ImmutableMessageMetaData.builder() //
102 .productName(PRODUCT_NAME) //
103 .vendorName(VENDOR_NAME) //
104 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
105 .sourceName(SOURCE_NAME) //
106 .startEpochMicrosec(START_EPOCH_MICROSEC) //
107 .timeZoneOffset(TIME_ZONE_OFFSET) //
108 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER) //
109 .changeType(FILE_READY_CHANGE_TYPE) //
113 private FileData createFileData(String location, Scheme scheme) {
114 return ImmutableFileData.builder() //
115 .name(PM_FILE_NAME) //
116 .location(location) //
117 .compression(GZIP_COMPRESSION) //
118 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
119 .fileFormatVersion(FILE_FORMAT_VERSION) //
121 .messageMetaData(createMessageMetaData()) //
125 private FilePublishInformation createExpectedFilePublishInformation(String location) {
126 return ImmutableFilePublishInformation.builder() //
127 .productName(PRODUCT_NAME) //
128 .vendorName(VENDOR_NAME) //
129 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
130 .sourceName(SOURCE_NAME) //
131 .startEpochMicrosec(START_EPOCH_MICROSEC) //
132 .timeZoneOffset(TIME_ZONE_OFFSET) //
133 .name(PM_FILE_NAME) //
134 .location(location) //
135 .internalLocation(LOCAL_FILE_LOCATION) //
136 .compression(GZIP_COMPRESSION) //
137 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
138 .fileFormatVersion(FILE_FORMAT_VERSION) //
139 .context(new HashMap<String, String>()) //
140 .changeIdentifier(CHANGE_IDENTIFIER) //
145 static void setUpConfiguration() {
146 when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
147 when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
148 when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
149 when(ftpesConfigMock.trustedCa()).thenReturn(TRUSTED_CA_PATH);
150 when(ftpesConfigMock.trustedCaPassword()).thenReturn(TRUSTED_CA_PASSWORD);
159 public void whenFtpesFile_returnCorrectResponse() throws Exception {
160 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
161 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
163 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
165 FilePublishInformation expectedfilePublishInformation =
166 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
168 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
169 .expectNext(expectedfilePublishInformation) //
172 verify(ftpsClientMock, times(1)).open();
173 verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
174 verify(ftpsClientMock, times(1)).close();
175 verifyNoMoreInteractions(ftpsClientMock);
177 assertEquals("collectedFiles should have been 1", counters.getNoOfCollectedFiles(), 1);
178 assertEquals("failedFtpAttempts should have been 0", counters.getNoOfFailedFtpAttempts(), 0);
182 public void whenSftpFile_returnCorrectResponse() throws Exception {
183 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
184 doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
186 FileData fileData = createFileData(SFTP_LOCATION_NO_PORT, Scheme.SFTP);
187 FilePublishInformation expectedfilePublishInformation =
188 createExpectedFilePublishInformation(SFTP_LOCATION_NO_PORT);
190 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
191 .expectNext(expectedfilePublishInformation) //
194 // The same again, but with port
195 fileData = createFileData(SFTP_LOCATION, Scheme.SFTP);
196 expectedfilePublishInformation = createExpectedFilePublishInformation(SFTP_LOCATION);
198 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
199 .expectNext(expectedfilePublishInformation) //
202 verify(sftpClientMock, times(2)).open();
203 verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
204 verify(sftpClientMock, times(2)).close();
205 verifyNoMoreInteractions(sftpClientMock);
207 assertEquals("collectedFiles should have been 2", counters.getNoOfCollectedFiles(), 2);
211 public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
212 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
213 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
215 FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
216 doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
217 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
219 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
220 .expectErrorMessage("Retries exhausted: 3/3") //
223 verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
225 assertEquals("collectedFiles should have been 0", counters.getNoOfCollectedFiles(), 0);
226 assertEquals("failedFtpAttempts should have been 4", counters.getNoOfFailedFtpAttempts(), 4);
230 public void whenFtpesFileAlwaysFail_failWithoutRetry() throws Exception {
231 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
232 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
234 FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
235 doThrow(new NonRetryableDatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
236 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
238 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
239 .expectErrorMessage("Non retryable file transfer failure") //
242 verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
244 assertEquals("collectedFiles should have been 0", counters.getNoOfCollectedFiles(), 0);
245 assertEquals("failedFtpAttempts should have been 1", counters.getNoOfFailedFtpAttempts(), 1);
249 public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
250 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
251 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
252 doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock)
253 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
255 FilePublishInformation expectedfilePublishInformation =
256 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
258 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
260 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
261 .expectNext(expectedfilePublishInformation) //
264 verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
266 assertEquals("collectedFiles should have been 1", counters.getNoOfCollectedFiles(), 1);
267 assertEquals("failedFtpAttempts should have been 1", counters.getNoOfFailedFtpAttempts(), 1);