8251a65a3f05077727031895f2b4e2ad680ffda5
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 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.doReturn;
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.io.File;
27
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
31 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
32 import org.onap.dcaegen2.collectors.datafile.ftp.ErrorData;
33 import org.onap.dcaegen2.collectors.datafile.ftp.FileCollectResult;
34 import org.onap.dcaegen2.collectors.datafile.ftp.FileServerData;
35 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
36 import org.onap.dcaegen2.collectors.datafile.ftp.ImmutableFileServerData;
37 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
38 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
39 import org.onap.dcaegen2.collectors.datafile.model.FileData;
40 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
41 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
42
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
51     private static final String PM_MEAS_CHANGE_IDINTIFIER = "PM_MEAS_FILES";
52     private static final String FILE_READY_CHANGE_TYPE = "FileReady";
53     private static final String FTPES_SCHEME = "ftpes://";
54     private static final String SFTP_SCHEME = "sftp://";
55     private static final String SERVER_ADDRESS = "192.168.0.101";
56     private static final int PORT_22 = 22;
57     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
58     private static final String REMOTE_FILE_LOCATION = "/ftp/rop/" + PM_FILE_NAME;
59     private static final String LOCAL_FILE_LOCATION = "target" + File.separator + PM_FILE_NAME;
60     private static final String USER = "usr";
61     private static final String PWD = "pwd";
62     private static final String FTPES_LOCATION =
63             FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
64     private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION;
65     private static final String GZIP_COMPRESSION = "gzip";
66     private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
67     private static final String FILE_FORMAT_VERSION = "V10";
68
69     private static final String FTP_KEY_PATH = "ftpKeyPath";
70     private static final String FTP_KEY_PASSWORD = "ftpKeyPassword";
71     private static final String TRUSTED_CA_PATH = "trustedCAPath";
72     private static final String TRUSTED_CA_PASSWORD = "trustedCAPassword";
73
74     private static AppConfig appConfigMock = mock(AppConfig.class);
75     private static FtpesConfig ftpesConfigMock = mock(FtpesConfig.class);
76
77     private FtpsClient ftpsClientMock = mock(FtpsClient.class);
78
79     private SftpClient sftpClientMock = mock(SftpClient.class);
80     private RetryTimer retryTimerMock = mock(RetryTimer.class);
81
82
83     @BeforeAll
84     public static void setUpConfiguration() {
85         when(appConfigMock.getFtpesConfiguration()).thenReturn(ftpesConfigMock);
86         when(ftpesConfigMock.keyCert()).thenReturn(FTP_KEY_PATH);
87         when(ftpesConfigMock.keyPassword()).thenReturn(FTP_KEY_PASSWORD);
88         when(ftpesConfigMock.trustedCA()).thenReturn(TRUSTED_CA_PATH);
89         when(ftpesConfigMock.trustedCAPassword()).thenReturn(TRUSTED_CA_PASSWORD);
90     }
91
92     @Test
93     public void whenFtpesFile_returnCorrectResponse() {
94         XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
95
96         FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
97                 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(FTPES_LOCATION)
98                 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
99                 .fileFormatVersion(FILE_FORMAT_VERSION).build();
100
101         FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId(USER)
102                 .password(PWD).port(PORT_22).build();
103         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
104                 .thenReturn(new FileCollectResult());
105
106         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder().name(PM_FILE_NAME)
107                 .location(LOCAL_FILE_LOCATION).compression(GZIP_COMPRESSION)
108                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
109
110         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
111                 .verifyComplete();
112
113         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
114         verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH);
115         verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD);
116         verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH);
117         verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD);
118         verifyNoMoreInteractions(ftpsClientMock);
119     }
120
121     @Test
122     public void whenSftpFile_returnCorrectResponse() {
123         XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
124
125         FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
126                 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(SFTP_LOCATION)
127                 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
128                 .fileFormatVersion(FILE_FORMAT_VERSION).build();
129
130         FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId("")
131                 .password("").port(PORT_22).build();
132         when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
133                 .thenReturn(new FileCollectResult());
134
135         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder().name(PM_FILE_NAME)
136                 .location(LOCAL_FILE_LOCATION).compression(GZIP_COMPRESSION)
137                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
138
139         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
140                 .verifyComplete();
141
142         verify(sftpClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
143         verifyNoMoreInteractions(sftpClientMock);
144     }
145
146     @Test
147     public void whenFtpesFileAlwaysFail_retryAndReturnEmpty() {
148         XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
149         collectorUndetTest.setRetryTimer(retryTimerMock);
150
151         FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
152                 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(FTPES_LOCATION)
153                 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
154                 .fileFormatVersion(FILE_FORMAT_VERSION).build();
155
156         FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId(USER)
157                 .password(PWD).port(PORT_22).build();
158         ErrorData errorData = new ErrorData();
159         errorData.addError("Unable to collect file.", new Exception());
160         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
161                 .thenReturn(new FileCollectResult(errorData));
162         doReturn(new FileCollectResult(errorData)).when(ftpsClientMock).retryCollectFile();
163
164         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
165
166         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
167         verify(ftpsClientMock, times(2)).retryCollectFile();
168     }
169
170     @Test
171     public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() {
172         XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
173         collectorUndetTest.setRetryTimer(retryTimerMock);
174
175         FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
176                 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location(FTPES_LOCATION)
177                 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
178                 .fileFormatVersion(FILE_FORMAT_VERSION).build();
179
180         FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId(USER)
181                 .password(PWD).port(PORT_22).build();
182         ErrorData errorData = new ErrorData();
183         errorData.addError("Unable to collect file.", new Exception());
184         when(ftpsClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
185         .thenReturn(new FileCollectResult(errorData));
186         doReturn(new FileCollectResult()).when(ftpsClientMock).retryCollectFile();
187
188         ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder().name(PM_FILE_NAME)
189                 .location(LOCAL_FILE_LOCATION).compression(GZIP_COMPRESSION)
190                 .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
191
192         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNext(expectedConsumerDmaapModel)
193                 .verifyComplete();
194
195
196         verify(ftpsClientMock, times(1)).collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);
197         verify(ftpsClientMock, times(1)).retryCollectFile();
198     }
199
200     @Test
201     public void whenWrongScheme_returnEmpty() {
202         XnfCollectorTaskImpl collectorUndetTest = new XnfCollectorTaskImpl(appConfigMock, ftpsClientMock, sftpClientMock);
203
204         FileData fileData = ImmutableFileData.builder().changeIdentifier(PM_MEAS_CHANGE_IDINTIFIER)
205                 .changeType(FILE_READY_CHANGE_TYPE).name(PM_FILE_NAME).location("http://host.com/file.zip")
206                 .compression(GZIP_COMPRESSION).fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE)
207                 .fileFormatVersion(FILE_FORMAT_VERSION).build();
208
209         FileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(SERVER_ADDRESS).userId("")
210                 .password("").port(PORT_22).build();
211         when(sftpClientMock.collectFile(fileServerData, REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION))
212                 .thenReturn(new FileCollectResult());
213
214         StepVerifier.create(collectorUndetTest.execute(fileData)).expectNextCount(0).verifyComplete();
215
216         verifyNoMoreInteractions(sftpClientMock);
217     }
218 }