-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/create test for authentication controller (#93)
- Added one more test to CategoryControllerTest complete all cases * Created news test and improved Exceptions
- Loading branch information
1 parent
1dac226
commit f27aa66
Showing
38 changed files
with
1,004 additions
and
447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 21 additions & 23 deletions
44
src/main/java/com/alura/aluraflixapi/controller/ControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,34 @@ | ||
package com.alura.aluraflixapi.controller; | ||
|
||
import com.alura.aluraflixapi.controller.dto.ErrorDto; | ||
import java.util.List; | ||
import com.alura.aluraflixapi.controller.dto.ErrorVO; | ||
import com.alura.aluraflixapi.infraestructure.exception.ErrorMessageVO; | ||
import com.alura.aluraflixapi.infraestructure.exception.ResourceNotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.List; | ||
|
||
@RestControllerAdvice | ||
public class ControllerAdvice { | ||
|
||
/** | ||
* Handle Invalid fields | ||
* @return List of ErrorDto with invalid fields | ||
*/ | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<List<ErrorDto>> handleInvalidFields( | ||
final MethodArgumentNotValidException ex) { | ||
var errors = ex.getFieldErrors(); | ||
return ResponseEntity.badRequest().body(errors.stream().map(ErrorDto::new).toList()); | ||
} | ||
/** | ||
* Handle Invalid fields | ||
* | ||
* @return List of ErrorDto with invalid fields | ||
*/ | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<List<ErrorVO>> handleInvalidFields( | ||
final MethodArgumentNotValidException ex) { | ||
var errors = ex.getFieldErrors(); | ||
return ResponseEntity.badRequest().body(errors.stream().map(ErrorVO::new).toList()); | ||
} | ||
|
||
/** | ||
* handle invalid credentials whe user atempt to login | ||
* @param ex HttpMessageNotReadableException | ||
* @return ResponseEntity status bad_request | ||
*/ | ||
@ExceptionHandler(HttpMessageNotReadableException.class) | ||
public ResponseEntity<String> handleLoginException( | ||
final HttpMessageNotReadableException ex) { | ||
return ResponseEntity.badRequest().body("Invalid Credentials"); | ||
} | ||
@ExceptionHandler(ResourceNotFoundException.class) | ||
public ResponseEntity<ErrorMessageVO> handlerResourceNotFoundException(final ResourceNotFoundException ex) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorMessageVO(ex.getMessage(), HttpStatus.NOT_FOUND)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
src/main/java/com/alura/aluraflixapi/controller/dto/ErrorDto.java
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
src/main/java/com/alura/aluraflixapi/controller/dto/ErrorVO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.alura.aluraflixapi.controller.dto; | ||
|
||
import org.springframework.validation.FieldError; | ||
|
||
public record ErrorVO(String field, String message) { | ||
|
||
public ErrorVO(FieldError fieldError) { | ||
this(fieldError.getField(), fieldError.getDefaultMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/alura/aluraflixapi/infraestructure/exception/CategoryServiceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.alura.aluraflixapi.infraestructure.exception; | ||
|
||
public class CategoryServiceException extends RuntimeException { | ||
public CategoryServiceException(String message) { | ||
super(message); | ||
} | ||
|
||
public CategoryServiceException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Oops, something went wrong.