8df91d3a084f0e8534ce0ccb7033a19fca117da2
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2021 Nokia. 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 package org.onap.dcaegen2.collectors.datafile.http;
17
18
19 import org.apache.http.HttpResponse;
20 import org.apache.http.client.methods.HttpGet;
21 import org.apache.http.conn.ConnectTimeoutException;
22 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.onap.dcaegen2.collectors.datafile.commons.ImmutableFileServerData;
29 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
30 import org.onap.dcaegen2.collectors.datafile.exceptions.NonRetryableDatafileTaskException;
31
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.file.Path;
35
36 import static org.junit.jupiter.api.Assertions.assertEquals;
37 import static org.junit.jupiter.api.Assertions.assertThrows;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.ArgumentMatchers.eq;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.doThrow;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
45
46 @ExtendWith(MockitoExtension.class)
47 class DfcHttpsClientTest {
48
49     private static final String USERNAME = "bob";
50     private static final String PASSWORD = "123";
51     private static final String XNF_ADDRESS = "127.0.0.1";
52     private static final int PORT = 443;
53     private static String remoteFile = "remoteFile";
54
55     @Mock
56     private PoolingHttpClientConnectionManager connectionManager;
57     @Mock
58     private Path localFile;
59
60     DfcHttpsClient dfcHttpsClientSpy;
61
62     @BeforeEach
63     public void setup() {
64         dfcHttpsClientSpy = spy(new DfcHttpsClient(createFileServerData(), connectionManager));
65     }
66
67     @Test
68     void fileServerData_properLocationBasicAuth() throws Exception {
69         boolean result  = dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow();
70         assertEquals(true, result);
71     }
72
73     @Test
74     void fileServerData_properLocationNoBasicAuth() throws Exception {
75         dfcHttpsClientSpy = spy(new DfcHttpsClient(emptyUserInFileServerData(), connectionManager));
76
77         boolean result  = dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow();
78         assertEquals(false, result);
79     }
80
81     @Test
82     void fileServerData_improperAuthDataExceptionOccurred() throws Exception {
83         dfcHttpsClientSpy = spy(new DfcHttpsClient(invalidUserInFileServerData(), connectionManager));
84
85         assertThrows(DatafileTaskException.class, () -> dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow());
86     }
87
88     @Test
89     void dfcHttpsClient_flow_successfulCallAndResponseProcessing() throws Exception {
90         doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
91             .executeHttpClient(any(HttpGet.class));
92         doReturn((long)3).when(dfcHttpsClientSpy).writeFile(eq(localFile), any(InputStream.class));
93
94         dfcHttpsClientSpy.open();
95         dfcHttpsClientSpy.collectFile(remoteFile, localFile);
96         dfcHttpsClientSpy.close();
97
98         verify(dfcHttpsClientSpy, times(1)).makeCall(any(HttpGet.class));
99         verify(dfcHttpsClientSpy, times(1))
100             .executeHttpClient(any(HttpGet.class));
101         verify(dfcHttpsClientSpy, times(1))
102             .processResponse(HttpClientResponseHelper.APACHE_RESPONSE_OK, localFile);
103         verify(dfcHttpsClientSpy, times(1))
104             .writeFile(eq(localFile), any(InputStream.class));
105     }
106
107     @Test
108     void dfcHttpsClient_flow_failedCallUnexpectedResponseCode() throws Exception {
109         doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
110             .executeHttpClient(any(HttpGet.class));
111         doReturn(false).when(dfcHttpsClientSpy).isResponseOk(any(HttpResponse.class));
112
113         dfcHttpsClientSpy.open();
114
115         assertThrows(NonRetryableDatafileTaskException.class,
116                 () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
117     }
118
119     @Test
120     void dfcHttpsClient_flow_failedCallConnectionTimeout() throws Exception {
121         doThrow(ConnectTimeoutException.class).when(dfcHttpsClientSpy)
122             .executeHttpClient(any(HttpGet.class));
123
124         dfcHttpsClientSpy.open();
125
126         assertThrows(NonRetryableDatafileTaskException.class,
127                 () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
128     }
129
130     @Test
131     void dfcHttpsClient_flow_failedCallIOExceptionForExecuteHttpClient() throws Exception {
132         doThrow(IOException.class).when(dfcHttpsClientSpy)
133             .executeHttpClient(any(HttpGet.class));
134
135         dfcHttpsClientSpy.open();
136
137         assertThrows(DatafileTaskException.class,
138                 () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
139     }
140
141     private ImmutableFileServerData createFileServerData() {
142         return ImmutableFileServerData.builder()
143                 .serverAddress(XNF_ADDRESS)
144                 .userId(USERNAME).password(PASSWORD)
145                 .port(PORT)
146                 .build();
147     }
148
149     private ImmutableFileServerData emptyUserInFileServerData() {
150         return ImmutableFileServerData.builder()
151                 .serverAddress(XNF_ADDRESS)
152                 .userId("").password("")
153                 .port(PORT)
154                 .build();
155     }
156
157     private ImmutableFileServerData invalidUserInFileServerData() {
158         return ImmutableFileServerData.builder()
159                 .serverAddress(XNF_ADDRESS)
160                 .userId("demo").password("")
161                 .port(PORT)
162                 .build();
163     }
164 }