diff options
Diffstat (limited to 'rest-services')
3 files changed, 123 insertions, 8 deletions
diff --git a/rest-services/common-dependency/pom.xml b/rest-services/common-dependency/pom.xml index 1b34fb67..4d9b2e08 100644 --- a/rest-services/common-dependency/pom.xml +++ b/rest-services/common-dependency/pom.xml @@ -56,5 +56,15 @@ <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>io.projectreactor</groupId> + <artifactId>reactor-test</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project>
\ No newline at end of file diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java index 33c39fa6..f645bc91 100644 --- a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java +++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/CloudHttpClient.java @@ -58,7 +58,7 @@ public class CloudHttpClient { this(HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))); } - private CloudHttpClient(HttpClient httpClient) { + CloudHttpClient(HttpClient httpClient) { this.httpClient = httpClient; } 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 2553b9be..bd5c9a25 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 @@ -20,24 +20,129 @@ package org.onap.dcaegen2.services.sdk.rest.services.adapters.http; -import static org.junit.jupiter.api.Assertions.*; +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 DmaapModel dmaapModel = mock(DmaapModel.class); + private JsonBodyBuilder<DmaapModel> jsonBodyBuilder = mock(JsonBodyBuilder.class); @Test - public void post() { - assertTrue(true); + void successfulPatchResponse() { + DisposableServer server = createValidServer(); + HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono<Integer> content = cloudHttpClient.patch(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); + + StepVerifier.create(content) + .expectNext(HttpResponseStatus.OK.code()) + .expectComplete() + .verify(); + server.disposeNow(); } @Test - public void get() { - assertTrue(true); + void errorPatchRequest() { + DisposableServer server = createInvalidServer(); + HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + when(jsonBodyBuilder.createJsonBody(dmaapModel)).thenReturn(JSON_BODY); + Mono<Integer> content = cloudHttpClient.patch(SAMPLE_URL, createRequestDiagnosticContext(), createCustomHeaders(), + jsonBodyBuilder, dmaapModel); + + StepVerifier.create(content) + .expectNext(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()) + .expectComplete() + .verify(); + server.disposeNow(); } @Test - public void patch() { - assertTrue(true); + void successfulGetResponse() { + DisposableServer server = createValidServer(); + HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + Mono<String> content = cloudHttpClient.get(SAMPLE_URL, String.class); + + StepVerifier.create(content) + .expectNext(SAMPLE_STRING) + .expectComplete() + .verify(); + server.disposeNow(); + } + @Test + void errorGetRequest() { + DisposableServer server = createInvalidServer(); + HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + Mono<String> content = cloudHttpClient.get(SAMPLE_URL, String.class); + + StepVerifier.create(content) + .expectError() + .verify(); + server.disposeNow(); + } + + private Map<String, String> createCustomHeaders() { + Map<String, String> customHeaders = new HashMap<>(); + customHeaders.put("X_INVOCATION_ID", UUID.randomUUID().toString()); + + return customHeaders; + } + + private DisposableServer createValidServer() { + Mono<String> 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 HttpClient createHttpClientForContextWithAddress(DisposableServer disposableServer, + ConnectionProvider connectionProvider) { + HttpClient client = connectionProvider == null? HttpClient.create() : HttpClient.create(connectionProvider); + return client.addressSupplier(disposableServer::address).wiretap(true); } }
\ No newline at end of file |