diff options
author | 2019-03-20 14:49:13 +0100 | |
---|---|---|
committer | 2019-03-22 09:04:14 +0100 | |
commit | ab718d73ce25edf2c8a92d10cea13d594be46e62 (patch) | |
tree | df3fed605b7c211b75ca9425ad00559a75532278 /rest-services/cbs-client/src/test/java/org | |
parent | 448cb9d42b072c6cbde73747c0b1ca14ca531e55 (diff) |
Create an evolution of HTTP Client
* simplify the API
* use new http client in old one for compatibility
* deprecate old one
Issue-ID: DCAEGEN2-1010
Change-Id: Ief681ba536a37b29c10d133c61a1326a003ed308
Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Diffstat (limited to 'rest-services/cbs-client/src/test/java/org')
4 files changed, 58 insertions, 107 deletions
diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java index e2833fe5..58e1e6cb 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java @@ -21,8 +21,8 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; import static org.assertj.core.api.Assertions.assertThat; -import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendResource; -import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendString; +import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendResource; +import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendString; import com.google.gson.JsonObject; import io.vavr.collection.Map; @@ -31,6 +31,7 @@ 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.test.DummyHttpServer; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.DataStreams; diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java index 617904f9..339b1efa 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java @@ -22,7 +22,6 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -30,7 +29,13 @@ import static org.mockito.Mockito.verify; import com.google.gson.JsonObject; import java.net.InetSocketAddress; import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; +import org.mockito.Mockito; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import reactor.core.publisher.Mono; @@ -39,7 +44,7 @@ import reactor.core.publisher.Mono; * @since February 2019 */ class CbsClientImplTest { - private final CloudHttpClient httpClient = mock(CloudHttpClient.class); + private final RxHttpClient httpClient = mock(RxHttpClient.class); @Test void shouldFetchUsingProperUrl() { @@ -47,8 +52,12 @@ class CbsClientImplTest { InetSocketAddress cbsAddress = InetSocketAddress.createUnresolved("cbshost", 6969); String serviceName = "dcaegen2-ves-collector"; final CbsClientImpl cut = CbsClientImpl.create(httpClient, cbsAddress, serviceName); - final JsonObject httpResponse = new JsonObject(); - given(httpClient.get(anyString(), any(RequestDiagnosticContext.class), any(Class.class))).willReturn(Mono.just(httpResponse)); + final HttpResponse httpResponse = ImmutableHttpResponse.builder() + .url("http://xxx") + .statusCode(200) + .rawBody("{}".getBytes()) + .build(); + given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse)); RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create(); // when @@ -56,7 +65,11 @@ class CbsClientImplTest { // then final String expectedUrl = "http://cbshost:6969/service_component/dcaegen2-ves-collector"; - verify(httpClient).get(expectedUrl, diagnosticContext, JsonObject.class); - assertThat(result).isSameAs(httpResponse); + verify(httpClient).call(ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(expectedUrl) + .diagnosticContext(diagnosticContext) + .build()); + assertThat(result.toString()).isEqualTo(httpResponse.bodyAsString()); } }
\ No newline at end of file diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookupTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookupTest.java index 94ff53f9..e16605de 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookupTest.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookupTest.java @@ -22,8 +22,11 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.isA; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -31,7 +34,11 @@ import com.google.gson.JsonParser; import java.io.InputStreamReader; import java.net.InetSocketAddress; import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; +import org.mockito.ArgumentCaptor; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.ServiceLookupException; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties; @@ -49,7 +56,7 @@ class CbsLookupTest { .consulHost("consul.local") .consulPort(8050) .appName("whatever").build(); - private final CloudHttpClient httpClient = mock(CloudHttpClient.class); + private final RxHttpClient httpClient = mock(RxHttpClient.class); private final CbsLookup cut = new CbsLookup(httpClient); @Test @@ -63,6 +70,14 @@ class CbsLookupTest { // then assertThat(result.getHostString()).isEqualTo("config-binding-service"); assertThat(result.getPort()).isEqualTo(10000); + + final String url = "http://" + + env.consulHost() + + ":" + + env.consulPort() + + "/v1/catalog/service/" + + env.cbsName(); + verifyHttpGetHasBeenCalled(url); } @Test @@ -82,14 +97,24 @@ class CbsLookupTest { } private void givenConsulResponse(JsonArray jsonArray) { - final String url = "http://" - + env.consulHost() - + ":" - + env.consulPort() - + "/v1/catalog/service/" - + env.cbsName(); - given(httpClient.get(url, JsonArray.class)) - .willReturn(Mono.just(jsonArray)); + given(httpClient.call(any(HttpRequest.class))) + .willReturn(Mono.just(ImmutableHttpResponse.builder() + .url("http://xxx") + .statusCode(200) + .rawBody(jsonArray.toString().getBytes()) + .build())); + } + + private void verifyHttpGetHasBeenCalled(String url) { + final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class); + verify(httpClient).call(httpRequestArgumentCaptor.capture()); + assertThat(httpRequestArgumentCaptor.getValue().url()) + .describedAs("HTTP request URL") + .isEqualTo(url); + assertThat(httpRequestArgumentCaptor.getValue().method()) + .describedAs("HTTP request method") + .isEqualTo(HttpMethod.GET); } + }
\ No newline at end of file diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java deleted file mode 100644 index 7835a5f9..00000000 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * ============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.cbs.client.impl; - -import io.vavr.CheckedFunction0; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.function.Consumer; -import org.reactivestreams.Publisher; -import reactor.core.publisher.Mono; -import reactor.netty.DisposableServer; -import reactor.netty.http.server.HttpServer; -import reactor.netty.http.server.HttpServerResponse; -import reactor.netty.http.server.HttpServerRoutes; - -/** - * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> - * @since February 2019 - */ -public class DummyHttpServer { - - private final DisposableServer server; - - private DummyHttpServer(DisposableServer server) { - this.server = server; - } - - public static DummyHttpServer start(Consumer<HttpServerRoutes> routes) { - return new DummyHttpServer(HttpServer.create() - .host("127.0.0.1") - .route(routes) - .bind() - .block()); - } - - public static Publisher<Void> sendResource(HttpServerResponse httpServerResponse, String resourcePath) { - return sendString(httpServerResponse, Mono.fromCallable(() -> readResource(resourcePath))); - } - - public static Publisher<Void> sendString(HttpServerResponse httpServerResponse, Publisher<String> content) { - return httpServerResponse.sendString(content); - } - - public void close() { - server.disposeNow(); - } - - public String host() { - return server.host(); - } - - public int port() { - return server.port(); - } - - private static String readResource(String resourcePath) { - try { - return CheckedFunction0.constant(resourcePath) - .andThen(DummyHttpServer.class::getResource) - .andThen(URL::toURI) - .andThen(Paths::get) - .andThen(Files::readAllBytes) - .andThen(String::new) - .apply(); - } catch (Throwable throwable) { - throw new RuntimeException(throwable); - } - } -} |