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.nio.file.Path;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.oran.datafile.exceptions.DatafileTaskException;
40 import org.oran.datafile.model.FileServerData;
41 import org.oran.datafile.oauth2.SecurityContext;
43 import reactor.core.publisher.Flux;
44 import reactor.netty.http.client.HttpClientConfig;
46 @ExtendWith(MockitoExtension.class)
47 class DfcHttpClientTest {
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 = 80;
55 private Path pathMock;
57 DfcHttpClient dfcHttpClientSpy;
61 SecurityContext ctx = new SecurityContext("");
62 dfcHttpClientSpy = spy(new DfcHttpClient(ctx, createFileServerData()));
66 void openConnection_successBasicAuthSetup() throws DatafileTaskException {
67 dfcHttpClientSpy.open();
68 HttpClientConfig config = dfcHttpClientSpy.client.configuration();
69 assertEquals(HttpUtils.basicAuthContent(USERNAME, PASSWORD), config.headers().get("Authorization"));
73 void collectFile_AllOk() throws Exception {
74 String REMOTE_FILE = "any";
75 Flux<InputStream> fis = Flux.just(new ByteArrayInputStream("ReturnedString".getBytes()));
77 dfcHttpClientSpy.open();
79 when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis);
80 doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any());
82 dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock);
83 dfcHttpClientSpy.close();
85 verify(dfcHttpClientSpy, times(1)).getServerResponse(REMOTE_FILE);
86 verify(dfcHttpClientSpy, times(1)).processDataFromServer(any(), any(), any());
87 verify(dfcHttpClientSpy, times(1)).isDownloadFailed(any());
91 void collectFile_No200ResponseWriteToErrorMessage() throws DatafileTaskException {
92 String ERROR_RESPONSE = "This is unexpected message";
93 String REMOTE_FILE = "any";
94 Flux<Throwable> fis = Flux.error(new Throwable(ERROR_RESPONSE));
96 dfcHttpClientSpy.open();
98 doReturn(fis).when(dfcHttpClientSpy).getServerResponse(any());
100 assertThatThrownBy(() -> dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock))
101 .hasMessageContaining(ERROR_RESPONSE);
102 verify(dfcHttpClientSpy, times(1)).getServerResponse(REMOTE_FILE);
103 verify(dfcHttpClientSpy, times(1)).processFailedConnectionWithServer(any(), any());
104 dfcHttpClientSpy.close();
108 void isResponseOk_validateResponse() {
109 assertTrue(dfcHttpClientSpy.isResponseOk(HttpClientResponseHelper.NETTY_RESPONSE_OK));
110 assertFalse(dfcHttpClientSpy.isResponseOk(HttpClientResponseHelper.RESPONSE_ANY_NO_OK));
113 private FileServerData createFileServerData() {
114 return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password(PASSWORD).port(PORT)