99e92bd2094f4a6fd33cc6f199c9139d4155b4e7
[dcaegen2/collectors/datafile.git] /
1 /*-
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
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.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;
28
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.time.Duration;
32 import java.util.HashMap;
33 import java.util.Map;
34
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.exceptions.NonRetryableDatafileTaskException;
41 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
42 import org.onap.dcaegen2.collectors.datafile.ftp.Scheme;
43 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
44 import org.onap.dcaegen2.collectors.datafile.model.Counters;
45 import org.onap.dcaegen2.collectors.datafile.model.FileData;
46 import org.onap.dcaegen2.collectors.datafile.model.FilePublishInformation;
47 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
48 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFilePublishInformation;
49 import org.onap.dcaegen2.collectors.datafile.model.ImmutableMessageMetaData;
50 import org.onap.dcaegen2.collectors.datafile.model.MessageMetaData;
51
52 import reactor.test.StepVerifier;
53
54 public class FileCollectorTest {
55     private static final String PRODUCT_NAME = "NrRadio";
56     private static final String VENDOR_NAME = "Ericsson";
57     private static final String LAST_EPOCH_MICROSEC = "8745745764578";
58     private static final String SOURCE_NAME = "oteNB5309";
59     private static final String START_EPOCH_MICROSEC = "8745745764578";
60     private static final String TIME_ZONE_OFFSET = "UTC+05:00";
61     private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
62     private static final String FILE_READY_CHANGE_TYPE = "FileReady";
63     private static final String FTPES_SCHEME = "ftpes://";
64     private static final String SFTP_SCHEME = "sftp://";
65     private static final String SERVER_ADDRESS = "192.168.0.101";
66     private static final int PORT_22 = 22;
67     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
68     private static final Path LOCAL_FILE_LOCATION = Paths.get(FileData.DATAFILE_TMPDIR, PM_FILE_NAME);
69     private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
70     private static final String USER = "usr";
71     private static final String PWD = "pwd";
72     private static final String FTPES_LOCATION =
73             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
74
75     private static final String FTPES_LOCATION_NO_PORT =
76             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
77     private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
78     private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
79
80     private static final String GZIP_COMPRESSION = "gzip";
81     private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
82     private static final String FILE_FORMAT_VERSION = "V10";
83
84     private static final String FTP_KEY_PATH = "ftpKeyPath";
85     private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
86     private static final String TRUSTED_CA_PATH = "trustedCAPath";
87     private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
88     private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
89
90     private static AppConfig appConfigMock = mock(AppConfig.class);
91     private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
92
93     private FtpsClient ftpsClientMock = mock(FtpsClient.class);
94
95     private SftpClient sftpClientMock = mock(SftpClient.class);
96     private final Map<String, String> contextMap = new HashMap<>();
97
98     private final Counters counters = new Counters();
99
100     private MessageMetaData createMessageMetaData() {
101         return ImmutableMessageMetaData.builder() //
102                 .productName(PRODUCT_NAME) //
103                 .vendorName(VENDOR_NAME) //
104                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
105                 .sourceName(SOURCE_NAME) //
106                 .startEpochMicrosec(START_EPOCH_MICROSEC) //
107                 .timeZoneOffset(TIME_ZONE_OFFSET) //
108                 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER) //
109                 .changeType(FILE_READY_CHANGE_TYPE) //
110                 .build();
111     }
112
113     private FileData createFileData(String location, Scheme scheme) {
114         return ImmutableFileData.builder() //
115                 .name(PM_FILE_NAME) //
116                 .location(location) //
117                 .compression(GZIP_COMPRESSION) //
118                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
119                 .fileFormatVersion(FILE_FORMAT_VERSION) //
120                 .scheme(scheme) //
121                 .messageMetaData(createMessageMetaData()) //
122                 .build();
123     }
124
125     private FilePublishInformation createExpectedFilePublishInformation(String location) {
126         return ImmutableFilePublishInformation.builder() //
127                 .productName(PRODUCT_NAME) //
128                 .vendorName(VENDOR_NAME) //
129                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
130                 .sourceName(SOURCE_NAME) //
131                 .startEpochMicrosec(START_EPOCH_MICROSEC) //
132                 .timeZoneOffset(TIME_ZONE_OFFSET) //
133                 .name(PM_FILE_NAME) //
134                 .location(location) //
135                 .internalLocation(LOCAL_FILE_LOCATION) //
136                 .compression(GZIP_COMPRESSION) //
137                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) //
138                 .fileFormatVersion(FILE_FORMAT_VERSION) //
139                 .context(new HashMap<String, String>()) //
140                 .changeIdentifier(CHANGE_IDENTIFIER) //
141                 .build();
142     }
143
144     /**
145      * Sets up the configuration.
146      */
147     @BeforeAll
148     public static void setUpConfiguration() {
149         when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
150         when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
151         when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
152         when(ftpesConfigMock.trustedCa()).thenReturn(TRUSTED_CA_PATH);
153         when(ftpesConfigMock.trustedCaPassword()).thenReturn(TRUSTED_CA_PASSWORD);
154     }
155
156     @Test
157     public void whenFtpesFile_returnCorrectResponse() throws Exception {
158         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
159         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
160
161         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
162
163         FilePublishInformation expectedfilePublishInformation =
164                 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
165
166         StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
167                 .expectNext(expectedfilePublishInformation) //
168                 .verifyComplete();
169
170         verify(ftpsClientMock, times(1)).open();
171         verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
172         verify(ftpsClientMock, times(1)).close();
173
174         verifyNoMoreInteractions(ftpsClientMock);
175     }
176
177     @Test
178     public void whenSftpFile_returnCorrectResponse() throws Exception {
179         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
180         doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
181
182
183         FileData fileData = createFileData(SFTP_LOCATION_NO_PORT, Scheme.SFTP);
184         FilePublishInformation expectedfilePublishInformation =
185                 createExpectedFilePublishInformation(SFTP_LOCATION_NO_PORT);
186
187         StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
188                 .expectNext(expectedfilePublishInformation) //
189                 .verifyComplete();
190
191         // The same again, but with port
192         fileData = createFileData(SFTP_LOCATION, Scheme.SFTP);
193         expectedfilePublishInformation = createExpectedFilePublishInformation(SFTP_LOCATION);
194
195         StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
196                 .expectNext(expectedfilePublishInformation) //
197                 .verifyComplete();
198
199         verify(sftpClientMock, times(2)).open();
200         verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
201         verify(sftpClientMock, times(2)).close();
202         verifyNoMoreInteractions(sftpClientMock);
203     }
204
205     @Test
206     public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
207         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
208         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
209
210         FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
211         doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
212                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
213
214         StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
215                 .expectErrorMessage("Retries exhausted: 3/3") //
216                 .verify();
217
218         verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
219     }
220
221     @Test
222     public void whenFtpesFileAlwaysFail_failWithoutRetry() throws Exception {
223         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, new Counters()));
224         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
225
226         FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
227         doThrow(new NonRetryableDatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
228                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
229
230         StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
231                 .expectErrorMessage("Non retryable file transfer failure") //
232                 .verify();
233
234         verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
235     }
236
237     @Test
238     public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
239         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
240         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
241         doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock)
242                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
243
244         FilePublishInformation expectedfilePublishInformation =
245                 createExpectedFilePublishInformation(FTPES_LOCATION_NO_PORT);
246
247         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
248
249         StepVerifier.create(collectorUndetTest.collectFile(fileData, 3, Duration.ofSeconds(0), contextMap))
250                 .expectNext(expectedfilePublishInformation) //
251                 .verifyComplete();
252
253         verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
254     }
255 }