mirror of
https://github.com/s-frick/effigenix.git
synced 2026-03-28 13:49:36 +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:
parent
14b59722f7
commit
72d59b4948
17 changed files with 1216 additions and 2 deletions
|
|
@ -0,0 +1,60 @@
|
|||
package de.effigenix.application.production;
|
||||
|
||||
import de.effigenix.application.production.command.CancelProductionOrderCommand;
|
||||
import de.effigenix.domain.production.*;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
import de.effigenix.shared.security.ActorId;
|
||||
import de.effigenix.shared.security.AuthorizationPort;
|
||||
|
||||
public class CancelProductionOrder {
|
||||
|
||||
private final ProductionOrderRepository productionOrderRepository;
|
||||
private final AuthorizationPort authorizationPort;
|
||||
private final UnitOfWork unitOfWork;
|
||||
|
||||
public CancelProductionOrder(
|
||||
ProductionOrderRepository productionOrderRepository,
|
||||
AuthorizationPort authorizationPort,
|
||||
UnitOfWork unitOfWork
|
||||
) {
|
||||
this.productionOrderRepository = productionOrderRepository;
|
||||
this.authorizationPort = authorizationPort;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public Result<ProductionOrderError, ProductionOrder> execute(CancelProductionOrderCommand cmd, ActorId performedBy) {
|
||||
if (!authorizationPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)) {
|
||||
return Result.failure(new ProductionOrderError.Unauthorized("Not authorized to cancel production orders"));
|
||||
}
|
||||
|
||||
var orderId = ProductionOrderId.of(cmd.productionOrderId());
|
||||
ProductionOrder order;
|
||||
switch (productionOrderRepository.findById(orderId)) {
|
||||
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.ProductionOrderNotFound(orderId));
|
||||
}
|
||||
order = opt.get();
|
||||
}
|
||||
}
|
||||
|
||||
switch (order.cancel()) {
|
||||
case Result.Failure(var err) -> { return Result.failure(err); }
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
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) -> { }
|
||||
}
|
||||
return Result.success(order);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package de.effigenix.application.production;
|
||||
|
||||
import de.effigenix.application.production.command.CompleteProductionOrderCommand;
|
||||
import de.effigenix.domain.production.*;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
import de.effigenix.shared.security.ActorId;
|
||||
import de.effigenix.shared.security.AuthorizationPort;
|
||||
|
||||
public class CompleteProductionOrder {
|
||||
|
||||
private final ProductionOrderRepository productionOrderRepository;
|
||||
private final BatchRepository batchRepository;
|
||||
private final AuthorizationPort authorizationPort;
|
||||
private final UnitOfWork unitOfWork;
|
||||
|
||||
public CompleteProductionOrder(
|
||||
ProductionOrderRepository productionOrderRepository,
|
||||
BatchRepository batchRepository,
|
||||
AuthorizationPort authorizationPort,
|
||||
UnitOfWork unitOfWork
|
||||
) {
|
||||
this.productionOrderRepository = productionOrderRepository;
|
||||
this.batchRepository = batchRepository;
|
||||
this.authorizationPort = authorizationPort;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public Result<ProductionOrderError, ProductionOrder> execute(CompleteProductionOrderCommand cmd, ActorId performedBy) {
|
||||
if (!authorizationPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)) {
|
||||
return Result.failure(new ProductionOrderError.Unauthorized("Not authorized to complete production orders"));
|
||||
}
|
||||
|
||||
var orderId = ProductionOrderId.of(cmd.productionOrderId());
|
||||
ProductionOrder order;
|
||||
switch (productionOrderRepository.findById(orderId)) {
|
||||
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.ProductionOrderNotFound(orderId));
|
||||
}
|
||||
order = opt.get();
|
||||
}
|
||||
}
|
||||
|
||||
// Batch must be COMPLETED (defense in depth – domain checks status transition)
|
||||
var batchId = order.batchId();
|
||||
if (batchId == null) {
|
||||
return Result.failure(new ProductionOrderError.ValidationFailure("Production order has no batch assigned"));
|
||||
}
|
||||
|
||||
Batch batch;
|
||||
switch (batchRepository.findById(batchId)) {
|
||||
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 '" + batchId.value() + "' not found"));
|
||||
}
|
||||
batch = opt.get();
|
||||
}
|
||||
}
|
||||
|
||||
if (batch.status() != BatchStatus.COMPLETED) {
|
||||
return Result.failure(new ProductionOrderError.BatchNotCompleted(batchId));
|
||||
}
|
||||
|
||||
switch (order.complete()) {
|
||||
case Result.Failure(var err) -> { return Result.failure(err); }
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
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) -> { }
|
||||
}
|
||||
return Result.success(order);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package de.effigenix.application.production.command;
|
||||
|
||||
public record CancelProductionOrderCommand(String productionOrderId, String reason) {
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package de.effigenix.application.production.command;
|
||||
|
||||
public record CompleteProductionOrderCommand(String productionOrderId) {
|
||||
}
|
||||
|
|
@ -22,6 +22,8 @@ import java.time.ZoneOffset;
|
|||
* 7. Only RELEASED → IN_PROGRESS transition allowed via startProduction(BatchId)
|
||||
* 8. BatchId is set exactly once (null → non-null) during startProduction()
|
||||
* 9. BatchId must not already be assigned (BatchAlreadyAssigned)
|
||||
* 10. Only IN_PROGRESS → COMPLETED transition allowed via complete() (Batch must be COMPLETED – enforced by Use Case)
|
||||
* 11. Only PLANNED or RELEASED → CANCELLED transition allowed via cancel()
|
||||
*/
|
||||
public class ProductionOrder {
|
||||
|
||||
|
|
@ -174,4 +176,22 @@ public class ProductionOrder {
|
|||
this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
return Result.success(null);
|
||||
}
|
||||
|
||||
public Result<ProductionOrderError, Void> complete() {
|
||||
if (status != ProductionOrderStatus.IN_PROGRESS) {
|
||||
return Result.failure(new ProductionOrderError.InvalidStatusTransition(status, ProductionOrderStatus.COMPLETED));
|
||||
}
|
||||
this.status = ProductionOrderStatus.COMPLETED;
|
||||
this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
return Result.success(null);
|
||||
}
|
||||
|
||||
public Result<ProductionOrderError, Void> cancel() {
|
||||
if (status != ProductionOrderStatus.PLANNED && status != ProductionOrderStatus.RELEASED) {
|
||||
return Result.failure(new ProductionOrderError.InvalidStatusTransition(status, ProductionOrderStatus.CANCELLED));
|
||||
}
|
||||
this.status = ProductionOrderStatus.CANCELLED;
|
||||
this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
return Result.success(null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,6 +45,11 @@ public sealed interface ProductionOrderError {
|
|||
@Override public String message() { return "Production order already has batch '" + batchId.value() + "' assigned"; }
|
||||
}
|
||||
|
||||
record BatchNotCompleted(BatchId batchId) implements ProductionOrderError {
|
||||
@Override public String code() { return "PRODUCTION_ORDER_BATCH_NOT_COMPLETED"; }
|
||||
@Override public String message() { return "Batch '" + batchId.value() + "' is not in COMPLETED status"; }
|
||||
}
|
||||
|
||||
record ValidationFailure(String message) implements ProductionOrderError {
|
||||
@Override public String code() { return "PRODUCTION_ORDER_VALIDATION_ERROR"; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import de.effigenix.application.production.ActivateRecipe;
|
|||
import de.effigenix.application.production.ArchiveRecipe;
|
||||
import de.effigenix.application.production.AddProductionStep;
|
||||
import de.effigenix.application.production.AddRecipeIngredient;
|
||||
import de.effigenix.application.production.CancelProductionOrder;
|
||||
import de.effigenix.application.production.CompleteProductionOrder;
|
||||
import de.effigenix.application.production.CreateProductionOrder;
|
||||
import de.effigenix.application.production.ReleaseProductionOrder;
|
||||
import de.effigenix.application.production.StartProductionOrder;
|
||||
|
|
@ -160,4 +162,19 @@ public class ProductionUseCaseConfiguration {
|
|||
UnitOfWork unitOfWork) {
|
||||
return new StartProductionOrder(productionOrderRepository, batchRepository, authorizationPort, unitOfWork);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CompleteProductionOrder completeProductionOrder(ProductionOrderRepository productionOrderRepository,
|
||||
BatchRepository batchRepository,
|
||||
AuthorizationPort authorizationPort,
|
||||
UnitOfWork unitOfWork) {
|
||||
return new CompleteProductionOrder(productionOrderRepository, batchRepository, authorizationPort, unitOfWork);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CancelProductionOrder cancelProductionOrder(ProductionOrderRepository productionOrderRepository,
|
||||
AuthorizationPort authorizationPort,
|
||||
UnitOfWork unitOfWork) {
|
||||
return new CancelProductionOrder(productionOrderRepository, authorizationPort, unitOfWork);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,17 @@
|
|||
package de.effigenix.infrastructure.production.web.controller;
|
||||
|
||||
import de.effigenix.application.production.CancelProductionOrder;
|
||||
import de.effigenix.application.production.CompleteProductionOrder;
|
||||
import de.effigenix.application.production.CreateProductionOrder;
|
||||
import de.effigenix.application.production.ReleaseProductionOrder;
|
||||
import de.effigenix.application.production.StartProductionOrder;
|
||||
import de.effigenix.application.production.command.CancelProductionOrderCommand;
|
||||
import de.effigenix.application.production.command.CompleteProductionOrderCommand;
|
||||
import de.effigenix.application.production.command.CreateProductionOrderCommand;
|
||||
import de.effigenix.application.production.command.ReleaseProductionOrderCommand;
|
||||
import de.effigenix.application.production.command.StartProductionOrderCommand;
|
||||
import de.effigenix.domain.production.ProductionOrderError;
|
||||
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.StartProductionOrderRequest;
|
||||
|
|
@ -33,13 +38,19 @@ public class ProductionOrderController {
|
|||
private final CreateProductionOrder createProductionOrder;
|
||||
private final ReleaseProductionOrder releaseProductionOrder;
|
||||
private final StartProductionOrder startProductionOrder;
|
||||
private final CompleteProductionOrder completeProductionOrder;
|
||||
private final CancelProductionOrder cancelProductionOrder;
|
||||
|
||||
public ProductionOrderController(CreateProductionOrder createProductionOrder,
|
||||
ReleaseProductionOrder releaseProductionOrder,
|
||||
StartProductionOrder startProductionOrder) {
|
||||
StartProductionOrder startProductionOrder,
|
||||
CompleteProductionOrder completeProductionOrder,
|
||||
CancelProductionOrder cancelProductionOrder) {
|
||||
this.createProductionOrder = createProductionOrder;
|
||||
this.releaseProductionOrder = releaseProductionOrder;
|
||||
this.startProductionOrder = startProductionOrder;
|
||||
this.completeProductionOrder = completeProductionOrder;
|
||||
this.cancelProductionOrder = cancelProductionOrder;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
|
|
@ -106,6 +117,43 @@ public class ProductionOrderController {
|
|||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/complete")
|
||||
@PreAuthorize("hasAuthority('PRODUCTION_ORDER_WRITE')")
|
||||
public ResponseEntity<ProductionOrderResponse> completeProductionOrder(
|
||||
@PathVariable String id,
|
||||
Authentication authentication
|
||||
) {
|
||||
logger.info("Completing production order: {} by actor: {}", id, authentication.getName());
|
||||
|
||||
var cmd = new CompleteProductionOrderCommand(id);
|
||||
var result = completeProductionOrder.execute(cmd, ActorId.of(authentication.getName()));
|
||||
|
||||
if (result.isFailure()) {
|
||||
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/cancel")
|
||||
@PreAuthorize("hasAuthority('PRODUCTION_ORDER_WRITE')")
|
||||
public ResponseEntity<ProductionOrderResponse> cancelProductionOrder(
|
||||
@PathVariable String id,
|
||||
@Valid @RequestBody CancelProductionOrderRequest request,
|
||||
Authentication authentication
|
||||
) {
|
||||
logger.info("Cancelling production order: {} by actor: {}, reason: {}", id, authentication.getName(), request.reason());
|
||||
|
||||
var cmd = new CancelProductionOrderCommand(id, request.reason());
|
||||
var result = cancelProductionOrder.execute(cmd, ActorId.of(authentication.getName()));
|
||||
|
||||
if (result.isFailure()) {
|
||||
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
}
|
||||
|
||||
public static class ProductionOrderDomainErrorException extends RuntimeException {
|
||||
private final ProductionOrderError error;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
package de.effigenix.infrastructure.production.web.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public record CancelProductionOrderRequest(@NotBlank String reason) {
|
||||
}
|
||||
|
|
@ -59,6 +59,7 @@ public final class ProductionErrorHttpStatusMapper {
|
|||
case ProductionOrderError.RecipeNotActive e -> 409;
|
||||
case ProductionOrderError.InvalidStatusTransition e -> 409;
|
||||
case ProductionOrderError.BatchAlreadyAssigned e -> 409;
|
||||
case ProductionOrderError.BatchNotCompleted e -> 409;
|
||||
case ProductionOrderError.ValidationFailure e -> 400;
|
||||
case ProductionOrderError.Unauthorized e -> 403;
|
||||
case ProductionOrderError.RepositoryFailure e -> 500;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue