8550644942018156ebad3187b42d510b96a7453d
[nonrtric/plt/ranpm.git] / datafilecollector / src / test / java / org / onap / dcaegen2 / collectors / datafile / http / DfcHttpClientTest.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2020-2021 Nokia. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16 package org.onap.dcaegen2.collectors.datafile.http;
17
18 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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.assertTrue;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.InputStream;
31 import java.net.URISyntaxException;
32 import java.nio.file.Path;
33
34 import org.apache.hc.core5.net.URIBuilder;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.ArgumentMatchers;
39 import org.mockito.Mock;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.onap.dcaegen2.collectors.datafile.commons.FileServerData;
42 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
43 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
44
45 import reactor.core.publisher.Flux;
46 import reactor.netty.http.client.HttpClientConfig;
47
48 @ExtendWith(MockitoExtension.class)
49 class DfcHttpClientTest {
50
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";
57
58     @Mock
59     private Path pathMock;
60
61     DfcHttpClient dfcHttpClientSpy;
62
63     @BeforeEach
64     public void setup() {
65         dfcHttpClientSpy = spy(new DfcHttpClient(createFileServerData()));
66     }
67
68     @Test
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"));
73     }
74
75     @Test
76     void openConnection_failedBasicAuthSetupThrowException() {
77         FileServerData serverData =
78             FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password("").port(PORT).build();
79
80         DfcHttpClient dfcHttpClientSpy = spy(new DfcHttpClient(serverData));
81
82         assertThatThrownBy(() -> dfcHttpClientSpy.open())
83             .hasMessageContaining("Not sufficient basic auth data for file.");
84     }
85
86     @Test
87     void collectFile_AllOk() throws Exception {
88         String REMOTE_FILE = "any";
89         Flux<InputStream> fis = Flux.just(new ByteArrayInputStream("ReturnedString".getBytes()));
90
91         dfcHttpClientSpy.open();
92
93         when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis);
94         doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any());
95
96         dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock);
97         dfcHttpClientSpy.close();
98
99         verify(dfcHttpClientSpy, times(1)).getServerResponse(REMOTE_FILE);
100         verify(dfcHttpClientSpy, times(1)).processDataFromServer(any(), any(), any());
101         verify(dfcHttpClientSpy, times(1)).isDownloadFailed(any());
102     }
103
104     @Test
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()));
109
110         dfcHttpClientSpy.open();
111         HttpClientConfig config = dfcHttpClientSpy.client.configuration();
112         assertEquals(HttpUtils.jwtAuthContent(JWT_PASSWORD), config.headers().get("Authorization"));
113
114         when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis);
115         doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any());
116
117         dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock);
118         dfcHttpClientSpy.close();
119
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());
123     }
124
125     @Test
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));
130
131         dfcHttpClientSpy.open();
132
133         doReturn(fis).when(dfcHttpClientSpy).getServerResponse(any());
134
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();
140     }
141
142     @Test
143     void isResponseOk_validateResponse() {
144         assertTrue(dfcHttpClientSpy.isResponseOk(HttpClientResponseHelper.NETTY_RESPONSE_OK));
145         assertFalse(dfcHttpClientSpy.isResponseOk(HttpClientResponseHelper.RESPONSE_ANY_NO_OK));
146     }
147
148     private FileServerData createFileServerData() {
149         return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password(PASSWORD).port(PORT)
150             .build();
151     }
152
153     private FileServerData fileServerDataWithJWTToken() throws URISyntaxException {
154         String query = "?" + ACCESS_TOKEN + "=" + JWT_PASSWORD;
155
156         return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
157             .queryParameters(new URIBuilder(query).getQueryParams()).build();
158     }
159 }