2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.dcaegen2.collectors.datafile.service;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import com.google.gson.Gson;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonSyntaxException;
29 import org.junit.jupiter.api.Test;
30 import org.springframework.web.reactive.function.client.WebClient;
31 import reactor.core.publisher.Mono;
32 import reactor.test.StepVerifier;
34 class HttpGetClientTest {
36 private static final String SOMEURL = "http://someurl";
37 private static final String DATA = "{}";
38 private Gson gson = new Gson();
39 private WebClient webClient = mock(WebClient.class);
40 private WebClient.RequestHeadersUriSpec requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class);
41 private WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class);
44 void shouldReturnJsonObjectOnGetCall() {
46 mockWebClientDependantObject();
47 HttpGetClient httpGetClient = new HttpGetClient(webClient);
48 when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(DATA));
51 StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription()
52 .expectNext(gson.fromJson(DATA, JsonObject.class)).verifyComplete();
56 void shouldReturnMonoErrorOnGetCall() {
58 mockWebClientDependantObject();
59 HttpGetClient httpGetClient = new HttpGetClient(webClient);
60 when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("some wrong data"));
63 StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription()
64 .expectError(JsonSyntaxException.class).verify();
68 private void mockWebClientDependantObject() {
69 doReturn(requestBodyUriSpec).when(webClient).get();
70 when(requestBodyUriSpec.uri(SOMEURL)).thenReturn(requestBodyUriSpec);
71 doReturn(responseSpec).when(requestBodyUriSpec).retrieve();
72 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
73 doReturn(responseSpec).when(responseSpec).onStatus(any(), any());