summaryrefslogtreecommitdiffstats
path: root/rest-services/cbs-client/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'rest-services/cbs-client/src/test')
-rw-r--r--rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClientTest.java80
-rw-r--r--rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfiguratonProviderWrapperTest.java (renamed from rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClientTest.java)8
-rw-r--r--rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java96
-rw-r--r--rest-services/cbs-client/src/test/resources/logback-test.xml21
4 files changed, 201 insertions, 4 deletions
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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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/DummyClientTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfiguratonProviderWrapperTest.java
index 97843426..746ad43f 100644
--- 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/providers/CloudConfiguratonProviderWrapperTest.java
@@ -16,15 +16,15 @@
* 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;
+package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers;
-import static org.junit.jupiter.api.Assertions.*;
/**
- * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/14/18
+ * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/16/18
*/
-class DummyClientTest {
+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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ ===============================LICENSE_START======================================
+ ~ Copyright © 2017 AT&T 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===========================================
+ -->
+<configuration>
+ <root level="OFF"/>
+</configuration>