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