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;
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;
45 import com.google.common.base.Optional;
49 * @author waqas.ikram@est.tech
51 @RunWith(MockitoJUnitRunner.class)
52 public class HttpRestServiceProviderImplTest {
54 private static final String BODY = "{}";
55 private static final String DUMMY_URL = "http://localhost:9000/dummy/url";
58 private RestTemplate mockRestTemplate;
61 private ResponseEntity<String> mockEntity;
64 public void test_get_returnOptionalPresentIfResponseIsOKAndHasBody() {
66 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
68 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
69 .thenReturn(mockEntity);
71 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
72 when(mockEntity.hasBody()).thenReturn(true);
73 when(mockEntity.getBody()).thenReturn(BODY);
75 final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
77 assertTrue(actual.isPresent());
78 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
83 public void test_get_returnOptionalPresentIfResponseIsNotOK() {
84 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
86 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
87 .thenReturn(mockEntity);
89 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
91 final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
93 assertFalse(actual.isPresent());
94 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
99 public void test_get_returnOptionalPresentIfResponseIsOKAndNoBody() {
100 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
102 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
103 .thenReturn(mockEntity);
105 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
106 when(mockEntity.hasBody()).thenReturn(false);
108 final Optional<String> actual = objUnderTest.get(DUMMY_URL, String.class);
110 assertFalse(actual.isPresent());
111 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class),
115 @Test(expected = InvalidRestRequestException.class)
116 public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusBadRequest() {
117 assertGetErrorScenario(HttpStatus.BAD_REQUEST);
121 @Test(expected = InvalidRestRequestException.class)
122 public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() {
123 assertGetErrorScenario(HttpStatus.NOT_FOUND);
126 @Test(expected = RestProcessingException.class)
127 public void test_get_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionOccured() {
128 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
130 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
131 .thenThrow(HttpClientErrorException.class);
133 objUnderTest.get(DUMMY_URL, String.class);
136 @Test(expected = RestProcessingException.class)
137 public void test_get_ThrowsInvalidRestRequestExceptionifRestProcessingExceptionOccured() {
138 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
140 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class)))
141 .thenThrow(RestClientException.class);
143 objUnderTest.get(DUMMY_URL, String.class);
147 public void test_post_returnOptionalPresentIfResponseIsOKAndHasBody() {
149 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
151 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
152 .thenReturn(mockEntity);
154 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
155 when(mockEntity.hasBody()).thenReturn(true);
156 when(mockEntity.getBody()).thenReturn(BODY);
158 final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
160 assertTrue(actual.isPresent());
161 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
166 public void test_post_returnOptionalPresentIfResponseIsOKAndHasNoBody() {
168 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
170 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
171 .thenReturn(mockEntity);
173 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.OK);
174 when(mockEntity.hasBody()).thenReturn(false);
176 final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
178 assertFalse(actual.isPresent());
179 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
185 public void test_post_returnOptionalPresentIfResponseIsNotOKAndHasBody() {
187 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
189 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
190 .thenReturn(mockEntity);
192 when(mockEntity.getStatusCode()).thenReturn(HttpStatus.PARTIAL_CONTENT);
194 final Optional<String> actual = objUnderTest.post(BODY, DUMMY_URL, String.class);
196 assertFalse(actual.isPresent());
197 verify(mockRestTemplate, atLeastOnce()).exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class),
201 @Test(expected = InvalidRestRequestException.class)
202 public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusBadRequest() {
203 assertPostErrorScenario(HttpStatus.BAD_REQUEST);
207 @Test(expected = InvalidRestRequestException.class)
208 public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionWithHttpStatusNotFoundHttpStatus() {
209 assertPostErrorScenario(HttpStatus.NOT_FOUND);
212 @Test(expected = RestProcessingException.class)
213 public void test_post_ThrowsInvalidRestRequestExceptionifHttpClientErrorExceptionOccured() {
214 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
216 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
217 .thenThrow(HttpClientErrorException.class);
219 objUnderTest.post(BODY, DUMMY_URL, String.class);
222 @Test(expected = RestProcessingException.class)
223 public void test_post_ThrowsInvalidRestRequestExceptionifRestProcessingExceptionOccured() {
224 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
226 when(mockRestTemplate.exchange(eq(DUMMY_URL), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
227 .thenThrow(RestClientException.class);
229 objUnderTest.post(BODY, DUMMY_URL, String.class);
232 private void assertPostErrorScenario(final HttpStatus status) {
233 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
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);
239 objUnderTest.post(BODY, DUMMY_URL, String.class);
242 private void assertGetErrorScenario(final HttpStatus status) {
243 final HttpRestServiceProvider objUnderTest = new HttpRestServiceProviderImpl(mockRestTemplate);
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);
249 objUnderTest.get(DUMMY_URL, String.class);