Skip to content

Commit 737e3c0

Browse files
committed
feat: added test controller and exception validation
1 parent 2d89add commit 737e3c0

File tree

9 files changed

+817
-8
lines changed

9 files changed

+817
-8
lines changed

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amplicode.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project version="4">
3+
<component name="AmplicodePersistenceUnitSettings">
4+
<persistence-units>
5+
<persistence-unit moduleName="service" name="Default">
6+
<packages>
7+
<package value="service.task.manager" />
8+
</packages>
9+
</persistence-unit>
10+
</persistence-units>
11+
</component>
12+
</project>

service/src/main/java/service/task/manager/controller/EpicController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public ResponseEntity<EpicResponseDto> update(@RequestBody @Valid EpicRequestUpd
6060
@ApiResponse(responseCode = "404", description = "Epic not found")
6161
})
6262
public ResponseEntity<EpicResponseDto> findById(
63-
@Parameter(description = "ID of the epic to retrieve") @PathVariable @Positive @NotNull Long id) {
63+
@Parameter(description = "ID of the epic to retrieve") @PathVariable @Positive(message = "id must be positive")
64+
@NotNull(message = "null id") Long id) {
6465
log.info("Fetching epic with ID: {}", id);
6566
EpicResponseDto epic = service.findById(id);
6667
return ResponseEntity.ok(epic);
@@ -82,7 +83,8 @@ public ResponseEntity<List<EpicResponseDto>> findAll() {
8283
@ApiResponse(responseCode = "404", description = "Epic not found")
8384
})
8485
public ResponseEntity<Void> delete(
85-
@Parameter(description = "ID of the epic to delete") @PathVariable @Positive @NotNull Long id) {
86+
@Parameter(description = "ID of the epic to delete") @PathVariable @Positive(message = "id must be positive")
87+
@NotNull(message = "null id") Long id) {
8688
log.info("Deleting epic with ID: {}", id);
8789
service.delete(id);
8890
return ResponseEntity.noContent().build();

service/src/main/java/service/task/manager/controller/SubtaskController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public ResponseEntity<SubtaskResponseDto> update(@RequestBody @Valid SubtaskRequ
6161
@ApiResponse(responseCode = "404", description = "Subtask not found")
6262
})
6363
public ResponseEntity<SubtaskResponseDto> findById(
64-
@Parameter(description = "ID of the subtask to retrieve") @PathVariable @Positive @NotNull Long id) {
64+
@Parameter(description = "ID of the subtask to retrieve") @PathVariable @Positive(message = "id must be positive")
65+
@NotNull(message = "null id") Long id) {
6566
log.info("Fetching subtask with ID: {}", id);
6667
SubtaskResponseDto subtask = service.findById(id);
6768
return ResponseEntity.ok(subtask);
@@ -83,7 +84,8 @@ public ResponseEntity<List<SubtaskResponseDto>> findAll() {
8384
@ApiResponse(responseCode = "404", description = "Subtask not found")
8485
})
8586
public ResponseEntity<Void> delete(
86-
@Parameter(description = "ID of the subtask to delete") @PathVariable @Positive @NotNull Long id) {
87+
@Parameter(description = "ID of the subtask to delete") @PathVariable @Positive(message = "id must be positive")
88+
@NotNull(message = "null id") Long id) {
8789
log.info("Deleting subtask with ID: {}", id);
8890
service.delete(id);
8991
return ResponseEntity.noContent().build();

service/src/main/java/service/task/manager/controller/TaskController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public ResponseEntity<TaskResponseDto> update(@RequestBody @Valid TaskRequestUpd
6060
@ApiResponse(responseCode = "404", description = "Task not found")
6161
})
6262
public ResponseEntity<TaskResponseDto> get(
63-
@Parameter(description = "ID of the task to retrieve") @PathVariable @Positive @NotNull Long id) {
63+
@Parameter(description = "ID of the task to retrieve") @PathVariable @Positive(message = "id must be positive")
64+
@NotNull(message = "null id") Long id) {
6465
log.info("Fetching task with ID: {}", id);
6566
TaskResponseDto task = service.findById(id);
6667
return ResponseEntity.ok(task);
@@ -82,7 +83,8 @@ public ResponseEntity<List<TaskResponseDto>> getAll() {
8283
@ApiResponse(responseCode = "404", description = "Task not found")
8384
})
8485
public ResponseEntity<Void> delete(
85-
@Parameter(description = "ID of the task to delete") @PathVariable @Positive @NotNull Long id) {
86+
@Parameter(description = "ID of the task to delete") @PathVariable @Positive(message = "id must be positive")
87+
@NotNull(message = "null id") Long id) {
8688
log.info("Deleting task with ID: {}", id);
8789
service.delete(id);
8890
return ResponseEntity.noContent().build();

service/src/main/java/service/task/manager/error/ErrorHandler.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package service.task.manager.error;
22

3+
import jakarta.validation.ConstraintViolation;
4+
import jakarta.validation.ConstraintViolationException;
5+
import org.springframework.context.MessageSourceResolvable;
36
import org.springframework.http.HttpStatus;
7+
import org.springframework.validation.FieldError;
8+
import org.springframework.validation.ObjectError;
9+
import org.springframework.web.bind.MethodArgumentNotValidException;
410
import org.springframework.web.bind.annotation.ExceptionHandler;
511
import org.springframework.web.bind.annotation.ResponseStatus;
612
import org.springframework.web.bind.annotation.RestControllerAdvice;
13+
import org.springframework.web.method.annotation.HandlerMethodValidationException;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
717

818
@RestControllerAdvice
919
public class ErrorHandler {
@@ -32,7 +42,47 @@ public ErrorResponse handleConflictException(ConflictException ex) {
3242
return new ErrorResponse(ex.getMessage());
3343
}
3444

35-
record ErrorResponse(String message) {
45+
@ExceptionHandler(MethodArgumentNotValidException.class)
46+
@ResponseStatus(HttpStatus.BAD_REQUEST)
47+
public Map<String, String> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
48+
Map<String, String> errors = new HashMap<>();
49+
for (FieldError error : ex.getBindingResult().getFieldErrors()) {
50+
errors.put(error.getField(), error.getDefaultMessage());
51+
}
52+
return errors;
53+
}
54+
55+
@ExceptionHandler(ConstraintViolationException.class)
56+
@ResponseStatus(HttpStatus.BAD_REQUEST)
57+
public Map<String, String> handleConstraintViolationException(ConstraintViolationException ex) {
58+
Map<String, String> errors = new HashMap<>();
59+
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
60+
String fieldName = violation.getPropertyPath().toString();
61+
fieldName = fieldName.substring(fieldName.lastIndexOf('.') + 1);
62+
errors.put(fieldName, violation.getMessage());
63+
}
64+
return errors;
3665
}
3766

38-
}
67+
@ExceptionHandler(HandlerMethodValidationException.class)
68+
@ResponseStatus(HttpStatus.BAD_REQUEST)
69+
public Map<String, String> handleHandlerMethodValidationException(HandlerMethodValidationException ex) {
70+
Map<String, String> errors = new HashMap<>();
71+
for (MessageSourceResolvable resolvable : ex.getAllErrors()) {
72+
if (resolvable instanceof FieldError fieldError) {
73+
errors.put(fieldError.getField(), fieldError.getDefaultMessage());
74+
} else if (resolvable instanceof ObjectError objectError) {
75+
// Для параметров метода (например, @PathVariable)
76+
String paramName = objectError.getObjectName();
77+
errors.put(paramName, objectError.getDefaultMessage());
78+
} else {
79+
// Для случаев, когда resolvable не является ObjectError
80+
errors.put("error", resolvable.getDefaultMessage());
81+
}
82+
}
83+
return errors;
84+
}
85+
86+
record ErrorResponse(String message) {
87+
}
88+
}

0 commit comments

Comments
 (0)