Added support for using oauth token for Kafka
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / http / DfcHttpsClientTest.java
1 /*-
2  * ============LICENSE_START======================================================================
3  *  Copyright (C) 2023 Nordix Foundation. All rights reserved.
4  * Copyright (C) 2021 Nokia. All rights reserved.
5  * ===============================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  * in compliance with the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  * ============LICENSE_END========================================================================
16  */
17 package org.oran.datafile.http;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.net.URISyntaxException;
33 import java.nio.file.Path;
34
35 import org.apache.hc.core5.net.URIBuilder;
36 import org.apache.http.HttpResponse;
37 import org.apache.http.client.methods.HttpGet;
38 import org.apache.http.conn.ConnectTimeoutException;
39 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.mockito.Mock;
44 import org.mockito.junit.jupiter.MockitoExtension;
45 import org.oran.datafile.commons.FileServerData;
46 import org.oran.datafile.exceptions.DatafileTaskException;
47 import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
48
49 @ExtendWith(MockitoExtension.class)
50 class DfcHttpsClientTest {
51
52     private static final String USERNAME = "bob";
53     private static final String PASSWORD = "123";
54     private static final String XNF_ADDRESS = "127.0.0.1";
55     private static final int PORT = 443;
56     private static final String JWT_PASSWORD = "thisIsThePassword";
57     private static String ACCESS_TOKEN = "access_token";
58     private static String remoteFile = "remoteFile";
59
60     @Mock
61     private PoolingHttpClientConnectionManager connectionManager;
62     @Mock
63     private Path localFile;
64
65     DfcHttpsClient dfcHttpsClientSpy;
66
67     @BeforeEach
68     public void setup() {
69         dfcHttpsClientSpy = spy(new DfcHttpsClient(createFileServerData(), connectionManager));
70     }
71
72     @Test
73     void fileServerData_properLocationBasicAuth() throws Exception {
74         boolean result = dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow();
75         assertEquals(true, result);
76     }
77
78     @Test
79     void fileServerData_properLocationNoBasicAuth() throws Exception {
80         dfcHttpsClientSpy = spy(new DfcHttpsClient(emptyUserInFileServerData(), connectionManager));
81
82         boolean result = dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow();
83         assertEquals(false, result);
84     }
85
86     @Test
87     void fileServerData_improperAuthDataExceptionOccurred() throws Exception {
88         dfcHttpsClientSpy = spy(new DfcHttpsClient(invalidUserInFileServerData(), connectionManager));
89
90         assertThrows(DatafileTaskException.class, () -> dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow());
91     }
92
93     @Test
94     void dfcHttpsClient_flow_successfulCallAndResponseProcessing() throws Exception {
95         doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
96             .executeHttpClient(any(HttpGet.class));
97         doReturn((long) 3).when(dfcHttpsClientSpy).writeFile(eq(localFile), any(InputStream.class));
98
99         dfcHttpsClientSpy.open();
100         dfcHttpsClientSpy.collectFile(remoteFile, localFile);
101         dfcHttpsClientSpy.close();
102
103         verify(dfcHttpsClientSpy, times(1)).makeCall(any(HttpGet.class));
104         verify(dfcHttpsClientSpy, times(1)).executeHttpClient(any(HttpGet.class));
105         verify(dfcHttpsClientSpy, times(1)).processResponse(HttpClientResponseHelper.APACHE_RESPONSE_OK, localFile);
106         verify(dfcHttpsClientSpy, times(1)).writeFile(eq(localFile), any(InputStream.class));
107     }
108
109     @Test
110     void dfcHttpsClient_flow_successfulCallWithJWTAndResponseProcessing() throws Exception {
111         FileServerData serverData = jWTTokenInFileServerData();
112         dfcHttpsClientSpy = spy(new DfcHttpsClient(serverData, connectionManager));
113
114         doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
115             .executeHttpClient(any(HttpGet.class));
116         doReturn((long) 3).when(dfcHttpsClientSpy).writeFile(eq(localFile), any(InputStream.class));
117
118         dfcHttpsClientSpy.open();
119         dfcHttpsClientSpy.collectFile(remoteFile, localFile);
120         dfcHttpsClientSpy.close();
121
122         verify(dfcHttpsClientSpy, times(1)).makeCall(any(HttpGet.class));
123         verify(dfcHttpsClientSpy, times(1)).executeHttpClient(any(HttpGet.class));
124         verify(dfcHttpsClientSpy, times(1)).processResponse(HttpClientResponseHelper.APACHE_RESPONSE_OK, localFile);
125         verify(dfcHttpsClientSpy, times(1)).writeFile(eq(localFile), any(InputStream.class));
126         String str = serverData.toString();
127         assertFalse(str.contains(JWT_PASSWORD));
128     }
129
130     @Test
131     void dfcHttpsClient_flow_failedCallUnexpectedResponseCode() throws Exception {
132         doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
133             .executeHttpClient(any(HttpGet.class));
134         doReturn(false).when(dfcHttpsClientSpy).isResponseOk(any(HttpResponse.class));
135
136         dfcHttpsClientSpy.open();
137
138         assertThrows(DatafileTaskException.class, () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
139     }
140
141     @Test
142     void dfcHttpsClient_flow_failedCallConnectionTimeout() throws Exception {
143         doThrow(ConnectTimeoutException.class).when(dfcHttpsClientSpy).executeHttpClient(any(HttpGet.class));
144
145         dfcHttpsClientSpy.open();
146
147         assertThrows(NonRetryableDatafileTaskException.class,
148             () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
149     }
150
151     @Test
152     void dfcHttpsClient_flow_failedCallIOExceptionForExecuteHttpClient() throws Exception {
153         doThrow(IOException.class).when(dfcHttpsClientSpy).executeHttpClient(any(HttpGet.class));
154
155         dfcHttpsClientSpy.open();
156
157         assertThrows(DatafileTaskException.class, () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
158     }
159
160     private FileServerData createFileServerData() {
161         return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password(PASSWORD).port(PORT)
162             .build();
163     }
164
165     private FileServerData emptyUserInFileServerData() {
166         return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT).build();
167     }
168
169     private FileServerData invalidUserInFileServerData() {
170         return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password("").port(PORT).build();
171     }
172
173     private FileServerData jWTTokenInFileServerData() throws URISyntaxException {
174         String query = "?" + ACCESS_TOKEN + "=" + JWT_PASSWORD;
175
176         return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
177             .queryParameters(new URIBuilder(query).getQueryParams()).build();
178     }
179 }