Add test infrastructure and common module

- Testcontainers 2.0.4 (PostgreSQL) for repository integration tests
- AbstractIntegrationTest base class with shared Postgres container
- application-test.yml for test profile
- Common module: ApiResponse/ApiError envelopes, GlobalExceptionHandler,
  ResourceNotFoundException, ConflictException, ValidationException,
  HouseholdContext record

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-01 21:05:17 +02:00
parent 10b4d567d3
commit 866603711d
12 changed files with 166 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
package com.recipeapp.common;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ApiError(
String status,
ErrorBody error
) {
public static ApiError of(String code, String message) {
return new ApiError("error", new ErrorBody(code, message, null));
}
public static ApiError of(String code, String message, List<String> details) {
return new ApiError("error", new ErrorBody(code, message, details));
}
public record ErrorBody(String code, String message, List<String> details) {}
}

View File

@@ -0,0 +1,22 @@
package com.recipeapp.common;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ApiResponse<T>(
String status,
T data,
Meta meta
) {
public static <T> ApiResponse<T> success(T data) {
return new ApiResponse<>("success", data, null);
}
public static <T> ApiResponse<T> success(T data, Meta meta) {
return new ApiResponse<>("success", data, meta);
}
public record Meta(Pagination pagination) {}
public record Pagination(long total, int limit, int offset, boolean hasMore) {}
}

View File

@@ -0,0 +1,7 @@
package com.recipeapp.common;
public class ConflictException extends RuntimeException {
public ConflictException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,40 @@
package com.recipeapp.common;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiError> handleValidation(MethodArgumentNotValidException ex) {
List<String> details = ex.getBindingResult().getFieldErrors().stream()
.map(e -> e.getField() + ": " + e.getDefaultMessage())
.toList();
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiError.of("VALIDATION_ERROR", "Validation failed", details));
}
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ApiError> handleNotFound(ResourceNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(ApiError.of("NOT_FOUND", ex.getMessage()));
}
@ExceptionHandler(ConflictException.class)
public ResponseEntity<ApiError> handleConflict(ConflictException ex) {
return ResponseEntity.status(HttpStatus.CONFLICT)
.body(ApiError.of("CONFLICT", ex.getMessage()));
}
@ExceptionHandler(ValidationException.class)
public ResponseEntity<ApiError> handleBusinessValidation(ValidationException ex) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY)
.body(ApiError.of("VALIDATION_ERROR", ex.getMessage()));
}
}

View File

@@ -0,0 +1,10 @@
package com.recipeapp.common;
import java.util.UUID;
public record HouseholdContext(UUID householdId, UUID userId, String role) {
public boolean isPlanner() {
return "planner".equals(role);
}
}

View File

@@ -0,0 +1,7 @@
package com.recipeapp.common;
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,7 @@
package com.recipeapp.common;
public class ValidationException extends RuntimeException {
public ValidationException(String message) {
super(message);
}
}