|
1 | 1 | package ca.bc.gov.educ.api.course.service;
|
2 | 2 |
|
3 | 3 | import ca.bc.gov.educ.api.course.exception.ServiceException;
|
| 4 | +import io.netty.channel.ConnectTimeoutException; |
4 | 5 | import org.junit.Assert;
|
5 | 6 | import org.junit.Before;
|
6 | 7 | import org.junit.Test;
|
|
10 | 11 | import org.springframework.beans.factory.annotation.Autowired;
|
11 | 12 | import org.springframework.boot.test.context.SpringBootTest;
|
12 | 13 | import org.springframework.boot.test.mock.mockito.MockBean;
|
| 14 | +import org.springframework.http.HttpHeaders; |
| 15 | +import org.springframework.http.HttpMethod; |
13 | 16 | import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
|
14 | 17 | import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
15 | 18 | import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
|
16 | 19 | import org.springframework.test.context.ActiveProfiles;
|
17 | 20 | import org.springframework.test.context.junit4.SpringRunner;
|
18 | 21 | import org.springframework.web.reactive.function.BodyInserter;
|
19 | 22 | import org.springframework.web.reactive.function.client.WebClient;
|
| 23 | +import org.springframework.web.reactive.function.client.WebClientRequestException; |
20 | 24 | import reactor.core.publisher.Mono;
|
21 | 25 |
|
22 | 26 | import java.util.function.Consumer;
|
@@ -96,4 +100,23 @@ public void testPostOverride_Given4xxErrorFromService_ExpectServiceError() {
|
96 | 100 | this.restService.post(TEST_URL, TEST_BODY, byte[].class);
|
97 | 101 | }
|
98 | 102 |
|
| 103 | + @Test(expected = ServiceException.class) |
| 104 | + public void testPost_Given5xxErrorFromService_ExpectConnectionError(){ |
| 105 | + when(requestBodyUriMock.uri(TEST_URL)).thenReturn(requestBodyMock); |
| 106 | + when(requestBodyMock.retrieve()).thenReturn(responseMock); |
| 107 | + |
| 108 | + when(responseMock.bodyToMono(byte[].class)).thenReturn(Mono.error(new ConnectTimeoutException("Connection closed"))); |
| 109 | + this.restService.post(TEST_URL, TEST_BODY, byte[].class); |
| 110 | + } |
| 111 | + |
| 112 | + @Test(expected = ServiceException.class) |
| 113 | + public void testPost_Given5xxErrorFromService_ExpectWebClientRequestError(){ |
| 114 | + when(requestBodyUriMock.uri(TEST_URL)).thenReturn(requestBodyMock); |
| 115 | + when(requestBodyMock.retrieve()).thenReturn(responseMock); |
| 116 | + |
| 117 | + Throwable cause = new RuntimeException("Simulated cause"); |
| 118 | + when(responseMock.bodyToMono(byte[].class)).thenReturn(Mono.error(new WebClientRequestException(cause, HttpMethod.POST, null, new HttpHeaders()))); |
| 119 | + this.restService.post(TEST_URL, TEST_BODY, byte[].class); |
| 120 | + } |
| 121 | + |
99 | 122 | }
|
0 commit comments