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;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
37 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
38 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
39 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
40 import org.onap.dcaegen2.collectors.datafile.ftp.Scheme;
41 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
42 import org.onap.dcaegen2.collectors.datafile.model.FileData;
43 import org.onap.dcaegen2.collectors.datafile.model.FilePublishInformation;
44 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
45 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFilePublishInformation;
46 import org.onap.dcaegen2.collectors.datafile.model.ImmutableMessageMetaData;
47 import org.onap.dcaegen2.collectors.datafile.model.MessageMetaData;
48 import reactor.test.StepVerifier;
50 public class FileCollectorTest {
51 private static final String PRODUCT_NAME = "NrRadio";
52 private static final String VENDOR_NAME = "Ericsson";
53 private static final String LAST_EPOCH_MICROSEC = "8745745764578";
54 private static final String SOURCE_NAME = "oteNB5309";
55 private static final String START_EPOCH_MICROSEC = "8745745764578";
56 private static final String TIME_ZONE_OFFSET = "UTC+05:00";
57 private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
58 private static final String FILE_READY_CHANGE_TYPE = "FileReady";
59 private static final String FTPES_SCHEME = "ftpes://";
60 private static final String SFTP_SCHEME = "sftp://";
61 private static final String SERVER_ADDRESS = "192.168.0.101";
62 private static final int PORT_22 = 22;
63 private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
64 private static final Path LOCAL_FILE_LOCATION = Paths.get(FileData.DATAFILE_TMPDIR, PM_FILE_NAME);
65 private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
66 private static final String USER = "usr";
67 private static final String PWD = "pwd";
68 private static final String FTPES_LOCATION =
69 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
71 private static final String FTPES_LOCATION_NO_PORT =
72 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
73 private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
74 private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
76 private static final String GZIP_COMPRESSION = "gzip";
77 private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
78 private static final String FILE_FORMAT_VERSION = "V10";
80 private static final String FTP_KEY_PATH = "ftpKeyPath";
81 private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
82 private static final String TRUSTED_CA_PATH = "trustedCAPath";
83 private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
85 private static AppConfig appConfigMock = mock(AppConfig.class);
86 private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
88 private FtpsClient ftpsClientMock = mock(FtpsClient.class);
90 private SftpClient sftpClientMock = mock(SftpClient.class);
91 private final Map<String, String> contextMap = new HashMap<>();
94 private MessageMetaData createMessageMetaData() {
95 return ImmutableMessageMetaData.builder() //
96 .productName(PRODUCT_NAME) //
97 .vendorName(VENDOR_NAME) //
98 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
99 .sourceName(SOURCE_NAME) //
100 .startEpochMicrosec(START_EPOCH_MICROSEC) //
101 .timeZoneOffset(TIME_ZONE_OFFSET) //
102 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER) //
103 .changeType(FILE_READY_CHANGE_TYPE) //
107 private FileData createFileData(String location, Scheme scheme) {
108 return ImmutableFileData.builder() //
109 .name(PM_FILE_NAME) //
110 .location(location) //
111 .compression(GZIP_COMPRESSION) //
112 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
113 .fileFormatVersion(FILE_FORMAT_VERSION) //
115 .messageMetaData(createMessageMetaData()) //
119 private FilePublishInformation createExpectedFilePublishInformation(String location) {
120 return ImmutableFilePublishInformation.builder() //
121 .productName(PRODUCT_NAME) //
122 .vendorName(VENDOR_NAME) //
123 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
124 .sourceName(SOURCE_NAME) //
125 .startEpochMicrosec(START_EPOCH_MICROSEC) //
126 .timeZoneOffset(TIME_ZONE_OFFSET) //
127 .name(PM_FILE_NAME) //
128 .location(location) //
129 .internalLocation(LOCAL_FILE_LOCATION) //
130 .compression(GZIP_COMPRESSION) //
131 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
132 .fileFormatVersion(FILE_FORMAT_VERSION) //
137 * Sets up the configuration.
140 public static void setUpConfiguration() {
141 when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
142 when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
143 when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
144 when(ftpesConfigMock.trustedCa()).thenReturn(TRUSTED_CA_PATH);
145 when(ftpesConfigMock.trustedCaPassword()).thenReturn(TRUSTED_CA_PASSWORD);
149 public void whenFtpesFile_returnCorrectResponse() throws Exception {
150 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
151 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
153 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
155 FilePublishInformation expectedfilePublishInformation =
156 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
158 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
159 .expectNext(expectedfilePublishInformation) //
162 verify(ftpsClientMock, times(1)).open();
163 verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
164 verify(ftpsClientMock, times(1)).close();
166 verifyNoMoreInteractions(ftpsClientMock);
170 public void whenSftpFile_returnCorrectResponse() throws Exception {
171 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
172 doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
175 FileData fileData = createFileData(SFTP_LOCATION_NO_PORT, Scheme.SFTP);
176 FilePublishInformation expectedfilePublishInformation =
177 createExpectedFilePublishInformation(SFTP_LOCATION_NO_PORT);
179 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
180 .expectNext(expectedfilePublishInformation) //
183 // The same again, but with port
184 fileData = createFileData(SFTP_LOCATION, Scheme.SFTP);
185 expectedfilePublishInformation = createExpectedFilePublishInformation(SFTP_LOCATION);
187 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
188 .expectNext(expectedfilePublishInformation) //
191 verify(sftpClientMock, times(2)).open();
192 verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
193 verify(sftpClientMock, times(2)).close();
194 verifyNoMoreInteractions(sftpClientMock);
198 public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
199 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
200 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
202 FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
203 doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
204 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
206 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
207 .expectErrorMessage("Retries exhausted: 3/3") //
210 verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
214 public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
215 FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
216 doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
217 doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock)
218 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
220 FilePublishInformation expectedfilePublishInformation =
221 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
223 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
225 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
226 .expectNext(expectedfilePublishInformation) //
229 verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);