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

0
backend/mvnw vendored Normal file → Executable file
View File

View File

@@ -16,6 +16,7 @@
<properties>
<java.version>21</java.version>
<springdoc.version>3.0.2</springdoc.version>
<testcontainers.version>2.0.4</testcontainers.version>
</properties>
<dependencies>
<dependency>
@@ -64,6 +65,18 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

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);
}
}

View File

@@ -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);
}
}

View 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