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;
/**
* <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));
});
}
}
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;
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
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) {
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
* @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();
}
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;
/**
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());
}
/**
* 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);
}
/**
@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();
}
/**
* 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();
}
}
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;
/**
/**
* 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 */
/**
* 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);
}
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;
}
@Override
- public Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) {
- return callConsulForConfigBindingServiceEndpoint(envProperties)
+ public Mono<JsonObject> callForServiceConfigurationReactive(CbsClientConfiguration configuration) {
+ return callConsulForConfigBindingServiceEndpoint(configuration)
.flatMap(this::callConfigBindingServiceForConfiguration);
}
}
@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) {
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
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;
/**
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;
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;
*/
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();
}
@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
@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
@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);
@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());
@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();
@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());
@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
@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
@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());
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()));
- }
}
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>
*/
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
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;
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")
//then
- StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
+ StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration))
.expectSubscription()
.expectNext(CONFIGURATION_JSON_MOCK).verifyComplete();
}
//then
- StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
+ StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration))
.expectSubscription()
.expectNext(CONFIGURATION_JSON_MOCK).verifyComplete();
//then
- StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
+ StepVerifier.create(provider.callForServiceConfigurationReactive(cbsClientConfiguration))
.expectSubscription()
.expectError(IllegalStateException.class).verify();
}