Skip to content

Commit

Permalink
Add tests for Controller layer
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Jan 2, 2024
1 parent 3a62904 commit dd8c8fe
Show file tree
Hide file tree
Showing 10 changed files with 553 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package by.andd3dfx.templateapp.error;

import by.andd3dfx.templateapp.error.dto.ExceptionResponse;
import by.andd3dfx.templateapp.error.exception.NotFoundException;
import by.andd3dfx.templateapp.error.exception.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.time.LocalDateTime;

/**
* Add more methods if needed.
*/
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

/**
* 401.
*/
@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<ExceptionResponse> handleUnauthorizedException(UnauthorizedException ex) {
return buildResponseEntity(ex, HttpStatus.UNAUTHORIZED);
}

/**
* 404.
*/
@ExceptionHandler(NotFoundException.class)
public ResponseEntity handleResourceNotFoundException(NotFoundException ex) {
return buildResponseEntity(ex, HttpStatus.NOT_FOUND);
}

/**
* 409.
*/
@ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class})
public ResponseEntity handleIllegalExceptions(RuntimeException ex) {
return buildResponseEntity(ex, HttpStatus.CONFLICT);
}

/**
* 500.
* Throw exception for any other unpredicted reason.
* Avoid modification of this method to make problem visible in logs.
* Don't delete this method to avoid descendants declare it and catch 'any error'.
*/
@ExceptionHandler(Exception.class)
public final ResponseEntity handleOtherExceptions(Exception ex) throws Exception {
throw ex;
}

private ResponseEntity<ExceptionResponse> buildResponseEntity(Exception ex, HttpStatus httpStatus) {
return ResponseEntity
.status(httpStatus)
.body(new ExceptionResponse(ex.getMessage(), httpStatus.name(), LocalDateTime.now()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package by.andd3dfx.templateapp.error.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExceptionResponse {

private String message;
private String code;
private LocalDateTime timestamp;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package by.andd3dfx.templateapp.exceptions;
package by.andd3dfx.templateapp.error.exception;

public class ArticleNotFoundException extends NotFoundException {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package by.andd3dfx.templateapp.error.exception;

public abstract class NotFoundException extends RuntimeException {

public NotFoundException(String name, Long id) {
super(String.format("Could not find %s by id=%d", name, id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package by.andd3dfx.templateapp.error.exception;

public class UnauthorizedException extends RuntimeException {

public UnauthorizedException(String message) {
super(message);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import by.andd3dfx.templateapp.dto.ArticleDto;
import by.andd3dfx.templateapp.dto.ArticleUpdateDto;
import by.andd3dfx.templateapp.exceptions.ArticleNotFoundException;
import by.andd3dfx.templateapp.error.exception.ArticleNotFoundException;
import by.andd3dfx.templateapp.mappers.ArticleMapper;
import by.andd3dfx.templateapp.persistence.dao.ArticleRepository;
import by.andd3dfx.templateapp.persistence.entities.Article;
import by.andd3dfx.templateapp.services.IArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
Expand Down
Loading

0 comments on commit dd8c8fe

Please sign in to comment.