6e17f27bcbb7a6c6693dbe0a77a554619815b0f9
[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.time.Duration;
31 import java.util.HashMap;
32 import java.util.Map;
33
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
49 import reactor.test.StepVerifier;
50
51 /**
52  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
53  *
54  */
55 public class FileCollectorTest {
56     private static final String PRODUCT_NAME = "NrRadio";
57     private static final String VENDOR_NAME = "Ericsson";
58     private static final String LAST_EPOCH_MICROSEC = "8745745764578";
59     private static final String SOURCE_NAME = "oteNB5309";
60     private static final String START_EPOCH_MICROSEC = "8745745764578";
61     private static final String TIME_ZONE_OFFSET = "UTC+05:00";
62     private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
63     private static final String FILE_READY_CHANGE_TYPE = "FileReady";
64     private static final String FTPES_SCHEME = "ftpes://";
65     private static final String SFTP_SCHEME = "sftp://";
66     private static final String SERVER_ADDRESS = "192.168.0.101";
67     private static final int PORT_22 = 22;
68     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
69     private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
70     private static final Path LOCAL_FILE_LOCATION = FileData.createLocalFileName(SOURCE_NAME, PM_FILE_NAME);
71     private static final String USER = "usr";
72     private static final String PWD = "pwd";
73     private static final String FTPES_LOCATION =
74             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
75
76     private static final String FTPES_LOCATION_NO_PORT =
77             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
78     private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
79     private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
80
81     private static final String GZIP_COMPRESSION = "gzip";
82     private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
83     private static final String FILE_FORMAT_VERSION = "V10";
84
85     private static final String FTP_KEY_PATH = "ftpKeyPath";
86     private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
87     private static final String TRUSTED_CA_PATH = "trustedCAPath";
88     private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
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
99     private MessageMetaData createMessageMetaData() {
100         // @formatter:off
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         // @formatter:on
112     }
113
114     private FileData createFileData(String location, Scheme scheme) {
115         // @formatter:off
116         return  ImmutableFileData.builder()
117             .name(PM_FILE_NAME)
118             .location(location)
119             .compression(GZIP_COMPRESSION)
120             .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
121             .fileFormatVersion(FILE_FORMAT_VERSION)
122             .scheme(scheme)
123             .messageMetaData(createMessageMetaData())
124             .build();
125         // @formatter:on
126     }
127
128     private ConsumerDmaapModel createExpectedConsumerDmaapModel(String location) {
129         // @formatter:off
130         return ImmutableConsumerDmaapModel.builder()
131             .productName(PRODUCT_NAME)
132             .vendorName(VENDOR_NAME)
133             .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
134             .sourceName(SOURCE_NAME)
135             .startEpochMicrosec(START_EPOCH_MICROSEC)
136             .timeZoneOffset(TIME_ZONE_OFFSET)
137             .name(PM_FILE_NAME)
138             .location(location)
139             .internalLocation(LOCAL_FILE_LOCATION)
140             .compression(GZIP_COMPRESSION)
141             .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
142             .fileFormatVersion(FILE_FORMAT_VERSION)
143             .build();
144       // @formatter:on
145     }
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));
159         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
160
161         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
162
163         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT);
164
165         StepVerifier.create(
166                 collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0), contextMap))
167                 .expectNext(expectedConsumerDmaapModel).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));
179         doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
180
181
182         FileData fileData = createFileData(SFTP_LOCATION_NO_PORT, Scheme.SFTP);
183         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(SFTP_LOCATION_NO_PORT);
184
185         StepVerifier
186                 .create(collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0),
187                         contextMap))
188                 .expectNext(expectedConsumerDmaapModel) //
189                 .verifyComplete();
190
191         // The same again, but with port
192         fileData = createFileData(SFTP_LOCATION, Scheme.SFTP);
193         expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(SFTP_LOCATION);
194
195         StepVerifier
196                 .create(collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0),
197                         contextMap))
198                 .expectNext(expectedConsumerDmaapModel) //
199                 .verifyComplete();
200
201         verify(sftpClientMock, times(2)).open();
202         verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
203         verify(sftpClientMock, times(2)).close();
204         verifyNoMoreInteractions(sftpClientMock);
205     }
206
207     @Test
208     public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
209         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
210         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
211
212         FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS);
213         doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
214                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
215
216         StepVerifier.create(
217                 collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0), contextMap))
218                 .expectErrorMessage("Retries exhausted: 3/3").verify();
219
220         verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
221     }
222
223     @Test
224     public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
225         FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock));
226         doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any());
227         doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock)
228                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
229
230         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT);
231
232         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS);
233
234         StepVerifier.create(
235                 collectorUndetTest.execute(fileData, 3, Duration.ofSeconds(0), contextMap))
236                 .expectNext(expectedConsumerDmaapModel).verifyComplete();
237
238         verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
239     }
240 }