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.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.doThrow;
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.verifyNoMoreInteractions;
27 import static org.mockito.Mockito.when;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.time.Duration;
32 import java.util.HashMap;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
38 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
39 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
40 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
41 import org.onap.dcaegen2.collectors.datafile.ftp.Scheme;
42 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
43 import org.onap.dcaegen2.collectors.datafile.model.FileData;
44 import org.onap.dcaegen2.collectors.datafile.model.FilePublishInformation;
45 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
46 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFilePublishInformation;
47 import org.onap.dcaegen2.collectors.datafile.model.ImmutableMessageMetaData;
48 import org.onap.dcaegen2.collectors.datafile.model.MessageMetaData;
50 import reactor.test.StepVerifier;
52 public class FileCollectorTest {
53 private static final String PRODUCT_NAME = "NrRadio";
54 private static final String VENDOR_NAME = "Ericsson";
55 private static final String LAST_EPOCH_MICROSEC = "8745745764578";
56 private static final String SOURCE_NAME = "oteNB5309";
57 private static final String START_EPOCH_MICROSEC = "8745745764578";
58 private static final String TIME_ZONE_OFFSET = "UTC+05:00";
59 private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
60 private static final String FILE_READY_CHANGE_TYPE = "FileReady";
61 private static final String FTPES_SCHEME = "ftpes://";
62 private static final String SFTP_SCHEME = "sftp://";
63 private static final String SERVER_ADDRESS = "192.168.0.101";
64 private static final int PORT_22 = 22;
65 private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
66 private static final Path LOCAL_FILE_LOCATION = Paths.get(FileData.DATAFILE_TMPDIR, PM_FILE_NAME);
67 private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
68 private static final String USER = "usr";
69 private static final String PWD = "pwd";
70 private static final String FTPES_LOCATION =
71 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
73 private static final String FTPES_LOCATION_NO_PORT =
74 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
75 private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
76 private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
78 private static final String GZIP_COMPRESSION = "gzip";
79 private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
80 private static final String FILE_FORMAT_VERSION = "V10";
82 private static final String FTP_KEY_PATH = "ftpKeyPath";
83 private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
84 private static final String TRUSTED_CA_PATH = "trustedCAPath";
85 private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
87 private static AppConfig appConfigMock = mock(AppConfig.class);
88 private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
90 private FtpsClient ftpsClientMock = mock(FtpsClient.class);
92 private SftpClient sftpClientMock = mock(SftpClient.class);
93 private final Map<String, String> contextMap = new HashMap<>();
96 private MessageMetaData createMessageMetaData() {
97 return ImmutableMessageMetaData.builder() //
98 .productName(PRODUCT_NAME) //
99 .vendorName(VENDOR_NAME) //
100 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
101 .sourceName(SOURCE_NAME) //
102 .startEpochMicrosec(START_EPOCH_MICROSEC) //
103 .timeZoneOffset(TIME_ZONE_OFFSET) //
104 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER) //
105 .changeType(FILE_READY_CHANGE_TYPE) //
109 private FileData createFileData(String location, Scheme scheme) {
110 return ImmutableFileData.builder() //
111 .name(PM_FILE_NAME) //
112 .location(location) //
113 .compression(GZIP_COMPRESSION) //
114 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
115 .fileFormatVersion(FILE_FORMAT_VERSION) //
117 .messageMetaData(createMessageMetaData()) //
121 private FilePublishInformation createExpectedFilePublishInformation(String location) {
122 return ImmutableFilePublishInformation.builder() //
123 .productName(PRODUCT_NAME) //
124 .vendorName(VENDOR_NAME) //
125 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
126 .sourceName(SOURCE_NAME) //
127 .startEpochMicrosec(START_EPOCH_MICROSEC) //
128 .timeZoneOffset(TIME_ZONE_OFFSET) //
129 .name(PM_FILE_NAME) //
130 .location(location) //
131 .internalLocation(LOCAL_FILE_LOCATION) //
132 .compression(GZIP_COMPRESSION) //
133 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
134 .fileFormatVersion(FILE_FORMAT_VERSION) //
135 .context(new HashMap<String,String>())
140 * Sets up the configuration.
143 public static void setUpConfiguration() {
144 when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
145 when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
146 when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
147 when(ftpesConfigMock.trustedCa()).thenReturn(TRUSTED_CA_PATH);
148 when(ftpesConfigMock.trustedCaPassword()).thenReturn(TRUSTED_CA_PASSWORD);
152 public void whenFtpesFile_returnCorrectResponse() throws Exception {
153 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
154 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
156 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
158 FilePublishInformation expectedfilePublishInformation =
159 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
161 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
162 .expectNext(expectedfilePublishInformation) //
165 verify(ftpsClientMock, times(1)).open();
166 verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
167 verify(ftpsClientMock, times(1)).close();
169 verifyNoMoreInteractions(ftpsClientMock);
173 public void whenSftpFile_returnCorrectResponse() throws Exception {
174 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
175 doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
178 FileData fileData = createFileData(SFTP_LOCATION_NO_PORT, Scheme.SFTP);
179 FilePublishInformation expectedfilePublishInformation =
180 createExpectedFilePublishInformation(SFTP_LOCATION_NO_PORT);
182 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
183 .expectNext(expectedfilePublishInformation) //
186 // The same again, but with port
187 fileData = createFileData(SFTP_LOCATION, Scheme.SFTP);
188 expectedfilePublishInformation = createExpectedFilePublishInformation(SFTP_LOCATION);
190 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
191 .expectNext(expectedfilePublishInformation) //
194 verify(sftpClientMock, times(2)).open();
195 verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
196 verify(sftpClientMock, times(2)).close();
197 verifyNoMoreInteractions(sftpClientMock);
201 public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
202 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
203 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
205 FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
206 doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
207 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
209 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
210 .expectErrorMessage("Retries exhausted: 3/3") //
213 verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
217 public void whenFtpesFileAlwaysFail_failWithoutRetry() throws Exception {
218 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
219 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
221 final boolean retry = false;
222 FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
223 doThrow(new DatafileTaskException("Unable to collect file.", retry)).when(ftpsClientMock)
224 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
226 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
227 .expectErrorMessage("Non retryable file transfer failure") //
230 verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
234 public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
235 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
236 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
237 doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock)
238 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
240 FilePublishInformation expectedfilePublishInformation =
241 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
243 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
245 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
246 .expectNext(expectedfilePublishInformation) //
249 verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);