mirror of
https://github.com/s-frick/effigenix.git
synced 2026-03-28 08:29:36 +01:00
feat(production): Batch bei Produktionsstart automatisch erstellen (#73)
- BatchNumber in allen ProductionOrder-Endpoints via BatchRepository auflösen - BatchCreationFailed Error-Variante statt generischem ValidationFailure - bestBeforeDate-Berechnung als Recipe.calculateBestBeforeDate() in die Domain verschoben
This commit is contained in:
parent
26adf21162
commit
600d0f9f06
20 changed files with 356 additions and 397 deletions
|
|
@ -11,22 +11,28 @@ public class StartProductionOrder {
|
|||
|
||||
private final ProductionOrderRepository productionOrderRepository;
|
||||
private final BatchRepository batchRepository;
|
||||
private final RecipeRepository recipeRepository;
|
||||
private final BatchNumberGenerator batchNumberGenerator;
|
||||
private final AuthorizationPort authorizationPort;
|
||||
private final UnitOfWork unitOfWork;
|
||||
|
||||
public StartProductionOrder(
|
||||
ProductionOrderRepository productionOrderRepository,
|
||||
BatchRepository batchRepository,
|
||||
RecipeRepository recipeRepository,
|
||||
BatchNumberGenerator batchNumberGenerator,
|
||||
AuthorizationPort authorizationPort,
|
||||
UnitOfWork unitOfWork
|
||||
) {
|
||||
this.productionOrderRepository = productionOrderRepository;
|
||||
this.batchRepository = batchRepository;
|
||||
this.recipeRepository = recipeRepository;
|
||||
this.batchNumberGenerator = batchNumberGenerator;
|
||||
this.authorizationPort = authorizationPort;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public Result<ProductionOrderError, ProductionOrder> execute(StartProductionOrderCommand cmd, ActorId performedBy) {
|
||||
public Result<ProductionOrderError, StartProductionResult> execute(StartProductionOrderCommand cmd, ActorId performedBy) {
|
||||
if (!authorizationPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)) {
|
||||
return Result.failure(new ProductionOrderError.Unauthorized("Not authorized to start production orders"));
|
||||
}
|
||||
|
|
@ -46,56 +52,80 @@ public class StartProductionOrder {
|
|||
}
|
||||
}
|
||||
|
||||
// Load batch
|
||||
var batchId = BatchId.of(cmd.batchId());
|
||||
Batch batch;
|
||||
switch (batchRepository.findById(batchId)) {
|
||||
// Load recipe
|
||||
Recipe recipe;
|
||||
switch (recipeRepository.findById(order.recipeId())) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var opt) -> {
|
||||
if (opt.isEmpty()) {
|
||||
return Result.failure(new ProductionOrderError.ValidationFailure("Batch '" + cmd.batchId() + "' not found"));
|
||||
return Result.failure(new ProductionOrderError.ValidationFailure(
|
||||
"Recipe '" + order.recipeId().value() + "' not found"));
|
||||
}
|
||||
batch = opt.get();
|
||||
recipe = opt.get();
|
||||
}
|
||||
}
|
||||
|
||||
// Batch must be PLANNED
|
||||
if (batch.status() != BatchStatus.PLANNED) {
|
||||
// Recipe must be ACTIVE
|
||||
if (recipe.status() != RecipeStatus.ACTIVE) {
|
||||
return Result.failure(new ProductionOrderError.ValidationFailure(
|
||||
"Batch '" + cmd.batchId() + "' is not in PLANNED status (current: " + batch.status() + ")"));
|
||||
"Recipe '" + recipe.id().value() + "' is not ACTIVE (current: " + recipe.status() + ")"));
|
||||
}
|
||||
|
||||
// Batch must reference the same recipe as the order
|
||||
if (!batch.recipeId().equals(order.recipeId())) {
|
||||
return Result.failure(new ProductionOrderError.ValidationFailure(
|
||||
"Batch recipe '" + batch.recipeId().value() + "' does not match order recipe '" + order.recipeId().value() + "'"));
|
||||
// Calculate bestBeforeDate (validates shelfLifeDays internally)
|
||||
java.time.LocalDate bestBeforeDate;
|
||||
switch (recipe.calculateBestBeforeDate(order.plannedDate())) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOrderError.ValidationFailure(
|
||||
"Recipe '" + recipe.id().value() + "' has no valid shelf life configured: " + err.message()));
|
||||
}
|
||||
case Result.Success(var val) -> bestBeforeDate = val;
|
||||
}
|
||||
|
||||
// Start production on order (RELEASED -> IN_PROGRESS, assigns batchId)
|
||||
switch (order.startProduction(batchId)) {
|
||||
case Result.Failure(var err) -> { return Result.failure(err); }
|
||||
case Result.Success(var ignored) -> { }
|
||||
// Generate batch number
|
||||
BatchNumber batchNumber;
|
||||
switch (batchNumberGenerator.generateNext(order.plannedDate())) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var val) -> batchNumber = val;
|
||||
}
|
||||
|
||||
// Build BatchDraft from order data
|
||||
var batchDraft = new BatchDraft(
|
||||
order.recipeId().value(),
|
||||
order.plannedQuantity().amount().toPlainString(),
|
||||
order.plannedQuantity().uom().name(),
|
||||
order.plannedDate(),
|
||||
bestBeforeDate
|
||||
);
|
||||
|
||||
// Create batch in PLANNED status
|
||||
Batch batch;
|
||||
switch (Batch.plan(batchDraft, batchNumber)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOrderError.BatchCreationFailed(err.code(), err.message()));
|
||||
}
|
||||
case Result.Success(var val) -> batch = val;
|
||||
}
|
||||
|
||||
// Start production on batch (PLANNED -> IN_PRODUCTION)
|
||||
switch (batch.startProduction()) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOrderError.ValidationFailure(err.message()));
|
||||
return Result.failure(new ProductionOrderError.BatchCreationFailed(err.code(), err.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
// Persist both atomically
|
||||
return unitOfWork.executeAtomically(() -> {
|
||||
switch (productionOrderRepository.save(order)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
// Start production on order (RELEASED -> IN_PROGRESS, assigns batchId)
|
||||
switch (order.startProduction(batch.id())) {
|
||||
case Result.Failure(var err) -> { return Result.failure(err); }
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
// Persist both atomically (batch first due to FK constraint on production_orders.batch_id)
|
||||
return unitOfWork.executeAtomically(() -> {
|
||||
switch (batchRepository.save(batch)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
|
||||
|
|
@ -103,7 +133,14 @@ public class StartProductionOrder {
|
|||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
return Result.success(order);
|
||||
switch (productionOrderRepository.save(order)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
return Result.success(new StartProductionResult(order, batch));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
package de.effigenix.application.production;
|
||||
|
||||
import de.effigenix.domain.production.Batch;
|
||||
import de.effigenix.domain.production.ProductionOrder;
|
||||
|
||||
public record StartProductionResult(ProductionOrder order, Batch batch) {
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package de.effigenix.application.production.command;
|
||||
|
||||
public record StartProductionOrderCommand(String productionOrderId, String batchId) {
|
||||
public record StartProductionOrderCommand(String productionOrderId) {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,6 +55,10 @@ public sealed interface ProductionOrderError {
|
|||
@Override public String message() { return "Cannot reschedule production order in status " + current; }
|
||||
}
|
||||
|
||||
record BatchCreationFailed(String batchErrorCode, String message) implements ProductionOrderError {
|
||||
@Override public String code() { return "PRODUCTION_ORDER_BATCH_CREATION_FAILED"; }
|
||||
}
|
||||
|
||||
record ValidationFailure(String message) implements ProductionOrderError {
|
||||
@Override public String code() { return "PRODUCTION_ORDER_VALIDATION_ERROR"; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -253,6 +253,16 @@ public class Recipe {
|
|||
return Result.success(null);
|
||||
}
|
||||
|
||||
// ==================== Business Logic ====================
|
||||
|
||||
public Result<RecipeError, java.time.LocalDate> calculateBestBeforeDate(java.time.LocalDate productionDate) {
|
||||
if (shelfLifeDays == null || shelfLifeDays <= 0) {
|
||||
return Result.failure(new RecipeError.InvalidShelfLife(
|
||||
"ShelfLifeDays must be > 0, was: " + shelfLifeDays));
|
||||
}
|
||||
return Result.success(productionDate.plusDays(shelfLifeDays));
|
||||
}
|
||||
|
||||
// ==================== Getters ====================
|
||||
|
||||
public RecipeId id() { return id; }
|
||||
|
|
|
|||
|
|
@ -161,9 +161,12 @@ public class ProductionUseCaseConfiguration {
|
|||
@Bean
|
||||
public StartProductionOrder startProductionOrder(ProductionOrderRepository productionOrderRepository,
|
||||
BatchRepository batchRepository,
|
||||
RecipeRepository recipeRepository,
|
||||
BatchNumberGenerator batchNumberGenerator,
|
||||
AuthorizationPort authorizationPort,
|
||||
UnitOfWork unitOfWork) {
|
||||
return new StartProductionOrder(productionOrderRepository, batchRepository, authorizationPort, unitOfWork);
|
||||
return new StartProductionOrder(productionOrderRepository, batchRepository, recipeRepository,
|
||||
batchNumberGenerator, authorizationPort, unitOfWork);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
|||
|
|
@ -14,13 +14,15 @@ import de.effigenix.application.production.command.CreateProductionOrderCommand;
|
|||
import de.effigenix.application.production.command.ReleaseProductionOrderCommand;
|
||||
import de.effigenix.application.production.command.RescheduleProductionOrderCommand;
|
||||
import de.effigenix.application.production.command.StartProductionOrderCommand;
|
||||
import de.effigenix.domain.production.BatchRepository;
|
||||
import de.effigenix.domain.production.ProductionOrder;
|
||||
import de.effigenix.domain.production.ProductionOrderError;
|
||||
import de.effigenix.domain.production.ProductionOrderStatus;
|
||||
import de.effigenix.infrastructure.production.web.dto.CancelProductionOrderRequest;
|
||||
import de.effigenix.infrastructure.production.web.dto.CreateProductionOrderRequest;
|
||||
import de.effigenix.infrastructure.production.web.dto.ProductionOrderResponse;
|
||||
import de.effigenix.infrastructure.production.web.dto.RescheduleProductionOrderRequest;
|
||||
import de.effigenix.infrastructure.production.web.dto.StartProductionOrderRequest;
|
||||
|
||||
import de.effigenix.shared.security.ActorId;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
|
@ -53,6 +55,7 @@ public class ProductionOrderController {
|
|||
private final CompleteProductionOrder completeProductionOrder;
|
||||
private final CancelProductionOrder cancelProductionOrder;
|
||||
private final ListProductionOrders listProductionOrders;
|
||||
private final BatchRepository batchRepository;
|
||||
|
||||
public ProductionOrderController(CreateProductionOrder createProductionOrder,
|
||||
GetProductionOrder getProductionOrder,
|
||||
|
|
@ -61,7 +64,8 @@ public class ProductionOrderController {
|
|||
StartProductionOrder startProductionOrder,
|
||||
CompleteProductionOrder completeProductionOrder,
|
||||
CancelProductionOrder cancelProductionOrder,
|
||||
ListProductionOrders listProductionOrders) {
|
||||
ListProductionOrders listProductionOrders,
|
||||
BatchRepository batchRepository) {
|
||||
this.createProductionOrder = createProductionOrder;
|
||||
this.getProductionOrder = getProductionOrder;
|
||||
this.releaseProductionOrder = releaseProductionOrder;
|
||||
|
|
@ -70,6 +74,7 @@ public class ProductionOrderController {
|
|||
this.completeProductionOrder = completeProductionOrder;
|
||||
this.cancelProductionOrder = cancelProductionOrder;
|
||||
this.listProductionOrders = listProductionOrders;
|
||||
this.batchRepository = batchRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
|
|
@ -104,7 +109,7 @@ public class ProductionOrderController {
|
|||
}
|
||||
|
||||
var responses = result.unsafeGetValue().stream()
|
||||
.map(ProductionOrderResponse::from)
|
||||
.map(order -> ProductionOrderResponse.from(order, resolveBatchNumber(order)))
|
||||
.toList();
|
||||
return ResponseEntity.ok(responses);
|
||||
}
|
||||
|
|
@ -120,7 +125,7 @@ public class ProductionOrderController {
|
|||
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
|
|
@ -147,7 +152,7 @@ public class ProductionOrderController {
|
|||
}
|
||||
|
||||
return ResponseEntity.status(HttpStatus.CREATED)
|
||||
.body(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
.body(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/reschedule")
|
||||
|
|
@ -166,7 +171,7 @@ public class ProductionOrderController {
|
|||
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/release")
|
||||
|
|
@ -184,26 +189,36 @@ public class ProductionOrderController {
|
|||
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/start")
|
||||
@PreAuthorize("hasAuthority('PRODUCTION_ORDER_WRITE')")
|
||||
public ResponseEntity<ProductionOrderResponse> startProductionOrder(
|
||||
@PathVariable String id,
|
||||
@Valid @RequestBody StartProductionOrderRequest request,
|
||||
Authentication authentication
|
||||
) {
|
||||
logger.info("Starting production for order: {} with batch: {} by actor: {}", id, request.batchId(), authentication.getName());
|
||||
logger.info("Starting production for order: {} by actor: {}", id, authentication.getName());
|
||||
|
||||
var cmd = new StartProductionOrderCommand(id, request.batchId());
|
||||
var cmd = new StartProductionOrderCommand(id);
|
||||
var result = startProductionOrder.execute(cmd, ActorId.of(authentication.getName()));
|
||||
|
||||
if (result.isFailure()) {
|
||||
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
var startResult = result.unsafeGetValue();
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(
|
||||
startResult.order(),
|
||||
startResult.batch().batchNumber().value()));
|
||||
}
|
||||
|
||||
private String resolveBatchNumber(ProductionOrder order) {
|
||||
if (order.batchId() == null) {
|
||||
return null;
|
||||
}
|
||||
return batchRepository.findById(order.batchId())
|
||||
.fold(err -> null, opt -> opt.map(batch -> batch.batchNumber().value()).orElse(null));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/complete")
|
||||
|
|
@ -221,7 +236,7 @@ public class ProductionOrderController {
|
|||
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/cancel")
|
||||
|
|
@ -240,7 +255,7 @@ public class ProductionOrderController {
|
|||
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
|
||||
}
|
||||
|
||||
public static class ProductionOrderDomainErrorException extends RuntimeException {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ public record ProductionOrderResponse(
|
|||
String recipeId,
|
||||
String status,
|
||||
String batchId,
|
||||
String batchNumber,
|
||||
String plannedQuantity,
|
||||
String plannedQuantityUnit,
|
||||
LocalDate plannedDate,
|
||||
|
|
@ -19,12 +20,13 @@ public record ProductionOrderResponse(
|
|||
OffsetDateTime createdAt,
|
||||
OffsetDateTime updatedAt
|
||||
) {
|
||||
public static ProductionOrderResponse from(ProductionOrder order) {
|
||||
public static ProductionOrderResponse from(ProductionOrder order, String batchNumber) {
|
||||
return new ProductionOrderResponse(
|
||||
order.id().value(),
|
||||
order.recipeId().value(),
|
||||
order.status().name(),
|
||||
order.batchId() != null ? order.batchId().value() : null,
|
||||
batchNumber,
|
||||
order.plannedQuantity().amount().toPlainString(),
|
||||
order.plannedQuantity().uom().name(),
|
||||
order.plannedDate(),
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
package de.effigenix.infrastructure.production.web.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public record StartProductionOrderRequest(@NotBlank String batchId) {
|
||||
}
|
||||
|
|
@ -61,6 +61,7 @@ public final class ProductionErrorHttpStatusMapper {
|
|||
case ProductionOrderError.RescheduleNotAllowed e -> 409;
|
||||
case ProductionOrderError.BatchAlreadyAssigned e -> 409;
|
||||
case ProductionOrderError.BatchNotCompleted e -> 409;
|
||||
case ProductionOrderError.BatchCreationFailed e -> 400;
|
||||
case ProductionOrderError.ValidationFailure e -> 400;
|
||||
case ProductionOrderError.Unauthorized e -> 403;
|
||||
case ProductionOrderError.RepositoryFailure e -> 500;
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ class StartProductionOrderTest {
|
|||
|
||||
@Mock private ProductionOrderRepository productionOrderRepository;
|
||||
@Mock private BatchRepository batchRepository;
|
||||
@Mock private RecipeRepository recipeRepository;
|
||||
@Mock private BatchNumberGenerator batchNumberGenerator;
|
||||
@Mock private AuthorizationPort authPort;
|
||||
@Mock private UnitOfWork unitOfWork;
|
||||
|
||||
|
|
@ -41,16 +43,19 @@ class StartProductionOrderTest {
|
|||
private ActorId performedBy;
|
||||
|
||||
private static final LocalDate PLANNED_DATE = LocalDate.now().plusDays(7);
|
||||
private static final int SHELF_LIFE_DAYS = 14;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
startProductionOrder = new StartProductionOrder(productionOrderRepository, batchRepository, authPort, unitOfWork);
|
||||
startProductionOrder = new StartProductionOrder(
|
||||
productionOrderRepository, batchRepository, recipeRepository,
|
||||
batchNumberGenerator, authPort, unitOfWork);
|
||||
performedBy = ActorId.of("admin-user");
|
||||
lenient().when(unitOfWork.executeAtomically(any())).thenAnswer(inv -> ((Supplier<?>) inv.getArgument(0)).get());
|
||||
}
|
||||
|
||||
private StartProductionOrderCommand validCommand() {
|
||||
return new StartProductionOrderCommand("order-1", "batch-1");
|
||||
return new StartProductionOrderCommand("order-1");
|
||||
}
|
||||
|
||||
private ProductionOrder releasedOrder() {
|
||||
|
|
@ -87,78 +92,90 @@ class StartProductionOrderTest {
|
|||
);
|
||||
}
|
||||
|
||||
private Batch plannedBatch() {
|
||||
return Batch.reconstitute(
|
||||
BatchId.of("batch-1"),
|
||||
new BatchNumber("P-2026-02-24-001"),
|
||||
private Recipe activeRecipe() {
|
||||
return Recipe.reconstitute(
|
||||
RecipeId.of("recipe-1"),
|
||||
BatchStatus.PLANNED,
|
||||
RecipeName.of("Test Recipe").unsafeGetValue(),
|
||||
1,
|
||||
RecipeType.FINISHED_PRODUCT,
|
||||
"Test",
|
||||
YieldPercentage.of(100).unsafeGetValue(),
|
||||
SHELF_LIFE_DAYS,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
null, null, null,
|
||||
PLANNED_DATE, PLANNED_DATE.plusDays(14),
|
||||
"ART-001",
|
||||
RecipeStatus.ACTIVE,
|
||||
List.of(),
|
||||
List.of(),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
null, null, null,
|
||||
1L,
|
||||
List.of()
|
||||
OffsetDateTime.now(ZoneOffset.UTC)
|
||||
);
|
||||
}
|
||||
|
||||
private Batch plannedBatchWithDifferentRecipe() {
|
||||
return Batch.reconstitute(
|
||||
BatchId.of("batch-1"),
|
||||
new BatchNumber("P-2026-02-24-001"),
|
||||
RecipeId.of("recipe-other"),
|
||||
BatchStatus.PLANNED,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
null, null, null,
|
||||
PLANNED_DATE, PLANNED_DATE.plusDays(14),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
null, null, null,
|
||||
1L,
|
||||
List.of()
|
||||
);
|
||||
}
|
||||
|
||||
private Batch inProductionBatch() {
|
||||
return Batch.reconstitute(
|
||||
BatchId.of("batch-1"),
|
||||
new BatchNumber("P-2026-02-24-001"),
|
||||
private Recipe draftRecipe() {
|
||||
return Recipe.reconstitute(
|
||||
RecipeId.of("recipe-1"),
|
||||
BatchStatus.IN_PRODUCTION,
|
||||
RecipeName.of("Test Recipe").unsafeGetValue(),
|
||||
1,
|
||||
RecipeType.FINISHED_PRODUCT,
|
||||
"Test",
|
||||
YieldPercentage.of(100).unsafeGetValue(),
|
||||
SHELF_LIFE_DAYS,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
null, null, null,
|
||||
PLANNED_DATE, PLANNED_DATE.plusDays(14),
|
||||
"ART-001",
|
||||
RecipeStatus.DRAFT,
|
||||
List.of(),
|
||||
List.of(),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
null, null, null,
|
||||
1L,
|
||||
List.of()
|
||||
OffsetDateTime.now(ZoneOffset.UTC)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should start production when order is RELEASED and batch is PLANNED")
|
||||
void should_StartProduction_When_ValidCommand() {
|
||||
private BatchNumber batchNumber() {
|
||||
return new BatchNumber("P-" + PLANNED_DATE + "-001");
|
||||
}
|
||||
|
||||
private void setupHappyPath() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedBatch())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(activeRecipe())));
|
||||
when(batchNumberGenerator.generateNext(PLANNED_DATE))
|
||||
.thenReturn(Result.success(batchNumber()));
|
||||
when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
|
||||
when(batchRepository.save(any())).thenReturn(Result.success(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should auto-create batch and start production when order is RELEASED")
|
||||
void should_StartProduction_When_ValidCommand() {
|
||||
setupHappyPath();
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isSuccess()).isTrue();
|
||||
var order = result.unsafeGetValue();
|
||||
assertThat(order.status()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
|
||||
assertThat(order.batchId()).isEqualTo(BatchId.of("batch-1"));
|
||||
var startResult = result.unsafeGetValue();
|
||||
assertThat(startResult.order().status()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
|
||||
assertThat(startResult.order().batchId()).isNotNull();
|
||||
assertThat(startResult.batch().status()).isEqualTo(BatchStatus.IN_PRODUCTION);
|
||||
assertThat(startResult.batch().batchNumber()).isEqualTo(batchNumber());
|
||||
assertThat(startResult.batch().recipeId()).isEqualTo(RecipeId.of("recipe-1"));
|
||||
verify(productionOrderRepository).save(any(ProductionOrder.class));
|
||||
verify(batchRepository).save(any(Batch.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should calculate bestBeforeDate from plannedDate + shelfLifeDays")
|
||||
void should_CalculateBestBeforeDate() {
|
||||
setupHappyPath();
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isSuccess()).isTrue();
|
||||
var batch = result.unsafeGetValue().batch();
|
||||
assertThat(batch.bestBeforeDate()).isEqualTo(PLANNED_DATE.plusDays(SHELF_LIFE_DAYS));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when actor lacks PRODUCTION_ORDER_WRITE permission")
|
||||
void should_Fail_When_Unauthorized() {
|
||||
|
|
@ -186,52 +203,36 @@ class StartProductionOrderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch not found")
|
||||
void should_Fail_When_BatchNotFound() {
|
||||
@DisplayName("should fail when recipe not found")
|
||||
void should_Fail_When_RecipeNotFound() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.empty()));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
assertThat(result.unsafeGetError().message()).contains("Recipe");
|
||||
assertThat(result.unsafeGetError().message()).contains("not found");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch is not PLANNED")
|
||||
void should_Fail_When_BatchNotPlanned() {
|
||||
@DisplayName("should fail when recipe is not ACTIVE")
|
||||
void should_Fail_When_RecipeNotActive() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
.thenReturn(Result.success(Optional.of(inProductionBatch())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(draftRecipe())));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch recipe does not match order recipe")
|
||||
void should_Fail_When_RecipeMismatch() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedBatchWithDifferentRecipe())));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
|
||||
assertThat(result.unsafeGetError().message()).contains("does not match order recipe");
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
assertThat(result.unsafeGetError().message()).contains("not ACTIVE");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -240,8 +241,10 @@ class StartProductionOrderTest {
|
|||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedBatch())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(activeRecipe())));
|
||||
when(batchNumberGenerator.generateNext(PLANNED_DATE))
|
||||
.thenReturn(Result.success(batchNumber()));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
|
|
@ -265,12 +268,12 @@ class StartProductionOrderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch repository findById returns error")
|
||||
void should_Fail_When_BatchRepositoryError() {
|
||||
@DisplayName("should fail when recipe repository returns error")
|
||||
void should_Fail_When_RecipeRepositoryError() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost")));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
|
@ -280,32 +283,16 @@ class StartProductionOrderTest {
|
|||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when order save fails")
|
||||
void should_Fail_When_OrderSaveFails() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedBatch())));
|
||||
when(productionOrderRepository.save(any()))
|
||||
.thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch save fails")
|
||||
void should_Fail_When_BatchSaveFails() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedBatch())));
|
||||
when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(activeRecipe())));
|
||||
when(batchNumberGenerator.generateNext(PLANNED_DATE))
|
||||
.thenReturn(Result.success(batchNumber()));
|
||||
when(batchRepository.save(any()))
|
||||
.thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
|
||||
|
||||
|
|
@ -316,68 +303,126 @@ class StartProductionOrderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch is COMPLETED")
|
||||
void should_Fail_When_BatchCompleted() {
|
||||
var completedBatch = Batch.reconstitute(
|
||||
BatchId.of("batch-1"),
|
||||
new BatchNumber("P-2026-02-24-001"),
|
||||
@DisplayName("should fail when order save fails")
|
||||
void should_Fail_When_OrderSaveFails() {
|
||||
setupHappyPath();
|
||||
when(productionOrderRepository.save(any()))
|
||||
.thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch number generation fails")
|
||||
void should_Fail_When_BatchNumberGenerationFails() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(activeRecipe())));
|
||||
when(batchNumberGenerator.generateNext(PLANNED_DATE))
|
||||
.thenReturn(Result.failure(new BatchError.RepositoryFailure("Sequence corrupted")));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
verify(batchRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when recipe has null shelfLifeDays")
|
||||
void should_Fail_When_ShelfLifeDaysNull() {
|
||||
var recipeWithoutShelfLife = Recipe.reconstitute(
|
||||
RecipeId.of("recipe-1"),
|
||||
BatchStatus.COMPLETED,
|
||||
RecipeName.of("Test Recipe").unsafeGetValue(),
|
||||
1,
|
||||
RecipeType.FINISHED_PRODUCT,
|
||||
"Test",
|
||||
YieldPercentage.of(100).unsafeGetValue(),
|
||||
null,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
Quantity.of(new BigDecimal("95"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
Quantity.of(new BigDecimal("5"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
"done",
|
||||
PLANNED_DATE, PLANNED_DATE.plusDays(14),
|
||||
"ART-001",
|
||||
RecipeStatus.ACTIVE,
|
||||
List.of(),
|
||||
List.of(),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
null, null,
|
||||
1L,
|
||||
List.of()
|
||||
OffsetDateTime.now(ZoneOffset.UTC)
|
||||
);
|
||||
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
.thenReturn(Result.success(Optional.of(completedBatch)));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(recipeWithoutShelfLife)));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
|
||||
assertThat(result.unsafeGetError().message()).contains("shelf life");
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch is CANCELLED")
|
||||
void should_Fail_When_BatchCancelled() {
|
||||
var cancelledBatch = Batch.reconstitute(
|
||||
BatchId.of("batch-1"),
|
||||
new BatchNumber("P-2026-02-24-001"),
|
||||
@DisplayName("should fail when recipe has zero shelfLifeDays")
|
||||
void should_Fail_When_ShelfLifeDaysZero() {
|
||||
var recipeWithZeroShelfLife = Recipe.reconstitute(
|
||||
RecipeId.of("recipe-1"),
|
||||
BatchStatus.CANCELLED,
|
||||
RecipeName.of("Test Recipe").unsafeGetValue(),
|
||||
1,
|
||||
RecipeType.FINISHED_PRODUCT,
|
||||
"Test",
|
||||
YieldPercentage.of(100).unsafeGetValue(),
|
||||
0,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
null, null, null,
|
||||
PLANNED_DATE, PLANNED_DATE.plusDays(14),
|
||||
"ART-001",
|
||||
RecipeStatus.ACTIVE,
|
||||
List.of(),
|
||||
List.of(),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
null,
|
||||
"Storniert", OffsetDateTime.now(ZoneOffset.UTC),
|
||||
1L,
|
||||
List.of()
|
||||
OffsetDateTime.now(ZoneOffset.UTC)
|
||||
);
|
||||
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(batchRepository.findById(BatchId.of("batch-1")))
|
||||
.thenReturn(Result.success(Optional.of(cancelledBatch)));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(recipeWithZeroShelfLife)));
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
assertThat(result.unsafeGetError().message()).contains("shelf life");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should set batch plannedQuantity and UOM from order")
|
||||
void should_PropagateQuantityFromOrderToBatch() {
|
||||
setupHappyPath();
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isSuccess()).isTrue();
|
||||
var batch = result.unsafeGetValue().batch();
|
||||
assertThat(batch.plannedQuantity().amount()).isEqualByComparingTo(new BigDecimal("100"));
|
||||
assertThat(batch.plannedQuantity().uom()).isEqualTo(UnitOfMeasure.KILOGRAM);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should assign generated batch id to the order")
|
||||
void should_AssignBatchIdToOrder() {
|
||||
setupHappyPath();
|
||||
|
||||
var result = startProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isSuccess()).isTrue();
|
||||
var startResult = result.unsafeGetValue();
|
||||
assertThat(startResult.order().batchId()).isEqualTo(startResult.batch().id());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,11 +39,7 @@ class ProductionOrderFuzzTest {
|
|||
int op = data.consumeInt(0, 4);
|
||||
switch (op) {
|
||||
case 0 -> order.release();
|
||||
case 1 -> {
|
||||
try {
|
||||
order.startProduction(BatchId.of(data.consumeString(50)));
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
case 1 -> order.startProduction(BatchId.of(data.consumeString(50)));
|
||||
case 2 -> order.complete();
|
||||
case 3 -> order.cancel(data.consumeString(50));
|
||||
case 4 -> order.reschedule(consumeLocalDate(data));
|
||||
|
|
|
|||
|
|
@ -389,41 +389,26 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
|
|||
class StartProductionOrderEndpoint {
|
||||
|
||||
@Test
|
||||
@DisplayName("RELEASED Order mit PLANNED Batch starten → 200, Status IN_PROGRESS")
|
||||
void startOrder_releasedWithPlannedBatch_returns200() throws Exception {
|
||||
String[] orderAndRecipe = createReleasedOrderWithRecipe();
|
||||
String orderId = orderAndRecipe[0];
|
||||
String batchId = createPlannedBatch(orderAndRecipe[1]);
|
||||
|
||||
String json = """
|
||||
{"batchId": "%s"}
|
||||
""".formatted(batchId);
|
||||
@DisplayName("RELEASED Order starten → 200, Batch automatisch erstellt, Status IN_PROGRESS")
|
||||
void startOrder_released_returns200() throws Exception {
|
||||
String orderId = createReleasedOrder();
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").value(orderId))
|
||||
.andExpect(jsonPath("$.status").value("IN_PROGRESS"))
|
||||
.andExpect(jsonPath("$.batchId").value(batchId));
|
||||
.andExpect(jsonPath("$.batchId").isNotEmpty())
|
||||
.andExpect(jsonPath("$.batchNumber").isNotEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("PLANNED Order starten → 409 (InvalidStatusTransition)")
|
||||
void startOrder_plannedOrder_returns409() throws Exception {
|
||||
String[] orderAndRecipe = createPlannedOrderWithRecipe();
|
||||
String orderId = orderAndRecipe[0];
|
||||
String batchId = createPlannedBatch(orderAndRecipe[1]);
|
||||
|
||||
String json = """
|
||||
{"batchId": "%s"}
|
||||
""".formatted(batchId);
|
||||
String orderId = createPlannedOrder();
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isConflict())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
|
||||
}
|
||||
|
|
@ -431,145 +416,54 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
|
|||
@Test
|
||||
@DisplayName("Order nicht gefunden → 404")
|
||||
void startOrder_notFound_returns404() throws Exception {
|
||||
String json = """
|
||||
{"batchId": "non-existent-batch"}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", "non-existent-id")
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isNotFound())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_NOT_FOUND"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Batch nicht gefunden → 400")
|
||||
void startOrder_batchNotFound_returns400() throws Exception {
|
||||
String orderId = createReleasedOrder();
|
||||
|
||||
String json = """
|
||||
{"batchId": "non-existent-batch"}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Ohne PRODUCTION_ORDER_WRITE → 403")
|
||||
void startOrder_withViewerToken_returns403() throws Exception {
|
||||
String json = """
|
||||
{"batchId": "any-batch"}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
|
||||
.header("Authorization", "Bearer " + viewerToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.header("Authorization", "Bearer " + viewerToken))
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Ohne Token → 401")
|
||||
void startOrder_withoutToken_returns401() throws Exception {
|
||||
String json = """
|
||||
{"batchId": "any-batch"}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id"))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Batch nicht PLANNED (bereits gestartet) → 400")
|
||||
void startOrder_batchNotPlanned_returns400() throws Exception {
|
||||
String[] orderAndRecipe = createReleasedOrderWithRecipe();
|
||||
String orderId = orderAndRecipe[0];
|
||||
String batchId = createStartedBatch(orderAndRecipe[1]);
|
||||
|
||||
String json = """
|
||||
{"batchId": "%s"}
|
||||
""".formatted(batchId);
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Bereits gestartete Order erneut starten → 409")
|
||||
void startOrder_alreadyStarted_returns409() throws Exception {
|
||||
String[] orderAndRecipe = createReleasedOrderWithRecipe();
|
||||
String orderId = orderAndRecipe[0];
|
||||
String recipeId = orderAndRecipe[1];
|
||||
String batchId1 = createPlannedBatch(recipeId);
|
||||
String batchId2 = createPlannedBatch(recipeId);
|
||||
|
||||
String json1 = """
|
||||
{"batchId": "%s"}
|
||||
""".formatted(batchId1);
|
||||
String orderId = createReleasedOrder();
|
||||
|
||||
// First start
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json1))
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
// Second start
|
||||
String json2 = """
|
||||
{"batchId": "%s"}
|
||||
""".formatted(batchId2);
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json2))
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isConflict())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Batch mit anderem Rezept → 400 (RecipeMismatch)")
|
||||
void startOrder_recipeMismatch_returns400() throws Exception {
|
||||
String orderId = createReleasedOrder();
|
||||
String batchId = createPlannedBatch(); // creates batch with different recipe
|
||||
|
||||
String json = """
|
||||
{"batchId": "%s"}
|
||||
""".formatted(batchId);
|
||||
@DisplayName("Order mit archiviertem Rezept starten → 400")
|
||||
void startOrder_archivedRecipe_returns400() throws Exception {
|
||||
String orderId = createReleasedOrderThenArchiveRecipe();
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isBadRequest())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"))
|
||||
.andExpect(jsonPath("$.message").value(org.hamcrest.Matchers.containsString("does not match order recipe")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("batchId leer → 400 (Bean Validation)")
|
||||
void startOrder_blankBatchId_returns400() throws Exception {
|
||||
String json = """
|
||||
{"batchId": ""}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.andExpect(status().isBadRequest());
|
||||
.andExpect(jsonPath("$.message").value(org.hamcrest.Matchers.containsString("not ACTIVE")));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1178,21 +1072,12 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
|
|||
return orderAndRecipe;
|
||||
}
|
||||
|
||||
/** Creates an IN_PROGRESS order (with a started batch). Returns orderId. */
|
||||
/** Creates an IN_PROGRESS order (batch auto-created by start). Returns orderId. */
|
||||
private String createInProgressOrder() throws Exception {
|
||||
String[] orderAndRecipe = createReleasedOrderWithRecipe();
|
||||
String orderId = orderAndRecipe[0];
|
||||
String recipeId = orderAndRecipe[1];
|
||||
String batchId = createPlannedBatch(recipeId);
|
||||
|
||||
String json = """
|
||||
{"batchId": "%s"}
|
||||
""".formatted(batchId);
|
||||
String orderId = createReleasedOrder();
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json))
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
return orderId;
|
||||
|
|
@ -1203,17 +1088,13 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
|
|||
String[] orderAndRecipe = createReleasedOrderWithRecipe();
|
||||
String orderId = orderAndRecipe[0];
|
||||
String recipeId = orderAndRecipe[1];
|
||||
String batchId = createPlannedBatch(recipeId);
|
||||
|
||||
String startJson = """
|
||||
{"batchId": "%s"}
|
||||
""".formatted(batchId);
|
||||
var startResult = mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(startJson))
|
||||
.andExpect(status().isOk());
|
||||
String batchId = objectMapper.readTree(startResult.getResponse().getContentAsString()).get("batchId").asText();
|
||||
|
||||
// Record a consumption (required to complete batch)
|
||||
String inputBatchId = createPlannedBatch(recipeId);
|
||||
|
|
@ -1236,8 +1117,18 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
|
|||
return new String[]{orderId, batchId};
|
||||
}
|
||||
|
||||
private String createPlannedBatch() throws Exception {
|
||||
return createPlannedBatch(createActiveRecipe());
|
||||
/** Creates a RELEASED order, then archives its recipe. Returns orderId. */
|
||||
private String createReleasedOrderThenArchiveRecipe() throws Exception {
|
||||
String[] orderAndRecipe = createReleasedOrderWithRecipe();
|
||||
String orderId = orderAndRecipe[0];
|
||||
String recipeId = orderAndRecipe[1];
|
||||
|
||||
// Archive the recipe
|
||||
mockMvc.perform(post("/api/recipes/{id}/archive", recipeId)
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
return orderId;
|
||||
}
|
||||
|
||||
private String createPlannedBatch(String recipeId) throws Exception {
|
||||
|
|
@ -1252,18 +1143,6 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
|
|||
return objectMapper.readTree(planResult.getResponse().getContentAsString()).get("id").asText();
|
||||
}
|
||||
|
||||
private String createStartedBatch() throws Exception {
|
||||
return createStartedBatch(createActiveRecipe());
|
||||
}
|
||||
|
||||
private String createStartedBatch(String recipeId) throws Exception {
|
||||
String batchId = createPlannedBatch(recipeId);
|
||||
mockMvc.perform(post("/api/production/batches/{id}/start", batchId)
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk());
|
||||
return batchId;
|
||||
}
|
||||
|
||||
private String createDraftRecipe() throws Exception {
|
||||
String json = """
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue