804b46e969298fe3ac27d572797a719ed69fe96f
[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
71     private static final String FTPES_LOCATION_NO_PORT =
72             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION;
73     private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
74     private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS +  REMOTE_FILE_LOCATION;
75
76     private static final String GZIP_COMPRESSION = "gzip";
77     private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
78     private static final String FILE_FORMAT_VERSION = "V10";
79
80     private static final String FTP_KEY_PATH = "ftpKeyPath";
81     private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
82     private static final String TRUSTED_CA_PATH = "trustedCAPath";
83     private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
84
85     private static AppConfig appConfigMock = mock(AppConfig.class);
86     private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
87
88     private FtpsClient ftpsClientMock = mock(FtpsClient.class);
89
90     private SftpClient sftpClientMock = mock(SftpClient.class);
91
92
93     private MessageMetaData createMessageMetaData() {
94         // @formatter:off
95         return ImmutableMessageMetaData.builder()
96                 .productName(PRODUCT_NAME)
97                 .vendorName(VENDOR_NAME)
98                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
99                 .sourceName(SOURCE_NAME)
100                 .startEpochMicrosec(START_EPOCH_MICROSEC)
101                 .timeZoneOffset(TIME_ZONE_OFFSET)
102                 .changeIdentifier(PM_MEAS_CHANGE_IDENTIFIER)
103                 .changeType(FILE_READY_CHANGE_TYPE)
104                 .build();
105         // @formatter:on
106     }
107
108     private FileData createFileData(String location) {
109         // @formatter:off
110         return  ImmutableFileData.builder()
111             .name(PM_FILE_NAME)
112             .location(location)
113             .compression(GZIP_COMPRESSION)
114             .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
115             .fileFormatVersion(FILE_FORMAT_VERSION)
116             .scheme(Scheme.FTPS)
117             .build();
118         // @formatter:on
119     }
120
121     private ConsumerDmaapModel createExpectedConsumerDmaapModel(String location) {
122         // @formatter:off
123         return ImmutableConsumerDmaapModel.builder()
124             .productName(PRODUCT_NAME)
125             .vendorName(VENDOR_NAME)
126             .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
127             .sourceName(SOURCE_NAME)
128             .startEpochMicrosec(START_EPOCH_MICROSEC)
129             .timeZoneOffset(TIME_ZONE_OFFSET)
130             .name(PM_FILE_NAME)
131             .location(location)
132             .internalLocation(LOCAL_FILE_LOCATION.toString())
133             .compression(GZIP_COMPRESSION)
134             .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
135             .fileFormatVersion(FILE_FORMAT_VERSION)
136             .build();
137       // @formatter:on
138     }
139
140     @BeforeAll
141     public static void setUpConfiguration() {
142         when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
143         when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
144         when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
145         when(ftpesConfigMock.trustedCA()).thenReturn(TRUSTED_CA_PATH);
146         when(ftpesConfigMock.trustedCAPassword()).thenReturn(TRUSTED_CA_PASSWORD);
147     }
148
149     @Test
150     public void whenFtpesFile_returnCorrectResponse() throws Exception {
151         FileCollector collectorUndetTest =
152                 new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock);
153
154         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT);
155
156         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT);
157
158         StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0)))
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         StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0)))
200                 .expectNext(expectedConsumerDmaapModel).verifyComplete();
201
202         verify(sftpClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
203         verifyNoMoreInteractions(sftpClientMock);
204     }
205
206     @Test
207     public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
208         FileCollector collectorUndetTest =
209                 new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock);
210         FileData fileData = createFileData(FTPES_LOCATION);
211         doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock)
212                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
213
214         StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0)))
215                 .expectErrorMessage("Retries exhausted: 3/3").verify();
216
217         verify(ftpsClientMock, times(4)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
218     }
219
220     @Test
221     public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
222         FileCollector collectorUndetTest =
223                 new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock);
224         doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock)
225                 .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
226
227         ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT);
228
229         FileData fileData = createFileData(FTPES_LOCATION_NO_PORT);
230         StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0)))
231                 .expectNext(expectedConsumerDmaapModel).verifyComplete();
232
233         verify(ftpsClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
234     }
235
236 }