Skip to content

Commit

Permalink
AYS-487 | Added exception handling for AysUserNotSuperAdminException …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
suhakopan committed Sep 23, 2024
1 parent 40d9f6d commit dc268fc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.ays.auth.util.exception.AysUserNotSuperAdminException;
import org.ays.common.model.response.AysErrorResponse;
import org.ays.common.util.exception.AysAlreadyException;
import org.ays.common.util.exception.AysAuthException;
Expand Down Expand Up @@ -163,6 +164,17 @@ AysErrorResponse handleAccessDeniedError(final AccessDeniedException exception)
.build();
}

@ExceptionHandler(AysUserNotSuperAdminException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
AysErrorResponse handleAysUserNotSuperAdminError(final AysUserNotSuperAdminException exception) {
log.error(exception.getMessage(), exception);

return AysErrorResponse.builder()
.header(AysErrorResponse.Header.AUTH_ERROR.getName())
.message(exception.getMessage())
.build();
}

@ExceptionHandler(SQLException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
AysErrorResponse handleSQLError(final SQLException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.ays.AysRestControllerTest;
import org.ays.auth.util.exception.AysUserNotSuperAdminException;
import org.ays.common.model.response.AysErrorResponse;
import org.ays.common.util.exception.AysAuthException;
import org.ays.common.util.exception.AysBadRequestException;
Expand All @@ -14,6 +15,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.springframework.core.MethodParameter;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpInputMessage;
Expand Down Expand Up @@ -358,4 +360,38 @@ private void checkAysError(AysErrorResponse mockErrorResponse, AysErrorResponse

}

@Test
void givenUserNotSuperAdmin_whenThrowAysUserNotSuperAdminException_thenReturnAysError() {
// Given
AysUserNotSuperAdminException mockException = Mockito.mock(AysUserNotSuperAdminException.class);
Mockito.when(mockException.getMessage()).thenReturn("User is not a super admin");

// When
AysErrorResponse mockErrorResponse = AysErrorResponse.builder()
.header(AysErrorResponse.Header.AUTH_ERROR.getName())
.message(mockException.getMessage())
.build();

// Then
AysErrorResponse errorResponse = globalExceptionHandler.handleAysUserNotSuperAdminError(mockException);
this.checkAysError(mockErrorResponse, errorResponse);
}

@Test
void givenUserNotSuperAdmin_whenThrowAysUserNotSuperAdminExceptionWithoutMessage_thenReturnAysError() {
// Given
AysUserNotSuperAdminException mockException = Mockito.mock(AysUserNotSuperAdminException.class);
Mockito.when(mockException.getMessage()).thenReturn(null);

// When
AysErrorResponse mockErrorResponse = AysErrorResponse.builder()
.header(AysErrorResponse.Header.AUTH_ERROR.getName())
.message(null)
.build();

// Then
AysErrorResponse errorResponse = globalExceptionHandler.handleAysUserNotSuperAdminError(mockException);
this.checkAysError(mockErrorResponse, errorResponse);
}

}

0 comments on commit dc268fc

Please sign in to comment.