2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018-2023 Nordix Foundation. All rights reserved.
4 * Copyright (C) 2020-2022 Nokia. All rights reserved.
5 * ===============================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License
12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing permissions and limitations under
15 * ============LICENSE_END========================================================================
18 package org.oran.datafile.tasks;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.doThrow;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyNoMoreInteractions;
29 import static org.mockito.Mockito.when;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.time.Duration;
34 import java.util.ArrayList;
35 import java.util.List;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.oran.datafile.configuration.AppConfig;
41 import org.oran.datafile.configuration.CertificateConfig;
42 import org.oran.datafile.exceptions.DatafileTaskException;
43 import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
44 import org.oran.datafile.ftp.FtpesClient;
45 import org.oran.datafile.ftp.SftpClient;
46 import org.oran.datafile.http.DfcHttpClient;
47 import org.oran.datafile.http.DfcHttpsClient;
48 import org.oran.datafile.model.Counters;
49 import org.oran.datafile.model.FileData;
50 import org.oran.datafile.model.FilePublishInformation;
51 import org.oran.datafile.model.FileReadyMessage;
52 import org.oran.datafile.oauth2.SecurityContext;
53 import reactor.test.StepVerifier;
55 class FileCollectorTest {
57 final static String DATAFILE_TMPDIR = "/tmp/onap_datafile/";
58 private static final String PRODUCT_NAME = "NrRadio";
59 private static final String VENDOR_NAME = "Ericsson";
60 private static final int LAST_EPOCH_MICROSEC = 87457457;
61 private static final String SOURCE_NAME = "oteNB5309";
62 private static final int START_EPOCH_MICROSEC = 874575764;
63 private static final String TIME_ZONE_OFFSET = "UTC+05:00";
64 private static final String FTPES_SCHEME = "ftpes://";
65 private static final String SFTP_SCHEME = "sftp://";
66 private static final String HTTP_SCHEME = "http://";
67 private static final String HTTPS_SCHEME = "https://";
68 private static final String SERVER_ADDRESS = "192.168.0.101";
69 private static final int PORT_22 = 22;
70 private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
71 private static final Path LOCAL_FILE_LOCATION = Paths.get(DATAFILE_TMPDIR, SOURCE_NAME, PM_FILE_NAME);
72 private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
73 private static final String USER = "usr";
74 private static final String PWD = "pwd";
75 private static final String FTPES_LOCATION =
76 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
78 private static final String FTPES_LOCATION_NO_PORT =
79 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
80 private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
81 private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
83 private static final String HTTP_LOCATION =
84 HTTP_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
85 private static final String HTTP_LOCATION_NO_PORT =
86 HTTP_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
87 private static final String HTTPS_LOCATION =
88 HTTPS_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
89 private static final String HTTPS_LOCATION_NO_PORT =
90 HTTPS_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
92 private static final String GZIP_COMPRESSION = "gzip";
93 private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
94 private static final String FILE_FORMAT_VERSION = "V10";
95 private static final String CERTIFICATE_KEY_PASSWORD_PATH = "certificateKeyPassword";
96 private static final String TRUSTED_CA_PATH = "trustedCAPath";
97 private static final String TRUSTED_CA_PASSWORD_PATH = "trustedCAPassword";
98 private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
99 private static final String FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
100 private static final String CHANGE_TYPE = "FileReady";
102 private static AppConfig appConfigMock = mock(AppConfig.class);
103 private static CertificateConfig certificateConfigMock = mock(CertificateConfig.class);
105 private FtpesClient ftpesClientMock = mock(FtpesClient.class);
107 private SftpClient sftpClientMock = mock(SftpClient.class);
109 private DfcHttpClient dfcHttpClientMock = mock(DfcHttpClient.class);
110 private DfcHttpsClient dfcHttpsClientMock = mock(DfcHttpsClient.class);
112 private Counters counters;
114 static final SecurityContext securityContext = new SecurityContext("");
116 FileReadyMessage.Event event(String location) {
117 FileReadyMessage.MessageMetaData messageMetaData = FileReadyMessage.MessageMetaData.builder() //
118 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
119 .sourceName(SOURCE_NAME) //
120 .startEpochMicrosec(START_EPOCH_MICROSEC) //
121 .timeZoneOffset(TIME_ZONE_OFFSET) //
122 .changeIdentifier(CHANGE_IDENTIFIER) //
123 .eventName("Noti_NrRadio-Ericsson_FileReady").build();
125 FileReadyMessage.FileInfo fileInfo = FileReadyMessage.FileInfo //
127 .fileFormatType(FILE_FORMAT_TYPE) //
128 .location(location) //
129 .fileFormatVersion(FILE_FORMAT_VERSION) //
130 .compression(GZIP_COMPRESSION) //
133 FileReadyMessage.ArrayOfNamedHashMap arrayOfNamedHashMap = FileReadyMessage.ArrayOfNamedHashMap //
134 .builder().name(PM_FILE_NAME) //
135 .hashMap(fileInfo).build();
137 List<FileReadyMessage.ArrayOfNamedHashMap> arrayOfNamedHashMapList = new ArrayList<>();
138 arrayOfNamedHashMapList.add(arrayOfNamedHashMap);
140 FileReadyMessage.NotificationFields notificationFields = FileReadyMessage.NotificationFields //
141 .builder().notificationFieldsVersion("notificationFieldsVersion") //
142 .changeType(CHANGE_TYPE).changeIdentifier(CHANGE_IDENTIFIER) //
143 .arrayOfNamedHashMap(arrayOfNamedHashMapList) //
146 return FileReadyMessage.Event.builder() //
147 .commonEventHeader(messageMetaData) //
148 .notificationFields(notificationFields).build();
151 private FileReadyMessage fileReadyMessage(String location) {
152 FileReadyMessage message = FileReadyMessage.builder() //
153 .event(event(location)) //
158 private FileData createFileData(String location) {
159 return FileData.createFileData(fileReadyMessage(location)).iterator().next();
162 private FilePublishInformation createExpectedFilePublishInformation(String location) {
163 return FilePublishInformation.builder() //
164 .productName(PRODUCT_NAME) //
165 .vendorName(VENDOR_NAME) //
166 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
167 .sourceName(SOURCE_NAME) //
168 .startEpochMicrosec(START_EPOCH_MICROSEC) //
169 .timeZoneOffset(TIME_ZONE_OFFSET) //
170 .name(SOURCE_NAME + "/" + PM_FILE_NAME) //
171 .compression(GZIP_COMPRESSION) //
172 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
173 .fileFormatVersion(FILE_FORMAT_VERSION) //
174 .changeIdentifier(CHANGE_IDENTIFIER) //
179 static void setUpConfiguration() {
180 when(appConfigMock.getCertificateConfiguration()).thenReturn(certificateConfigMock);
181 when(appConfigMock.getCollectedFilesPath()).thenReturn(DATAFILE_TMPDIR);
182 certificateConfigMock.keyPasswordPath = CERTIFICATE_KEY_PASSWORD_PATH;
183 certificateConfigMock.trustedCa = TRUSTED_CA_PATH;
184 certificateConfigMock.trustedCaPasswordPath = TRUSTED_CA_PASSWORD_PATH;
189 counters = new Counters();
193 void whenFtpesFile_returnCorrectResponse() throws Exception {
194 FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
195 doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());
197 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT);
199 FilePublishInformation expectedfilePublishInformation =
200 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
202 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
203 .expectNext(expectedfilePublishInformation) //
206 verify(ftpesClientMock, times(1)).open();
207 verify(ftpesClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
208 verify(ftpesClientMock, times(1)).close();
209 verifyNoMoreInteractions(ftpesClientMock);
211 assertEquals(1, counters.getNoOfCollectedFiles(), "collectedFiles should have been 1");
212 assertEquals(0, counters.getNoOfFailedFtpAttempts(), "failedFtpAttempts should have been 0");
213 assertEquals(0, counters.getNoOfFailedHttpAttempts(), "failedHttpAttempts should have been 0");
217 void whenSftpFile_returnCorrectResponse() throws Exception {
218 FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
219 doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
221 FileData fileData = createFileData(SFTP_LOCATION_NO_PORT);
222 FilePublishInformation expectedfilePublishInformation =
223 createExpectedFilePublishInformation(SFTP_LOCATION_NO_PORT);
225 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
226 .expectNext(expectedfilePublishInformation) //
229 // The same again, but with port
230 fileData = createFileData(SFTP_LOCATION);
231 expectedfilePublishInformation = createExpectedFilePublishInformation(SFTP_LOCATION);
233 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
234 .expectNext(expectedfilePublishInformation) //
237 verify(sftpClientMock, times(2)).open();
238 verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
239 verify(sftpClientMock, times(2)).close();
240 verifyNoMoreInteractions(sftpClientMock);
242 assertEquals(2, counters.getNoOfCollectedFiles(), "collectedFiles should have been 2");
246 void whenHttpFile_returnCorrectResponse() throws Exception {
247 FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
248 doReturn(dfcHttpClientMock).when(collectorUndetTest).createHttpClient(any());
250 FileData fileData = createFileData(HTTP_LOCATION_NO_PORT);
252 FilePublishInformation expectedfilePublishInformation =
253 createExpectedFilePublishInformation(HTTP_LOCATION_NO_PORT);
255 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
256 .expectNext(expectedfilePublishInformation) //
259 // The same again, but with port
260 fileData = createFileData(HTTP_LOCATION);
261 expectedfilePublishInformation = createExpectedFilePublishInformation(HTTP_LOCATION);
263 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
264 .expectNext(expectedfilePublishInformation) //
267 verify(dfcHttpClientMock, times(2)).open();
268 verify(dfcHttpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
269 verify(dfcHttpClientMock, times(2)).close();
270 verifyNoMoreInteractions(dfcHttpClientMock);
272 assertEquals(2, counters.getNoOfCollectedFiles(), "collectedFiles should have been 1");
273 assertEquals(0, counters.getNoOfFailedFtpAttempts(), "failedFtpAttempts should have been 0");
274 assertEquals(0, counters.getNoOfFailedHttpAttempts(), "failedHttpAttempts should have been 0");
278 void whenHttpsFile_returnCorrectResponse() throws Exception {
279 FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
280 doReturn(dfcHttpsClientMock).when(collectorUndetTest).createHttpsClient(any());
282 FileData fileData = createFileData(HTTPS_LOCATION_NO_PORT);
284 FilePublishInformation expectedfilePublishInformation =
285 createExpectedFilePublishInformation(HTTPS_LOCATION_NO_PORT);
287 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
288 .expectNext(expectedfilePublishInformation) //
291 // The same again, but with port
292 fileData = createFileData(HTTPS_LOCATION);
293 expectedfilePublishInformation = createExpectedFilePublishInformation(HTTPS_LOCATION);
295 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
296 .expectNext(expectedfilePublishInformation) //
299 verify(dfcHttpsClientMock, times(2)).open();
300 verify(dfcHttpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
301 verify(dfcHttpsClientMock, times(2)).close();
302 verifyNoMoreInteractions(dfcHttpsClientMock);
304 assertEquals(2, counters.getNoOfCollectedFiles(), "collectedFiles should have been 1");
305 assertEquals(0, counters.getNoOfFailedFtpAttempts(), "failedFtpAttempts should have been 0");
306 assertEquals(0, counters.getNoOfFailedHttpAttempts(), "failedHttpAttempts should have been 0");
310 void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
311 FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
312 doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());
314 FileData fileData = createFileData(FTPES_LOCATION);
315 doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpesClientMock)
316 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
318 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
319 .expectErrorMessage("Retries exhausted: 3/3") //
322 verify(ftpesClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
324 assertEquals(0, counters.getNoOfCollectedFiles(), "collectedFiles should have been 0");
325 assertEquals(4, counters.getNoOfFailedFtpAttempts(), "failedFtpAttempts should have been 4");
326 assertEquals(0, counters.getNoOfFailedHttpAttempts(), "failedHttpAttempts should have been 0");
330 void whenFtpesFileAlwaysFail_failWithoutRetry() throws Exception {
331 FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
332 doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());
334 FileData fileData = createFileData(FTPES_LOCATION);
335 doThrow(new NonRetryableDatafileTaskException("Unable to collect file.")).when(ftpesClientMock)
336 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
338 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
339 .expectErrorMessage("Non retryable file transfer failure") //
342 verify(ftpesClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
344 assertEquals(0, counters.getNoOfCollectedFiles(), "collectedFiles should have been 0");
345 assertEquals(1, counters.getNoOfFailedFtpAttempts(), "failedFtpAttempts should have been 1");
346 assertEquals(0, counters.getNoOfFailedHttpAttempts(), "failedHttpAttempts should have been 0");
350 void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
351 FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
352 doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());
353 doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpesClientMock)
354 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
356 FilePublishInformation expectedfilePublishInformation =
357 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
359 FileData fileData = createFileData(FTPES_LOCATION_NO_PORT);
361 StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0)))
362 .expectNext(expectedfilePublishInformation) //
365 verify(ftpesClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
367 assertEquals(1, counters.getNoOfCollectedFiles(), "collectedFiles should have been 1");
368 assertEquals(1, counters.getNoOfFailedFtpAttempts(), "failedFtpAttempts should have been 1");
369 assertEquals(0, counters.getNoOfFailedHttpAttempts(), "failedHttpAttempts should have been 0");