Skip to content

Commit

Permalink
Merge branch 'dev' into 7988-synchronize-places-from-database-with-go…
Browse files Browse the repository at this point in the history
…ogle-places-api
  • Loading branch information
Warded120 authored Jan 14, 2025
2 parents c6b8591 + 9726035 commit ff58960
Show file tree
Hide file tree
Showing 21 changed files with 325 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import jakarta.validation.ValidationException;
import lombok.experimental.StandardException;

/**
* Exception thrown when an invalid EventDto is passed in a request. This
* exception indicates that the provided EventDto does not meet the required
* validation constraints.
*
*/
@StandardException
public class EventDtoValidationException extends ValidationException {
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package greencity.exception.handler;

import greencity.exception.exceptions.*;
import jakarta.validation.ConstraintDeclarationException;
import jakarta.validation.ValidationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -17,11 +19,15 @@
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MultipartException;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.when;

@ExtendWith(MockitoExtension.class)
class CustomExceptionHandlerTest {
Expand Down Expand Up @@ -51,6 +57,117 @@ void init() {
objectMap.put("trace", "Internal Server Error");
}

@Test
void handleTooLargeMultipartFileRequest() {
MultipartException multipartException = new MultipartException("test");

ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleTooLargeMultipartFileRequest(
multipartException, webRequest),
ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE).body(exceptionResponse));
}

@Test
void handleConstraintDeclarationException() {
ConstraintDeclarationException constraintDeclarationException = new ConstraintDeclarationException("test");

ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleConstraintDeclarationException(
constraintDeclarationException, webRequest),
ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exceptionResponse));
}

@Test
void handleValidationException() {
ValidationException validationException = new ValidationException("test");

ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleValidationException(
validationException, webRequest),
ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exceptionResponse));
}

@Test
void handleForbiddenException() {
ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleForbiddenException(
webRequest),
ResponseEntity.status(HttpStatus.FORBIDDEN).body(exceptionResponse));
}

@Test
void handleNotFoundException() {
ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleNotFoundException(
webRequest),
ResponseEntity.status(HttpStatus.NOT_FOUND).body(exceptionResponse));
}

@Test
void handleIllegalArgumentException() {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException("test");

ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleIllegalArgumentException(
illegalArgumentException, webRequest),
ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exceptionResponse));
}

@Test
void handleStatusException() {
InvalidStatusException statusException = new InvalidStatusException("test");

ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleStatusException(
statusException, webRequest),
ResponseEntity.status(HttpStatus.CONFLICT).body(exceptionResponse));
}

@Test
void handleOperationException() {
ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleOperationException(
webRequest),
ResponseEntity.status(HttpStatus.CONFLICT).body(exceptionResponse));
}

@Test
void handleUnsupportedOperationException() {
UnsupportedOperationException unsupportedOperationException = new UnsupportedOperationException("test");

ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);

assertEquals(customExceptionHandler.handleUnsupportedOperationException(
unsupportedOperationException, webRequest),
ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(exceptionResponse));
}

@Test
void handleMethodArgumentNotValid() {
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
Expand Down Expand Up @@ -90,29 +207,10 @@ void testHandleBadSocialNetworkLinkException() {
void handleBadRequestException() {
BadRequestException badRequestException = new BadRequestException("test");
ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class), any(ErrorAttributeOptions.class)))
.thenReturn(objectMap);
assertEquals(customExceptionHandler.handleBadRequestException(badRequestException, webRequest),
ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exceptionResponse));
}

@Test
void handleNotFoundException() {
NotFoundException notFoundException = new NotFoundException("test");
ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class), any(ErrorAttributeOptions.class)))
.thenReturn(objectMap);
assertEquals(customExceptionHandler.handleNotFoundException(notFoundException, webRequest),
ResponseEntity.status(HttpStatus.NOT_FOUND).body(exceptionResponse));
}

@Test
void handleWrongIdException() {
WrongIdException wrongIdException = new WrongIdException("test");
ExceptionResponse exceptionResponse = new ExceptionResponse(objectMap);
when(errorAttributes.getErrorAttributes(any(WebRequest.class), any(ErrorAttributeOptions.class)))
.thenReturn(objectMap);
assertEquals(customExceptionHandler.handleWrongIdException(wrongIdException, webRequest),
when(errorAttributes.getErrorAttributes(any(WebRequest.class),
any(ErrorAttributeOptions.class))).thenReturn(objectMap);
assertEquals(customExceptionHandler.handleBadRequestException(
badRequestException, webRequest),
ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exceptionResponse));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
* @author Kateryna Horokh
*/
@StandardException
public class BadCategoryRequestException extends RuntimeException {
public class BadCategoryRequestException extends BadRequestException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import jakarta.validation.ConstraintDeclarationException;
import lombok.experimental.StandardException;

/**
* Exception we get when we receive wrong social network links.
*
* @version 1.0
*/
@StandardException
public class BadSocialNetworkLinksException extends ConstraintDeclarationException {
public BadSocialNetworkLinksException(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
* @author Rostyslav Khasanov
*/
@StandardException
public class BadUpdateRequestException extends RuntimeException {
public class BadUpdateRequestException extends BadRequestException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import jakarta.validation.ConstraintDeclarationException;
import lombok.experimental.StandardException;

/**
* Exception we get when we try to use not unique (duplicated) tags.
*
* @version 1.0
*/
@StandardException
public class DuplicatedTagException extends ConstraintDeclarationException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import jakarta.validation.ConstraintDeclarationException;
import lombok.experimental.StandardException;

/**
* Exception we get when we try to use more than 3 tags.
*
* @version 1.0
*/
@StandardException
public class InvalidNumOfTagsException extends ConstraintDeclarationException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
import jakarta.validation.ConstraintDeclarationException;
import lombok.experimental.StandardException;

/**
* Exception we get when we receive a malformed URL or received string could not
* be parsed as a URI reference.
*
* @version 1.0
*/
@StandardException
public class InvalidURLException extends ConstraintDeclarationException {
/**
* Constructor for InvalidURLException.
*
* @param message - giving message.
*/
public InvalidURLException(String message) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import lombok.experimental.StandardException;

/**
* Exception we get when an unsubscribe token is invalid.
*
* @version 1.0
*/
@StandardException
public class InvalidUnsubscribeToken extends BadRequestException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
* exist in database.
*/
@StandardException
public class LanguageNotFoundException extends RuntimeException {
public class LanguageNotFoundException extends NotFoundException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import lombok.experimental.StandardException;

/**
* Exception we get when the user is not the current user.
*
* @version 1.0
*/
@StandardException
public class NotCurrentUserException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import lombok.experimental.StandardException;

/**
* Exception that is thrown when an entity has an invalid status for the current
* operation, specifically related to place status.
*
* @version 1.0
*/
@StandardException
public class PlaceStatusException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import lombok.experimental.StandardException;

/**
* Exception that we get when we try to get a tag that is not in the database.
*
* @version 1.0
*/
@StandardException
public class TagNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import lombok.experimental.StandardException;

/**
* Exception that get if user write not supported sorting operation.
*
* @version 1.0
*/
@StandardException
public class UnsupportedSortException extends UnsupportedOperationException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
import lombok.experimental.StandardException;

@StandardException
/**
* Exception we get when a user already has an enrolled habit assign.
*
* @version 1.0
*/
public class UserAlreadyHasEnrolledHabitAssign extends BadRequestException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
import lombok.experimental.StandardException;

@StandardException
/**
* Exception thrown when a user already has a habit assigned.
*
* @version 1.0
*/

public class UserAlreadyHasHabitAssignedException extends BadRequestException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import lombok.experimental.StandardException;

/**
* Exception thrown when a user already has the maximum number of active habit
* assignments.
*
* @version 1.0
*/
@StandardException
public class UserAlreadyHasMaxNumberOfActiveHabitAssigns extends BadRequestException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import lombok.experimental.StandardException;

/**
* Exception thrown when a user has reached beyond the allowed enrollment range.
*
* @version 1.0
*/
@StandardException
public class UserHasReachedOutOfEnrollRange extends BadRequestException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

/**
* Exception that we get when user by this email not found.
*
* @version 1.0
*/
@StandardException
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class WrongEmailException extends RuntimeException {
public class WrongEmailException extends BadRequestException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Exception that we get when in some logic we have bad ID.
*
* @author Nazar Stasyuk
* @version 1.0
*/
@StandardException
public class WrongIdException extends RuntimeException {
public class WrongIdException extends BadRequestException {
}

0 comments on commit ff58960

Please sign in to comment.