2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 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.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.verifyNoMoreInteractions;
24 import static org.mockito.Mockito.when;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
31 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
32 import org.onap.dcaegen2.collectors.datafile.ftp.ErrorData;
33 import org.onap.dcaegen2.collectors.datafile.ftp.FileCollectResult;
34 import org.onap.dcaegen2.collectors.datafile.ftp.FileServerData;
35 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
36 import org.onap.dcaegen2.collectors.datafile.ftp.ImmutableFileServerData;
37 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
38 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
39 import org.onap.dcaegen2.collectors.datafile.model.FileData;
40 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
41 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
43 import reactor.test.StepVerifier;
46 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
49 public class XnfCollectorTaskImplTest {
51 private static final String PM_MEAS_CHANGE_IDINTIFIER = "PM_MEAS_FILES";
52 private static final String FILE_READY_CHANGE_TYPE = "FileReady";
53 private static final String FTPES_SCHEME = "ftpes://";
54 private static final String SFTP_SCHEME = "sftp://";
55 private static final String SERVER_ADDRESS = "192.168.0.101";
56 private static final int PORT_22 = 22;
57 private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
58 private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
59 private static final String LOCAL_FILE_LOCATION = "target" + File.separator + PM_FILE_NAME;
60 private static final String USER = "usr";
61 private static final String PWD = "pwd";
62 private static final String FTPES_LOCATION =
63 FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
64 private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
65 private static final String GZIP_COMPRESSION = "gzip";
66 private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
67 private static final String FILE_FORMAT_VERSION = "V10";
69 private static final String FTP_KEY_PATH = "ftpKeyPath";
70 private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
71 private static final String TRUSTED_CA_PATH = "trustedCAPath";
72 private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
74 private static AppConfig appConfigMock = mock(AppConfig.class);
75 private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
77 private FtpsClient ftpsClientMock = mock(FtpsClient.class);
79 private SftpClient sftpClientMock = mock(SftpClient.class);
80 private RetryTimer retryTimerMock = mock(RetryTimer.class);
84 public static void setUpConfiguration() {
85 when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
86 when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
87 when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
88 when(ftpesConfigMock.trustedCA()).thenReturn(TRUSTED_CA_PATH);
89 when(ftpesConfigMock.trustedCAPassword()).thenReturn(TRUSTED_CA_PASSWORD);
93 public void whenFtpesFile_returnCorrectResponse() {
94 XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
96 FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
97 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(FTPES_LOCATION)
98 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
99 .fileFormatVersion(FILE_FORMAT_VERSION).build();
101 FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId(USER)
102 .password(PWD).port(PORT_22).build();
103 when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
104 .thenReturn(new FileCollectResult());
106 ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder().name(PM_FILE_NAME)
107 .location(LOCAL_FILE_LOCATION).compression(GZIP_COMPRESSION)
108 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
110 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
113 verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
114 verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH);
115 verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD);
116 verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH);
117 verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD);
118 verifyNoMoreInteractions(ftpsClientMock);
122 public void whenSftpFile_returnCorrectResponse() {
123 XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
125 FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
126 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(SFTP_LOCATION)
127 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
128 .fileFormatVersion(FILE_FORMAT_VERSION).build();
130 FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId("")
131 .password("").port(PORT_22).build();
132 when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
133 .thenReturn(new FileCollectResult());
135 ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder().name(PM_FILE_NAME)
136 .location(LOCAL_FILE_LOCATION).compression(GZIP_COMPRESSION)
137 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
139 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
142 verify(sftpClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
143 verifyNoMoreInteractions(sftpClientMock);
147 public void whenFtpesFileAlwaysFail_retryAndReturnEmpty() {
148 XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
149 collectorUndetTest.setRetryTimer(retryTimerMock);
151 FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
152 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(FTPES_LOCATION)
153 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
154 .fileFormatVersion(FILE_FORMAT_VERSION).build();
156 FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId(USER)
157 .password(PWD).port(PORT_22).build();
158 ErrorData errorData = new ErrorData();
159 errorData.addError("Unable to collect file.", new Exception());
160 when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
161 .thenReturn(new FileCollectResult(errorData));
162 doReturn(new FileCollectResult(errorData)).when(ftpsClientMock).retryCollectFile();
164 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
166 verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
167 verify(ftpsClientMock, times(2)).retryCollectFile();
171 public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() {
172 XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
173 collectorUndetTest.setRetryTimer(retryTimerMock);
175 FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
176 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(FTPES_LOCATION)
177 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
178 .fileFormatVersion(FILE_FORMAT_VERSION).build();
180 FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId(USER)
181 .password(PWD).port(PORT_22).build();
182 ErrorData errorData = new ErrorData();
183 errorData.addError("Unable to collect file.", new Exception());
184 when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
185 .thenReturn(new FileCollectResult(errorData));
186 doReturn(new FileCollectResult()).when(ftpsClientMock).retryCollectFile();
188 ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder().name(PM_FILE_NAME)
189 .location(LOCAL_FILE_LOCATION).compression(GZIP_COMPRESSION)
190 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
192 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
196 verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
197 verify(ftpsClientMock, times(1)).retryCollectFile();
201 public void whenWrongScheme_returnEmpty() {
202 XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
204 FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
205 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location("http://host.com/file.zip")
206 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
207 .fileFormatVersion(FILE_FORMAT_VERSION).build();
209 FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId("")
210 .password("").port(PORT_22).build();
211 when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
212 .thenReturn(new FileCollectResult());
214 StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
216 verifyNoMoreInteractions(sftpClientMock);