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

feat(production): Produktion über Auftrag starten (US-P15)

RELEASED ProductionOrder kann mit einer PLANNED Batch verknüpft und
in Produktion gestartet werden. Dabei wechselt der Order auf IN_PROGRESS
und die Batch auf IN_PRODUCTION. Neuer REST-Endpoint POST /{id}/start,
StartOrderProduction Use Case, BatchAlreadyAssigned Error, Liquibase-
Migration für batch_id FK auf production_orders.
This commit is contained in:
Sebastian Frick 2026-02-24 22:46:23 +01:00
parent a8bbe3a951
commit bfae3eff73
19 changed files with 985 additions and 17 deletions

View file

@ -54,6 +54,7 @@ class ReleaseProductionOrderTest {
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-1"),
ProductionOrderStatus.PLANNED,
null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
PLANNED_DATE,
Priority.NORMAL,
@ -69,6 +70,7 @@ class ReleaseProductionOrderTest {
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-1"),
ProductionOrderStatus.RELEASED,
null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
PLANNED_DATE,
Priority.NORMAL,

View file

@ -0,0 +1,377 @@
package de.effigenix.application.production;
import de.effigenix.application.production.command.StartProductionOrderCommand;
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.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 static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@DisplayName("StartProductionOrder Use Case")
class StartProductionOrderTest {
@Mock private ProductionOrderRepository productionOrderRepository;
@Mock private BatchRepository batchRepository;
@Mock private AuthorizationPort authPort;
private StartProductionOrder startProductionOrder;
private ActorId performedBy;
private static final LocalDate PLANNED_DATE = LocalDate.now().plusDays(7);
@BeforeEach
void setUp() {
startProductionOrder = new StartProductionOrder(productionOrderRepository, batchRepository, authPort);
performedBy = ActorId.of("admin-user");
}
private StartProductionOrderCommand validCommand() {
return new StartProductionOrderCommand("order-1", "batch-1");
}
private ProductionOrder releasedOrder() {
return ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-1"),
ProductionOrderStatus.RELEASED,
null,
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 plannedBatch() {
return Batch.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-001"),
RecipeId.of("recipe-1"),
BatchStatus.PLANNED,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
null, null, null,
PLANNED_DATE, PLANNED_DATE.plusDays(14),
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
null, null, null,
1L,
List.of()
);
}
private Batch plannedBatchWithDifferentRecipe() {
return Batch.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-001"),
RecipeId.of("recipe-other"),
BatchStatus.PLANNED,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
null, null, null,
PLANNED_DATE, PLANNED_DATE.plusDays(14),
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
null, null, null,
1L,
List.of()
);
}
private Batch inProductionBatch() {
return Batch.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-001"),
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 start production when order is RELEASED and batch is PLANNED")
void should_StartProduction_When_ValidCommand() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(plannedBatch())));
when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
when(batchRepository.save(any())).thenReturn(Result.success(null));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isSuccess()).isTrue();
var order = result.unsafeGetValue();
assertThat(order.status()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
assertThat(order.batchId()).isEqualTo(BatchId.of("batch-1"));
verify(productionOrderRepository).save(any(ProductionOrder.class));
verify(batchRepository).save(any(Batch.class));
}
@Test
@DisplayName("should fail when actor lacks PRODUCTION_ORDER_WRITE permission")
void should_Fail_When_Unauthorized() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(false);
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.Unauthorized.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when production 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 = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ProductionOrderNotFound.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when batch not found")
void should_Fail_When_BatchNotFound() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.empty()));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when batch is not PLANNED")
void should_Fail_When_BatchNotPlanned() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(inProductionBatch())));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when batch recipe does not match order recipe")
void should_Fail_When_RecipeMismatch() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(plannedBatchWithDifferentRecipe())));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
assertThat(result.unsafeGetError().message()).contains("does not match order recipe");
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order is not RELEASED")
void should_Fail_When_OrderNotReleased() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(plannedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(plannedBatch())));
var result = startProductionOrder.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 repository returns error")
void should_Fail_When_OrderRepositoryError() {
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 = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when batch repository findById returns error")
void should_Fail_When_BatchRepositoryError() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost")));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when order save fails")
void should_Fail_When_OrderSaveFails() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(plannedBatch())));
when(productionOrderRepository.save(any()))
.thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
}
@Test
@DisplayName("should fail when batch save fails")
void should_Fail_When_BatchSaveFails() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(plannedBatch())));
when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
when(batchRepository.save(any()))
.thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
}
@Test
@DisplayName("should fail when batch is COMPLETED")
void should_Fail_When_BatchCompleted() {
var completedBatch = Batch.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-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()
);
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(completedBatch)));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any());
}
@Test
@DisplayName("should fail when batch is CANCELLED")
void should_Fail_When_BatchCancelled() {
var cancelledBatch = Batch.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-001"),
RecipeId.of("recipe-1"),
BatchStatus.CANCELLED,
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,
"Storniert", OffsetDateTime.now(ZoneOffset.UTC),
1L,
List.of()
);
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(cancelledBatch)));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any());
}
}

View file

@ -305,6 +305,7 @@ class ProductionOrderTest {
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-123"),
status,
null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
FUTURE_DATE,
Priority.NORMAL,
@ -379,6 +380,129 @@ class ProductionOrderTest {
}
}
@Nested
@DisplayName("startProduction()")
class StartProduction {
private ProductionOrder orderWithStatus(ProductionOrderStatus status) {
return ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-123"),
status,
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
);
}
private ProductionOrder releasedOrder() {
return orderWithStatus(ProductionOrderStatus.RELEASED);
}
@Test
@DisplayName("should start production for RELEASED order")
void should_StartProduction_When_Released() {
var order = releasedOrder();
var beforeUpdate = order.updatedAt();
var batchId = BatchId.of("batch-1");
var result = order.startProduction(batchId);
assertThat(result.isSuccess()).isTrue();
assertThat(order.status()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
assertThat(order.batchId()).isEqualTo(batchId);
assertThat(order.updatedAt()).isAfter(beforeUpdate);
}
@Test
@DisplayName("should fail when order is PLANNED")
void should_Fail_When_Planned() {
var order = orderWithStatus(ProductionOrderStatus.PLANNED);
var result = order.startProduction(BatchId.of("batch-1"));
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.PLANNED);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
}
@Test
@DisplayName("should fail when order is IN_PROGRESS")
void should_Fail_When_InProgress() {
var order = orderWithStatus(ProductionOrderStatus.IN_PROGRESS);
var result = order.startProduction(BatchId.of("batch-1"));
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
assertThat(err.target()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
}
@Test
@DisplayName("should fail when order is COMPLETED")
void should_Fail_When_Completed() {
var order = orderWithStatus(ProductionOrderStatus.COMPLETED);
var result = order.startProduction(BatchId.of("batch-1"));
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.COMPLETED);
}
@Test
@DisplayName("should fail when order is CANCELLED")
void should_Fail_When_Cancelled() {
var order = orderWithStatus(ProductionOrderStatus.CANCELLED);
var result = order.startProduction(BatchId.of("batch-1"));
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
assertThat(err.current()).isEqualTo(ProductionOrderStatus.CANCELLED);
}
@Test
@DisplayName("should fail when batchId already assigned")
void should_Fail_When_BatchAlreadyAssigned() {
var order = ProductionOrder.reconstitute(
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-123"),
ProductionOrderStatus.RELEASED,
BatchId.of("existing-batch"),
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
FUTURE_DATE,
Priority.NORMAL,
null,
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
1L
);
var result = order.startProduction(BatchId.of("new-batch"));
assertThat(result.isFailure()).isTrue();
var err = (ProductionOrderError.BatchAlreadyAssigned) result.unsafeGetError();
assertThat(err.batchId().value()).isEqualTo("existing-batch");
}
@Test
@DisplayName("batchId should be null after create")
void should_HaveNullBatchId_After_Create() {
var result = ProductionOrder.create(validDraft());
assertThat(result.isSuccess()).isTrue();
assertThat(result.unsafeGetValue().batchId()).isNull();
}
}
@Nested
@DisplayName("reconstitute()")
class Reconstitute {
@ -390,6 +514,7 @@ class ProductionOrderTest {
ProductionOrderId.of("order-1"),
RecipeId.of("recipe-123"),
ProductionOrderStatus.PLANNED,
null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
FUTURE_DATE,
Priority.HIGH,

View file

@ -3,6 +3,7 @@ package de.effigenix.infrastructure.production.web;
import de.effigenix.domain.usermanagement.RoleName;
import de.effigenix.infrastructure.AbstractIntegrationTest;
import de.effigenix.infrastructure.production.web.dto.CreateProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.PlanBatchRequest;
import de.effigenix.infrastructure.usermanagement.persistence.entity.RoleEntity;
import de.effigenix.infrastructure.usermanagement.persistence.entity.UserEntity;
import org.junit.jupiter.api.BeforeEach;
@ -35,7 +36,7 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
UserEntity viewer = createUser("po.viewer", "po.viewer@test.com", Set.of(viewerRole), "BRANCH-01");
adminToken = generateToken(admin.getId(), "po.admin",
"PRODUCTION_ORDER_WRITE,PRODUCTION_ORDER_READ,RECIPE_WRITE,RECIPE_READ");
"PRODUCTION_ORDER_WRITE,PRODUCTION_ORDER_READ,RECIPE_WRITE,RECIPE_READ,BATCH_WRITE,BATCH_READ");
viewerToken = generateToken(viewer.getId(), "po.viewer", "USER_READ");
}
@ -382,9 +383,203 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
}
}
@Nested
@DisplayName("POST /api/production/production-orders/{id}/start Produktion starten")
class StartProductionOrderEndpoint {
@Test
@DisplayName("RELEASED Order mit PLANNED Batch starten → 200, Status IN_PROGRESS")
void startOrder_releasedWithPlannedBatch_returns200() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe();
String orderId = orderAndRecipe[0];
String batchId = createPlannedBatch(orderAndRecipe[1]);
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(orderId))
.andExpect(jsonPath("$.status").value("IN_PROGRESS"))
.andExpect(jsonPath("$.batchId").value(batchId));
}
@Test
@DisplayName("PLANNED Order starten → 409 (InvalidStatusTransition)")
void startOrder_plannedOrder_returns409() throws Exception {
String[] orderAndRecipe = createPlannedOrderWithRecipe();
String orderId = orderAndRecipe[0];
String batchId = createPlannedBatch(orderAndRecipe[1]);
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", 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("Order nicht gefunden → 404")
void startOrder_notFound_returns404() throws Exception {
String json = """
{"batchId": "non-existent-batch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", "non-existent-id")
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_NOT_FOUND"));
}
@Test
@DisplayName("Batch nicht gefunden → 400")
void startOrder_batchNotFound_returns400() throws Exception {
String orderId = createReleasedOrder();
String json = """
{"batchId": "non-existent-batch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"));
}
@Test
@DisplayName("Ohne PRODUCTION_ORDER_WRITE → 403")
void startOrder_withViewerToken_returns403() throws Exception {
String json = """
{"batchId": "any-batch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
.header("Authorization", "Bearer " + viewerToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isForbidden());
}
@Test
@DisplayName("Ohne Token → 401")
void startOrder_withoutToken_returns401() throws Exception {
String json = """
{"batchId": "any-batch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isUnauthorized());
}
@Test
@DisplayName("Batch nicht PLANNED (bereits gestartet) → 400")
void startOrder_batchNotPlanned_returns400() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe();
String orderId = orderAndRecipe[0];
String batchId = createStartedBatch(orderAndRecipe[1]);
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"));
}
@Test
@DisplayName("Bereits gestartete Order erneut starten → 409")
void startOrder_alreadyStarted_returns409() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe();
String orderId = orderAndRecipe[0];
String recipeId = orderAndRecipe[1];
String batchId1 = createPlannedBatch(recipeId);
String batchId2 = createPlannedBatch(recipeId);
String json1 = """
{"batchId": "%s"}
""".formatted(batchId1);
// First start
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json1))
.andExpect(status().isOk());
// Second start
String json2 = """
{"batchId": "%s"}
""".formatted(batchId2);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json2))
.andExpect(status().isConflict())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
}
@Test
@DisplayName("Batch mit anderem Rezept → 400 (RecipeMismatch)")
void startOrder_recipeMismatch_returns400() throws Exception {
String orderId = createReleasedOrder();
String batchId = createPlannedBatch(); // creates batch with different recipe
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"))
.andExpect(jsonPath("$.message").value(org.hamcrest.Matchers.containsString("does not match order recipe")));
}
@Test
@DisplayName("batchId leer → 400 (Bean Validation)")
void startOrder_blankBatchId_returns400() throws Exception {
String json = """
{"batchId": ""}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest());
}
}
// ==================== Hilfsmethoden ====================
private String createPlannedOrder() throws Exception {
return createPlannedOrderWithRecipe()[0];
}
/** Returns [orderId, recipeId] */
private String[] createPlannedOrderWithRecipe() throws Exception {
String recipeId = createActiveRecipe();
var request = new CreateProductionOrderRequest(
recipeId, "100", "KILOGRAM", PLANNED_DATE, "NORMAL", null);
@ -396,7 +591,8 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
.andExpect(status().isCreated())
.andReturn();
return objectMapper.readTree(result.getResponse().getContentAsString()).get("id").asText();
String orderId = objectMapper.readTree(result.getResponse().getContentAsString()).get("id").asText();
return new String[]{orderId, recipeId};
}
private String createOrderWithArchivedRecipe() throws Exception {
@ -444,6 +640,49 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
return recipeId;
}
private String createReleasedOrder() throws Exception {
return createReleasedOrderWithRecipe()[0];
}
/** Returns [orderId, recipeId] */
private String[] createReleasedOrderWithRecipe() throws Exception {
String[] orderAndRecipe = createPlannedOrderWithRecipe();
mockMvc.perform(post("/api/production/production-orders/{id}/release", orderAndRecipe[0])
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isOk());
return orderAndRecipe;
}
private String createPlannedBatch() throws Exception {
return createPlannedBatch(createActiveRecipe());
}
private String createPlannedBatch(String recipeId) throws Exception {
var planRequest = new PlanBatchRequest(
recipeId, "100", "KILOGRAM", PLANNED_DATE, PLANNED_DATE.plusDays(14));
var planResult = mockMvc.perform(post("/api/production/batches")
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(planRequest)))
.andExpect(status().isCreated())
.andReturn();
return objectMapper.readTree(planResult.getResponse().getContentAsString()).get("id").asText();
}
private String createStartedBatch() throws Exception {
return createStartedBatch(createActiveRecipe());
}
private String createStartedBatch(String recipeId) throws Exception {
String batchId = createPlannedBatch(recipeId);
mockMvc.perform(post("/api/production/batches/{id}/start", batchId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isOk());
return batchId;
}
private String createDraftRecipe() throws Exception {
String json = """
{