diff options
author | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2019-04-03 10:07:23 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-04-03 10:07:23 +0000 |
commit | b216682dc03e696ce75231135fe72dec6ce4e166 (patch) | |
tree | 598065497269eedda8a7590a1ceb3334051a3ba8 | |
parent | bf0927e896ed703fc25367fe80a0c21dffdbee31 (diff) | |
parent | 4b56423d975c55652ad40720848f675c9947b192 (diff) |
Merge "Replace CloudHttpClient with RxHttpClient in ReactiveCloudConfigurationProvider"
4 files changed, 157 insertions, 54 deletions
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/streams/gson/GsonUtils.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/streams/gson/GsonUtils.java index 7776a1ef..0881c082 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/streams/gson/GsonUtils.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/streams/gson/GsonUtils.java @@ -22,6 +22,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gso import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -86,6 +87,10 @@ public final class GsonUtils { return readFromResource(resource).getAsJsonObject(); } + public static JsonArray readObjectArrayFromResource(String resource) throws IOException{ + return readFromResource(resource).getAsJsonArray(); + } + public static JsonElement readFromResource(String resource) throws IOException { try (Reader reader = new InputStreamReader(GsonUtils.class.getResourceAsStream(resource))) { return new JsonParser().parse(reader); diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java index b7507884..27d36583 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * DCAEGEN2-SERVICES-SDK * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 NOKIA Intellectual Property. 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. @@ -23,7 +23,10 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -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.HttpRequest; +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.model.EnvProperties; import org.onap.dcaegen2.services.sdk.rest.services.uri.URI; import org.slf4j.Logger; @@ -38,15 +41,14 @@ public final class ReactiveCloudConfigurationProvider implements CloudConfigurat private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveCloudConfigurationProvider.class); private static final String EXCEPTION_MESSAGE = "Unsupported method call: "; - private final CloudHttpClient cloudHttpClient; + private final RxHttpClient rxHttpClient; public ReactiveCloudConfigurationProvider() { - this(new CloudHttpClient()); + this(RxHttpClient.create()); } - ReactiveCloudConfigurationProvider( - CloudHttpClient cloudHttpClient) { - this.cloudHttpClient = cloudHttpClient; + ReactiveCloudConfigurationProvider(RxHttpClient rxHttpClient) { + this.rxHttpClient = rxHttpClient; } @Override @@ -73,8 +75,17 @@ public final class ReactiveCloudConfigurationProvider implements CloudConfigurat private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { LOGGER.info("Retrieving Config Binding Service endpoint from Consul"); - return cloudHttpClient.get(getConsulUrl(envProperties), JsonArray.class) - .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName())); + + HttpRequest httpRequest = ImmutableHttpRequest.builder() + .url(getConsulUrl(envProperties)).method(HttpMethod.GET).build(); + + return rxHttpClient.call(httpRequest) + .map(resp -> resp.bodyAsJson(JsonArray.class)) + .flatMap(jsonArray -> + this.createConfigBindingServiceUrl( + jsonArray, + envProperties.appName()) + ); } private String getConsulUrl(EnvProperties envProperties) { @@ -84,7 +95,11 @@ public final class ReactiveCloudConfigurationProvider implements CloudConfigurat private Mono<JsonObject> callConfigBindingServiceForConfiguration(String configBindingServiceUri) { LOGGER.info("Retrieving configuration"); - return cloudHttpClient.get(configBindingServiceUri, JsonObject.class); + HttpRequest httpRequest = ImmutableHttpRequest.builder() + .url(configBindingServiceUri).method(HttpMethod.GET).build(); + + return rxHttpClient.call(httpRequest) + .map(httpResponse -> httpResponse.bodyAsJson(JsonObject.class)); } diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java index 9bb4ad39..fd9e0adc 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * DCAEGEN2-SERVICES-SDK * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 NOKIA Intellectual Property. 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. @@ -20,14 +20,24 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; - import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import java.io.IOException; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; 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.HttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.GsonUtils; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties; import reactor.core.publisher.Mono; @@ -39,58 +49,87 @@ import reactor.test.StepVerifier; class ReactiveCloudConfigurationProviderTest { private static final Gson gson = new Gson(); - private static final String configBindingService = "[{\"ID\":\"9c8dd674-34ce-7049-d318-e98d93a64303\",\"Node\"" - + ":\"dcae-bootstrap\",\"Address\":\"10.42.52.82\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":" - + "{\"lan\":\"10.42.52.82\",\"wan\":\"10.42.52.82\"},\"NodeMeta\":{\"consul-network-segment\":\"\"}," - + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[]," - + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false," - + "\"CreateIndex\":14352,\"ModifyIndex\":14352},{\"ID\":\"35c6f540-a29c-1a92-23b0-1305bd8c81f5\",\"Node\":" - + "\"dev-consul-server-1\",\"Address\":\"10.42.165.51\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":" - + "{\"lan\":\"10.42.165.51\",\"wan\":\"10.42.165.51\"},\"NodeMeta\":{\"consul-network-segment\":\"\"}," - + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[]," - + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false," - + "\"CreateIndex\":803,\"ModifyIndex\":803}]"; - private static final JsonArray configBindingServiceJson = gson.fromJson(configBindingService, JsonArray.class); - private static final JsonArray emptyConfigBindingServiceJson = gson.fromJson("[]", JsonArray.class); - private static final String configurationMock = "{\"test\":1}"; - private static final JsonObject configurationJsonMock = gson.fromJson(configurationMock, JsonObject.class); + private static final String CONFIGURATION_MOCK = "{\"test\":1}"; + private static final JsonObject CONFIGURATION_JSON_MOCK = gson + .fromJson(CONFIGURATION_MOCK, JsonObject.class); + + private final RxHttpClient httpClient = mock(RxHttpClient.class); + private final JsonArray configBindingService = GsonUtils.readObjectArrayFromResource("/sample_config_binding_service.json"); private EnvProperties envProperties = ImmutableEnvProperties.builder() - .appName("dcae-prh") - .cbsName("config-binding-service") - .consulHost("consul") - .consulPort(8500) - .build(); + .appName("dcae-prh") + .cbsName("config-binding-service") + .consulHost("consul") + .consulPort(8500) + .build(); + + private HttpResponse response; + private ReactiveCloudConfigurationProvider provider; + + ReactiveCloudConfigurationProviderTest() throws IOException { + } + + + @BeforeEach + void setUp() { + response = mock(HttpResponse.class); + provider = new ReactiveCloudConfigurationProvider(httpClient); + } + + @Test + void shouldReturnPrhConfiguration(){ + //when + when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response)); + when(response.bodyAsJson(JsonArray.class)).thenReturn(configBindingService); + when(response.bodyAsJson(JsonObject.class)).thenReturn(CONFIGURATION_JSON_MOCK); + + + //then + StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)) + .expectSubscription() + .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete(); + } @Test - void shouldReturnPrhConfiguration() { + void shouldRequestCorrectUrl(){ // given - CloudHttpClient webClient = mock(CloudHttpClient.class); - when( - webClient.get("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) - .thenReturn(Mono.just(configBindingServiceJson)); - when(webClient.get("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class)) - .thenReturn(Mono.just(configurationJsonMock)); - - ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient); - - //when/then - StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription() - .expectNext(configurationJsonMock).verifyComplete(); + String consulRequestUrl = "http://consul:8500/v1/catalog/service/config-binding-service"; + String configRequestUrl = "http://config-binding-service:10000/service_component/dcae-prh"; + + //when + when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response)); + when(response.bodyAsJson(JsonArray.class)).thenReturn(configBindingService); + when(response.bodyAsJson(JsonObject.class)).thenReturn(CONFIGURATION_JSON_MOCK); + + + //then + StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)) + .expectSubscription() + .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete(); + + + ArgumentCaptor<HttpRequest> httpReq = ArgumentCaptor + .forClass(HttpRequest.class); + verify(httpClient, times(2)).call(httpReq.capture()); + + List<HttpRequest> allRequests = httpReq.getAllValues(); + assertThat(allRequests.get(0).url()).isEqualTo(consulRequestUrl); + assertThat(allRequests.get(1).url()).isEqualTo(configRequestUrl); } @Test void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() { // given - CloudHttpClient webClient = mock(CloudHttpClient.class); - when( - webClient.get("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) - .thenReturn(Mono.just(emptyConfigBindingServiceJson)); + JsonArray emptyArray = gson.fromJson("[]", JsonArray.class); + + //when + when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response)); + when(response.bodyAsJson(JsonArray.class)).thenReturn(emptyArray); - ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient); - //when/then - StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription() - .expectError(IllegalStateException.class).verify(); + //then + StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)) + .expectSubscription() + .expectError(IllegalStateException.class).verify(); } }
\ No newline at end of file diff --git a/rest-services/cbs-client/src/test/resources/sample_config_binding_service.json b/rest-services/cbs-client/src/test/resources/sample_config_binding_service.json new file mode 100644 index 00000000..3779344f --- /dev/null +++ b/rest-services/cbs-client/src/test/resources/sample_config_binding_service.json @@ -0,0 +1,44 @@ +[ + { + "ID": "9c8dd674-34ce-7049-d318-e98d93a64303", + "Node": "dcae-bootstrap", + "Address": "10.42.52.82", + "Datacenter": "dc1", + "TaggedAddresses": { + "lan": "10.42.52.82", + "wan": "10.42.52.82" + }, + "NodeMeta": { + "consul-network-segment": "" + }, + "ServiceID": "dcae-cbs1", + "ServiceName": "config-binding-service", + "ServiceTags": [], + "ServiceAddress": "config-binding-service", + "ServicePort": 10000, + "ServiceEnableTagOverride": false, + "CreateIndex": 14352, + "ModifyIndex": 14352 + }, + { + "ID": "35c6f540-a29c-1a92-23b0-1305bd8c81f5", + "Node": "dev-consul-server-1", + "Address": "10.42.165.51", + "Datacenter": "dc1", + "TaggedAddresses": { + "lan": "10.42.165.51", + "wan": "10.42.165.51" + }, + "NodeMeta": { + "consul-network-segment": "" + }, + "ServiceID": "dcae-cbs1", + "ServiceName": "config-binding-service", + "ServiceTags": [], + "ServiceAddress": "config-binding-service", + "ServicePort": 10000, + "ServiceEnableTagOverride": false, + "CreateIndex": 803, + "ModifyIndex": 803 + } +]
\ No newline at end of file |