diff options
3 files changed, 64 insertions, 5 deletions
diff --git a/rest-services/common-dependency/pom.xml b/rest-services/common-dependency/pom.xml index 1b34fb67..4bda1750 100644 --- a/rest-services/common-dependency/pom.xml +++ b/rest-services/common-dependency/pom.xml @@ -56,5 +56,10 @@ <artifactId>junit-jupiter-engine</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..328df2dc 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,78 @@ package org.onap.dcaegen2.services.sdk.rest.services.adapters.http; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; +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 String SAMPLE_URL = "/sampleURL"; + private static final int MAX_CONNECTIONS = 1; + private static final ConnectionProvider connectionProvider = ConnectionProvider.fixed("test", MAX_CONNECTIONS); + @Test public void post() { assertTrue(true); } @Test - public void get() { + public void patch() { assertTrue(true); } @Test - public void patch() { - assertTrue(true); + void successfulGetResponse() { + String sampleString = "sampleString"; + Mono<String> response = Mono.just(sampleString); + DisposableServer server = + HttpServer.create() + .handle((req, resp) -> resp.sendString(response)) + .wiretap(true) + .bindNow(); + HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider); + CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient); + + Mono<String> content = cloudHttpClient.get(SAMPLE_URL, String.class); + + StepVerifier.create(content) + .expectNext(sampleString) + .expectComplete() + .verify(); + server.disposeNow(); + } + + @Test + void errorGetRequest() { + DisposableServer server = + HttpServer.create() + .handle((req, resp) -> Mono.error(new Exception("returnError"))) + .wiretap(true) + .bindNow(); + 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 HttpClient createHttpClientForContextWithAddress(DisposableServer context) { + return createHttpClientForContextWithAddress(context, null); + } + + 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 |