Replace CloudHttpClient with RxHttpClient in ReactiveCloudConfigurationProvider
[dcaegen2/services/sdk.git] / rest-services / cbs-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / cbs / client / providers / ReactiveCloudConfigurationProviderTest.java
index 9bb4ad3..fd9e0ad 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * DCAEGEN2-SERVICES-SDK
  * ================================================================================
- * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
 
 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers;
 
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
-
 import com.google.gson.Gson;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
+import java.io.IOException;
+import java.util.List;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
+import org.mockito.ArgumentCaptor;
+import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest;
+import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
+import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.GsonUtils;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
 import reactor.core.publisher.Mono;
@@ -39,58 +49,87 @@ import reactor.test.StepVerifier;
 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 configurationMock = "{\"test\":1}";
-    private static final JsonObject configurationJsonMock = gson.fromJson(configurationMock, JsonObject.class);
+    private static final String CONFIGURATION_MOCK = "{\"test\":1}";
+    private static final JsonObject CONFIGURATION_JSON_MOCK = gson
+            .fromJson(CONFIGURATION_MOCK, JsonObject.class);
+
+    private final RxHttpClient httpClient = mock(RxHttpClient.class);
+    private final JsonArray configBindingService = GsonUtils.readObjectArrayFromResource("/sample_config_binding_service.json");
 
     private EnvProperties envProperties = ImmutableEnvProperties.builder()
-        .appName("dcae-prh")
-        .cbsName("config-binding-service")
-        .consulHost("consul")
-        .consulPort(8500)
-        .build();
+            .appName("dcae-prh")
+            .cbsName("config-binding-service")
+            .consulHost("consul")
+            .consulPort(8500)
+            .build();
+
+    private HttpResponse response;
+    private ReactiveCloudConfigurationProvider provider;
+
+    ReactiveCloudConfigurationProviderTest() throws IOException {
+    }
+
+
+    @BeforeEach
+    void setUp() {
+        response = mock(HttpResponse.class);
+        provider = new ReactiveCloudConfigurationProvider(httpClient);
+    }
+
+    @Test
+    void shouldReturnPrhConfiguration(){
+        //when
+        when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
+        when(response.bodyAsJson(JsonArray.class)).thenReturn(configBindingService);
+        when(response.bodyAsJson(JsonObject.class)).thenReturn(CONFIGURATION_JSON_MOCK);
+
+
+        //then
+        StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
+                .expectSubscription()
+                .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete();
+    }
 
     @Test
-    void shouldReturnPrhConfiguration() {
+    void shouldRequestCorrectUrl(){
         // given
-        CloudHttpClient webClient = mock(CloudHttpClient.class);
-        when(
-            webClient.get("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
-            .thenReturn(Mono.just(configBindingServiceJson));
-        when(webClient.get("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class))
-            .thenReturn(Mono.just(configurationJsonMock));
-
-        ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient);
-
-        //when/then
-        StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription()
-            .expectNext(configurationJsonMock).verifyComplete();
+        String consulRequestUrl = "http://consul:8500/v1/catalog/service/config-binding-service";
+        String configRequestUrl = "http://config-binding-service:10000/service_component/dcae-prh";
+
+        //when
+        when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
+        when(response.bodyAsJson(JsonArray.class)).thenReturn(configBindingService);
+        when(response.bodyAsJson(JsonObject.class)).thenReturn(CONFIGURATION_JSON_MOCK);
+
+
+        //then
+        StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
+                .expectSubscription()
+                .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete();
+
+
+        ArgumentCaptor<HttpRequest> httpReq = ArgumentCaptor
+                .forClass(HttpRequest.class);
+        verify(httpClient, times(2)).call(httpReq.capture());
+
+        List<HttpRequest> allRequests = httpReq.getAllValues();
+        assertThat(allRequests.get(0).url()).isEqualTo(consulRequestUrl);
+        assertThat(allRequests.get(1).url()).isEqualTo(configRequestUrl);
     }
 
     @Test
     void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() {
         // given
-        CloudHttpClient webClient = mock(CloudHttpClient.class);
-        when(
-            webClient.get("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class))
-            .thenReturn(Mono.just(emptyConfigBindingServiceJson));
+        JsonArray emptyArray = gson.fromJson("[]", JsonArray.class);
+
+        //when
+        when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
+        when(response.bodyAsJson(JsonArray.class)).thenReturn(emptyArray);
 
-        ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient);
 
-        //when/then
-        StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription()
-            .expectError(IllegalStateException.class).verify();
+        //then
+        StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
+                .expectSubscription()
+                .expectError(IllegalStateException.class).verify();
     }
 }
\ No newline at end of file