diff options
Diffstat (limited to 'datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/http/DfcHttpClientTest.java')
-rw-r--r-- | datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/http/DfcHttpClientTest.java | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/http/DfcHttpClientTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/http/DfcHttpClientTest.java index 2013950a..98804a0c 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/http/DfcHttpClientTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/http/DfcHttpClientTest.java @@ -15,6 +15,7 @@ */ package org.onap.dcaegen2.collectors.datafile.http; +import org.apache.hc.core5.net.URIBuilder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -29,6 +30,7 @@ import reactor.netty.http.client.HttpClientConfig; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.net.URISyntaxException; import java.nio.file.Path; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -49,6 +51,8 @@ class DfcHttpClientTest { private static final String PASSWORD = "123"; private static final String XNF_ADDRESS = "127.0.0.1"; private static final int PORT = 80; + private static final String JWT_PASSWORD = "thisIsThePassword"; + private static String ACCESS_TOKEN = "access_token"; @Mock private Path pathMock; @@ -61,10 +65,10 @@ class DfcHttpClientTest { } @Test - void openConnection_successAuthSetup() throws DatafileTaskException { + void openConnection_successBasicAuthSetup() throws DatafileTaskException { dfcHttpClientSpy.open(); HttpClientConfig config = dfcHttpClientSpy.client.configuration(); - assertEquals(HttpUtils.basicAuth(USERNAME, PASSWORD), config.headers().get("Authorization")); + assertEquals(HttpUtils.basicAuthContent(USERNAME, PASSWORD), config.headers().get("Authorization")); } @Test @@ -81,25 +85,34 @@ class DfcHttpClientTest { .hasMessageContaining("Not sufficient basic auth data for file."); } + @Test - void prepareUri_UriWithoutPort() { - ImmutableFileServerData serverData = ImmutableFileServerData.builder() - .serverAddress(XNF_ADDRESS) - .userId(USERNAME).password(PASSWORD) - .build(); - DfcHttpClient clientNoPortSpy = spy(new DfcHttpClient(serverData)); + void collectFile_AllOk() throws Exception { String REMOTE_FILE = "any"; + Flux<InputStream> fis = Flux.just(new ByteArrayInputStream("ReturnedString".getBytes())); + + dfcHttpClientSpy.open(); + + when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis); + doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any()); + + dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock); + dfcHttpClientSpy.close(); - String retrievedUri = clientNoPortSpy.prepareUri(REMOTE_FILE); - assertTrue(retrievedUri.startsWith("http://" + XNF_ADDRESS + ":80")); + verify(dfcHttpClientSpy, times(1)).getServerResponse(ArgumentMatchers.eq(REMOTE_FILE)); + verify(dfcHttpClientSpy, times(1)).processDataFromServer(any(), any(), any()); + verify(dfcHttpClientSpy, times(1)).isDownloadFailed(any()); } @Test - void collectFile_AllOk() throws Exception { + void collectFile_AllOkWithJWTToken() throws Exception { + dfcHttpClientSpy = spy(new DfcHttpClient(fileServerDataWithJWTToken())); String REMOTE_FILE = "any"; Flux<InputStream> fis = Flux.just(new ByteArrayInputStream("ReturnedString".getBytes())); dfcHttpClientSpy.open(); + HttpClientConfig config = dfcHttpClientSpy.client.configuration(); + assertEquals(HttpUtils.jwtAuthContent(JWT_PASSWORD), config.headers().get("Authorization")); when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis); doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any()); @@ -123,7 +136,7 @@ class DfcHttpClientTest { doReturn(fis).when(dfcHttpClientSpy).getServerResponse(any()); assertThatThrownBy(() -> dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock)) - .hasMessageContaining("Error occured during datafile download: "); + .hasMessageContaining(ERROR_RESPONSE); verify(dfcHttpClientSpy, times(1)).getServerResponse(REMOTE_FILE); verify(dfcHttpClientSpy, times(1)).processFailedConnectionWithServer(any(), any()); dfcHttpClientSpy.close(); @@ -142,4 +155,16 @@ class DfcHttpClientTest { .port(PORT) .build(); } + + private ImmutableFileServerData fileServerDataWithJWTToken() throws URISyntaxException { + String query = "?" + ACCESS_TOKEN + "=" + JWT_PASSWORD; + + return ImmutableFileServerData.builder() + .serverAddress(XNF_ADDRESS) + .userId("") + .password("") + .port(PORT) + .queryParameters(new URIBuilder(query).getQueryParams()) + .build(); + } } |