diff options
author | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2019-03-20 14:49:13 +0100 |
---|---|---|
committer | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2019-03-22 09:04:14 +0100 |
commit | ab718d73ce25edf2c8a92d10cea13d594be46e62 (patch) | |
tree | df3fed605b7c211b75ca9425ad00559a75532278 /rest-services/cbs-client/src | |
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')
7 files changed, 88 insertions, 118 deletions
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java index 989bd2db..379daf97 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java @@ -20,7 +20,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api; import org.jetbrains.annotations.NotNull; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientImpl; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsLookup; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; @@ -53,7 +53,7 @@ public class CbsClientFactory { */ public static @NotNull Mono<CbsClient> createCbsClient(EnvProperties env) { return Mono.defer(() -> { - final CloudHttpClient httpClient = new CloudHttpClient(); + final RxHttpClient httpClient = RxHttpClient.create(); final CbsLookup lookup = new CbsLookup(httpClient); return lookup.lookup(env) .map(addr -> CbsClientImpl.create(httpClient, addr, env.appName())); diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java index 9be08e3c..72c1b267 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java @@ -24,22 +24,24 @@ import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.URL; import org.jetbrains.annotations.NotNull; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import reactor.core.publisher.Mono; public class CbsClientImpl implements CbsClient { - private final CloudHttpClient httpClient; + private final RxHttpClient httpClient; private final String fetchUrl; - CbsClientImpl(CloudHttpClient httpClient, URL fetchUrl) { + CbsClientImpl(RxHttpClient httpClient, URL fetchUrl) { this.httpClient = httpClient; this.fetchUrl = fetchUrl.toString(); } - public static CbsClientImpl create(CloudHttpClient httpClient, InetSocketAddress cbsAddress, String serviceName) { + public static CbsClientImpl create(RxHttpClient httpClient, InetSocketAddress cbsAddress, String serviceName) { return new CbsClientImpl(httpClient, constructUrl(cbsAddress, serviceName)); } @@ -57,6 +59,14 @@ public class CbsClientImpl implements CbsClient { @Override public @NotNull Mono<JsonObject> get(RequestDiagnosticContext diagnosticContext) { - return Mono.defer(() -> httpClient.get(fetchUrl, diagnosticContext, JsonObject.class)); + return Mono.defer(() -> { + final ImmutableHttpRequest request = ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(fetchUrl) + .diagnosticContext(diagnosticContext) + .build(); + return httpClient.call(request) + .map(resp -> resp.bodyAsJson(JsonObject.class)); + }); } } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java index 89daebc8..3d528c33 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java @@ -23,7 +23,10 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.net.InetSocketAddress; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; +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.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 reactor.core.publisher.Mono; @@ -36,9 +39,9 @@ public class CbsLookup { private static final String CONSUL_JSON_SERVICE_ADDRESS = "ServiceAddress"; private static final String CONSUL_JSON_SERVICE_PORT = "ServicePort"; - private final CloudHttpClient httpClient; + private final RxHttpClient httpClient; - public CbsLookup(CloudHttpClient httpClient) { + public CbsLookup(RxHttpClient httpClient) { this.httpClient = httpClient; } @@ -54,7 +57,13 @@ public class CbsLookup { } private Mono<JsonArray> fetchHttpData(String consulUrl) { - return httpClient.get(consulUrl, JsonArray.class); + return httpClient.call( + ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(consulUrl) + .build()) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(resp -> resp.bodyAsJson(JsonArray.class)); } private Mono<JsonObject> firstService(JsonArray services) { 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); - } - } -} |