55fa639f9c9f927f1d13694fa350d9b63f72d7c8
[dcaegen2/collectors/datafile.git] /
1 /*
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.tasks;
18
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;
25
26 import java.io.File;
27
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.FileMetaData;
41 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
42 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
43 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileMetaData;
44
45 import reactor.test.StepVerifier;
46
47 /**
48  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
49  *
50  */
51 public class XnfCollectorTaskImplTest {
52     private static final String PRODUCT_NAME = "NrRadio";
53     private static final String VENDOR_NAME = "Ericsson";
54     private static final String LAST_EPOCH_MICROSEC = "8745745764578";
55     private static final String SOURCE_NAME = "oteNB5309";
56     private static final String START_EPOCH_MICROSEC = "8745745764578";
57     private static final String TIME_ZONE_OFFSET = "UTC+05:00";
58     private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
59     private static final String FILE_READY_CHANGE_TYPE = "FileReady";
60     private static final String FTPES_SCHEME = "ftpes://";
61     private static final String SFTP_SCHEME = "sftp://";
62     private static final String SERVER_ADDRESS = "192.168.0.101";
63     private static final int PORT_22 = 22;
64     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
65     private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
66     private static final String LOCAL_FILE_LOCATION = "target" + File.separator + PM_FILE_NAME;
67     private static final String USER = "usr";
68     private static final String PWD = "pwd";
69     private static final String FTPES_LOCATION =
70             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
71     private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
72     private static final String GZIP_COMPRESSION = "gzip";
73     private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
74     private static final String FILE_FORMAT_VERSION = "V10";
75
76     private static final String FTP_KEY_PATH = "ftpKeyPath";
77     private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
78     private static final String TRUSTED_CA_PATH = "trustedCAPath";
79     private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
80
81     private static AppConfig appConfigMock = mock(AppConfig.class);
82     private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
83
84     private FtpsClient ftpsClientMock = mock(FtpsClient.class);
85
86     private SftpClient sftpClientMock = mock(SftpClient.class);
87     private RetryTimer retryTimerMock = mock(RetryTimer.class);
88     // @formatter:off
89     private FileMetaData fileMetaData = ImmutableFileMetaData.builder()
90                 .productName(PRODUCT_NAME)
91                 .vendorName(VENDOR_NAME)
92                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
93                 .sourceName(SOURCE_NAME)
94                 .startEpochMicrosec(START_EPOCH_MICROSEC)
95                 .timeZoneOffset(TIME_ZONE_OFFSET)
96                 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER)
97                 .changeType(FILE_READY_CHANGE_TYPE)
98                 .build();;
99                 // @formatter:on
100
101     @BeforeAll
102     public static void setUpConfiguration() {
103         when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
104         when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
105         when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
106         when(ftpesConfigMock.trustedCA()).thenReturn(TRUSTED_CA_PATH);
107         when(ftpesConfigMock.trustedCAPassword()).thenReturn(TRUSTED_CA_PASSWORD);
108     }
109
110     @Test
111     public void whenFtpesFile_returnCorrectResponse() {
112         XnfCollectorTaskImpl collectorUndetTest =
113                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
114
115        // @formatter:off
116         FileData fileData = ImmutableFileData.builder()
117                 .fileMetaData(fileMetaData)
118                 .name(PM_FILE_NAME)
119                 .location(FTPES_LOCATION)
120                 .compression(GZIP_COMPRESSION)
121                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
122                 .fileFormatVersion(FILE_FORMAT_VERSION)
123                 .build();
124
125         FileServerData fileServerData = ImmutableFileServerData.builder()
126                 .serverAddress(SERVER_ADDRESS)
127                 .userId(USER)
128                 .password(PWD)
129                 .port(PORT_22)
130                 .build();
131         // @formatter:on
132         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
133                 .thenReturn(new FileCollectResult());
134
135         // @formatter:off
136         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
137                 .productName(PRODUCT_NAME)
138                 .vendorName(VENDOR_NAME)
139                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
140                 .sourceName(SOURCE_NAME)
141                 .startEpochMicrosec(START_EPOCH_MICROSEC)
142                 .timeZoneOffset(TIME_ZONE_OFFSET)
143                 .name(PM_FILE_NAME)
144                 .location(FTPES_LOCATION)
145                 .internalLocation(LOCAL_FILE_LOCATION)
146                 .compression(GZIP_COMPRESSION)
147                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
148                 .fileFormatVersion(FILE_FORMAT_VERSION)
149                 .build();
150         // @formatter:on
151
152         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
153                 .verifyComplete();
154
155         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
156         verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH);
157         verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD);
158         verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH);
159         verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD);
160         verifyNoMoreInteractions(ftpsClientMock);
161     }
162
163     @Test
164     public void whenSftpFile_returnCorrectResponse() {
165         XnfCollectorTaskImpl collectorUndetTest =
166                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
167         // @formatter:off
168         FileData fileData = ImmutableFileData.builder()
169                 .fileMetaData(fileMetaData)
170                 .name(PM_FILE_NAME)
171                 .location(SFTP_LOCATION)
172                 .compression(GZIP_COMPRESSION)
173                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
174                 .fileFormatVersion(FILE_FORMAT_VERSION)
175                 .build();
176
177
178         FileServerData fileServerData = ImmutableFileServerData.builder()
179                 .serverAddress(SERVER_ADDRESS)
180                 .userId("")
181                 .password("")
182                 .port(PORT_22)
183                 .build();
184         // @formatter:on
185         when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
186                 .thenReturn(new FileCollectResult());
187         // @formatter:off
188         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
189                 .productName(PRODUCT_NAME)
190                 .vendorName(VENDOR_NAME)
191                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
192                 .sourceName(SOURCE_NAME)
193                 .startEpochMicrosec(START_EPOCH_MICROSEC)
194                 .timeZoneOffset(TIME_ZONE_OFFSET)
195                 .name(PM_FILE_NAME)
196                 .location(SFTP_LOCATION)
197                 .internalLocation(LOCAL_FILE_LOCATION)
198                 .compression(GZIP_COMPRESSION)
199                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
200                 .fileFormatVersion(FILE_FORMAT_VERSION)
201                 .build();
202         // @formatter:on
203         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
204                 .verifyComplete();
205
206         verify(sftpClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
207         verifyNoMoreInteractions(sftpClientMock);
208     }
209
210     @Test
211     public void whenFtpesFileAlwaysFail_retryAndReturnEmpty() {
212         XnfCollectorTaskImpl collectorUndetTest =
213                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
214         collectorUndetTest.setRetryTimer(retryTimerMock);
215         // @formatter:off
216         FileData fileData = ImmutableFileData.builder()
217                 .fileMetaData(fileMetaData)
218                 .name(PM_FILE_NAME)
219                 .location(FTPES_LOCATION)
220                 .compression(GZIP_COMPRESSION)
221                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
222                 .fileFormatVersion(FILE_FORMAT_VERSION)
223                 .build();
224
225         FileServerData fileServerData = ImmutableFileServerData.builder()
226                 .serverAddress(SERVER_ADDRESS)
227                 .userId(USER)
228                 .password(PWD)
229                 .port(PORT_22)
230                 .build();
231         // @formatter:on
232         ErrorData errorData = new ErrorData();
233         errorData.addError("Unable to collect file.", new Exception());
234         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
235                 .thenReturn(new FileCollectResult(errorData));
236         doReturn(new FileCollectResult(errorData)).when(ftpsClientMock).retryCollectFile();
237
238         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
239
240         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
241         verify(ftpsClientMock, times(2)).retryCollectFile();
242     }
243
244     @Test
245     public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() {
246         XnfCollectorTaskImpl collectorUndetTest =
247                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
248         collectorUndetTest.setRetryTimer(retryTimerMock);
249         // @formatter:off
250         FileData fileData = ImmutableFileData.builder()
251                 .fileMetaData(fileMetaData)
252                 .name(PM_FILE_NAME)
253                 .location(FTPES_LOCATION)
254                 .compression(GZIP_COMPRESSION)
255                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
256                 .fileFormatVersion(FILE_FORMAT_VERSION)
257                 .build();
258
259         FileServerData fileServerData = ImmutableFileServerData.builder()
260                 .serverAddress(SERVER_ADDRESS)
261                 .userId(USER)
262                 .password(PWD)
263                 .port(PORT_22)
264                 .build();
265         // @formatter:on
266         ErrorData errorData = new ErrorData();
267         errorData.addError("Unable to collect file.", new Exception());
268         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
269                 .thenReturn(new FileCollectResult(errorData));
270         doReturn(new FileCollectResult()).when(ftpsClientMock).retryCollectFile();
271         // @formatter:off
272         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
273                 .productName(PRODUCT_NAME)
274                 .vendorName(VENDOR_NAME)
275                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
276                 .sourceName(SOURCE_NAME)
277                 .startEpochMicrosec(START_EPOCH_MICROSEC)
278                 .timeZoneOffset(TIME_ZONE_OFFSET)
279                 .name(PM_FILE_NAME)
280                 .location(FTPES_LOCATION)
281                 .internalLocation(LOCAL_FILE_LOCATION)
282                 .compression(GZIP_COMPRESSION)
283                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
284                 .fileFormatVersion(FILE_FORMAT_VERSION)
285                 .build();
286         // @formatter:on
287         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
288                 .verifyComplete();
289
290
291         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
292         verify(ftpsClientMock, times(1)).retryCollectFile();
293     }
294
295     @Test
296     public void whenWrongScheme_returnEmpty() {
297         XnfCollectorTaskImpl collectorUndetTest =
298                 new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
299         // @formatter:off
300         FileData fileData = ImmutableFileData.builder()
301                 .fileMetaData(fileMetaData)
302                 .name(PM_FILE_NAME)
303                 .location("http://host.com/file.zip")
304                 .compression(GZIP_COMPRESSION)
305                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
306                 .fileFormatVersion(FILE_FORMAT_VERSION)
307                 .build();
308
309         FileServerData fileServerData = ImmutableFileServerData.builder()
310                 .serverAddress(SERVER_ADDRESS)
311                 .userId("")
312                 .password("")
313                 .port(PORT_22)
314                 .build();
315         // @formatter:on
316         when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
317                 .thenReturn(new FileCollectResult());
318
319         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
320
321         verifyNoMoreInteractions(sftpClientMock);
322     }
323 }