From da54f3e4c7e825908ef332e7913e1c4eb4fa82d1 Mon Sep 17 00:00:00 2001 From: Filip Krzywka Date: Tue, 28 May 2019 12:28:54 +0200 Subject: 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 --- .../services/cbs/client/api/CbsClientFactory.java | 16 +-- .../rest/services/cbs/client/api/CbsRequests.java | 1 - .../rest/services/cbs/client/impl/CbsLookup.java | 63 ++--------- .../cbs/client/model/CbsClientConfiguration.java | 115 +++++++++++++++++++++ .../services/cbs/client/model/EnvProperties.java | 79 -------------- .../client/providers/CloudConfigurationClient.java | 20 ++-- .../providers/CloudConfigurationProvider.java | 10 +- .../ReactiveCloudConfigurationProvider.java | 20 ++-- .../cbs/client/api/CbsClientConfigurationTest.java | 38 +++++++ .../services/cbs/client/api/CbsRequestsTest.java | 2 - .../services/cbs/client/api/EnvPropertiesTest.java | 38 ------- .../services/cbs/client/impl/CbsClientImplIT.java | 53 ++++------ .../services/cbs/client/impl/CbsLookupTest.java | 96 +++-------------- .../ReactiveCloudConfigurationProviderTest.java | 12 +-- 14 files changed, 233 insertions(+), 330 deletions(-) create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java delete mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/EnvProperties.java create mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientConfigurationTest.java delete mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/EnvPropertiesTest.java (limited to 'rest-services/cbs-client/src') 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 { *

Creates Mono which will emit instance of {@link CbsClient} when service discovery is complete.

* *

- * 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. *

*

* 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. *

* - * @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 createCbsClient(EnvProperties env) { + public static @NotNull Mono 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 Piotr Jaszczyk * @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 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 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 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 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/CbsClientConfiguration.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java new file mode 100644 index 00000000..e3c7d2ea --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java @@ -0,0 +1,115 @@ +/* + * ============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.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 + * CloudConfigurationClient.class + * + * @author Przemysław Wąsala on 11/16/18 + * @version 1.0.0 can be passed to ReactiveCloudConfigurationProvider, can be constructed out of + * org.onap.dcaegen2.services.sdk library. + * @since 1.0.0 + */ +@Value.Immutable(prehash = true) +public interface CbsClientConfiguration { + + /** + * Name of environment variable containing Config Binding Service network hostname. + */ + String ENV_CBS_HOSTNAME = "CONFIG_BINDING_SERVICE"; + + /** + * Name of environment variable containing Config Binding Service network port. + */ + String ENV_CBS_PORT = "CONFIG_BINDING_SERVICE_SERVICE_PORT"; + + /** + * Name of environment variable containing current application name. + */ + String ENV_APP_NAME = "HOSTNAME"; + + /** + * 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 service name 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 + @Nullable + String hostname(); + + @Value.Parameter + @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 CbsClientConfiguration from system environment variables. + * + * @return an instance of CbsClientConfiguration + * @throws NullPointerException when at least one of required parameters is missing + */ + static CbsClientConfiguration fromEnvironment() { + return ImmutableCbsClientConfiguration.builder() + .consulHost(System.getenv(ENV_CONSUL_HOST)) + .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/model/EnvProperties.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/EnvProperties.java deleted file mode 100644 index 8a40c2c0..00000000 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/EnvProperties.java +++ /dev/null @@ -1,79 +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.model; - -import org.immutables.value.Value; - -/** - * Immutable object which helps with construction of cloudRequestObject for specified Client. For usage take a look in - * CloudConfigurationClient.class - * - * @author Przemysław Wąsala on 11/16/18 - * @version 1.0.0 can be passed to ReactiveCloudConfigurationProvider, can be constructed out of - * org.onap.dcaegen2.services.sdk library. - * @since 1.0.0 - */ -@Value.Immutable(prehash = true) -public interface EnvProperties { - - /** - * Name of environment variable containing Consul host name. - */ - String ENV_CONSUL_HOST = "CONSUL_HOST"; - - /** - * Name of environment variable containing Config Binding Service service name as registered in Consul - * services API. - */ - String ENV_CBS_NAME = "CONFIG_BINDING_SERVICE"; - - /** - * Name of environment variable containing current application name. - */ - String ENV_APP_NAME = "HOSTNAME"; - - @Value.Parameter - String consulHost(); - - @Value.Parameter - Integer consulPort(); - - @Value.Parameter - String cbsName(); - - @Value.Parameter - String appName(); - - /** - * Creates EnvProperties from system environment variables. - * - * @return an instance of EnvProperties - * @throws NullPointerException when at least one of required parameters is missing - */ - static EnvProperties fromEnvironment() { - return ImmutableEnvProperties.builder() - .consulHost(System.getenv(ENV_CONSUL_HOST)) - .consulPort(8500) - .cbsName(System.getenv(ENV_CBS_NAME)) - .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 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 callForServiceConfigurationReactive(EnvProperties envProperties) { - return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties); + public Mono 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 callForServiceConfigurationReactive(EnvProperties envProperties); + Mono 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 callForServiceConfigurationReactive(EnvProperties envProperties) { - return callConsulForConfigBindingServiceEndpoint(envProperties) + public Mono 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 callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { + private Mono 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 callConfigBindingServiceForConfiguration(String configBindingServiceUri) { diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientConfigurationTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientConfigurationTest.java new file mode 100644 index 00000000..e00fd6bd --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientConfigurationTest.java @@ -0,0 +1,38 @@ +/* + * ============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.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.CbsClientConfiguration; + +/** + * @author Piotr Jaszczyk + * @since February 2019 + */ +class CbsClientConfigurationTest { + @Test + void fromEnvironmentShouldFailWhenEnvVariablesAreMissing() { + 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/api/EnvPropertiesTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/EnvPropertiesTest.java deleted file mode 100644 index 73217cdb..00000000 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/EnvPropertiesTest.java +++ /dev/null @@ -1,38 +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.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; - -/** - * @author Piotr Jaszczyk - * @since February 2019 - */ -class EnvPropertiesTest { - @Test - void fromEnvironmentShouldFailWhenEnvVariablesAreMissing() { - assertThatExceptionOfType(NullPointerException.class).isThrownBy(EnvProperties::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/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 sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // when @@ -115,7 +107,7 @@ class CbsClientImplIT { @Test void testCbsClientWithPeriodicCall() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // when @@ -133,7 +125,7 @@ class CbsClientImplIT { @Test void testCbsClientWithUpdatesCall() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono 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 sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); final StreamFromGsonParser kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser(); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -176,7 +168,7 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsingUsingSwitch() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); // TODO: Use these parsers below final StreamFromGsonParser kafkaSinkParser = StreamFromGsonParsers.kafkaSinkParser(); @@ -212,7 +204,7 @@ class CbsClientImplIT { @Test void testCbsClientWithStreamsParsingWhenUsingInvalidParser() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); final StreamFromGsonParser kafkaSourceParser = StreamFromGsonParsers.kafkaSourceParser(); final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create()); @@ -236,7 +228,7 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleAllRequest() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono sut = CbsClientFactory.createCbsClient(sampleConfiguration); final CbsRequest request = CbsRequests.getAll(RequestDiagnosticContext.create()); // when @@ -257,7 +249,7 @@ class CbsClientImplIT { @Test void testCbsClientWithSingleKeyRequest() { // given - final Mono sut = CbsClientFactory.createCbsClient(sampleEnvironment); + final Mono 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 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 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 Piotr Jaszczyk @@ -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 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 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(); } -- cgit 1.2.3-korg