2 * ============LICENSE_START======================================================================
3 * Copyright (C) 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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
16 package org.onap.dcaegen2.collectors.datafile.http;
19 import org.apache.http.HttpResponse;
20 import org.apache.http.client.methods.HttpGet;
21 import org.apache.http.conn.ConnectTimeoutException;
22 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.onap.dcaegen2.collectors.datafile.commons.ImmutableFileServerData;
29 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
30 import org.onap.dcaegen2.collectors.datafile.exceptions.NonRetryableDatafileTaskException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.file.Path;
36 import static org.junit.jupiter.api.Assertions.assertEquals;
37 import static org.junit.jupiter.api.Assertions.assertThrows;
38 import static org.mockito.ArgumentMatchers.any;
39 import static org.mockito.ArgumentMatchers.eq;
40 import static org.mockito.Mockito.doReturn;
41 import static org.mockito.Mockito.doThrow;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
46 @ExtendWith(MockitoExtension.class)
47 class DfcHttpsClientTest {
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 = 443;
53 private static String remoteFile = "remoteFile";
56 private PoolingHttpClientConnectionManager connectionManager;
58 private Path localFile;
60 DfcHttpsClient dfcHttpsClientSpy;
64 dfcHttpsClientSpy = spy(new DfcHttpsClient(createFileServerData(), connectionManager));
68 void fileServerData_properLocationBasicAuth() throws Exception {
69 boolean result = dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow();
70 assertEquals(true, result);
74 void fileServerData_properLocationNoBasicAuth() throws Exception {
75 dfcHttpsClientSpy = spy(new DfcHttpsClient(emptyUserInFileServerData(), connectionManager));
77 boolean result = dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow();
78 assertEquals(false, result);
82 void fileServerData_improperAuthDataExceptionOccurred() throws Exception {
83 dfcHttpsClientSpy = spy(new DfcHttpsClient(invalidUserInFileServerData(), connectionManager));
85 assertThrows(DatafileTaskException.class, () -> dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow());
89 void dfcHttpsClient_flow_successfulCallAndResponseProcessing() throws Exception {
90 doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
91 .executeHttpClient(any(HttpGet.class));
92 doReturn((long)3).when(dfcHttpsClientSpy).writeFile(eq(localFile), any(InputStream.class));
94 dfcHttpsClientSpy.open();
95 dfcHttpsClientSpy.collectFile(remoteFile, localFile);
96 dfcHttpsClientSpy.close();
98 verify(dfcHttpsClientSpy, times(1)).makeCall(any(HttpGet.class));
99 verify(dfcHttpsClientSpy, times(1))
100 .executeHttpClient(any(HttpGet.class));
101 verify(dfcHttpsClientSpy, times(1))
102 .processResponse(HttpClientResponseHelper.APACHE_RESPONSE_OK, localFile);
103 verify(dfcHttpsClientSpy, times(1))
104 .writeFile(eq(localFile), any(InputStream.class));
108 void dfcHttpsClient_flow_failedCallUnexpectedResponseCode() throws Exception {
109 doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
110 .executeHttpClient(any(HttpGet.class));
111 doReturn(false).when(dfcHttpsClientSpy).isResponseOk(any(HttpResponse.class));
113 dfcHttpsClientSpy.open();
115 assertThrows(NonRetryableDatafileTaskException.class,
116 () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
120 void dfcHttpsClient_flow_failedCallConnectionTimeout() throws Exception {
121 doThrow(ConnectTimeoutException.class).when(dfcHttpsClientSpy)
122 .executeHttpClient(any(HttpGet.class));
124 dfcHttpsClientSpy.open();
126 assertThrows(NonRetryableDatafileTaskException.class,
127 () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
131 void dfcHttpsClient_flow_failedCallIOExceptionForExecuteHttpClient() throws Exception {
132 doThrow(IOException.class).when(dfcHttpsClientSpy)
133 .executeHttpClient(any(HttpGet.class));
135 dfcHttpsClientSpy.open();
137 assertThrows(DatafileTaskException.class,
138 () -> dfcHttpsClientSpy.collectFile(remoteFile, localFile));
141 private ImmutableFileServerData createFileServerData() {
142 return ImmutableFileServerData.builder()
143 .serverAddress(XNF_ADDRESS)
144 .userId(USERNAME).password(PASSWORD)
149 private ImmutableFileServerData emptyUserInFileServerData() {
150 return ImmutableFileServerData.builder()
151 .serverAddress(XNF_ADDRESS)
152 .userId("").password("")
157 private ImmutableFileServerData invalidUserInFileServerData() {
158 return ImmutableFileServerData.builder()
159 .serverAddress(XNF_ADDRESS)
160 .userId("demo").password("")