From ec06ba416837d760e0d17de953f956ab87cd4248 Mon Sep 17 00:00:00 2001 From: wasala Date: Fri, 16 Nov 2018 15:08:25 +0100 Subject: Added cbs API * one main cloudConfigurationProvider * Mixing string and object property passed to API * added maven-shade plugin Change-Id: Idcd66fd70c20c859b5b6f94524f3fdb709fdfc57 Issue-ID: DCAEGEN2-982 Signed-off-by: wasala --- rest-services/cbs-client/pom.xml | 39 +++++++ .../sdk/rest/services/cbs/client/DummyClient.java | 35 ------ .../client/http/configuration/CloudHttpClient.java | 97 ++++++++++++++++ .../client/http/configuration/EnvProperties.java | 48 ++++++++ .../client/providers/CloudConfigurationClient.java | 116 ++++++++++++++++++++ .../providers/CloudConfigurationProvider.java | 90 +++++++++++++++ .../ReactiveCloudConfigurationProvider.java | 122 +++++++++++++++++++++ .../cbs-client/src/main/resources/logback.xml | 38 +++++++ .../rest/services/cbs/client/DummyClientTest.java | 30 ----- .../http/configuration/CloudHttpClientTest.java | 80 ++++++++++++++ .../CloudConfiguratonProviderWrapperTest.java | 30 +++++ .../ReactiveCloudConfigurationProviderTest.java | 96 ++++++++++++++++ .../cbs-client/src/test/resources/logback-test.xml | 21 ++++ rest-services/dmaap-client/pom.xml | 2 +- 14 files changed, 778 insertions(+), 66 deletions(-) delete mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java create mode 100644 rest-services/cbs-client/src/main/resources/logback.xml delete mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClientTest.java create mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClientTest.java create mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfiguratonProviderWrapperTest.java create mode 100644 rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java create mode 100644 rest-services/cbs-client/src/test/resources/logback-test.xml (limited to 'rest-services') diff --git a/rest-services/cbs-client/pom.xml b/rest-services/cbs-client/pom.xml index 5a03285e..ecde84ce 100644 --- a/rest-services/cbs-client/pom.xml +++ b/rest-services/cbs-client/pom.xml @@ -20,6 +20,36 @@ jar + + org.springframework + spring-webflux + + + org.immutables + value + provided + + + org.immutables + gson + + + io.projectreactor + reactor-core + + + ch.qos.logback + logback-classic + provided + + + org.slf4j + jul-to-slf4j + + + org.slf4j + log4j-over-slf4j + org.mockito mockito-core @@ -45,6 +75,15 @@ junit-vintage-engine test + + io.projectreactor + reactor-test + test + + + org.springframework + spring-context + diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java deleted file mode 100644 index 9a80eeda..00000000 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * DCAEGEN2-SERVICES-SDK - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. 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; - -/** - * @author Przemysław Wąsala on 11/14/18 - * @version 1.0.0 - * - * Sample Class empty class / checking that java documentation will be created by maven - */ -public class DummyClient { - - public void print() { - - } - -} diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java new file mode 100644 index 00000000..f3177e4c --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java @@ -0,0 +1,97 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.http.configuration; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.ExchangeFilterFunction; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +/** + * @author Przemysław Wąsala on 11/15/18 + */ + +public class CloudHttpClient { + + private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class); + + private final Gson gson; + private final WebClient webClient; + + public CloudHttpClient() { + this(WebClient.builder().filter(logRequest()).filter(logResponse()).build()); + } + + CloudHttpClient(WebClient webClient) { + this.gson = new Gson(); + this.webClient = webClient; + } + + public Mono callHttpGet(String url, Class genericClassDeclaration) { + return webClient + .get() + .uri(url) + .retrieve() + .onStatus(HttpStatus::is4xxClientError, response -> Mono.error(getException(response))) + .onStatus(HttpStatus::is5xxServerError, response -> Mono.error(getException(response))) + .bodyToMono(String.class) + .flatMap(body -> getJsonFromRequest(body, genericClassDeclaration)); + } + + private RuntimeException getException(ClientResponse response) { + return new RuntimeException(String.format("Request for cloud config failed: HTTP %d", + response.statusCode().value())); + } + + private Mono getJsonFromRequest(String body, Class genericClassDeclaration) { + try { + return Mono.just(parseJson(body, genericClassDeclaration)); + } catch (JsonSyntaxException | IllegalStateException e) { + return Mono.error(e); + } + } + + private T parseJson(String body, Class genericClassDeclaration) { + return gson.fromJson(body, genericClassDeclaration); + } + + private static ExchangeFilterFunction logResponse() { + return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { + LOGGER.info("Response status {}", clientResponse.statusCode()); + return Mono.just(clientResponse); + }); + } + + private static ExchangeFilterFunction logRequest() { + return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { + LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url()); + clientRequest.headers() + .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value))); + return Mono.just(clientRequest); + }); + } + +} diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java new file mode 100644 index 00000000..2438a2b8 --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.http.configuration; + +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 { + + @Value.Parameter + String consulHost(); + + @Value.Parameter + Integer consulPort(); + + @Value.Parameter + String cbsName(); + + @Value.Parameter + String appName(); +} 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 new file mode 100644 index 00000000..3fb8da60 --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java @@ -0,0 +1,116 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.providers; + +import com.google.gson.JsonObject; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +/** + * Complete CloudConfiguration HTTPClient API. + * + * @author Przemysław Wąsala on 11/16/18 + * @version 1.0.0 + * @since 1.0.0 + */ +@Service +public final class CloudConfigurationClient implements CloudConfigurationProvider { + + private final CloudConfigurationProvider cloudConfigurationProvider; + + /** + * Default constructor for CloudConfigurationClient, set CloudConfigurationProvider cloudConfigurationProvider + * property by calling: {@link ReactiveCloudConfigurationProvider}. + * Calls other constructor in this class {@link #CloudConfigurationClient(CloudConfigurationProvider)}. + */ + public CloudConfigurationClient() { + this(new ReactiveCloudConfigurationProvider()); + } + + /** + * Constructor for CloudConfigurationClient, set loudConfigurationProvider cloudConfigurationProvider property + * by passing them in constructor {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider} + * implementation client. + * + * @param cloudConfigurationProvider - client provider for calling ConfigBindingService + */ + public CloudConfigurationClient( + CloudConfigurationProvider cloudConfigurationProvider) { + this.cloudConfigurationProvider = cloudConfigurationProvider; + } + + /** + * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider} + * + * @param consulHost - Hostname/IPAddress of consul Database + * @param consulPort - Port number of consul Database + * @param cbsName - ConfigBindingService url + * @param appName - ApplicationName for each config will be returned + */ + @Override + public Mono callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName, + String appName) { + return cloudConfigurationProvider.callForServiceConfigurationReactive( + ImmutableEnvProperties.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 + * been defined in dcaegen2 cloud environment. + */ + @Override + public Mono callForServiceConfigurationReactive(EnvProperties envProperties) { + return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties); + } + + /** + * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider}. + * + * @param consulHost - Hostname/IPAddress of consul Database + * @param consulPort - Port number of consul Database + * @param cbsName - ConfigBindingService url + * @param appName - ApplicationName for each config will be returned + */ + @Override + public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) { + return cloudConfigurationProvider.callForServiceConfigurationReactive( + ImmutableEnvProperties.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 + */ + @Override + public JsonObject callForServiceConfiguration(EnvProperties envProperties) { + return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties).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 new file mode 100644 index 00000000..f8486f6f --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java @@ -0,0 +1,90 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.providers; + +import com.google.gson.JsonObject; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties; +import reactor.core.publisher.Mono; + +/** + * Rest CloudConfigurationClient interface which will define all necessary methods in connection with downloading JSON + * for each component in DCAEGEN2 repository. + * + * This interface holds contract for user-defined client or our defined implementation in CBS-client module. + * + * @author Przemysław Wąsala on 11/16/18 + * @version 1.0.0 has got only one implementation of this interface: 1. ReactiveCloudConfigurationProvider.class + * @since 1.0.0 + */ +public interface CloudConfigurationProvider { + + /* callForServiceConfigurationReactive */ + + /** + * Getting configuration for appName from ConfigBindingService. + * + * @param envProperties - 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); + + /* callForServiceConfigurationReactive */ + + /** + * Getting configuration for appName from ConfigBindingService. + * + * @param consulHost - Hostname/IPAddress of consul Database + * @param consulPort - Port number of consul Database + * @param cbsName - ConfigBindingService url + * @param appName - ApplicationName for each config will be returned + * @return rective configuration for specified application in dcaegen2 cloud infrastructure. + */ + Mono callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName, + String appName); + + + /*callForServiceConfiguration*/ + + /** + * Getting configuration for appName from ConfigBindingService. + * + * @param consulHost - Hostname/IPAddress of consul Database + * @param consulPort - Port number of consul Database + * @param cbsName - ConfigBindingService url + * @param appName - ApplicationName for each config will be returned + * @return configuration for specified application in dcaegen2 cloud infrastructure. + */ + JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName); + + /*callForServiceConfiguration*/ + + /** + * Getting configuration for appName from ConfigBindingService. + * + * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @return configuration for specified application in dcaegen2 cloud infrastructure. + */ + JsonObject callForServiceConfiguration(EnvProperties envProperties); + +} 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 new file mode 100644 index 00000000..88072985 --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java @@ -0,0 +1,122 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.providers; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.util.DefaultUriBuilderFactory; +import reactor.core.publisher.Mono; + +/** + * @author Przemysław Wąsala on 11/15/18 + */ +final class ReactiveCloudConfigurationProvider implements CloudConfigurationProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveCloudConfigurationProvider.class); + private static final String EXCEPTION_MESSAGE = "Unsupported method call: "; + + private final CloudHttpClient cloudHttpClient; + + public ReactiveCloudConfigurationProvider() { + this(new CloudHttpClient()); + } + + ReactiveCloudConfigurationProvider( + CloudHttpClient cloudHttpClient) { + this.cloudHttpClient = cloudHttpClient; + } + + @Override + public Mono callForServiceConfigurationReactive(EnvProperties envProperties) { + return callConsulForConfigBindingServiceEndpoint(envProperties) + .flatMap(this::callConfigBindingServiceForPrhConfiguration); + } + + @Override + public Mono callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName, + String appName) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this); + } + + @Override + public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this); + } + + @Override + public JsonObject callForServiceConfiguration(EnvProperties envProperties) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this); + } + + private Mono callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { + LOGGER.info("Retrieving Config Binding Service endpoint from Consul"); + return cloudHttpClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class) + .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName())); + } + + private String getConsulUrl(EnvProperties envProperties) { + return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service", + envProperties.cbsName()); + } + + private Mono callConfigBindingServiceForPrhConfiguration(String configBindingServiceUri) { + LOGGER.info("Retrieving PRH configuration"); + return cloudHttpClient.callHttpGet(configBindingServiceUri, JsonObject.class); + } + + + private Mono createConfigBindingServiceUrl(JsonArray jsonArray, String appName) { + return getConfigBindingObject(jsonArray) + .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName)); + } + + private Mono buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) { + return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(), + jsonObject.get("ServicePort").getAsInt(), "/service_component", appName)); + } + + private Mono getConfigBindingObject(JsonArray jsonArray) { + try { + if (jsonArray.size() > 0) { + return Mono.just(jsonArray.get(0).getAsJsonObject()); + } else { + throw new IllegalStateException("JSON Array was empty"); + } + } catch (IllegalStateException e) { + LOGGER.warn("Failed to retrieve JSON Object from array", e); + return Mono.error(e); + } + } + + private String getUri(String host, Integer port, String... paths) { + return new DefaultUriBuilderFactory().builder() + .scheme("http") + .host(host) + .port(port) + .path(String.join("/", paths)) + .build().toString(); + } +} diff --git a/rest-services/cbs-client/src/main/resources/logback.xml b/rest-services/cbs-client/src/main/resources/logback.xml new file mode 100644 index 00000000..09ecdb38 --- /dev/null +++ b/rest-services/cbs-client/src/main/resources/logback.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + ${defaultPattern} + + ${log-path}/${outputFilename}.log + + ${archive}/${outputFilename}.%d{yyyy-MM-dd}.%i.log.zip + ${maxFileSize} + ${maxHistory} + ${totalSizeCap} + + + + + + diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClientTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClientTest.java deleted file mode 100644 index 97843426..00000000 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClientTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * DCAEGEN2-SERVICES-SDK - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. 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; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author Przemysław Wąsala on 11/14/18 - */ -class DummyClientTest { - -} \ 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/http/configuration/CloudHttpClientTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClientTest.java new file mode 100644 index 00000000..e3e7a1d3 --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClientTest.java @@ -0,0 +1,80 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.http.configuration; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import org.junit.jupiter.api.Test; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +/** + * @author Przemysław Wąsala on 11/16/18 + */ +class CloudHttpClientTest { + + private static final String SOMEURL = "http://someurl"; + private static final String DATA = "{}"; + private Gson gson = new Gson(); + private WebClient webClient = mock(WebClient.class); + private WebClient.RequestHeadersUriSpec requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class); + private WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class); + + @Test + void shouldReturnJsonObjectOnGetCall() { + //given + mockWebClientDependantObject(); + CloudHttpClient httpGetClient = new CloudHttpClient(webClient); + when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(DATA)); + + //when/then + StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription() + .expectNext(gson.fromJson(DATA, JsonObject.class)).verifyComplete(); + } + + @Test + void shouldReturnMonoErrorOnGetCall() { + //given + mockWebClientDependantObject(); + CloudHttpClient httpGetClient = new CloudHttpClient(webClient); + when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("some wrong data")); + + //when/then + StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription() + .expectError(JsonSyntaxException.class).verify(); + } + + + private void mockWebClientDependantObject() { + doReturn(requestBodyUriSpec).when(webClient).get(); + when(requestBodyUriSpec.uri(SOMEURL)).thenReturn(requestBodyUriSpec); + doReturn(responseSpec).when(requestBodyUriSpec).retrieve(); + doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); + doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); + } +} \ 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/CloudConfiguratonProviderWrapperTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfiguratonProviderWrapperTest.java new file mode 100644 index 00000000..746ad43f --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfiguratonProviderWrapperTest.java @@ -0,0 +1,30 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.providers; + + +/** + * @author Przemysław Wąsala on 11/16/18 + */ +class CloudConfiguratonProviderWrapperTest { + +} \ 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 new file mode 100644 index 00000000..afc5a255 --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java @@ -0,0 +1,96 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.providers; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +/** + * @author Przemysław Wąsala on 11/15/18 + */ +class ReactiveCloudConfigurationProviderTest { + + private static final Gson gson = new Gson(); + private static final String configBindingService = "[{\"ID\":\"9c8dd674-34ce-7049-d318-e98d93a64303\",\"Node\"" + + ":\"dcae-bootstrap\",\"Address\":\"10.42.52.82\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":" + + "{\"lan\":\"10.42.52.82\",\"wan\":\"10.42.52.82\"},\"NodeMeta\":{\"consul-network-segment\":\"\"}," + + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[]," + + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false," + + "\"CreateIndex\":14352,\"ModifyIndex\":14352},{\"ID\":\"35c6f540-a29c-1a92-23b0-1305bd8c81f5\",\"Node\":" + + "\"dev-consul-server-1\",\"Address\":\"10.42.165.51\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":" + + "{\"lan\":\"10.42.165.51\",\"wan\":\"10.42.165.51\"},\"NodeMeta\":{\"consul-network-segment\":\"\"}," + + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[]," + + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false," + + "\"CreateIndex\":803,\"ModifyIndex\":803}]"; + private static final JsonArray configBindingServiceJson = gson.fromJson(configBindingService, JsonArray.class); + private static final JsonArray emptyConfigBindingServiceJson = gson.fromJson("[]", JsonArray.class); + private static final String prhMockConfiguration = "{\"test\":1}"; + private static final JsonObject prhMockConfigurationJson = gson.fromJson(prhMockConfiguration, JsonObject.class); + + private EnvProperties envProperties = ImmutableEnvProperties.builder() + .appName("dcae-prh") + .cbsName("config-binding-service") + .consulHost("consul") + .consulPort(8500) + .build(); + + @Test + void shouldReturnPrhConfiguration() { + // given + CloudHttpClient webClient = mock(CloudHttpClient.class); + when( + webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) + .thenReturn(Mono.just(configBindingServiceJson)); + when(webClient.callHttpGet("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class)) + .thenReturn(Mono.just(prhMockConfigurationJson)); + + ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient); + + //when/then + StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription() + .expectNext(prhMockConfigurationJson).verifyComplete(); + } + + @Test + void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() { + // given + CloudHttpClient webClient = mock(CloudHttpClient.class); + when( + webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) + .thenReturn(Mono.just(emptyConfigBindingServiceJson)); + + ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient); + + //when/then + StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription() + .expectError(IllegalStateException.class).verify(); + } +} \ No newline at end of file diff --git a/rest-services/cbs-client/src/test/resources/logback-test.xml b/rest-services/cbs-client/src/test/resources/logback-test.xml new file mode 100644 index 00000000..c1f00665 --- /dev/null +++ b/rest-services/cbs-client/src/test/resources/logback-test.xml @@ -0,0 +1,21 @@ + + + + + diff --git a/rest-services/dmaap-client/pom.xml b/rest-services/dmaap-client/pom.xml index 3f1d2581..6d7ec332 100644 --- a/rest-services/dmaap-client/pom.xml +++ b/rest-services/dmaap-client/pom.xml @@ -10,7 +10,7 @@ 1.0.0-SNAPSHOT ../pom.xml - + org.onap.dcaegen2.services.sdk.rest.services dmaap-client 1.0.0-SNAPSHOT -- cgit 1.2.3-korg