2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
4 * Copyright (C) 2020-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.assertj.core.api.Assertions.assertThatThrownBy;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.io.ByteArrayInputStream;
31 import java.io.InputStream;
32 import java.net.URISyntaxException;
33 import java.nio.file.Path;
35 import org.apache.hc.core5.net.URIBuilder;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.ArgumentMatchers;
40 import org.mockito.Mock;
41 import org.mockito.junit.jupiter.MockitoExtension;
42 import org.oran.datafile.exceptions.DatafileTaskException;
43 import org.oran.datafile.model.FileServerData;
45 import reactor.core.publisher.Flux;
46 import reactor.netty.http.client.HttpClientConfig;
48 @ExtendWith(MockitoExtension.class)
49 class DfcHttpClientTest {
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 = 80;
55 private static final String JWT_PASSWORD = "thisIsThePassword";
56 private static String ACCESS_TOKEN = "access_token";
59 private Path pathMock;
61 DfcHttpClient dfcHttpClientSpy;
65 dfcHttpClientSpy = spy(new DfcHttpClient(createFileServerData()));
69 void openConnection_successBasicAuthSetup() throws DatafileTaskException {
70 dfcHttpClientSpy.open();
71 HttpClientConfig config = dfcHttpClientSpy.client.configuration();
72 assertEquals(HttpUtils.basicAuthContent(USERNAME, PASSWORD), config.headers().get("Authorization"));
76 void openConnection_failedBasicAuthSetupThrowException() {
77 FileServerData serverData =
78 FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password("").port(PORT).build();
80 DfcHttpClient dfcHttpClientSpy = spy(new DfcHttpClient(serverData));
82 assertThatThrownBy(() -> dfcHttpClientSpy.open())
83 .hasMessageContaining("Not sufficient basic auth data for file.");
87 void collectFile_AllOk() throws Exception {
88 String REMOTE_FILE = "any";
89 Flux<InputStream> fis = Flux.just(new ByteArrayInputStream("ReturnedString".getBytes()));
91 dfcHttpClientSpy.open();
93 when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis);
94 doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any());
96 dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock);
97 dfcHttpClientSpy.close();
99 verify(dfcHttpClientSpy, times(1)).getServerResponse(REMOTE_FILE);
100 verify(dfcHttpClientSpy, times(1)).processDataFromServer(any(), any(), any());
101 verify(dfcHttpClientSpy, times(1)).isDownloadFailed(any());
105 void collectFile_AllOkWithJWTToken() throws Exception {
106 dfcHttpClientSpy = spy(new DfcHttpClient(fileServerDataWithJWTToken()));
107 String REMOTE_FILE = "any";
108 Flux<InputStream> fis = Flux.just(new ByteArrayInputStream("ReturnedString".getBytes()));
110 dfcHttpClientSpy.open();
111 HttpClientConfig config = dfcHttpClientSpy.client.configuration();
112 assertEquals(HttpUtils.jwtAuthContent(JWT_PASSWORD), config.headers().get("Authorization"));
114 when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis);
115 doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any());
117 dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock);
118 dfcHttpClientSpy.close();
120 verify(dfcHttpClientSpy, times(1)).getServerResponse(ArgumentMatchers.eq(REMOTE_FILE));
121 verify(dfcHttpClientSpy, times(1)).processDataFromServer(any(), any(), any());
122 verify(dfcHttpClientSpy, times(1)).isDownloadFailed(any());
126 void collectFile_No200ResponseWriteToErrorMessage() throws DatafileTaskException {
127 String ERROR_RESPONSE = "This is unexpected message";
128 String REMOTE_FILE = "any";
129 Flux<Throwable> fis = Flux.error(new Throwable(ERROR_RESPONSE));
131 dfcHttpClientSpy.open();
133 doReturn(fis).when(dfcHttpClientSpy).getServerResponse(any());
135 assertThatThrownBy(() -> dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock))
136 .hasMessageContaining(ERROR_RESPONSE);
137 verify(dfcHttpClientSpy, times(1)).getServerResponse(REMOTE_FILE);
138 verify(dfcHttpClientSpy, times(1)).processFailedConnectionWithServer(any(), any());
139 dfcHttpClientSpy.close();
143 void isResponseOk_validateResponse() {
144 assertTrue(dfcHttpClientSpy.isResponseOk(HttpClientResponseHelper.NETTY_RESPONSE_OK));
145 assertFalse(dfcHttpClientSpy.isResponseOk(HttpClientResponseHelper.RESPONSE_ANY_NO_OK));
148 private FileServerData createFileServerData() {
149 return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password(PASSWORD).port(PORT)
153 private FileServerData fileServerDataWithJWTToken() throws URISyntaxException {
154 String query = "?" + ACCESS_TOKEN + "=" + JWT_PASSWORD;
156 return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
157 .queryParameters(new URIBuilder(query).getQueryParams()).build();