2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2018 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.http.configuration;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import com.google.gson.Gson;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonSyntaxException;
31 import org.junit.jupiter.api.Test;
32 import org.springframework.web.reactive.function.client.WebClient;
33 import reactor.core.publisher.Mono;
34 import reactor.test.StepVerifier;
37 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/16/18
39 class CloudHttpClientTest {
41 private static final String SOMEURL = "http://someurl";
42 private static final String DATA = "{}";
43 private Gson gson = new Gson();
44 private WebClient webClient = mock(WebClient.class);
45 private WebClient.RequestHeadersUriSpec requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
46 private WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class);
49 void shouldReturnJsonObjectOnGetCall() {
51 mockWebClientDependantObject();
52 CloudHttpClient httpGetClient = new CloudHttpClient(webClient);
53 when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(DATA));
56 StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription()
57 .expectNext(gson.fromJson(DATA, JsonObject.class)).verifyComplete();
61 void shouldReturnMonoErrorOnGetCall() {
63 mockWebClientDependantObject();
64 CloudHttpClient httpGetClient = new CloudHttpClient(webClient);
65 when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("some wrong data"));
68 StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription()
69 .expectError(JsonSyntaxException.class).verify();
73 private void mockWebClientDependantObject() {
74 doReturn(requestBodyUriSpec).when(webClient).get();
75 when(requestBodyUriSpec.uri(SOMEURL)).thenReturn(requestBodyUriSpec);
76 doReturn(responseSpec).when(requestBodyUriSpec).retrieve();
77 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
78 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());