From 0c4750650a84caa64c6df79c5d4042916d529799 Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Wed, 27 Feb 2019 11:43:39 +0100 Subject: Add MDC support to CBS client Change-Id: Id1f1f9016b03658eca0afd0bd3bd724afc0bea96 Issue-ID: DCAEGEN2-1233 Signed-off-by: Piotr Jaszczyk --- .../services/cbs/client/impl/CbsClientImplIT.java | 58 ++++++++++++++++------ .../cbs/client/impl/CbsClientImplTest.java | 9 ++-- .../services/cbs/client/impl/CbsLookupTest.java | 2 +- .../ReactiveCloudConfigurationProviderTest.java | 6 +-- 4 files changed, 53 insertions(+), 22 deletions(-) (limited to 'rest-services/cbs-client/src/test/java') 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 761cc5c1..309bb62f 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 @@ -24,14 +24,17 @@ import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.Dummy import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendString; import com.google.gson.JsonObject; +import io.vavr.collection.Stream; 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.model.logging.RequestDiagnosticContext; 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.EnvProperties; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.ImmutableEnvProperties; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -41,21 +44,30 @@ import reactor.test.StepVerifier; */ class CbsClientImplIT { - private static final String CONSUL_RESP = "[\n" + private static final String CONSUL_RESPONSE = "[\n" + " {\n" + " \"ServiceAddress\": \"HOST\",\n" + " \"ServiceName\": \"the_cbs\",\n" + " \"ServicePort\": PORT\n" + " }\n" + "]\n"; - private static final String RES_CONFIG = "/sample_config.json"; + private static final String SAMPLE_CONFIG = "/sample_config.json"; + private static final String SAMPLE_CONFIG_KEY = "keystore.path"; + private static final String EXPECTED_CONFIG_VALUE = "/var/run/security/keystore.p12"; + private static EnvProperties sampleEnvironment; private static DummyHttpServer server; @BeforeAll static void setUp() { server = DummyHttpServer.start(routes -> routes.get("/v1/catalog/service/the_cbs", (req, resp) -> sendString(resp, lazyConsulResponse())) - .get("/service_component/dcae-component", (req, resp) -> sendResource(resp, RES_CONFIG))); + .get("/service_component/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_CONFIG))); + sampleEnvironment = ImmutableEnvProperties.builder() + .appName("dcae-component") + .cbsName("the_cbs") + .consulHost(server.host()) + .consulPort(server.port()) + .build(); } @AfterAll @@ -64,28 +76,44 @@ class CbsClientImplIT { } @Test - void testCbsClient() { + void testCbsClientWithSingleCall() { // given - final EnvProperties env = ImmutableEnvProperties.builder() - .appName("dcae-component") - .cbsName("the_cbs") - .consulHost(server.host()) - .consulPort(server.port()) - .build(); - final Mono sut = CbsClientFactory.createCbsClient(env); + final Mono sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create(); + + // when + final Mono result = sut.flatMap(cbsClient -> cbsClient.get(diagnosticContext)); + + // then + StepVerifier.create(result.map(this::sampleConfigValue)) + .expectNext(EXPECTED_CONFIG_VALUE) + .expectComplete() + .verify(Duration.ofSeconds(5)); + } + + @Test + void testCbsClientWithPeriodicCall() { + // given + final Mono sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create(); // when - final Mono result = sut.flatMap(CbsClient::get); + final Flux result = sut.flatMapMany(cbsClient -> cbsClient.get(diagnosticContext, Duration.ZERO, Duration.ofMillis(10))); // then - StepVerifier.create(result.map(obj -> obj.get("keystore.path").getAsString())) - .expectNext("/var/run/security/keystore.p12") + final int itemsToTake = 5; + StepVerifier.create(result.take(itemsToTake).map(this::sampleConfigValue)) + .expectNextSequence(Stream.of(EXPECTED_CONFIG_VALUE).cycle(itemsToTake)) .expectComplete() .verify(Duration.ofSeconds(5)); } + private String sampleConfigValue(JsonObject obj) { + return obj.get(SAMPLE_CONFIG_KEY).getAsString(); + } + private static Mono lazyConsulResponse() { - return Mono.just(CONSUL_RESP) + return Mono.just(CONSUL_RESPONSE) .map(CbsClientImplIT::processConsulResponseTemplate); } 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 65284c5f..606d00b3 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 @@ -30,6 +30,7 @@ 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.model.logging.RequestDiagnosticContext; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.adapters.CloudHttpClient; import reactor.core.publisher.Mono; @@ -47,13 +48,15 @@ class CbsClientImplTest { String serviceName = "dcaegen2-ves-collector"; final CbsClientImpl cut = CbsClientImpl.create(httpClient, cbsAddress, serviceName); final JsonObject httpResponse = new JsonObject(); - given(httpClient.callHttpGet(anyString(), any(Class.class))).willReturn(Mono.just(httpResponse)); + given(httpClient.get(anyString(), any(RequestDiagnosticContext.class), any(Class.class))).willReturn(Mono.just(httpResponse)); + RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create(); // when - final JsonObject result = cut.get().block(); + final JsonObject result = cut.get(diagnosticContext).block(); // then - verify(httpClient).callHttpGet("http://cbshost:6969/service_component/dcaegen2-ves-collector", JsonObject.class); + final String expectedUrl = "http://cbshost:6969/service_component/dcaegen2-ves-collector"; + verify(httpClient).get(expectedUrl, diagnosticContext, JsonObject.class); assertThat(result).isSameAs(httpResponse); } } \ 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 e7513852..b46b958b 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 @@ -87,7 +87,7 @@ class CbsLookupTest { + env.consulPort() + "/v1/catalog/service/" + env.cbsName(); - given(httpClient.callHttpGet(url, JsonArray.class)) + given(httpClient.get(url, JsonArray.class)) .willReturn(Mono.just(jsonArray)); } 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 4e8782b6..3f720c37 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 @@ -67,9 +67,9 @@ class ReactiveCloudConfigurationProviderTest { // given CloudHttpClient webClient = mock(CloudHttpClient.class); when( - webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) + webClient.get("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) .thenReturn(Mono.just(configBindingServiceJson)); - when(webClient.callHttpGet("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class)) + when(webClient.get("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class)) .thenReturn(Mono.just(configurationJsonMock)); ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient); @@ -84,7 +84,7 @@ class ReactiveCloudConfigurationProviderTest { // given CloudHttpClient webClient = mock(CloudHttpClient.class); when( - webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) + webClient.get("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) .thenReturn(Mono.just(emptyConfigBindingServiceJson)); ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient); -- cgit 1.2.3-korg