e3e7a1d3590403882901151c771f9dac37c4b455
[dcaegen2/services/sdk.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration;
22
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;
27
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;
35
36 /**
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/16/18
38  */
39 class CloudHttpClientTest {
40
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);
47
48     @Test
49     void shouldReturnJsonObjectOnGetCall() {
50         //given
51         mockWebClientDependantObject();
52         CloudHttpClient httpGetClient = new CloudHttpClient(webClient);
53         when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(DATA));
54
55         //when/then
56         StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription()
57             .expectNext(gson.fromJson(DATA, JsonObject.class)).verifyComplete();
58     }
59
60     @Test
61     void shouldReturnMonoErrorOnGetCall() {
62         //given
63         mockWebClientDependantObject();
64         CloudHttpClient httpGetClient = new CloudHttpClient(webClient);
65         when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("some wrong data"));
66
67         //when/then
68         StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription()
69             .expectError(JsonSyntaxException.class).verify();
70     }
71
72
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());
79     }
80 }