d95e34177a5e540293843b0563cd499f80eafe8d
[dcaegen2/collectors/datafile.git] /
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.service;
20
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;
25
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;
33
34 class HttpGetClientTest {
35
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);
42
43     @Test
44     void shouldReturnJsonObjectOnGetCall() {
45         //given
46         mockWebClientDependantObject();
47         HttpGetClient httpGetClient = new HttpGetClient(webClient);
48         when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(DATA));
49
50         //when/then
51         StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription()
52             .expectNext(gson.fromJson(DATA, JsonObject.class)).verifyComplete();
53     }
54
55     @Test
56     void shouldReturnMonoErrorOnGetCall() {
57         //given
58         mockWebClientDependantObject();
59         HttpGetClient httpGetClient = new HttpGetClient(webClient);
60         when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("some wrong data"));
61
62         //when/then
63         StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription()
64             .expectError(JsonSyntaxException.class).verify();
65     }
66
67
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());
74     }
75 }