Commit 9b9bc1db authored by Larissa Pereira's avatar Larissa Pereira
Browse files

Handling cosmosdb too many requests exception

parent 0ce04afe
Pipeline #65734 passed with stages
in 45 minutes and 14 seconds
package org.opengroup.osdu.storage.provider.azure.util;
import com.azure.cosmos.implementation.RequestRateTooLargeException;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.storage.util.GlobalExceptionMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalCosmosExceptionMapper {
private GlobalExceptionMapper mapper;
public GlobalCosmosExceptionMapper(GlobalExceptionMapper mapper) {
this.mapper = mapper;
}
@ExceptionHandler(RequestRateTooLargeException.class)
protected ResponseEntity<Object> handleCosmosdbException(Exception e) {
return mapper.getErrorResponse(
new AppException(HttpStatus.SERVICE_UNAVAILABLE.value(), "Too many requests on cosmosdb.",
e.getMessage(), e));
}
}
package org.opengroup.osdu.storage.provider.azure.util;
import com.azure.cosmos.implementation.RequestRateTooLargeException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.AppError;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.storage.util.GlobalExceptionMapper;
import org.springframework.http.ResponseEntity;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE;
@RunWith(MockitoJUnitRunner.class)
public class GlobalCosmosExceptionMapperTest {
@InjectMocks
private GlobalCosmosExceptionMapper sut;
@Mock
private GlobalExceptionMapper mapper;
@Test
public void should_returnServiceUnavailable_with_correct_reason_when_RequestRateTooLargeException_Is_Captured() {
RequestRateTooLargeException exception = Mockito.mock(RequestRateTooLargeException.class);
AppError expectedBody = new AppError(SERVICE_UNAVAILABLE.value(), "Too many requests on cosmosdb.", "Cosmosdb error.");
when(mapper.getErrorResponse(any(AppException.class))).thenReturn(new ResponseEntity<>(expectedBody, SERVICE_UNAVAILABLE));
ResponseEntity response = this.sut.handleCosmosdbException(exception);
assertEquals(503, response.getStatusCodeValue());
assertEquals("Too many requests on cosmosdb.", ((AppError)response.getBody()).getReason());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment