diff options
author | elinuxhenrik <henrik.b.andersson@est.tech> | 2018-10-01 16:51:05 +0200 |
---|---|---|
committer | elinuxhenrik <henrik.b.andersson@est.tech> | 2018-10-02 10:38:30 +0200 |
commit | 18f56936071b59b6efd61c2764c51ecd52ced74c (patch) | |
tree | 0a348e5e788398f75e561b6cf76957238dcab375 /datafile-dmaap-client/src/test/java/org/onap | |
parent | f3e28b23e4256b5c88d328743bb66d0d6766f542 (diff) |
Handle redirect from DataRouter properly
Change-Id: I1eae8b45a2437b97bccedcb0f5cc02ac29f5044f
Issue-ID: DCAEGEN2-850
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Diffstat (limited to 'datafile-dmaap-client/src/test/java/org/onap')
-rw-r--r-- | datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java | 83 |
1 files changed, 33 insertions, 50 deletions
diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java index 5f4c1a58..5dbc9089 100644 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java +++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java @@ -17,32 +17,26 @@ package org.onap.dcaegen2.collectors.datafile.service.producer; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; - -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.URI; -import java.util.ArrayList; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration; -import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions; import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel; import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModelForUnitTest; -import org.springframework.http.HttpHeaders; -import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec; -import org.springframework.web.reactive.function.client.WebClient.ResponseSpec; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.util.DefaultUriBuilderFactory; -import reactor.core.publisher.Flux; import reactor.test.StepVerifier; /** @@ -57,70 +51,59 @@ class DmaapProducerReactiveHttpClientTest { private static final String X_ATT_DR_META = "X-ATT-DR-META"; private static final String HOST = "54.45.33.2"; - private static final String HTTP_SCHEME = "http"; + private static final String HTTPS_SCHEME = "https"; private static final int PORT = 1234; private static final String APPLICATION_OCTET_STREAM_CONTENT_TYPE = "application/octet-stream"; + private static final String URI_SEPARATOR = "/"; private static final String PUBLISH_TOPIC = "publish"; private static final String DEFAULT_FEED_ID = "1"; + private static final String FILE_CONTENT = "Just a string."; private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient; private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class); private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); - private WebClient webClientMock = mock(WebClient.class); - private RequestBodyUriSpec requestBodyUriSpecMock; - private ResponseSpec responseSpecMock; + + private IFileSystemResource fileSystemResourceMock = mock(IFileSystemResource.class); + private IRestTemplate restTemplateMock = mock(IRestTemplate.class); + private ResponseEntity<String> responseEntityMock = mock(ResponseEntity.class); @BeforeEach void setUp() { when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn(HOST); - when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTP_SCHEME); + when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn(HTTPS_SCHEME); when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(PORT); - when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("DATAFILE"); - when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("DATAFILE"); + when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("dradmin"); + when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("dradmin"); when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE); when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC); dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock); - - webClientMock = spy(WebClient.builder() - .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType()) - .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(), - dmaapPublisherConfigurationMock.dmaapUserPassword())) - .build()); - requestBodyUriSpecMock = mock(RequestBodyUriSpec.class); - responseSpecMock = mock(ResponseSpec.class); + dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock); + dmaapProducerReactiveHttpClient.setRestTemplate(restTemplateMock); } @Test - void getHttpResponse_Success() { + void getHttpResponse_Success() throws Exception { mockWebClientDependantObject(); - dmaapProducerReactiveHttpClient.createDmaapWebClient(webClientMock); - List<ConsumerDmaapModel> consumerDmaapModelList = new ArrayList<ConsumerDmaapModel>(); - consumerDmaapModelList.add(consumerDmaapModel); StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel)) - .expectNext("200").verifyComplete(); - - verify(requestBodyUriSpecMock).header(HttpHeaders.CONTENT_TYPE, APPLICATION_OCTET_STREAM_CONTENT_TYPE); - JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(consumerDmaapModel)); - metaData.getAsJsonObject().remove(LOCATION_JSON_TAG); - metaData.getAsJsonObject().remove(NAME_JSON_TAG); - verify(requestBodyUriSpecMock).header(X_ATT_DR_META, metaData.toString()); - URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTP_SCHEME).host(HOST).port(PORT) - .path(PUBLISH_TOPIC + "/" + DEFAULT_FEED_ID + "/" + FILE_NAME).build(); - verify(requestBodyUriSpecMock).uri(expectedUri); - verify(requestBodyUriSpecMock).body(any()); + .expectNext(HttpStatus.OK.toString()).verifyComplete(); + + URI expectedUri = new DefaultUriBuilderFactory().builder().scheme(HTTPS_SCHEME).host(HOST).port(PORT) + .path(PUBLISH_TOPIC + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + FILE_NAME).build(); + + verify(restTemplateMock) + .exchange(eq(expectedUri), eq(HttpMethod.PUT), any(), eq(String.class)); } - private void mockWebClientDependantObject() { - when(webClientMock.post()).thenReturn(requestBodyUriSpecMock); - when(requestBodyUriSpecMock.uri((URI) any())).thenReturn(requestBodyUriSpecMock); + private void mockWebClientDependantObject() throws IOException { + InputStream fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes()); + when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream); - when(requestBodyUriSpecMock.retrieve()).thenReturn(responseSpecMock); - when(responseSpecMock.onStatus(any(), any())).thenReturn(responseSpecMock); - Flux<String> expectedResult = Flux.just("200"); - when(responseSpecMock.bodyToFlux(String.class)).thenReturn(expectedResult); + when(restTemplateMock.exchange(any(), any(), any(), any())) + .thenReturn(responseEntityMock); + when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK); } } |