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