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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.rest.service;
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 import com.google.common.base.Optional;
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;
47 * @author waqas.ikram@est.tech
49 @RunWith(MockitoJUnitRunner.class)
50 public class HttpRestServiceProviderImplTest {
52 private static final String BODY = "{}";
53 private static final String DUMMY_URL = "http://localhost:9000/dummy/url";
56 private RestTemplate mockRestTemplate;
59 private ResponseEntity<String> mockEntity;
62 public void test_get_returnOptionalPresentIfResponseIsOKAndHasBody() {
64 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
66 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
67 .thenReturn(mockEntity);
69 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
70 when(mockEntity.hasBody()).thenReturn(true);
71 when(mockEntity.getBody()).thenReturn(BODY);
73 final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
75 assertTrue(actual.isPresent());
76 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
81 public void test_get_returnOptionalPresentIfResponseIsNotOK() {
82 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
84 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
85 .thenReturn(mockEntity);
87 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
89 final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
91 assertFalse(actual.isPresent());
92 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
97 public void test_get_returnOptionalPresentIfResponseIsOKAndNoBody() {
98 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
100 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
101 .thenReturn(mockEntity);
103 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
104 when(mockEntity.hasBody()).thenReturn(false);
106 final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
108 assertFalse(actual.isPresent());
109 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
113 @Test(expected = InvalidRestRequestException.class)
114 public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusBadRequest() {
115 assertGetErrorScenario(HttpStatus.BAD_REQUEST);
119 @Test(expected = InvalidRestRequestException.class)
120 public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() {
121 assertGetErrorScenario(HttpStatus.NOT_FOUND);
124 @Test(expected = RestProcessingException.class)
125 public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionOccured() {
126 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
128 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
129 .thenThrow(HttpClientErrorException.class);
131 objUnderTest.get(DUMMY_URL, String.class);
134 @Test(expected = RestProcessingException.class)
135 public void test_get_ThrowsInvalidRestRequestExceptionifRestProcessingExceptionOccured() {
136 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
138 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
139 .thenThrow(RestClientException.class);
141 objUnderTest.get(DUMMY_URL, String.class);
145 public void test_post_returnOptionalPresentIfResponseIsOKAndHasBody() {
147 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
149 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
150 .thenReturn(mockEntity);
152 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
153 when(mockEntity.hasBody()).thenReturn(true);
154 when(mockEntity.getBody()).thenReturn(BODY);
156 final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
158 assertTrue(actual.isPresent());
159 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
164 public void test_post_returnOptionalPresentIfResponseIsOKAndHasNoBody() {
166 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
168 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
169 .thenReturn(mockEntity);
171 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
172 when(mockEntity.hasBody()).thenReturn(false);
174 final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
176 assertFalse(actual.isPresent());
177 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
182 public void test_put_returnOptionalPresentIfResponseIsOKAndHasBody() {
184 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
186 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.PUT), any(HttpEntity.class), eq(String.class)))
187 .thenReturn(mockEntity);
189 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
190 when(mockEntity.hasBody()).thenReturn(true);
191 when(mockEntity.getBody()).thenReturn(BODY);
193 final Optional<String> actual = objUnderTest.put(BODY, DUMMY_URL, String.class);
195 assertTrue(actual.isPresent());
196 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.PUT), any(HttpEntity.class),
201 public void test_put_returnOptionalPresentIfResponseIsOKAndHasNoBody() {
203 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
205 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.PUT), any(HttpEntity.class), eq(String.class)))
206 .thenReturn(mockEntity);
208 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
209 when(mockEntity.hasBody()).thenReturn(false);
211 final Optional<String> actual = objUnderTest.put(BODY, DUMMY_URL, String.class);
213 assertFalse(actual.isPresent());
214 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.PUT), any(HttpEntity.class),
220 public void test_post_returnOptionalPresentIfResponseIsNotOKAndHasBody() {
222 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
224 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
225 .thenReturn(mockEntity);
227 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.PARTIAL_CONTENT);
229 final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
231 assertFalse(actual.isPresent());
232 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
236 @Test(expected = InvalidRestRequestException.class)
237 public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusBadRequest() {
238 assertPostErrorScenario(HttpStatus.BAD_REQUEST);
242 @Test(expected = InvalidRestRequestException.class)
243 public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() {
244 assertPostErrorScenario(HttpStatus.NOT_FOUND);
247 @Test(expected = RestProcessingException.class)
248 public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionOccured() {
249 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
251 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
252 .thenThrow(HttpClientErrorException.class);
254 objUnderTest.post(BODY, DUMMY_URL, String.class);
257 @Test(expected = RestProcessingException.class)
258 public void test_post_ThrowsInvalidRestRequestExceptionifRestProcessingExceptionOccured() {
259 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
261 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
262 .thenThrow(RestClientException.class);
264 objUnderTest.post(BODY, DUMMY_URL, String.class);
267 private void assertPostErrorScenario(final HttpStatus status) {
268 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
270 final HttpClientErrorException errorException = new HttpClientErrorException(status);
271 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
272 .thenThrow(errorException);
274 objUnderTest.post(BODY, DUMMY_URL, String.class);
277 private void assertGetErrorScenario(final HttpStatus status) {
278 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
280 final HttpClientErrorException errorException = new HttpClientErrorException(status);
281 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
282 .thenThrow(errorException);
284 objUnderTest.get(DUMMY_URL, String.class);