Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/on.pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
with:
sarif_file: 'trivy-results.sarif'
- name: Cache SonarCloud packages
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.io.IOException;
import java.time.Duration;

@Service
Expand Down Expand Up @@ -42,7 +44,7 @@ public <T> T get(String url, Class<T> clazz) {
// only does retry if initial error was 5xx as service may be temporarily down
// 4xx errors will always happen if 404, 401, 403 etc, so does not retry
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2))
.filter(ServiceException.class::isInstance)
.filter(ex -> ex instanceof ServiceException || ex instanceof IOException || ex instanceof WebClientRequestException)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
throw new ServiceException(getErrorMessage(url, SERVICE_FAILED_ERROR), HttpStatus.SERVICE_UNAVAILABLE.value());
}))
Expand All @@ -68,7 +70,7 @@ public <T> T post(String url, Object body, Class<T> clazz) {
clientResponse -> Mono.error(new ServiceException(getErrorMessage(url, SERVER_ERROR), clientResponse.statusCode().value())))
.bodyToMono(clazz)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(2))
.filter(ServiceException.class::isInstance)
.filter(ex -> ex instanceof ServiceException || ex instanceof IOException || ex instanceof WebClientRequestException)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
throw new ServiceException(getErrorMessage(url, SERVICE_FAILED_ERROR), HttpStatus.SERVICE_UNAVAILABLE.value());
}))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.bc.gov.educ.api.gradbusiness.service;

import ca.bc.gov.educ.api.gradbusiness.exception.ServiceException;
import io.netty.channel.ConnectTimeoutException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -9,10 +10,13 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;
Expand Down Expand Up @@ -83,4 +87,25 @@ public void testGetOverride_Given4xxErrorFromService_ExpectServiceError(){
when(this.responseMock.bodyToMono(ServiceException.class)).thenReturn(Mono.just(new ServiceException("Error", 500)));
this.restService.get(TEST_URL_403, String.class);
}

@Test(expected = ServiceException.class)
public void testGet_Given5xxErrorFromService_ExpectConnectionError(){
when(requestBodyUriMock.uri(TEST_URL_503)).thenReturn(requestBodyMock);
when(requestBodyMock.retrieve()).thenReturn(responseMock);

Throwable cause = new RuntimeException("Simulated cause");
when(responseMock.bodyToMono(String.class)).thenReturn(Mono.error(new ConnectTimeoutException("Connection closed")));
restService.get(TEST_URL_503, String.class);
}

@Test(expected = ServiceException.class)
public void testGet_Given5xxErrorFromService_ExpectWebClientRequestError(){
when(requestBodyUriMock.uri(TEST_URL_503)).thenReturn(requestBodyMock);
when(requestBodyMock.retrieve()).thenReturn(responseMock);

Throwable cause = new RuntimeException("Simulated cause");
when(responseMock.bodyToMono(String.class)).thenReturn(Mono.error(new WebClientRequestException(cause, HttpMethod.GET, null, new HttpHeaders())));
restService.get(TEST_URL_503, String.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ca.bc.gov.educ.api.gradbusiness.exception.ServiceException;
import ca.bc.gov.educ.api.gradbusiness.util.ThreadLocalStateUtil;
import io.netty.channel.ConnectTimeoutException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -13,11 +14,14 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;
Expand Down Expand Up @@ -87,4 +91,23 @@ public void testPostOverride_Given4xxErrorFromService_ExpectServiceError() {
this.restService.post(TEST_URL, TEST_BODY, byte[].class);
}

@Test(expected = ServiceException.class)
public void testPost_Given5xxErrorFromService_ExpectConnectionError(){
when(requestBodyUriMock.uri(TEST_URL)).thenReturn(requestBodyMock);
when(requestBodyMock.retrieve()).thenReturn(responseMock);

when(responseMock.bodyToMono(byte[].class)).thenReturn(Mono.error(new ConnectTimeoutException("Connection closed")));
this.restService.post(TEST_URL, TEST_BODY, byte[].class);
}

@Test(expected = ServiceException.class)
public void testPost_Given5xxErrorFromService_ExpectWebClientRequestError(){
when(requestBodyUriMock.uri(TEST_URL)).thenReturn(requestBodyMock);
when(requestBodyMock.retrieve()).thenReturn(responseMock);

Throwable cause = new RuntimeException("Simulated cause");
when(responseMock.bodyToMono(byte[].class)).thenReturn(Mono.error(new WebClientRequestException(cause, HttpMethod.POST, null, new HttpHeaders())));
this.restService.post(TEST_URL, TEST_BODY, byte[].class);
}

}