From b1e0ceb789a9c0781bb3b97d4b1ff0e75396fd29 Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Wed, 8 May 2019 14:52:24 +0200 Subject: Release DMaaP client API * remove @ExperimentalApi annotation * deprecate old API * extract http-client module + refactor * change DmaapClientFactory so it's more configurable Change-Id: I710d20558eece8cc3d7c0740e765d34737134b3a Issue-ID: DCAEGEN2-1492 Signed-off-by: Piotr Jaszczyk --- .../services/adapters/http/CloudHttpClientIT.java | 220 +++++++++++++++++++++ .../services/adapters/http/RxHttpClientIT.java | 161 +++++++++++++++ 2 files changed, 381 insertions(+) create mode 100644 rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClientIT.java create mode 100644 rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java (limited to 'rest-services/http-client/src/test/java') diff --git a/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClientIT.java b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClientIT.java new file mode 100644 index 00000000..f4efe1ab --- /dev/null +++ b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClientIT.java @@ -0,0 +1,220 @@ +/* + * ============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.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import io.netty.handler.codec.http.HttpResponseStatus; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.model.DmaapModel; +import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; +import reactor.core.publisher.Mono; +import reactor.netty.DisposableServer; +import reactor.netty.http.client.HttpClient; +import reactor.netty.http.server.HttpServer; +import reactor.netty.resources.ConnectionProvider; +import reactor.test.StepVerifier; + +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\"}"; + private static final ConnectionProvider connectionProvider = ConnectionProvider.fixed("test", MAX_CONNECTIONS); + + private final DmaapModel dmaapModel = mock(DmaapModel.class); + private final JsonBodyBuilder jsonBodyBuilder = mock(JsonBodyBuilder.class); + + @Test + void successfulPatchResponse() { + DisposableServer server = createValidServer(); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono content = cloudHttpClient + .patch(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); + HttpResponse httpClientResponse = content.block(); + + assertEquals(HttpResponseStatus.OK.code(), httpClientResponse.statusCode()); + server.disposeNow(); + } + + @Test + void errorPatchRequest() { + DisposableServer server = createInvalidServer(); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono content = cloudHttpClient + .patch(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); + HttpResponse httpClientResponse = content.block(); + + assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), httpClientResponse.statusCode()); + server.disposeNow(); + } + + @Test + void successfulPostResponse() { + DisposableServer server = createValidServer(); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono content = cloudHttpClient + .post(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); + HttpResponse httpClientResponse = content.block(); + + assertEquals(HttpResponseStatus.OK.code(), httpClientResponse.statusCode()); + server.disposeNow(); + } + + @Test + void errorPostRequest() { + DisposableServer server = createInvalidServer(); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono content = cloudHttpClient + .post(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); + HttpResponse httpClientResponse = content.block(); + + assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), httpClientResponse.statusCode()); + server.disposeNow(); + } + + @Test + void successfulGetResponse() { + DisposableServer server = createValidServer(); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono content = cloudHttpClient.get(SAMPLE_URL, String.class); + Mono contentWithHeaders = cloudHttpClient.get(SAMPLE_URL, createRequestDiagnosticContext(), + createCustomHeaders(), String.class); + + StepVerifier.create(content) + .expectNext(SAMPLE_STRING) + .expectComplete() + .verify(); + StepVerifier.create(contentWithHeaders) + .expectNext(SAMPLE_STRING) + .expectComplete() + .verify(); + server.disposeNow(); + } + + @Test + void errorGetRequest() { + DisposableServer server = createInvalidServer(); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + Mono content = cloudHttpClient.get(SAMPLE_URL, String.class); + + StepVerifier.create(content) + .expectError() + .verify(); + server.disposeNow(); + } + + @Test + void successfulPutResponse() { + DisposableServer server = createValidServer(); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono content = cloudHttpClient + .put(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); + HttpResponse httpClientResponse = content.block(); + + assertEquals(HttpResponseStatus.OK.code(), httpClientResponse.statusCode()); + server.disposeNow(); + } + + @Test + void errorPutRequest() { + DisposableServer server = createInvalidServer(); + RxHttpClient httpClient = createHttpClientForContextWithAddress(server); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono content = cloudHttpClient + .put(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); + HttpResponse httpClientResponse = content.block(); + + assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), httpClientResponse.statusCode()); + server.disposeNow(); + } + + private Map createCustomHeaders() { + Map customHeaders = new HashMap<>(); + customHeaders.put("X_INVOCATION_ID", UUID.randomUUID().toString()); + return customHeaders; + } + + private DisposableServer createValidServer() { + Mono response = Mono.just(SAMPLE_STRING); + return HttpServer.create() + .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(); + } + + private RequestDiagnosticContext createRequestDiagnosticContext() { + return ImmutableRequestDiagnosticContext.builder() + .invocationId(UUID.randomUUID()).requestId(UUID.randomUUID()).build(); + } + + 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/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java new file mode 100644 index 00000000..cdddaeff --- /dev/null +++ b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java @@ -0,0 +1,161 @@ +/* + * ============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.assertj.core.api.Assertions.assertThat; +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 = RxHttpClientFactory.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("/headers-post", (req, resp) -> resp + .sendString(Mono.just(req.requestHeaders().toString()))) + .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 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 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 bodyAsString = cut.call(httpRequest) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(HttpResponse::bodyAsString); + + // then + StepVerifier.create(bodyAsString) + .expectNext(requestBody) + .expectComplete() + .verify(TIMEOUT); + } + + @Test + void testChunkedEncoding() throws Exception { + // given + final String requestBody = "hello world"; + final HttpRequest httpRequest = requestFor("/headers-post") + .method(HttpMethod.POST) + .body(RequestBody.chunkedFromString(Mono.just(requestBody))) + .build(); + + // when + final Mono bodyAsString = cut.call(httpRequest) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(HttpResponse::bodyAsString); + + // then + StepVerifier.create(bodyAsString.map(String::toLowerCase)) + .consumeNextWith(responseBody -> { + assertThat(responseBody).contains("transfer-encoding: chunked"); + assertThat(responseBody).doesNotContain("content-length"); + }) + .expectComplete() + .verify(TIMEOUT); + } + + @Test + void testUnchunkedEncoding() throws Exception { + // given + final String requestBody = "hello world"; + final HttpRequest httpRequest = requestFor("/headers-post") + .method(HttpMethod.POST) + .body(RequestBody.fromString(requestBody)) + .build(); + + // when + final Mono bodyAsString = cut.call(httpRequest) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(HttpResponse::bodyAsString); + + // then + StepVerifier.create(bodyAsString.map(String::toLowerCase)) + .consumeNextWith(responseBody -> { + assertThat(responseBody).doesNotContain("transfer-encoding"); + assertThat(responseBody).contains("content-length"); + }) + .expectComplete() + .verify(TIMEOUT); + } +} \ No newline at end of file -- cgit 1.2.3-korg