a738afe5658644f023ae3ec6b2516ac8607cc629
[so.git] / common / src / test / java / org / onap / so / rest / service / HttpRestServiceProviderImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.rest.service;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.atLeastOnce;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.so.rest.exceptions.InvalidRestRequestException;
36 import org.onap.so.rest.exceptions.RestProcessingException;
37 import org.springframework.http.HttpEntity;
38 import org.springframework.http.HttpMethod;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.client.HttpClientErrorException;
42 import org.springframework.web.client.RestClientException;
43 import org.springframework.web.client.RestTemplate;
44
45 import com.google.common.base.Optional;
46
47
48 /**
49  * @author waqas.ikram@est.tech
50  */
51 @RunWith(MockitoJUnitRunner.class)
52 public class HttpRestServiceProviderImplTest {
53
54     private static final String BODY = "{}";
55     private static final String DUMMY_URL = "http://localhost:9000/dummy/url";
56
57     @Mock
58     private RestTemplate mockRestTemplate;
59
60     @Mock
61     private ResponseEntity<String> mockEntity;
62
63     @Test
64     public void test_get_returnOptionalPresentIfResponseIsOKAndHasBody() {
65
66         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
67
68         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
69                 .thenReturn(mockEntity);
70
71         when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
72         when(mockEntity.hasBody()).thenReturn(true);
73         when(mockEntity.getBody()).thenReturn(BODY);
74
75         final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
76
77         assertTrue(actual.isPresent());
78         verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
79                 eq(String.class));
80     }
81
82     @Test
83     public void test_get_returnOptionalPresentIfResponseIsNotOK() {
84         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
85
86         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
87                 .thenReturn(mockEntity);
88
89         when(mockEntity.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
90
91         final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
92
93         assertFalse(actual.isPresent());
94         verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
95                 eq(String.class));
96     }
97
98     @Test
99     public void test_get_returnOptionalPresentIfResponseIsOKAndNoBody() {
100         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
101
102         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
103                 .thenReturn(mockEntity);
104
105         when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
106         when(mockEntity.hasBody()).thenReturn(false);
107
108         final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
109
110         assertFalse(actual.isPresent());
111         verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
112                 eq(String.class));
113     }
114
115     @Test(expected = InvalidRestRequestException.class)
116     public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusBadRequest() {
117         assertGetErrorScenario(HttpStatus.BAD_REQUEST);
118
119     }
120
121     @Test(expected = InvalidRestRequestException.class)
122     public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() {
123         assertGetErrorScenario(HttpStatus.NOT_FOUND);
124     }
125
126     @Test(expected = RestProcessingException.class)
127     public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionOccured() {
128         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
129
130         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
131                 .thenThrow(HttpClientErrorException.class);
132
133         objUnderTest.get(DUMMY_URL, String.class);
134     }
135
136     @Test(expected = RestProcessingException.class)
137     public void test_get_ThrowsInvalidRestRequestExceptionifRestProcessingExceptionOccured() {
138         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
139
140         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
141                 .thenThrow(RestClientException.class);
142
143         objUnderTest.get(DUMMY_URL, String.class);
144     }
145
146     @Test
147     public void test_post_returnOptionalPresentIfResponseIsOKAndHasBody() {
148
149         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
150
151         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
152                 .thenReturn(mockEntity);
153
154         when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
155         when(mockEntity.hasBody()).thenReturn(true);
156         when(mockEntity.getBody()).thenReturn(BODY);
157
158         final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
159
160         assertTrue(actual.isPresent());
161         verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
162                 eq(String.class));
163     }
164
165     @Test
166     public void test_post_returnOptionalPresentIfResponseIsOKAndHasNoBody() {
167
168         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
169
170         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
171                 .thenReturn(mockEntity);
172
173         when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
174         when(mockEntity.hasBody()).thenReturn(false);
175
176         final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
177
178         assertFalse(actual.isPresent());
179         verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
180                 eq(String.class));
181     }
182
183
184     @Test
185     public void test_post_returnOptionalPresentIfResponseIsNotOKAndHasBody() {
186
187         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
188
189         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
190                 .thenReturn(mockEntity);
191
192         when(mockEntity.getStatusCode()).thenReturn(HttpStatus.PARTIAL_CONTENT);
193
194         final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
195
196         assertFalse(actual.isPresent());
197         verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
198                 eq(String.class));
199     }
200     
201     @Test(expected = InvalidRestRequestException.class)
202     public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusBadRequest() {
203         assertPostErrorScenario(HttpStatus.BAD_REQUEST);
204
205     }
206
207     @Test(expected = InvalidRestRequestException.class)
208     public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() {
209         assertPostErrorScenario(HttpStatus.NOT_FOUND);
210     }
211     
212     @Test(expected = RestProcessingException.class)
213     public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionOccured() {
214         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
215
216         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
217                 .thenThrow(HttpClientErrorException.class);
218
219         objUnderTest.post(BODY, DUMMY_URL, String.class);
220     }
221
222     @Test(expected = RestProcessingException.class)
223     public void test_post_ThrowsInvalidRestRequestExceptionifRestProcessingExceptionOccured() {
224         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
225
226         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
227                 .thenThrow(RestClientException.class);
228
229         objUnderTest.post(BODY, DUMMY_URL, String.class);
230     }
231
232     private void assertPostErrorScenario(final HttpStatus status) {
233         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
234
235         final HttpClientErrorException errorException = new HttpClientErrorException(status);
236         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
237                 .thenThrow(errorException);
238
239         objUnderTest.post(BODY, DUMMY_URL, String.class);
240     }
241
242     private void assertGetErrorScenario(final HttpStatus status) {
243         final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
244
245         final HttpClientErrorException errorException = new HttpClientErrorException(status);
246         when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
247                 .thenThrow(errorException);
248
249         objUnderTest.get(DUMMY_URL, String.class);
250     }
251
252 }