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:
20
backend/src/main/java/com/recipeapp/common/ApiError.java
Normal file
20
backend/src/main/java/com/recipeapp/common/ApiError.java
Normal 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) {}
|
||||
}
|
||||
22
backend/src/main/java/com/recipeapp/common/ApiResponse.java
Normal file
22
backend/src/main/java/com/recipeapp/common/ApiResponse.java
Normal 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) {}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.recipeapp.common;
|
||||
|
||||
public class ConflictException extends RuntimeException {
|
||||
public ConflictException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.recipeapp.common;
|
||||
|
||||
public class ResourceNotFoundException extends RuntimeException {
|
||||
public ResourceNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.recipeapp.common;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public ValidationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.recipeapp;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
@SpringBootTest
|
||||
@Testcontainers
|
||||
@ActiveProfiles("test")
|
||||
public abstract class AbstractIntegrationTest {
|
||||
|
||||
@Container
|
||||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine")
|
||||
.withDatabaseName("mealprep")
|
||||
.withUsername("mealprep")
|
||||
.withPassword("mealprep");
|
||||
|
||||
@DynamicPropertySource
|
||||
static void configureProperties(DynamicPropertyRegistry registry) {
|
||||
registry.add("spring.datasource.url", postgres::getJdbcUrl);
|
||||
registry.add("spring.datasource.username", postgres::getUsername);
|
||||
registry.add("spring.datasource.password", postgres::getPassword);
|
||||
}
|
||||
}
|
||||
12
backend/src/test/resources/application-test.yml
Normal file
12
backend/src/test/resources/application-test.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:tc:postgresql:16-alpine:///mealprep
|
||||
username: mealprep
|
||||
password: mealprep
|
||||
jpa:
|
||||
open-in-view: false
|
||||
hibernate:
|
||||
ddl-auto: validate
|
||||
flyway:
|
||||
enabled: true
|
||||
locations: classpath:db/migration
|
||||
Reference in New Issue
Block a user