mirror of
https://github.com/s-frick/effigenix.git
synced 2026-03-28 17:49:57 +01:00
feat(production): Produktionsauftrag freigeben (US-P14, #39)
This commit is contained in:
parent
ba37ff647b
commit
b77b209f10
12 changed files with 585 additions and 4 deletions
|
|
@ -0,0 +1,242 @@
|
|||
package de.effigenix.application.production;
|
||||
|
||||
import de.effigenix.application.production.command.ReleaseProductionOrderCommand;
|
||||
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("ReleaseProductionOrder Use Case")
|
||||
class ReleaseProductionOrderTest {
|
||||
|
||||
@Mock private ProductionOrderRepository productionOrderRepository;
|
||||
@Mock private RecipeRepository recipeRepository;
|
||||
@Mock private AuthorizationPort authPort;
|
||||
|
||||
private ReleaseProductionOrder releaseProductionOrder;
|
||||
private ActorId performedBy;
|
||||
|
||||
private static final LocalDate PLANNED_DATE = LocalDate.now().plusDays(7);
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
releaseProductionOrder = new ReleaseProductionOrder(productionOrderRepository, recipeRepository, authPort);
|
||||
performedBy = ActorId.of("admin-user");
|
||||
}
|
||||
|
||||
private ReleaseProductionOrderCommand validCommand() {
|
||||
return new ReleaseProductionOrderCommand("order-1");
|
||||
}
|
||||
|
||||
private ProductionOrder plannedOrder() {
|
||||
return ProductionOrder.reconstitute(
|
||||
ProductionOrderId.of("order-1"),
|
||||
RecipeId.of("recipe-1"),
|
||||
ProductionOrderStatus.PLANNED,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
PLANNED_DATE,
|
||||
Priority.NORMAL,
|
||||
null,
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
1L
|
||||
);
|
||||
}
|
||||
|
||||
private ProductionOrder releasedOrder() {
|
||||
return ProductionOrder.reconstitute(
|
||||
ProductionOrderId.of("order-1"),
|
||||
RecipeId.of("recipe-1"),
|
||||
ProductionOrderStatus.RELEASED,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
PLANNED_DATE,
|
||||
Priority.NORMAL,
|
||||
null,
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
1L
|
||||
);
|
||||
}
|
||||
|
||||
private Recipe activeRecipe() {
|
||||
return Recipe.reconstitute(
|
||||
RecipeId.of("recipe-1"), new RecipeName("Bratwurst"), 1, RecipeType.FINISHED_PRODUCT,
|
||||
null, new YieldPercentage(85), 14,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
"article-123", RecipeStatus.ACTIVE, List.of(), List.of(),
|
||||
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC)
|
||||
);
|
||||
}
|
||||
|
||||
private Recipe archivedRecipe() {
|
||||
return Recipe.reconstitute(
|
||||
RecipeId.of("recipe-1"), new RecipeName("Bratwurst"), 1, RecipeType.FINISHED_PRODUCT,
|
||||
null, new YieldPercentage(85), 14,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
"article-123", RecipeStatus.ARCHIVED, List.of(), List.of(),
|
||||
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should release PLANNED order when recipe is ACTIVE")
|
||||
void should_Release_When_ValidCommand() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedOrder())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(activeRecipe())));
|
||||
when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
|
||||
|
||||
var result = releaseProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isSuccess()).isTrue();
|
||||
assertThat(result.unsafeGetValue().status()).isEqualTo(ProductionOrderStatus.RELEASED);
|
||||
verify(productionOrderRepository).save(any(ProductionOrder.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 = releaseProductionOrder.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 = releaseProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ProductionOrderNotFound.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when recipe is not ACTIVE (ARCHIVED)")
|
||||
void should_Fail_When_RecipeNotActive() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedOrder())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(archivedRecipe())));
|
||||
|
||||
var result = releaseProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RecipeNotActive.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when recipe not found")
|
||||
void should_Fail_When_RecipeNotFound() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedOrder())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.empty()));
|
||||
|
||||
var result = releaseProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RecipeNotFound.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when order is already RELEASED (invalid status transition)")
|
||||
void should_Fail_When_AlreadyReleased() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(releasedOrder())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(activeRecipe())));
|
||||
|
||||
var result = releaseProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.InvalidStatusTransition.class);
|
||||
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
|
||||
assertThat(err.current()).isEqualTo(ProductionOrderStatus.RELEASED);
|
||||
assertThat(err.target()).isEqualTo(ProductionOrderStatus.RELEASED);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when order repository findById 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 = releaseProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when recipe repository returns error")
|
||||
void should_Fail_When_RecipeRepositoryError() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedOrder())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost")));
|
||||
|
||||
var result = releaseProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
|
||||
verify(productionOrderRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when save fails")
|
||||
void should_Fail_When_SaveFails() {
|
||||
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
|
||||
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
|
||||
.thenReturn(Result.success(Optional.of(plannedOrder())));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1")))
|
||||
.thenReturn(Result.success(Optional.of(activeRecipe())));
|
||||
when(productionOrderRepository.save(any()))
|
||||
.thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
|
||||
|
||||
var result = releaseProductionOrder.execute(validCommand(), performedBy);
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -296,6 +296,89 @@ class ProductionOrderTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("release()")
|
||||
class Release {
|
||||
|
||||
private ProductionOrder orderWithStatus(ProductionOrderStatus status) {
|
||||
return ProductionOrder.reconstitute(
|
||||
ProductionOrderId.of("order-1"),
|
||||
RecipeId.of("recipe-123"),
|
||||
status,
|
||||
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 release PLANNED order")
|
||||
void should_Release_When_Planned() {
|
||||
var order = orderWithStatus(ProductionOrderStatus.PLANNED);
|
||||
var beforeUpdate = order.updatedAt();
|
||||
|
||||
var result = order.release();
|
||||
|
||||
assertThat(result.isSuccess()).isTrue();
|
||||
assertThat(order.status()).isEqualTo(ProductionOrderStatus.RELEASED);
|
||||
assertThat(order.updatedAt()).isAfter(beforeUpdate);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when releasing RELEASED order")
|
||||
void should_Fail_When_AlreadyReleased() {
|
||||
var order = orderWithStatus(ProductionOrderStatus.RELEASED);
|
||||
|
||||
var result = order.release();
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
|
||||
assertThat(err.current()).isEqualTo(ProductionOrderStatus.RELEASED);
|
||||
assertThat(err.target()).isEqualTo(ProductionOrderStatus.RELEASED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when releasing IN_PROGRESS order")
|
||||
void should_Fail_When_InProgress() {
|
||||
var order = orderWithStatus(ProductionOrderStatus.IN_PROGRESS);
|
||||
|
||||
var result = order.release();
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
|
||||
assertThat(err.current()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
|
||||
assertThat(err.target()).isEqualTo(ProductionOrderStatus.RELEASED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when releasing COMPLETED order")
|
||||
void should_Fail_When_Completed() {
|
||||
var order = orderWithStatus(ProductionOrderStatus.COMPLETED);
|
||||
|
||||
var result = order.release();
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
|
||||
assertThat(err.current()).isEqualTo(ProductionOrderStatus.COMPLETED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when releasing CANCELLED order")
|
||||
void should_Fail_When_Cancelled() {
|
||||
var order = orderWithStatus(ProductionOrderStatus.CANCELLED);
|
||||
|
||||
var result = order.release();
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
var err = (ProductionOrderError.InvalidStatusTransition) result.unsafeGetError();
|
||||
assertThat(err.current()).isEqualTo(ProductionOrderStatus.CANCELLED);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("reconstitute()")
|
||||
class Reconstitute {
|
||||
|
|
|
|||
|
|
@ -313,8 +313,115 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("POST /api/production/production-orders/{id}/release – Produktionsauftrag freigeben")
|
||||
class ReleaseProductionOrderEndpoint {
|
||||
|
||||
@Test
|
||||
@DisplayName("PLANNED Order freigeben → 200, Status RELEASED")
|
||||
void releaseOrder_planned_returns200() throws Exception {
|
||||
String orderId = createPlannedOrder();
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/release", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.id").value(orderId))
|
||||
.andExpect(jsonPath("$.status").value("RELEASED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Bereits RELEASED Order erneut freigeben → 409")
|
||||
void releaseOrder_alreadyReleased_returns409() throws Exception {
|
||||
String orderId = createPlannedOrder();
|
||||
|
||||
// First release
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/release", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
// Second release
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/release", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isConflict())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Order nicht gefunden → 404")
|
||||
void releaseOrder_notFound_returns404() throws Exception {
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/release", "non-existent-id")
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isNotFound())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_NOT_FOUND"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Ohne PRODUCTION_ORDER_WRITE → 403")
|
||||
void releaseOrder_withViewerToken_returns403() throws Exception {
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/release", "any-id")
|
||||
.header("Authorization", "Bearer " + viewerToken))
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Ohne Token → 401")
|
||||
void releaseOrder_withoutToken_returns401() throws Exception {
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/release", "any-id"))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Order mit archiviertem Rezept freigeben → 409")
|
||||
void releaseOrder_archivedRecipe_returns409() throws Exception {
|
||||
String orderId = createOrderWithArchivedRecipe();
|
||||
|
||||
mockMvc.perform(post("/api/production/production-orders/{id}/release", orderId)
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isConflict())
|
||||
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_RECIPE_NOT_ACTIVE"));
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Hilfsmethoden ====================
|
||||
|
||||
private String createPlannedOrder() throws Exception {
|
||||
String recipeId = createActiveRecipe();
|
||||
var request = new CreateProductionOrderRequest(
|
||||
recipeId, "100", "KILOGRAM", PLANNED_DATE, "NORMAL", null);
|
||||
|
||||
var result = mockMvc.perform(post("/api/production/production-orders")
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(request)))
|
||||
.andExpect(status().isCreated())
|
||||
.andReturn();
|
||||
|
||||
return objectMapper.readTree(result.getResponse().getContentAsString()).get("id").asText();
|
||||
}
|
||||
|
||||
private String createOrderWithArchivedRecipe() throws Exception {
|
||||
// Create active recipe, create order, then archive the recipe
|
||||
String recipeId = createActiveRecipe();
|
||||
var request = new CreateProductionOrderRequest(
|
||||
recipeId, "100", "KILOGRAM", PLANNED_DATE, "NORMAL", null);
|
||||
|
||||
var result = mockMvc.perform(post("/api/production/production-orders")
|
||||
.header("Authorization", "Bearer " + adminToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(request)))
|
||||
.andExpect(status().isCreated())
|
||||
.andReturn();
|
||||
|
||||
String orderId = objectMapper.readTree(result.getResponse().getContentAsString()).get("id").asText();
|
||||
|
||||
// Archive the recipe
|
||||
mockMvc.perform(post("/api/recipes/{id}/archive", recipeId)
|
||||
.header("Authorization", "Bearer " + adminToken))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
return orderId;
|
||||
}
|
||||
|
||||
private String createActiveRecipe() throws Exception {
|
||||
String recipeId = createDraftRecipe();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue