b5d3c159803ceb135bbb55acfe19b97858a44e3e
[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.Mockito.doThrow;
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 import java.nio.file.Path;
26 import java.time.Duration;
27 import java.util.HashMap;
28 import java.util.Map;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
32 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
33 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
34 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
35 import org.onap.dcaegen2.collectors.datafile.ftp.Scheme;
36 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
37 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
38 import org.onap.dcaegen2.collectors.datafile.model.FileData;
39 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
40 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
41 import org.onap.dcaegen2.collectors.datafile.model.ImmutableMessageMetaData;
42 import org.onap.dcaegen2.collectors.datafile.model.MessageMetaData;
43 import reactor.test.StepVerifier;
44
45 /**
46  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
47  *
48  */
49 public class XnfCollectorTaskImplTest {
50     private static final String PRODUCT_NAME = "NrRadio";
51     private static final String VENDOR_NAME = "Ericsson";
52     private static final String LAST_EPOCH_MICROSEC = "8745745764578";
53     private static final String SOURCE_NAME = "oteNB5309";
54     private static final String START_EPOCH_MICROSEC = "8745745764578";
55     private static final String TIME_ZONE_OFFSET = "UTC+05:00";
56     private static final String PM_MEAS_CHANGE_IDENTIFIER = "PM_MEAS_FILES";
57     private static final String FILE_READY_CHANGE_TYPE = "FileReady";
58     private static final String FTPES_SCHEME = "ftpes://";
59     private static final String SFTP_SCHEME = "sftp://";
60     private static final String SERVER_ADDRESS = "192.168.0.101";
61     private static final int PORT_22 = 22;
62     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
63     private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
64     private static final Path LOCAL_FILE_LOCATION = FileData.createLocalFileName(SERVER_ADDRESS, PM_FILE_NAME);
65     private static final String USER = "usr";
66     private static final String PWD = "pwd";
67     private static final String FTPES_LOCATION =
68             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
69
70     private static final String FTPES_LOCATION_NO_PORT =
71             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
72     private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
73     private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS +  REMOTE_FILE_LOCATION;
74
75     private static final String GZIP_COMPRESSION = "gzip";
76     private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
77     private static final String FILE_FORMAT_VERSION = "V10";
78
79     private static final String FTP_KEY_PATH = "ftpKeyPath";
80     private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
81     private static final String TRUSTED_CA_PATH = "trustedCAPath";
82     private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
83
84     private static AppConfig appConfigMock = mock(AppConfig.class);
85     private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
86
87     private FtpsClient ftpsClientMock = mock(FtpsClient.class);
88
89     private SftpClient sftpClientMock = mock(SftpClient.class);
90
91
92     private MessageMetaData createMessageMetaData() {
93         // @formatter:off
94         return ImmutableMessageMetaData.builder()
95                 .productName(PRODUCT_NAME)
96                 .vendorName(VENDOR_NAME)
97                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
98                 .sourceName(SOURCE_NAME)
99                 .startEpochMicrosec(START_EPOCH_MICROSEC)
100                 .timeZoneOffset(TIME_ZONE_OFFSET)
101                 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER)
102                 .changeType(FILE_READY_CHANGE_TYPE)
103                 .build();
104         // @formatter:on
105     }
106
107     private FileData createFileData(String location) {
108         // @formatter:off
109         return  ImmutableFileData.builder()
110             .name(PM_FILE_NAME)
111             .location(location)
112             .compression(GZIP_COMPRESSION)
113             .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
114             .fileFormatVersion(FILE_FORMAT_VERSION)
115             .scheme(Scheme.FTPS)
116             .build();
117         // @formatter:on
118     }
119
120     private ConsumerDmaapModel createExpectedConsumerDmaapModel(String location) {
121         // @formatter:off
122         return ImmutableConsumerDmaapModel.builder()
123             .productName(PRODUCT_NAME)
124             .vendorName(VENDOR_NAME)
125             .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
126             .sourceName(SOURCE_NAME)
127             .startEpochMicrosec(START_EPOCH_MICROSEC)
128             .timeZoneOffset(TIME_ZONE_OFFSET)
129             .name(PM_FILE_NAME)
130             .location(location)
131             .internalLocation(LOCAL_FILE_LOCATION.toString())
132             .compression(GZIP_COMPRESSION)
133             .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
134             .fileFormatVersion(FILE_FORMAT_VERSION)
135             .build();
136       // @formatter:on
137     }
138
139     @BeforeAll
140     public static void setUpConfiguration() {
141         when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
142         when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
143         when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
144         when(ftpesConfigMock.trustedCA()).thenReturn(TRUSTED_CA_PATH);
145         when(ftpesConfigMock.trustedCAPassword()).thenReturn(TRUSTED_CA_PASSWORD);
146     }
147
148     @Test
149     public void whenFtpesFile_returnCorrectResponse() throws Exception {
150         FileCollector collectorUndetTest =
151                 new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock);
152
153         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT);
154
155         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT);
156
157         Map<String, String> contextMap = new HashMap<>();
158         StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0), contextMap))
159                 .expectNext(expectedConsumerDmaapModel).verifyComplete();
160
161         verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
162         verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH);
163         verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD);
164         verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH);
165         verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD);
166         verifyNoMoreInteractions(ftpsClientMock);
167     }
168
169     @Test
170     public void whenSftpFile_returnCorrectResponse() throws Exception {
171         FileCollector collectorUndetTest =
172                 new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock);
173         // @formatter:off
174         FileData fileData = ImmutableFileData.builder()
175                 .name(PM_FILE_NAME)
176                 .location(SFTP_LOCATION_NO_PORT)
177                 .compression(GZIP_COMPRESSION)
178                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
179                 .fileFormatVersion(FILE_FORMAT_VERSION)
180                 .scheme(Scheme.SFTP)
181                 .build();
182
183         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder()
184                 .productName(PRODUCT_NAME)
185                 .vendorName(VENDOR_NAME)
186                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
187                 .sourceName(SOURCE_NAME)
188                 .startEpochMicrosec(START_EPOCH_MICROSEC)
189                 .timeZoneOffset(TIME_ZONE_OFFSET)
190                 .name(PM_FILE_NAME)
191                 .location(SFTP_LOCATION_NO_PORT)
192                 .internalLocation(LOCAL_FILE_LOCATION.toString())
193                 .compression(GZIP_COMPRESSION)
194                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
195                 .fileFormatVersion(FILE_FORMAT_VERSION)
196                 .build();
197         // @formatter:on
198
199         Map<String, String> contextMap = new HashMap<>();
200         StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0), contextMap))
201                 .expectNext(expectedConsumerDmaapModel).verifyComplete();
202
203         verify(sftpClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
204         verifyNoMoreInteractions(sftpClientMock);
205     }
206
207     @Test
208     public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
209         FileCollector collectorUndetTest =
210                 new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock);
211         FileData fileData = createFileData(FTPES_LOCATION);
212         doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
213                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
214
215         Map<String, String> contextMap = new HashMap<>();
216         StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 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 =
225                 new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock);
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);
232         Map<String, String> contextMap = new HashMap<>();
233         StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0), contextMap))
234                 .expectNext(expectedConsumerDmaapModel).verifyComplete();
235
236         verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
237     }
238
239 }