diff options
Diffstat (limited to 'rest-services/common-dependency/src/test/java')
2 files changed, 154 insertions, 38 deletions
diff --git a/rest-services/common-dependency/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClientIT.java b/rest-services/common-dependency/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClientIT.java index 936ef0c5..a913a93f 100644 --- a/rest-services/common-dependency/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClientIT.java +++ b/rest-services/common-dependency/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClientIT.java @@ -42,26 +42,28 @@ import reactor.test.StepVerifier; import reactor.netty.http.client.HttpClientResponse; class CloudHttpClientIT { + private static final int MAX_CONNECTIONS = 1; private static final String SAMPLE_STRING = "sampleString"; private static final String SAMPLE_URL = "/sampleURL"; private static final String JSON_BODY = "{\"correlationId\":\"NOKnhfsadhff\"," - + "\"ipaddress-v4\":\"256.22.33.155\", " - + "\"ipaddress-v6\":\"200J:0db8:85a3:0000:0000:8a2e:0370:7334\"}"; + + "\"ipaddress-v4\":\"256.22.33.155\", " + + "\"ipaddress-v6\":\"200J:0db8:85a3:0000:0000:8a2e:0370:7334\"}"; private static final ConnectionProvider connectionProvider = ConnectionProvider.fixed("test", MAX_CONNECTIONS); - private DmaapModel dmaapModel = mock(DmaapModel.class); - private JsonBodyBuilder<DmaapModel> jsonBodyBuilder = mock(JsonBodyBuilder.class); + private final DmaapModel dmaapModel = mock(DmaapModel.class); + private final JsonBodyBuilder<DmaapModel> jsonBodyBuilder = mock(JsonBodyBuilder.class); @Test void successfulPatchResponse() { DisposableServer server = createValidServer(); - HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); - Mono<HttpClientResponse> content = cloudHttpClient.patch(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), - jsonBodyBuilder, dmaapModel); + Mono<HttpClientResponse> content = cloudHttpClient + .patch(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); HttpClientResponse httpClientResponse = content.block(); assertEquals(HttpResponseStatus.OK, httpClientResponse.status()); @@ -71,12 +73,13 @@ class CloudHttpClientIT { @Test void errorPatchRequest() { DisposableServer server = createInvalidServer(); - HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); - Mono<HttpClientResponse> content = cloudHttpClient.patch(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), - jsonBodyBuilder, dmaapModel); + Mono<HttpClientResponse> content = cloudHttpClient + .patch(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); HttpClientResponse httpClientResponse = content.block(); assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, httpClientResponse.status()); @@ -86,12 +89,13 @@ class CloudHttpClientIT { @Test void successfulPostResponse() { DisposableServer server = createValidServer(); - HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); - Mono<HttpClientResponse> content = cloudHttpClient.post(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), - jsonBodyBuilder, dmaapModel); + Mono<HttpClientResponse> content = cloudHttpClient + .post(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); HttpClientResponse httpClientResponse = content.block(); assertEquals(HttpResponseStatus.OK, httpClientResponse.status()); @@ -101,12 +105,13 @@ class CloudHttpClientIT { @Test void errorPostRequest() { DisposableServer server = createInvalidServer(); - HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); - Mono<HttpClientResponse> content = cloudHttpClient.post(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), - jsonBodyBuilder, dmaapModel); + Mono<HttpClientResponse> content = cloudHttpClient + .post(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); HttpClientResponse httpClientResponse = content.block(); assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, httpClientResponse.status()); @@ -116,36 +121,36 @@ class CloudHttpClientIT { @Test void successfulGetResponse() { DisposableServer server = createValidServer(); - HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); Mono<String> content = cloudHttpClient.get(SAMPLE_URL, String.class); Mono<String> contentWithHeaders = cloudHttpClient.get(SAMPLE_URL, createRequestDiagnosticContext(), - createCustomHeaders(), String.class); + createCustomHeaders(), String.class); StepVerifier.create(content) - .expectNext(SAMPLE_STRING) - .expectComplete() - .verify(); + .expectNext(SAMPLE_STRING) + .expectComplete() + .verify(); StepVerifier.create(contentWithHeaders) - .expectNext(SAMPLE_STRING) - .expectComplete() - .verify(); + .expectNext(SAMPLE_STRING) + .expectComplete() + .verify(); server.disposeNow(); } @Test void errorGetRequest() { DisposableServer server = createInvalidServer(); - HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); Mono<String> content = cloudHttpClient.get(SAMPLE_URL, String.class); StepVerifier.create(content) - .expectError() - .verify(); + .expectError() + .verify(); server.disposeNow(); } @@ -158,26 +163,27 @@ class CloudHttpClientIT { private DisposableServer createValidServer() { Mono<String> response = Mono.just(SAMPLE_STRING); return HttpServer.create() - .handle((req, resp) -> resp.sendString(response)) - .wiretap(true) - .bindNow(); + .handle((req, resp) -> resp.sendString(response)) + .wiretap(true) + .bindNow(); } private DisposableServer createInvalidServer() { return HttpServer.create() - .handle((req, resp) -> Mono.error(new Exception("returnError"))) - .wiretap(true) - .bindNow(); + .handle((req, resp) -> Mono.error(new Exception("returnError"))) + .wiretap(true) + .bindNow(); } private RequestDiagnosticContext createRequestDiagnosticContext() { return ImmutableRequestDiagnosticContext.builder() - .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build(); + .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build(); } - private HttpClient createHttpClientForContextWithAddress(DisposableServer disposableServer, - ConnectionProvider connectionProvider) { - HttpClient client = connectionProvider == null? HttpClient.create() : HttpClient.create(connectionProvider); - return client.addressSupplier(disposableServer::address).wiretap(true); + private RxHttpClient createHttpClientForContextWithAddress(DisposableServer disposableServer) { + HttpClient client = HttpClient.create(connectionProvider) + .addressSupplier(disposableServer::address) + .wiretap(true); + return new RxHttpClient(client); } }
\ No newline at end of file diff --git a/rest-services/common-dependency/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java b/rest-services/common-dependency/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java new file mode 100644 index 00000000..5ae62c87 --- /dev/null +++ b/rest-services/common-dependency/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java @@ -0,0 +1,110 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.adapters.http; + +import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendString; + +import io.netty.handler.codec.http.HttpResponseStatus; +import java.net.MalformedURLException; +import java.net.URL; +import java.time.Duration; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.exceptions.HttpException; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +class RxHttpClientIT { + + private static final Duration TIMEOUT = Duration.ofHours(5); + private final RxHttpClient cut = RxHttpClient.create(); + private static DummyHttpServer httpServer; + + @BeforeAll + static void setUpClass() { + httpServer = DummyHttpServer.start(routes -> + routes.get("/sample-get", (req, resp) -> sendString(resp, Mono.just("OK"))) + .get("/sample-get-500", (req, resp) -> resp.status(HttpResponseStatus.INTERNAL_SERVER_ERROR).send()) + .post("/echo-post", (req, resp) -> resp.send(req.receive().retain())) + ); + } + + @AfterAll + static void tearDownClass() { + httpServer.close(); + } + + private ImmutableHttpRequest.Builder requestFor(String path) throws MalformedURLException { + return ImmutableHttpRequest.builder() + .url(new URL("http", httpServer.host(), httpServer.port(), path).toString()); + } + + @Test + void simpleGet() throws Exception { + // given + final HttpRequest httpRequest = requestFor("/sample-get").method(HttpMethod.GET).build(); + + // when + final Mono<String> bodyAsString = cut.call(httpRequest) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(HttpResponse::bodyAsString); + + // then + StepVerifier.create(bodyAsString).expectNext("OK").expectComplete().verify(TIMEOUT); + } + + @Test + void getWithError() throws Exception { + // given + final HttpRequest httpRequest = requestFor("/sample-get-500").method(HttpMethod.GET).build(); + + // when + final Mono<String> bodyAsString = cut.call(httpRequest) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(HttpResponse::bodyAsString); + + // then + StepVerifier.create(bodyAsString).expectError(HttpException.class).verify(TIMEOUT); + } + + @Test + void simplePost() throws Exception { + // given + final String requestBody = "hello world"; + final HttpRequest httpRequest = requestFor("/echo-post") + .method(HttpMethod.POST) + .body(RequestBody.fromString(requestBody)) + .build(); + + // when + final Mono<String> bodyAsString = cut.call(httpRequest) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(HttpResponse::bodyAsString); + + // then + StepVerifier.create(bodyAsString) + .expectNext(requestBody) + .expectComplete() + .verify(TIMEOUT); + } +}
\ No newline at end of file |