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