diff options
author | Filip Krzywka <filip.krzywka@nokia.com> | 2019-05-28 12:28:54 +0200 |
---|---|---|
committer | Filip Krzywka <filip.krzywka@nokia.com> | 2019-05-30 12:46:02 +0200 |
commit | da54f3e4c7e825908ef332e7913e1c4eb4fa82d1 (patch) | |
tree | d6560f5ae3af50a48fc6a364e0bec19c98ae9d5a /rest-services/cbs-client/src/test | |
parent | 7e893708bbdc36698a5d90502b318a5fd4f3be21 (diff) |
Omit CBS lookup in Consul
This is first iteration of changes related to switching from "CBS lookup"
mechanism to direct CBS usage.
Change-Id: I910612c689e10be5d9506643c61ae3cd8ffd0dca
Issue-ID: DCAEGEN2-1521
Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
Diffstat (limited to 'rest-services/cbs-client/src/test')
-rw-r--r-- | rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientConfigurationTest.java (renamed from rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/EnvPropertiesTest.java) | 6 | ||||
-rw-r--r-- | rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequestsTest.java | 2 | ||||
-rw-r--r-- | rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java | 53 | ||||
-rw-r--r-- | rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookupTest.java | 96 | ||||
-rw-r--r-- | rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java | 12 |
5 files changed, 42 insertions, 127 deletions
diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/EnvPropertiesTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientConfigurationTest.java index 73217cdb..e00fd6bd 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/EnvPropertiesTest.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientConfigurationTest.java @@ -24,15 +24,15 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; /** * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> * @since February 2019 */ -class EnvPropertiesTest { +class CbsClientConfigurationTest { @Test void fromEnvironmentShouldFailWhenEnvVariablesAreMissing() { - assertThatExceptionOfType(NullPointerException.class).isThrownBy(EnvProperties::fromEnvironment); + assertThatExceptionOfType(NullPointerException.class).isThrownBy(CbsClientConfiguration::fromEnvironment); } }
\ 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/api/CbsRequestsTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequestsTest.java index 50233d3c..d2229a52 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequestsTest.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequestsTest.java @@ -24,8 +24,6 @@ import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest; -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 org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; /** 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 eb1f2b3e..43b2a7bb 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,12 +24,13 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.onap.dcaegen2.services.sdk.model.streams.StreamType.KAFKA; import static org.onap.dcaegen2.services.sdk.model.streams.StreamType.MESSAGE_ROUTER; 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 static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamPredicates.streamOfType; 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; @@ -47,8 +48,8 @@ import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.DataS import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParser; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParsers; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest; -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 org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -60,34 +61,25 @@ import reactor.test.StepVerifier; */ class CbsClientImplIT { - private static final String CONSUL_RESPONSE = "[\n" - + " {\n" - + " \"ServiceAddress\": \"HOST\",\n" - + " \"ServiceName\": \"the_cbs\",\n" - + " \"ServicePort\": PORT\n" - + " }\n" - + "]\n"; private static final String SAMPLE_CONFIG = "/sample_service_config.json"; private static final String SAMPLE_ALL = "/sample_all.json"; private static final String SAMPLE_KEY = "/sample_key.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 CbsClientConfiguration sampleConfiguration; 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, SAMPLE_CONFIG)) + routes.get("/service_component/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_CONFIG)) .get("/service_component_all/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_ALL)) .get("/sampleKey/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_KEY)) ); - sampleEnvironment = ImmutableEnvProperties.builder() + sampleConfiguration = ImmutableCbsClientConfiguration.builder() .appName("dcae-component") - .cbsName("the_cbs") - .consulHost(server.host()) - .consulPort(server.port()) + .hostname(server.host()) + .port(server.port()) .build(); } @@ -99,7 +91,7 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleCall() { // given - final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // when @@ -115,7 +107,7 @@ class CbsClientImplIT { @Test void testCbsClientWithPeriodicCall() { // given - final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // when @@ -133,7 +125,7 @@ class CbsClientImplIT { @Test void testCbsClientWithUpdatesCall() { // given - final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); final Duration period = Duration.ofMillis(10); @@ -152,7 +144,7 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsing() { // given - final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfiguration); final StreamFromGsonParser<KafkaSink> kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser(); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -176,7 +168,7 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsingUsingSwitch() { // given - final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // TODO: Use these parsers below final StreamFromGsonParser<KafkaSink> kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser(); @@ -212,7 +204,7 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsingWhenUsingInvalidParser() { // given - final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfiguration); final StreamFromGsonParser<KafkaSource> kafkaSourceParser = StreamFromGsonParsers.kafkaSourceParser(); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -236,7 +228,7 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleAllRequest() { // given - final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getAll(RequestDiagnosticContext.create()); // when @@ -257,7 +249,7 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleKeyRequest() { // given - final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getByKey(RequestDiagnosticContext.create(), "sampleKey"); // when @@ -276,7 +268,7 @@ class CbsClientImplIT { @Test void testCbsClientWhenTheConfigurationWasNotFound() { // given - final EnvProperties unknownAppEnv = ImmutableEnvProperties.copyOf(sampleEnvironment).withAppName("unknown_app"); + final CbsClientConfiguration unknownAppEnv = ImmutableCbsClientConfiguration.copyOf(sampleConfiguration).withAppName("unknown_app"); final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(unknownAppEnv); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -293,13 +285,4 @@ class CbsClientImplIT { return obj.get(SAMPLE_CONFIG_KEY).getAsString(); } - private static Mono<String> lazyConsulResponse() { - return Mono.just(CONSUL_RESPONSE) - .map(CbsClientImplIT::processConsulResponseTemplate); - } - - private static String processConsulResponseTemplate(String resp) { - return resp.replaceAll("HOST", server.host()) - .replaceAll("PORT", Integer.toString(server.port())); - } } 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 e16605de..70f31c8b 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 @@ -20,30 +20,13 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration; -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; -import com.google.gson.JsonParser; -import java.io.InputStreamReader; import java.net.InetSocketAddress; -import org.junit.jupiter.api.Test; -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; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; + +import static org.assertj.core.api.Assertions.assertThat; /** * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> @@ -51,70 +34,21 @@ import reactor.test.StepVerifier; */ class CbsLookupTest { - private final EnvProperties env = ImmutableEnvProperties.builder() - .cbsName("cbs-service") - .consulHost("consul.local") - .consulPort(8050) + private static final String cbsHostname = "cbs-service"; + private static final int cbsPort = 10000; + private final CbsClientConfiguration configuration = ImmutableCbsClientConfiguration.builder() + .hostname(cbsHostname) + .port(cbsPort) .appName("whatever").build(); - private final RxHttpClient httpClient = mock(RxHttpClient.class); - private final CbsLookup cut = new CbsLookup(httpClient); + private final CbsLookup cut = new CbsLookup(); @Test - void lookupShouldReturnValidConfiguration() { - // given - givenConsulResponse(parseResource("/consul_cbs_service.json").getAsJsonArray()); - + void lookupShouldReturnValidSocketAddressFromEnvironment() { // when - final InetSocketAddress result = cut.lookup(env).block(); + final InetSocketAddress result = cut.lookup(configuration).block(); // 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); + assertThat(result.getHostString()).isEqualTo(cbsHostname); + assertThat(result.getPort()).isEqualTo(cbsPort); } - - @Test - void lookupShouldEmitErrorWhenServiceArrayIsEmpty() { - // given - givenConsulResponse(new JsonArray()); - - // when - final Mono<InetSocketAddress> result = cut.lookup(env); - - // then - StepVerifier.create(result).verifyError(ServiceLookupException.class); - } - - private JsonElement parseResource(String resource) { - return new JsonParser().parse(new InputStreamReader(CbsLookupTest.class.getResourceAsStream(resource))); - } - - private void givenConsulResponse(JsonArray 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/providers/ReactiveCloudConfigurationProviderTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java index fd9e0adc..de0870d0 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 @@ -38,8 +38,8 @@ 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 org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -56,7 +56,7 @@ class ReactiveCloudConfigurationProviderTest { private final RxHttpClient httpClient = mock(RxHttpClient.class); private final JsonArray configBindingService = GsonUtils.readObjectArrayFromResource("/sample_config_binding_service.json"); - private EnvProperties envProperties = ImmutableEnvProperties.builder() + private CbsClientConfiguration cbsClientConfiguration = ImmutableCbsClientConfiguration.builder() .appName("dcae-prh") .cbsName("config-binding-service") .consulHost("consul") @@ -85,7 +85,7 @@ class ReactiveCloudConfigurationProviderTest { //then - StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)) + StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration)) .expectSubscription() .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete(); } @@ -103,7 +103,7 @@ class ReactiveCloudConfigurationProviderTest { //then - StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)) + StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration)) .expectSubscription() .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete(); @@ -128,7 +128,7 @@ class ReactiveCloudConfigurationProviderTest { //then - StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)) + StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration)) .expectSubscription() .expectError(IllegalStateException.class).verify(); } |