1
0
Fork 0
mirror of https://github.com/s-frick/effigenix.git synced 2026-03-28 12:09:35 +01:00

feat(production): Produktionsauftrag abschließen und stornieren (US-P16)

Complete: IN_PROGRESS → COMPLETED (nur wenn Batch COMPLETED)
Cancel: PLANNED/RELEASED → CANCELLED (nicht aus IN_PROGRESS)

Domain-, UseCase-, Integration- und Lasttests ergänzt.
This commit is contained in:
Sebastian Frick 2026-02-25 21:41:45 +01:00
parent 14b59722f7
commit 72d59b4948
17 changed files with 1216 additions and 2 deletions

View file

@ -0,0 +1,180 @@
package de.effigenix.application.production;
import de.effigenix.application.production.command.CancelProductionOrderCommand;
import de.effigenix.domain.production.*;
import de.effigenix.shared.common.Quantity;
import de.effigenix.shared.common.RepositoryError;
import de.effigenix.shared.common.Result;
import de.effigenix.shared.common.UnitOfMeasure;
import de.effigenix.shared.persistence.UnitOfWork;
import de.effigenix.shared.security.ActorId;
import de.effigenix.shared.security.AuthorizationPort;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Optional;
import java.util.function.Supplier;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@DisplayName("CancelProductionOrder Use Case")
class CancelProductionOrderTest {
@Mock private ProductionOrderRepository productionOrderRepository;
@Mock private AuthorizationPort authPort;
@Mock private UnitOfWork unitOfWork;
private CancelProductionOrder cancelProductionOrder;
private ActorId performedBy;
private static final LocalDate PLANNED_DATE = LocalDate.now().plusDays(7);
@BeforeEach
void setUp() {
cancelProductionOrder = new CancelProductionOrder(productionOrderRepository, authPort, unitOfWork);
performedBy = ActorId.of("admin-user");
lenient().when(unitOfWork.executeAtomically(any())).thenAnswer(inv -> ((Supplier<?>) inv.getArgument(0)).get());
}
private CancelProductionOrderCommand validCommand() {
return new CancelProductionOrderCommand("order-1", "Kunde hat storniert");
}
private ProductionOrder orderWithStatus(ProductionOrderStatus status) {
return ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-1"),
status,
status == ProductionOrderStatus.IN_PROGRESS ? BatchId.of("batch-1") : null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
PLANNED_DATE,
Priority.NORMAL,
null,
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
1L
);
}
@Test
@DisplayName("should cancel PLANNED order")
void should_Cancel_PlannedOrder() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(orderWithStatus(ProductionOrderStatus.PLANNED))));
when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
var result = cancelProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isSuccess()).isTrue();
assertThat(result.unsafeGetValue().status()).isEqualTo(ProductionOrderStatus.CANCELLED);
verify(productionOrderRepository).save(any(ProductionOrder.class));
}
@Test
@DisplayName("should cancel RELEASED order")
void should_Cancel_ReleasedOrder() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(orderWithStatus(ProductionOrderStatus.RELEASED))));
when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
var result = cancelProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isSuccess()).isTrue();
assertThat(result.unsafeGetValue().status()).isEqualTo(ProductionOrderStatus.CANCELLED);
verify(productionOrderRepository).save(any(ProductionOrder.class));
}
@Test
@DisplayName("should fail when order is IN_PROGRESS")
void should_Fail_When_InProgress() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(orderWithStatus(ProductionOrderStatus.IN_PROGRESS))));
var result = cancelProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.InvalidStatusTransition.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order is COMPLETED")
void should_Fail_When_Completed() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(orderWithStatus(ProductionOrderStatus.COMPLETED))));
var result = cancelProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.InvalidStatusTransition.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order is already CANCELLED")
void should_Fail_When_AlreadyCancelled() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(orderWithStatus(ProductionOrderStatus.CANCELLED))));
var result = cancelProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.InvalidStatusTransition.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order not found")
void should_Fail_When_OrderNotFound() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.empty()));
var result = cancelProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ProductionOrderNotFound.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when unauthorized")
void should_Fail_When_Unauthorized() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(false);
var result = cancelProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.Unauthorized.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when repository returns error")
void should_Fail_When_RepositoryError() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost")));
var result = cancelProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
}
}

View file

@ -0,0 +1,255 @@
package de.effigenix.application.production;
import de.effigenix.application.production.command.CompleteProductionOrderCommand;
import de.effigenix.domain.production.*;
import de.effigenix.shared.common.Quantity;
import de.effigenix.shared.common.RepositoryError;
import de.effigenix.shared.common.Result;
import de.effigenix.shared.common.UnitOfMeasure;
import de.effigenix.shared.persistence.UnitOfWork;
import de.effigenix.shared.security.ActorId;
import de.effigenix.shared.security.AuthorizationPort;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@DisplayName("CompleteProductionOrder Use Case")
class CompleteProductionOrderTest {
@Mock private ProductionOrderRepository productionOrderRepository;
@Mock private BatchRepository batchRepository;
@Mock private AuthorizationPort authPort;
@Mock private UnitOfWork unitOfWork;
private CompleteProductionOrder completeProductionOrder;
private ActorId performedBy;
private static final LocalDate PLANNED_DATE = LocalDate.now().plusDays(7);
@BeforeEach
void setUp() {
completeProductionOrder = new CompleteProductionOrder(productionOrderRepository, batchRepository, authPort, unitOfWork);
performedBy = ActorId.of("admin-user");
lenient().when(unitOfWork.executeAtomically(any())).thenAnswer(inv -> ((Supplier<?>) inv.getArgument(0)).get());
}
private CompleteProductionOrderCommand validCommand() {
return new CompleteProductionOrderCommand("order-1");
}
private ProductionOrder inProgressOrder() {
return ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-1"),
ProductionOrderStatus.IN_PROGRESS,
BatchId.of("batch-1"),
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
PLANNED_DATE,
Priority.NORMAL,
null,
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
1L
);
}
private ProductionOrder plannedOrder() {
return ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-1"),
ProductionOrderStatus.PLANNED,
null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
PLANNED_DATE,
Priority.NORMAL,
null,
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
1L
);
}
private Batch completedBatch() {
return Batch.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-25-001"),
RecipeId.of("recipe-1"),
BatchStatus.COMPLETED,
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),
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
null, null,
1L,
List.of()
);
}
private Batch inProductionBatch() {
return Batch.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-25-001"),
RecipeId.of("recipe-1"),
BatchStatus.IN_PRODUCTION,
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()
);
}
@Test
@DisplayName("should complete order when IN_PROGRESS and batch is COMPLETED")
void should_Complete_When_InProgressAndBatchCompleted() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(inProgressOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(completedBatch())));
when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
var result = completeProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isSuccess()).isTrue();
assertThat(result.unsafeGetValue().status()).isEqualTo(ProductionOrderStatus.COMPLETED);
verify(productionOrderRepository).save(any(ProductionOrder.class));
}
@Test
@DisplayName("should fail when batch is not COMPLETED")
void should_Fail_When_BatchNotCompleted() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(inProgressOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(inProductionBatch())));
var result = completeProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.BatchNotCompleted.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order is PLANNED (no batch assigned)")
void should_Fail_When_OrderPlanned() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(plannedOrder())));
var result = completeProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order is already COMPLETED")
void should_Fail_When_AlreadyCompleted() {
var completedOrder = ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"), RecipeId.of("recipe-1"),
ProductionOrderStatus.COMPLETED, BatchId.of("batch-1"),
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
PLANNED_DATE, Priority.NORMAL, null,
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC), 1L);
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(completedOrder)));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(completedBatch())));
var result = completeProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.InvalidStatusTransition.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order is CANCELLED")
void should_Fail_When_OrderCancelled() {
var cancelledOrder = ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"), RecipeId.of("recipe-1"),
ProductionOrderStatus.CANCELLED, null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
PLANNED_DATE, Priority.NORMAL, null,
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC), 1L);
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(cancelledOrder)));
var result = completeProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
// CANCELLED order has no batchId ValidationFailure
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order not found")
void should_Fail_When_OrderNotFound() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.empty()));
var result = completeProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ProductionOrderNotFound.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when unauthorized")
void should_Fail_When_Unauthorized() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(false);
var result = completeProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.Unauthorized.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order repository returns error")
void should_Fail_When_RepositoryError() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost")));
var result = completeProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
}
}

View file

@ -503,6 +503,180 @@ class ProductionOrderTest {
}
}
@Nested
@DisplayName("complete()")
class Complete {
private ProductionOrder orderWithStatus(ProductionOrderStatus status) {
return ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-123"),
status,
status == ProductionOrderStatus.IN_PROGRESS || status == ProductionOrderStatus.COMPLETED
? BatchId.of("batch-1") : null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
FUTURE_DATE,
Priority.NORMAL,
null,
OffsetDateTime.now(ZoneOffset.UTC).minusHours(1),
OffsetDateTime.now(ZoneOffset.UTC).minusHours(1),
1L
);
}
@Test
@DisplayName("should complete IN_PROGRESS order")
void should_Complete_When_InProgress() {
var order = orderWithStatus(ProductionOrderStatus.IN_PROGRESS);
var beforeUpdate = order.updatedAt();
var result = order.complete();
assertThat(result.isSuccess()).isTrue();
assertThat(order.status()).isEqualTo(ProductionOrderStatus.COMPLETED);
assertThat(order.updatedAt()).isAfter(beforeUpdate);
}
@Test
@DisplayName("should fail when completing PLANNED order")
void should_Fail_When_Planned() {
var order = orderWithStatus(ProductionOrderStatus.PLANNED);
var result = order.complete();
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.PLANNED);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.COMPLETED);
}
@Test
@DisplayName("should fail when completing RELEASED order")
void should_Fail_When_Released() {
var order = orderWithStatus(ProductionOrderStatus.RELEASED);
var result = order.complete();
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.RELEASED);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.COMPLETED);
}
@Test
@DisplayName("should fail when completing already COMPLETED order")
void should_Fail_When_AlreadyCompleted() {
var order = orderWithStatus(ProductionOrderStatus.COMPLETED);
var result = order.complete();
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.COMPLETED);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.COMPLETED);
}
@Test
@DisplayName("should fail when completing CANCELLED order")
void should_Fail_When_Cancelled() {
var order = orderWithStatus(ProductionOrderStatus.CANCELLED);
var result = order.complete();
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.CANCELLED);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.COMPLETED);
}
}
@Nested
@DisplayName("cancel()")
class Cancel {
private ProductionOrder orderWithStatus(ProductionOrderStatus status) {
return ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-123"),
status,
status == ProductionOrderStatus.IN_PROGRESS || status == ProductionOrderStatus.COMPLETED
? BatchId.of("batch-1") : null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
FUTURE_DATE,
Priority.NORMAL,
null,
OffsetDateTime.now(ZoneOffset.UTC).minusHours(1),
OffsetDateTime.now(ZoneOffset.UTC).minusHours(1),
1L
);
}
@Test
@DisplayName("should cancel PLANNED order")
void should_Cancel_When_Planned() {
var order = orderWithStatus(ProductionOrderStatus.PLANNED);
var beforeUpdate = order.updatedAt();
var result = order.cancel();
assertThat(result.isSuccess()).isTrue();
assertThat(order.status()).isEqualTo(ProductionOrderStatus.CANCELLED);
assertThat(order.updatedAt()).isAfter(beforeUpdate);
}
@Test
@DisplayName("should cancel RELEASED order")
void should_Cancel_When_Released() {
var order = orderWithStatus(ProductionOrderStatus.RELEASED);
var beforeUpdate = order.updatedAt();
var result = order.cancel();
assertThat(result.isSuccess()).isTrue();
assertThat(order.status()).isEqualTo(ProductionOrderStatus.CANCELLED);
assertThat(order.updatedAt()).isAfter(beforeUpdate);
}
@Test
@DisplayName("should fail when cancelling IN_PROGRESS order")
void should_Fail_When_InProgress() {
var order = orderWithStatus(ProductionOrderStatus.IN_PROGRESS);
var result = order.cancel();
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.CANCELLED);
}
@Test
@DisplayName("should fail when cancelling COMPLETED order")
void should_Fail_When_Completed() {
var order = orderWithStatus(ProductionOrderStatus.COMPLETED);
var result = order.cancel();
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.COMPLETED);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.CANCELLED);
}
@Test
@DisplayName("should fail when cancelling already CANCELLED order")
void should_Fail_When_AlreadyCancelled() {
var order = orderWithStatus(ProductionOrderStatus.CANCELLED);
var result = order.cancel();
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.CANCELLED);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.CANCELLED);
}
}
@Nested
@DisplayName("reconstitute()")
class Reconstitute {

View file

@ -2,8 +2,10 @@ package de.effigenix.infrastructure.production.web;
import de.effigenix.domain.usermanagement.RoleName;
import de.effigenix.infrastructure.AbstractIntegrationTest;
import de.effigenix.infrastructure.production.web.dto.CompleteBatchRequest;
import de.effigenix.infrastructure.production.web.dto.CreateProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.PlanBatchRequest;
import de.effigenix.infrastructure.production.web.dto.RecordConsumptionRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
@ -34,7 +36,7 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
String viewerId = createUser("po.viewer", "po.viewer@test.com", Set.of(viewerRoleId), "BRANCH-01");
adminToken = generateToken(adminId, "po.admin",
"PRODUCTION_ORDER_WRITE,PRODUCTION_ORDER_READ,RECIPE_WRITE,RECIPE_READ,BATCH_WRITE,BATCH_READ");
"PRODUCTION_ORDER_WRITE,PRODUCTION_ORDER_READ,RECIPE_WRITE,RECIPE_READ,BATCH_WRITE,BATCH_READ,BATCH_COMPLETE,BATCH_CANCEL");
viewerToken = generateToken(viewerId, "po.viewer", "USER_READ");
}
@ -570,6 +572,261 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
}
}
@Nested
@DisplayName("POST /api/production/production-orders/{id}/complete Produktionsauftrag abschließen")
class CompleteProductionOrderEndpoint {
@Test
@DisplayName("IN_PROGRESS Order mit COMPLETED Batch abschließen → 200")
void completeOrder_withCompletedBatch_returns200() throws Exception {
String[] orderAndBatch = createInProgressOrderWithCompletedBatch();
String orderId = orderAndBatch[0];
mockMvc.perform(post("/api/production/production-orders/{id}/complete", orderId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(orderId))
.andExpect(jsonPath("$.status").value("COMPLETED"));
}
@Test
@DisplayName("IN_PROGRESS Order mit nicht-COMPLETED Batch → 409")
void completeOrder_batchNotCompleted_returns409() throws Exception {
String orderId = createInProgressOrder();
mockMvc.perform(post("/api/production/production-orders/{id}/complete", orderId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isConflict())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_BATCH_NOT_COMPLETED"));
}
@Test
@DisplayName("PLANNED Order abschließen → 400 (kein Batch zugewiesen)")
void completeOrder_plannedOrder_returns400() throws Exception {
String orderId = createPlannedOrder();
mockMvc.perform(post("/api/production/production-orders/{id}/complete", orderId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"));
}
@Test
@DisplayName("RELEASED Order abschließen → 400 (kein Batch zugewiesen)")
void completeOrder_releasedOrder_returns400() throws Exception {
String orderId = createReleasedOrder();
mockMvc.perform(post("/api/production/production-orders/{id}/complete", orderId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"));
}
@Test
@DisplayName("Order nicht gefunden → 404")
void completeOrder_notFound_returns404() throws Exception {
mockMvc.perform(post("/api/production/production-orders/{id}/complete", "non-existent-id")
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_NOT_FOUND"));
}
@Test
@DisplayName("Bereits COMPLETED Order erneut abschließen → 409")
void completeOrder_alreadyCompleted_returns409() throws Exception {
String[] orderAndBatch = createInProgressOrderWithCompletedBatch();
String orderId = orderAndBatch[0];
// First complete
mockMvc.perform(post("/api/production/production-orders/{id}/complete", orderId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isOk());
// Second complete
mockMvc.perform(post("/api/production/production-orders/{id}/complete", orderId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isConflict())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
}
@Test
@DisplayName("Ohne PRODUCTION_ORDER_WRITE → 403")
void completeOrder_withViewerToken_returns403() throws Exception {
mockMvc.perform(post("/api/production/production-orders/{id}/complete", "any-id")
.header("Authorization", "Bearer " + viewerToken))
.andExpect(status().isForbidden());
}
@Test
@DisplayName("Ohne Token → 401")
void completeOrder_withoutToken_returns401() throws Exception {
mockMvc.perform(post("/api/production/production-orders/{id}/complete", "any-id"))
.andExpect(status().isUnauthorized());
}
}
@Nested
@DisplayName("POST /api/production/production-orders/{id}/cancel Produktionsauftrag stornieren")
class CancelProductionOrderEndpoint {
@Test
@DisplayName("PLANNED Order stornieren → 200")
void cancelOrder_planned_returns200() throws Exception {
String orderId = createPlannedOrder();
String json = """
{"reason": "Kunde hat storniert"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(orderId))
.andExpect(jsonPath("$.status").value("CANCELLED"));
}
@Test
@DisplayName("RELEASED Order stornieren → 200")
void cancelOrder_released_returns200() throws Exception {
String orderId = createReleasedOrder();
String json = """
{"reason": "Material nicht verfügbar"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(orderId))
.andExpect(jsonPath("$.status").value("CANCELLED"));
}
@Test
@DisplayName("IN_PROGRESS Order stornieren → 409")
void cancelOrder_inProgress_returns409() throws Exception {
String orderId = createInProgressOrder();
String json = """
{"reason": "Storno-Versuch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isConflict())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
}
@Test
@DisplayName("COMPLETED Order stornieren → 409")
void cancelOrder_completed_returns409() throws Exception {
String[] orderAndBatch = createInProgressOrderWithCompletedBatch();
String orderId = orderAndBatch[0];
// Complete it first
mockMvc.perform(post("/api/production/production-orders/{id}/complete", orderId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isOk());
String json = """
{"reason": "Storno-Versuch nach Abschluss"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isConflict())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
}
@Test
@DisplayName("Bereits CANCELLED Order erneut stornieren → 409")
void cancelOrder_alreadyCancelled_returns409() throws Exception {
String orderId = createPlannedOrder();
String json = """
{"reason": "Erster Storno"}
""";
// First cancel
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk());
// Second cancel
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isConflict())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
}
@Test
@DisplayName("Ohne reason → 400 (Bean Validation)")
void cancelOrder_blankReason_returns400() throws Exception {
String json = """
{"reason": ""}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", "any-id")
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest());
}
@Test
@DisplayName("Order nicht gefunden → 404")
void cancelOrder_notFound_returns404() throws Exception {
String json = """
{"reason": "Test"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", "non-existent-id")
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_NOT_FOUND"));
}
@Test
@DisplayName("Ohne PRODUCTION_ORDER_WRITE → 403")
void cancelOrder_withViewerToken_returns403() throws Exception {
String json = """
{"reason": "Test"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", "any-id")
.header("Authorization", "Bearer " + viewerToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isForbidden());
}
@Test
@DisplayName("Ohne Token → 401")
void cancelOrder_withoutToken_returns401() throws Exception {
String json = """
{"reason": "Test"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/cancel", "any-id")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isUnauthorized());
}
}
// ==================== Hilfsmethoden ====================
private String createPlannedOrder() throws Exception {
@ -653,6 +910,64 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
return orderAndRecipe;
}
/** Creates an IN_PROGRESS order (with a started batch). 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);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk());
return orderId;
}
/** Creates an IN_PROGRESS order with a COMPLETED batch. Returns [orderId, batchId]. */
private String[] createInProgressOrderWithCompletedBatch() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe();
String orderId = orderAndRecipe[0];
String recipeId = orderAndRecipe[1];
String batchId = createPlannedBatch(recipeId);
String startJson = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(startJson))
.andExpect(status().isOk());
// Record a consumption (required to complete batch)
String inputBatchId = createPlannedBatch(recipeId);
var consumptionRequest = new RecordConsumptionRequest(
inputBatchId, UUID.randomUUID().toString(), "10", "KILOGRAM");
mockMvc.perform(post("/api/production/batches/{id}/consumptions", batchId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(consumptionRequest)))
.andExpect(status().isCreated());
// Complete the batch
var completeBatchRequest = new CompleteBatchRequest("95", "KILOGRAM", "5", "KILOGRAM", "Fertig");
mockMvc.perform(post("/api/production/batches/{id}/complete", batchId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(completeBatchRequest)))
.andExpect(status().isOk());
return new String[]{orderId, batchId};
}
private String createPlannedBatch() throws Exception {
return createPlannedBatch(createActiveRecipe());
}