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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
15 * ============LICENSE_END========================================================================
17 package org.oran.datafile.http;
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;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.net.URISyntaxException;
32 import java.nio.file.Path;
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.oran.datafile.exceptions.DatafileTaskException;
45 import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
46 import org.oran.datafile.model.FileServerData;
47 import org.oran.datafile.oauth2.SecurityContext;
49 @ExtendWith(MockitoExtension.class)
50 class DfcHttpsClientTest {
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";
61 private PoolingHttpClientConnectionManager connectionManager;
63 private Path localFile;
65 DfcHttpsClient dfcHttpsClientSpy;
69 SecurityContext ctx = new SecurityContext("");
70 dfcHttpsClientSpy = spy(new DfcHttpsClient(ctx, createFileServerData(), connectionManager));
74 void dfcHttpsClient_flow_successfulCallAndResponseProcessing() throws Exception {
75 doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
76 .executeHttpClient(any(HttpGet.class));
77 doReturn((long) 3).when(dfcHttpsClientSpy).writeFile(eq(localFile), any(InputStream.class));
79 dfcHttpsClientSpy.open();
80 dfcHttpsClientSpy.collectFile(remoteFile, localFile);
81 dfcHttpsClientSpy.close();
83 verify(dfcHttpsClientSpy, times(1)).makeCall(any(HttpGet.class));
84 verify(dfcHttpsClientSpy, times(1)).executeHttpClient(any(HttpGet.class));
85 verify(dfcHttpsClientSpy, times(1)).processResponse(HttpClientResponseHelper.APACHE_RESPONSE_OK, localFile);
86 verify(dfcHttpsClientSpy, times(1)).writeFile(eq(localFile), any(InputStream.class));
90 void dfcHttpsClient_flow_successfulCallWithJWTAndResponseProcessing() throws Exception {
91 FileServerData serverData = jWTTokenInFileServerData();
92 SecurityContext ctx = new SecurityContext("");
93 dfcHttpsClientSpy = spy(new DfcHttpsClient(ctx, serverData, connectionManager));
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));
99 dfcHttpsClientSpy.open();
100 dfcHttpsClientSpy.collectFile(remoteFile, localFile);
101 dfcHttpsClientSpy.close();
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 String str = serverData.toString();
108 assertFalse(str.contains(JWT_PASSWORD));
112 void dfcHttpsClient_flow_failedCallUnexpectedResponseCode() throws Exception {
113 doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
114 .executeHttpClient(any(HttpGet.class));
115 doReturn(false).when(dfcHttpsClientSpy).isResponseOk(any(HttpResponse.class));
117 dfcHttpsClientSpy.open();
119 assertThrows(DatafileTaskException.class, () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
123 void dfcHttpsClient_flow_failedCallConnectionTimeout() throws Exception {
124 doThrow(ConnectTimeoutException.class).when(dfcHttpsClientSpy).executeHttpClient(any(HttpGet.class));
126 dfcHttpsClientSpy.open();
128 assertThrows(NonRetryableDatafileTaskException.class,
129 () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
133 void dfcHttpsClient_flow_failedCallIOExceptionForExecuteHttpClient() throws Exception {
134 doThrow(IOException.class).when(dfcHttpsClientSpy).executeHttpClient(any(HttpGet.class));
136 dfcHttpsClientSpy.open();
138 assertThrows(DatafileTaskException.class, () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
141 private FileServerData createFileServerData() {
142 return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password(PASSWORD).port(PORT)
146 private FileServerData emptyUserInFileServerData() {
147 return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT).build();
150 private FileServerData invalidUserInFileServerData() {
151 return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password("").port(PORT).build();
154 private FileServerData jWTTokenInFileServerData() throws URISyntaxException {
155 String query = "?" + ACCESS_TOKEN + "=" + JWT_PASSWORD;
157 return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
158 .queryParameters(new URIBuilder(query).getQueryParams()).build();