aboutsummaryrefslogtreecommitdiffstats
path: root/datafile-app-server
diff options
context:
space:
mode:
authormicdzied <michal.1.dziedzic@nokia.com>2019-01-08 09:38:12 +0100
committermicdzied <michal.1.dziedzic@nokia.com>2019-01-08 09:40:22 +0100
commita224b4adbe65b476cdbd74efb3169aacbf292382 (patch)
tree17386959d44d82eb9c65ee3da24c5b862d62eb98 /datafile-app-server
parentf5791850232005aaf47c639f89f76007c67beff7 (diff)
use sdk-cbs
Change-Id: I615d57927c163d022683ad3cae2703adda48cf88 Issue-ID: DCAEGEN2-1064 Signed-off-by: micdzied <michal.1.dziedzic@nokia.com>
Diffstat (limited to 'datafile-app-server')
-rw-r--r--datafile-app-server/pom.xml4
-rw-r--r--datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfiguration.java18
-rw-r--r--datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/EnvironmentProcessor.java4
-rw-r--r--datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/model/EnvProperties.java41
-rw-r--r--datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DatafileConfigurationProvider.java102
-rw-r--r--datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/HttpGetClient.java91
-rw-r--r--datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImpl.java2
-rw-r--r--datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/DatafileConfigurationProviderTest.java90
-rw-r--r--datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/HttpGetClientTest.java75
9 files changed, 15 insertions, 412 deletions
diff --git a/datafile-app-server/pom.xml b/datafile-app-server/pom.xml
index aaa9c1a0..5d5317ac 100644
--- a/datafile-app-server/pom.xml
+++ b/datafile-app-server/pom.xml
@@ -131,6 +131,10 @@
<dependencies>
<dependency>
+ <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId>
+ <artifactId>cbs-client</artifactId>
+ </dependency>
+ <dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfiguration.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfiguration.java
index f7722053..34ccd76c 100644
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfiguration.java
+++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/CloudConfiguration.java
@@ -23,13 +23,14 @@ import java.util.Properties;
import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
-import org.onap.dcaegen2.collectors.datafile.model.EnvProperties;
-import org.onap.dcaegen2.collectors.datafile.service.DatafileConfigurationProvider;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.ReactiveCloudConfigurationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -42,14 +43,13 @@ import reactor.core.scheduler.Schedulers;
* @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
*/
@Configuration
+@ComponentScan("org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers")
@EnableConfigurationProperties
@EnableScheduling
@Primary
-public class CloudConfiguration extends AppConfig {
-
+public class CloudConfiguration extends AppConfig {
private static final Logger logger = LoggerFactory.getLogger(CloudConfiguration.class);
-
- private DatafileConfigurationProvider datafileConfigurationProvider;
+ private ReactiveCloudConfigurationProvider reactiveCloudConfigurationProvider;
private DmaapPublisherConfiguration dmaapPublisherCloudConfiguration;
private DmaapConsumerConfiguration dmaapConsumerCloudConfiguration;
private FtpesConfig ftpesCloudConfiguration;
@@ -58,8 +58,8 @@ public class CloudConfiguration extends AppConfig {
private Properties systemEnvironment;
@Autowired
- public void setThreadPoolTaskScheduler(DatafileConfigurationProvider datafileConfigurationProvider) {
- this.datafileConfigurationProvider = datafileConfigurationProvider;
+ public void setThreadPoolTaskScheduler(ReactiveCloudConfigurationProvider reactiveCloudConfigurationProvider) {
+ this.reactiveCloudConfigurationProvider = reactiveCloudConfigurationProvider;
}
@@ -78,7 +78,7 @@ public class CloudConfiguration extends AppConfig {
private void parsingConfigSuccess(EnvProperties envProperties) {
logger.info("Fetching Datafile Collector configuration from ConfigBindingService/Consul");
- datafileConfigurationProvider.callForDataFileCollectorConfiguration(envProperties)
+ reactiveCloudConfigurationProvider.callForServiceConfigurationReactive(envProperties)
.subscribe(this::parseCloudConfig, this::cloudConfigError);
}
diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/EnvironmentProcessor.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/EnvironmentProcessor.java
index 9f6b2737..106725ce 100644
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/EnvironmentProcessor.java
+++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/configuration/EnvironmentProcessor.java
@@ -21,8 +21,8 @@ package org.onap.dcaegen2.collectors.datafile.configuration;
import java.util.Optional;
import java.util.Properties;
import org.onap.dcaegen2.collectors.datafile.exceptions.EnvironmentLoaderException;
-import org.onap.dcaegen2.collectors.datafile.model.EnvProperties;
-import org.onap.dcaegen2.collectors.datafile.model.ImmutableEnvProperties;
+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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/model/EnvProperties.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/model/EnvProperties.java
deleted file mode 100644
index 86549343..00000000
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/model/EnvProperties.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * 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.collectors.datafile.model;
-
-import org.immutables.value.Value;
-
-/**
- * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
- */
-@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/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DatafileConfigurationProvider.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DatafileConfigurationProvider.java
deleted file mode 100644
index aca87e5b..00000000
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/DatafileConfigurationProvider.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * 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.collectors.datafile.service;
-
-import com.google.gson.JsonArray;
-import com.google.gson.JsonObject;
-import org.onap.dcaegen2.collectors.datafile.model.EnvProperties;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Service;
-import org.springframework.web.util.DefaultUriBuilderFactory;
-import reactor.core.publisher.Mono;
-
-/**
- * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
- */
-@Service
-public class DatafileConfigurationProvider {
-
- private static final Logger logger = LoggerFactory.getLogger(DatafileConfigurationProvider.class);
-
- private final HttpGetClient httpGetClient;
-
- DatafileConfigurationProvider() {
- this(new HttpGetClient());
- }
-
- DatafileConfigurationProvider(HttpGetClient httpGetClient) {
- this.httpGetClient = httpGetClient;
- }
-
- public Mono<JsonObject> callForDataFileCollectorConfiguration(EnvProperties envProperties) {
- return callConsulForConfigBindingServiceEndpoint(envProperties)
- .flatMap(this::callConfigBindingServiceForDatafileConfiguration);
- }
-
- private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) {
- logger.info("Retrieving Config Binding Service endpoint from Consul");
- return httpGetClient.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<JsonObject> callConfigBindingServiceForDatafileConfiguration(String configBindingServiceUri) {
- logger.info("Retrieving Datafile configuration");
- return httpGetClient.callHttpGet(configBindingServiceUri, JsonObject.class);
- }
-
-
- private Mono<String> createConfigBindingServiceUrl(JsonArray jsonArray, String appName) {
- return getConfigBindingObject(jsonArray)
- .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName));
- }
-
- private Mono<String> buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) {
- return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(),
- jsonObject.get("ServicePort").getAsInt(), "/service_component", appName));
- }
-
- private Mono<JsonObject> 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/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/HttpGetClient.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/HttpGetClient.java
deleted file mode 100644
index 796dbbd8..00000000
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/HttpGetClient.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * 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.collectors.datafile.service;
-
-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;
-
-
-public class HttpGetClient {
-
- private static final Logger logger = LoggerFactory.getLogger(HttpGetClient.class);
-
- private final WebClient webClient;
- private final Gson gson;
-
- HttpGetClient() {
- this(WebClient.builder().filter(logRequest()).filter(logResponse()).build());
- }
-
- HttpGetClient(WebClient webClient) {
- this.webClient = webClient;
- this.gson = new Gson();
- }
-
- <T> Mono<T> callHttpGet(String url, Class<T> 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 <T> Mono<T> getJsonFromRequest(String body, Class<T> genericClassDeclaration) {
- try {
- return Mono.just(parseJson(body, genericClassDeclaration));
- } catch (JsonSyntaxException | IllegalStateException e) {
- return Mono.error(e);
- }
- }
-
- private <T> T parseJson(String body, Class<T> 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/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImpl.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImpl.java
index b861653a..c03d903a 100644
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImpl.java
+++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImpl.java
@@ -47,10 +47,8 @@ public class XnfCollectorTaskImpl implements XnfCollectorTask {
private static final String FTPES = "ftpes";
private static final String FTPS = "ftps";
private static final String SFTP = "sftp";
-
private static final Logger logger = LoggerFactory.getLogger(XnfCollectorTaskImpl.class);
private Config datafileAppConfig;
-
private final FtpsClient ftpsClient;
private final SftpClient sftpClient;
private RetryTimer retryTimer;
diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/DatafileConfigurationProviderTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/DatafileConfigurationProviderTest.java
deleted file mode 100644
index efd89837..00000000
--- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/DatafileConfigurationProviderTest.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * 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.collectors.datafile.service;
-
-
-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.collectors.datafile.model.EnvProperties;
-import org.onap.dcaegen2.collectors.datafile.model.ImmutableEnvProperties;
-import reactor.core.publisher.Mono;
-import reactor.test.StepVerifier;
-
-class DatafileConfigurationProviderTest {
- 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 datafileCollectorMockConfiguration = "{\"test\":1}";
- private static final JsonObject datafileCollectorMockConfigurationJson = gson.fromJson(datafileCollectorMockConfiguration, JsonObject.class);
-
- private EnvProperties envProperties = ImmutableEnvProperties.builder()
- .appName("dcae-datafile-collector")
- .cbsName("config-binding-service")
- .consulHost("consul")
- .consulPort(8500)
- .build();
-
- @Test
- void shouldReturnDatafileCollectorConfiguration() {
- // given
- HttpGetClient webClient = mock(HttpGetClient.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-datafile-collector", JsonObject.class))
- .thenReturn(Mono.just(datafileCollectorMockConfigurationJson));
-
- DatafileConfigurationProvider provider = new DatafileConfigurationProvider(webClient);
-
- //when/then
- StepVerifier.create(provider.callForDataFileCollectorConfiguration(envProperties)).expectSubscription()
- .expectNext(datafileCollectorMockConfigurationJson).verifyComplete();
- }
-
- @Test
- void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() {
- // given
- HttpGetClient webClient = mock(HttpGetClient.class);
- when(
- webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
- .thenReturn(Mono.just(emptyConfigBindingServiceJson));
-
- DatafileConfigurationProvider provider = new DatafileConfigurationProvider(webClient);
-
- //when/then
- StepVerifier.create(provider.callForDataFileCollectorConfiguration(envProperties)).expectSubscription()
- .expectError(IllegalStateException.class).verify();
- }
-} \ No newline at end of file
diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/HttpGetClientTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/HttpGetClientTest.java
deleted file mode 100644
index d95e3417..00000000
--- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/HttpGetClientTest.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * 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.collectors.datafile.service;
-
-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;
-
-class HttpGetClientTest {
-
- 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();
- HttpGetClient httpGetClient = new HttpGetClient(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();
- HttpGetClient httpGetClient = new HttpGetClient(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