1
0
Fork 0
mirror of https://github.com/s-frick/effigenix.git synced 2026-03-28 15:29:34 +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:
Sebastian Frick 2026-02-26 09:13:51 +01:00
parent 26adf21162
commit 600d0f9f06
20 changed files with 356 additions and 397 deletions

View file

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

View file

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

View file

@ -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 = """
{