2  * ============LICENSE_START=======================================================
 
   3  * DCAEGEN2-SERVICES-SDK
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018-2019 NOKIA Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers;
 
  23 import static org.assertj.core.api.Assertions.assertThat;
 
  24 import static org.mockito.ArgumentMatchers.any;
 
  25 import static org.mockito.Mockito.mock;
 
  26 import static org.mockito.Mockito.times;
 
  27 import static org.mockito.Mockito.verify;
 
  28 import static org.mockito.Mockito.when;
 
  29 import com.google.gson.Gson;
 
  30 import com.google.gson.JsonArray;
 
  31 import com.google.gson.JsonObject;
 
  32 import java.io.IOException;
 
  33 import java.util.List;
 
  34 import org.junit.jupiter.api.BeforeEach;
 
  35 import org.junit.jupiter.api.Test;
 
  36 import org.mockito.ArgumentCaptor;
 
  37 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest;
 
  38 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
 
  39 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
 
  40 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.GsonUtils;
 
  41 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
 
  42 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
 
  43 import reactor.core.publisher.Mono;
 
  44 import reactor.test.StepVerifier;
 
  47  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
 
  49 class ReactiveCloudConfigurationProviderTest {
 
  51     private static final Gson gson = new Gson();
 
  52     private static final String CONFIGURATION_MOCK = "{\"test\":1}";
 
  53     private static final JsonObject CONFIGURATION_JSON_MOCK = gson
 
  54             .fromJson(CONFIGURATION_MOCK, JsonObject.class);
 
  56     private final RxHttpClient httpClient = mock(RxHttpClient.class);
 
  57     private final JsonArray configBindingService = GsonUtils.readObjectArrayFromResource("/sample_config_binding_service.json");
 
  59     private EnvProperties envProperties = ImmutableEnvProperties.builder()
 
  61             .cbsName("config-binding-service")
 
  66     private HttpResponse response;
 
  67     private ReactiveCloudConfigurationProvider provider;
 
  69     ReactiveCloudConfigurationProviderTest() throws IOException {
 
  75         response = mock(HttpResponse.class);
 
  76         provider = new ReactiveCloudConfigurationProvider(httpClient);
 
  80     void shouldReturnPrhConfiguration(){
 
  82         when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
 
  83         when(response.bodyAsJson(JsonArray.class)).thenReturn(configBindingService);
 
  84         when(response.bodyAsJson(JsonObject.class)).thenReturn(CONFIGURATION_JSON_MOCK);
 
  88         StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
 
  90                 .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete();
 
  94     void shouldRequestCorrectUrl(){
 
  96         String consulRequestUrl = "http://consul:8500/v1/catalog/service/config-binding-service";
 
  97         String configRequestUrl = "http://config-binding-service:10000/service_component/dcae-prh";
 
 100         when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
 
 101         when(response.bodyAsJson(JsonArray.class)).thenReturn(configBindingService);
 
 102         when(response.bodyAsJson(JsonObject.class)).thenReturn(CONFIGURATION_JSON_MOCK);
 
 106         StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
 
 107                 .expectSubscription()
 
 108                 .expectNext(CONFIGURATION_JSON_MOCK).verifyComplete();
 
 111         ArgumentCaptor<HttpRequest> httpReq = ArgumentCaptor
 
 112                 .forClass(HttpRequest.class);
 
 113         verify(httpClient, times(2)).call(httpReq.capture());
 
 115         List<HttpRequest> allRequests = httpReq.getAllValues();
 
 116         assertThat(allRequests.get(0).url()).isEqualTo(consulRequestUrl);
 
 117         assertThat(allRequests.get(1).url()).isEqualTo(configRequestUrl);
 
 121     void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() {
 
 123         JsonArray emptyArray = gson.fromJson("[]", JsonArray.class);
 
 126         when(httpClient.call(any(HttpRequest.class))).thenReturn(Mono.just(response));
 
 127         when(response.bodyAsJson(JsonArray.class)).thenReturn(emptyArray);
 
 131         StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties))
 
 132                 .expectSubscription()
 
 133                 .expectError(IllegalStateException.class).verify();