DFC shall provide a bearer authorization token in HTTP
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / oran / datafile / http / DfcHttpClientTest.java
1 /*-
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
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.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;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.InputStream;
32 import java.nio.file.Path;
33
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;
42
43 import reactor.core.publisher.Flux;
44 import reactor.netty.http.client.HttpClientConfig;
45
46 @ExtendWith(MockitoExtension.class)
47 class DfcHttpClientTest {
48
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;
53
54     @Mock
55     private Path pathMock;
56
57     DfcHttpClient dfcHttpClientSpy;
58
59     @BeforeEach
60     public void setup() {
61         SecurityContext ctx = new SecurityContext("");
62         dfcHttpClientSpy = spy(new DfcHttpClient(ctx, createFileServerData()));
63     }
64
65     @Test
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"));
70     }
71
72     @Test
73     void collectFile_AllOk() throws Exception {
74         String REMOTE_FILE = "any";
75         Flux<InputStream> fis = Flux.just(new ByteArrayInputStream("ReturnedString".getBytes()));
76
77         dfcHttpClientSpy.open();
78
79         when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis);
80         doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any());
81
82         dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock);
83         dfcHttpClientSpy.close();
84
85         verify(dfcHttpClientSpy, times(1)).getServerResponse(REMOTE_FILE);
86         verify(dfcHttpClientSpy, times(1)).processDataFromServer(any(), any(), any());
87         verify(dfcHttpClientSpy, times(1)).isDownloadFailed(any());
88     }
89
90     @Test
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));
95
96         dfcHttpClientSpy.open();
97
98         doReturn(fis).when(dfcHttpClientSpy).getServerResponse(any());
99
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();
105     }
106
107     @Test
108     void isResponseOk_validateResponse() {
109         assertTrue(dfcHttpClientSpy.isResponseOk(HttpClientResponseHelper.NETTY_RESPONSE_OK));
110         assertFalse(dfcHttpClientSpy.isResponseOk(HttpClientResponseHelper.RESPONSE_ANY_NO_OK));
111     }
112
113     private FileServerData createFileServerData() {
114         return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password(PASSWORD).port(PORT)
115             .build();
116     }
117 }