diff options
Diffstat (limited to 'rest-services/cbs-client/src')
12 files changed, 135 insertions, 232 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 053c60c5..821805fc 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 @@ -24,7 +24,7 @@ import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClientFactory; 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; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import reactor.core.publisher.Mono; /** @@ -40,24 +40,24 @@ public class CbsClientFactory { * <p>Creates Mono which will emit instance of {@link CbsClient} when service discovery is complete.</p> * * <p> - * This method will do a lookup of Config Binding Service using Consul as service discovery mechanism and create - * client configured with found address. Created client will be published in returned Mono instance. + * This method will do a lookup of Config Binding Service and create client configured with found address. + * Created client will be published in returned Mono instance. * </p> * <p> * In case of failure during CBS resolution, returned Mono will emit error signal with possible cause. * User is expected to handle this signal and possibly retry subscription to returned Mono. * </p> * - * @param env required environment properties + * @param configuration required CBS configuration as viewed by client application * @return non-null {@link Mono} of {@link CbsClient} instance * @since 1.1.2 */ - public static @NotNull Mono<CbsClient> createCbsClient(EnvProperties env) { + public static @NotNull Mono<CbsClient> createCbsClient(CbsClientConfiguration configuration) { return Mono.defer(() -> { final RxHttpClient httpClient = RxHttpClientFactory.create(); - final CbsLookup lookup = new CbsLookup(httpClient); - return lookup.lookup(env) - .map(addr -> new CbsClientImpl(httpClient, env.appName(), addr)); + final CbsLookup lookup = new CbsLookup(); + return lookup.lookup(configuration) + .map(addr -> new CbsClientImpl(httpClient, configuration.appName(), addr)); }); } } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequests.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequests.java index 3724338d..99ad3b20 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequests.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequests.java @@ -22,7 +22,6 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api; import org.jetbrains.annotations.NotNull; 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.ImmutableCbsRequest; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; 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 99058772..c07ed8e4 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 @@ -20,20 +20,13 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import java.net.InetSocketAddress; -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 org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; +import java.net.InetSocketAddress; + /** * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> * @since February 2019 @@ -41,56 +34,16 @@ import reactor.core.publisher.Mono; public class CbsLookup { private static final Logger LOGGER = LoggerFactory.getLogger(CbsLookup.class); - private static final String CONSUL_JSON_SERVICE_ADDRESS = "ServiceAddress"; - private static final String CONSUL_JSON_SERVICE_PORT = "ServicePort"; - private final RxHttpClient httpClient; - - public CbsLookup(RxHttpClient httpClient) { - this.httpClient = httpClient; - } - public Mono<InetSocketAddress> lookup(EnvProperties env) { - return Mono.fromCallable(() -> createConsulUrl(env)) - .doOnNext(this::logConsulRequestUrl) - .flatMap(this::fetchHttpData) - .doOnNext(this::logConsulResponse) - .flatMap(this::firstService) - .map(this::parseServiceEntry) + public Mono<InetSocketAddress> lookup(CbsClientConfiguration configuration) { + return Mono.just(createCbsAddress(configuration)) .doOnNext(this::logCbsServiceAddress); } - private String createConsulUrl(EnvProperties env) { - return String.format("http://%s:%s/v1/catalog/service/%s", env.consulHost(), env.consulPort(), env.cbsName()); - } - - private void logConsulRequestUrl(String consulUrl) { - LOGGER.debug("Calling Consul for CBS address. consulUrl={}", consulUrl); - } - - private Mono<JsonArray> fetchHttpData(String consulUrl) { - return httpClient.call( - ImmutableHttpRequest.builder() - .method(HttpMethod.GET) - .url(consulUrl) - .build()) - .doOnNext(HttpResponse::throwIfUnsuccessful) - .map(resp -> resp.bodyAsJson(JsonArray.class)); - } - - private void logConsulResponse(JsonArray consulResponse) { - LOGGER.debug("Consul response with CBS service list. Will use 1st one. response={}", consulResponse); - } - - private Mono<JsonObject> firstService(JsonArray services) { - return services.size() == 0 - ? Mono.error(new ServiceLookupException("Consul server did not return any service with given name")) - : Mono.just(services.get(0).getAsJsonObject()); - } - - private InetSocketAddress parseServiceEntry(JsonObject service) { + private InetSocketAddress createCbsAddress(CbsClientConfiguration configuration) { return InetSocketAddress.createUnresolved( - service.get(CONSUL_JSON_SERVICE_ADDRESS).getAsString(), - service.get(CONSUL_JSON_SERVICE_PORT).getAsInt()); + configuration.hostname(), + configuration.port()); } private void logCbsServiceAddress(InetSocketAddress address) { diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/EnvProperties.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java index 8a40c2c0..e3c7d2ea 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/EnvProperties.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java @@ -21,6 +21,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model; import org.immutables.value.Value; +import org.jetbrains.annotations.Nullable; /** * Immutable object which helps with construction of cloudRequestObject for specified Client. For usage take a look in @@ -32,47 +33,82 @@ import org.immutables.value.Value; * @since 1.0.0 */ @Value.Immutable(prehash = true) -public interface EnvProperties { +public interface CbsClientConfiguration { /** - * Name of environment variable containing Consul host name. + * Name of environment variable containing Config Binding Service network hostname. */ - String ENV_CONSUL_HOST = "CONSUL_HOST"; + String ENV_CBS_HOSTNAME = "CONFIG_BINDING_SERVICE"; /** - * Name of environment variable containing Config Binding Service <em>service name</em> as registered in Consul - * services API. + * Name of environment variable containing Config Binding Service network port. */ - String ENV_CBS_NAME = "CONFIG_BINDING_SERVICE"; + String ENV_CBS_PORT = "CONFIG_BINDING_SERVICE_SERVICE_PORT"; /** * Name of environment variable containing current application name. */ String ENV_APP_NAME = "HOSTNAME"; - @Value.Parameter - String consulHost(); + /** + * Name of environment variable containing Consul host name. + * + * @deprecated CBS lookup in Consul service should not be needed, + * instead {@link #ENV_CBS_HOSTNAME} should be used directly. + */ + @Deprecated + String ENV_CONSUL_HOST = "CONSUL_HOST"; + + /** + * Name of environment variable containing Config Binding Service <em>service name</em> as registered in Consul + * services API. + * + * @deprecated CBS lookup in Consul service should not be needed, + * instead {@link #ENV_CBS_HOSTNAME} should be used directly. + */ + @Deprecated + String ENV_CBS_NAME = "CONFIG_BINDING_SERVICE"; @Value.Parameter - Integer consulPort(); + @Nullable + String hostname(); @Value.Parameter - String cbsName(); + @Nullable + Integer port(); @Value.Parameter String appName(); + @Value.Default + @Deprecated + default String consulHost() { + return "consul-server"; + } + + @Value.Default + @Deprecated + default Integer consulPort() { + return 8500; + } + + @Value.Default + @Deprecated + default String cbsName() { + return "config-binding-service"; + } + /** - * Creates EnvProperties from system environment variables. + * Creates CbsClientConfiguration from system environment variables. * - * @return an instance of EnvProperties + * @return an instance of CbsClientConfiguration * @throws NullPointerException when at least one of required parameters is missing */ - static EnvProperties fromEnvironment() { - return ImmutableEnvProperties.builder() + static CbsClientConfiguration fromEnvironment() { + return ImmutableCbsClientConfiguration.builder() .consulHost(System.getenv(ENV_CONSUL_HOST)) - .consulPort(8500) - .cbsName(System.getenv(ENV_CBS_NAME)) + .hostname(System.getenv(ENV_CBS_HOSTNAME)) + .port(Integer.valueOf(System.getenv(ENV_CBS_PORT))) .appName(System.getenv(ENV_APP_NAME)) .build(); } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java index 534397ca..e1dd7aff 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java @@ -22,8 +22,8 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers; import com.google.gson.JsonObject; -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; /** @@ -70,7 +70,7 @@ public final class CloudConfigurationClient implements CloudConfigurationProvide public Mono<JsonObject> callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName, String appName) { return cloudConfigurationProvider.callForServiceConfigurationReactive( - ImmutableEnvProperties.builder().consulHost(consulHost) + ImmutableCbsClientConfiguration.builder().consulHost(consulHost) .consulPort(consulPort).cbsName(cbsName) .appName(appName).build()); } @@ -78,12 +78,12 @@ public final class CloudConfigurationClient implements CloudConfigurationProvide /** * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider}. * - * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @param cbsClientConfiguration - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have * been defined in dcaegen2 cloud environment. */ @Override - public Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) { - return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties); + public Mono<JsonObject> callForServiceConfigurationReactive(CbsClientConfiguration cbsClientConfiguration) { + return cloudConfigurationProvider.callForServiceConfigurationReactive(cbsClientConfiguration); } /** @@ -97,7 +97,7 @@ public final class CloudConfigurationClient implements CloudConfigurationProvide @Override public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) { return cloudConfigurationProvider.callForServiceConfigurationReactive( - ImmutableEnvProperties.builder().consulHost(consulHost) + ImmutableCbsClientConfiguration.builder().consulHost(consulHost) .consulPort(consulPort).cbsName(cbsName) .appName(appName).build()).block(); } @@ -105,10 +105,10 @@ public final class CloudConfigurationClient implements CloudConfigurationProvide /** * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider}. * - * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @param cbsClientConfiguration - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have */ @Override - public JsonObject callForServiceConfiguration(EnvProperties envProperties) { - return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties).block(); + public JsonObject callForServiceConfiguration(CbsClientConfiguration cbsClientConfiguration) { + return cloudConfigurationProvider.callForServiceConfigurationReactive(cbsClientConfiguration).block(); } } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java index d82d5d31..c0dbd5f9 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java @@ -22,7 +22,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers; import com.google.gson.JsonObject; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import reactor.core.publisher.Mono; /** @@ -42,12 +42,12 @@ public interface CloudConfigurationProvider { /** * Getting configuration for appName from ConfigBindingService. * - * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @param cbsClientConfiguration - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have * been defined in dcaegen2 cloud environment. * @return Single reactive response which @Mono which holds inside them JsonObject with configuration for specified * application Name */ - Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties); + Mono<JsonObject> callForServiceConfigurationReactive(CbsClientConfiguration cbsClientConfiguration); /* callForServiceConfigurationReactive */ @@ -82,9 +82,9 @@ public interface CloudConfigurationProvider { /** * Getting configuration for appName from ConfigBindingService. * - * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @param cbsClientConfiguration - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have * @return configuration for specified application in dcaegen2 cloud infrastructure. */ - JsonObject callForServiceConfiguration(EnvProperties envProperties); + JsonObject callForServiceConfiguration(CbsClientConfiguration cbsClientConfiguration); } 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 dbc94802..a50edd38 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 @@ -28,7 +28,7 @@ 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.adapters.http.RxHttpClientFactory; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.uri.URI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,8 +53,8 @@ public final class ReactiveCloudConfigurationProvider implements CloudConfigurat } @Override - public Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) { - return callConsulForConfigBindingServiceEndpoint(envProperties) + public Mono<JsonObject> callForServiceConfigurationReactive(CbsClientConfiguration configuration) { + return callConsulForConfigBindingServiceEndpoint(configuration) .flatMap(this::callConfigBindingServiceForConfiguration); } @@ -70,28 +70,28 @@ public final class ReactiveCloudConfigurationProvider implements CloudConfigurat } @Override - public JsonObject callForServiceConfiguration(EnvProperties envProperties) { + public JsonObject callForServiceConfiguration(CbsClientConfiguration configuration) { throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this); } - private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { + private Mono<String> callConsulForConfigBindingServiceEndpoint(CbsClientConfiguration configuration) { LOGGER.info("Retrieving Config Binding Service endpoint from Consul"); HttpRequest httpRequest = ImmutableHttpRequest.builder() - .url(getConsulUrl(envProperties)).method(HttpMethod.GET).build(); + .url(getConsulUrl(configuration)).method(HttpMethod.GET).build(); return rxHttpClient.call(httpRequest) .map(resp -> resp.bodyAsJson(JsonArray.class)) .flatMap(jsonArray -> this.createConfigBindingServiceUrl( jsonArray, - envProperties.appName()) + configuration.appName()) ); } - private String getConsulUrl(EnvProperties envProperties) { - return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service", - envProperties.cbsName()); + private String getConsulUrl(CbsClientConfiguration configuration) { + return getUri(configuration.consulHost(), configuration.consulPort(), "/v1/catalog/service", + configuration.cbsName()); } private Mono<JsonObject> callConfigBindingServiceForConfiguration(String configBindingServiceUri) { 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(); } |