8382717746dd7b1e1dd1ba333d9db22d89817f2b
[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 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
37 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
38 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
39 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
40 import org.onap.dcaegen2.collectors.datafile.ftp.Scheme;
41 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
42 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
43 import org.onap.dcaegen2.collectors.datafile.model.FileData;
44 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
45 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
46 import org.onap.dcaegen2.collectors.datafile.model.ImmutableMessageMetaData;
47 import org.onap.dcaegen2.collectors.datafile.model.MessageMetaData;
48 import reactor.test.StepVerifier;
49
50 /**
51  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
52  *
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 String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
69     private static final Path LOCAL_FILE_LOCATION = Paths.get(FileData.DATAFILE_TMPDIR, 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
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
98     private MessageMetaData createMessageMetaData() {
99         // @formatter:off
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         // @formatter:on
111     }
112
113     private FileData createFileData(String location, Scheme scheme) {
114         // @formatter:off
115         return  ImmutableFileData.builder()
116             .name(PM_FILE_NAME)
117             .location(location)
118             .compression(GZIP_COMPRESSION)
119             .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
120             .fileFormatVersion(FILE_FORMAT_VERSION)
121             .scheme(scheme)
122             .messageMetaData(createMessageMetaData())
123             .build();
124         // @formatter:on
125     }
126
127     private ConsumerDmaapModel createExpectedConsumerDmaapModel(String location) {
128         // @formatter:off
129         return ImmutableConsumerDmaapModel.builder()
130             .productName(PRODUCT_NAME)
131             .vendorName(VENDOR_NAME)
132             .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
133             .sourceName(SOURCE_NAME)
134             .startEpochMicrosec(START_EPOCH_MICROSEC)
135             .timeZoneOffset(TIME_ZONE_OFFSET)
136             .name(PM_FILE_NAME)
137             .location(location)
138             .internalLocation(LOCAL_FILE_LOCATION)
139             .compression(GZIP_COMPRESSION)
140             .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
141             .fileFormatVersion(FILE_FORMAT_VERSION)
142             .build();
143       // @formatter:on
144     }
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));
158         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
159
160         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
161
162         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT);
163
164         StepVerifier.create(
165                 collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0), contextMap))
166                 .expectNext(expectedConsumerDmaapModel).verifyComplete();
167
168         verify(ftpsClientMock, times(1)).open();
169         verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
170         verify(ftpsClientMock, times(1)).close();
171
172         verifyNoMoreInteractions(ftpsClientMock);
173     }
174
175     @Test
176     public void whenSftpFile_returnCorrectResponse() throws Exception {
177         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
178         doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
179
180
181         FileData fileData = createFileData(SFTP_LOCATION_NO_PORT, Scheme.SFTP);
182         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(SFTP_LOCATION_NO_PORT);
183
184         StepVerifier
185                 .create(collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0),
186                         contextMap))
187                 .expectNext(expectedConsumerDmaapModel) //
188                 .verifyComplete();
189
190         // The same again, but with port
191         fileData = createFileData(SFTP_LOCATION, Scheme.SFTP);
192         expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(SFTP_LOCATION);
193
194         StepVerifier
195                 .create(collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0),
196                         contextMap))
197                 .expectNext(expectedConsumerDmaapModel) //
198                 .verifyComplete();
199
200         verify(sftpClientMock, times(2)).open();
201         verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
202         verify(sftpClientMock, times(2)).close();
203         verifyNoMoreInteractions(sftpClientMock);
204     }
205
206     @Test
207     public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
208         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
209         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
210
211         FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
212         doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
213                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
214
215         StepVerifier.create(
216                 collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0), contextMap))
217                 .expectErrorMessage("Retries exhausted: 3/3").verify();
218
219         verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
220     }
221
222     @Test
223     public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
224         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
225         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
226         doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock)
227                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
228
229         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT);
230
231         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
232
233         StepVerifier.create(
234                 collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0), contextMap))
235                 .expectNext(expectedConsumerDmaapModel).verifyComplete();
236
237         verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
238     }
239 }