mirror of
https://github.com/s-frick/effigenix.git
synced 2026-03-28 14:09:34 +01:00
feat(production,inventory): Produktionsergebnis automatisch einbuchen (US-7.2)
Event-Infrastruktur (DomainEvent, DomainEventPublisher) im Shared Kernel eingeführt. CompleteBatch publiziert BatchCompleted-Event mit articleId aus Recipe. ProductionDomainEventPublisher konvertiert in IntegrationEvent, BatchCompletedInventoryListener bucht automatisch StockBatch (PRODUCED) + StockMovement (PRODUCTION_OUTPUT) am PRODUCTION_AREA-Lagerort ein. TUI RecordConsumptionScreen: Rezeptbasierte Zutatenauswahl mit skalierten Soll-Mengen, Stock-Batch-Picker und Mengen-Vorbelegung.
This commit is contained in:
parent
e9f2948e61
commit
aa7ac785bb
16 changed files with 797 additions and 117 deletions
|
|
@ -0,0 +1,131 @@
|
|||
package de.effigenix.application.inventory;
|
||||
|
||||
import de.effigenix.application.inventory.command.BookProductionOutputCommand;
|
||||
import de.effigenix.domain.inventory.*;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
|
||||
public class BookProductionOutput {
|
||||
|
||||
private final StockRepository stockRepository;
|
||||
private final StorageLocationRepository storageLocationRepository;
|
||||
private final StockMovementRepository stockMovementRepository;
|
||||
private final UnitOfWork unitOfWork;
|
||||
|
||||
public BookProductionOutput(StockRepository stockRepository,
|
||||
StorageLocationRepository storageLocationRepository,
|
||||
StockMovementRepository stockMovementRepository,
|
||||
UnitOfWork unitOfWork) {
|
||||
this.stockRepository = stockRepository;
|
||||
this.storageLocationRepository = storageLocationRepository;
|
||||
this.stockMovementRepository = stockMovementRepository;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public Result<ProductionOutputError, Void> execute(BookProductionOutputCommand cmd) {
|
||||
// 1. Find active PRODUCTION_AREA storage location
|
||||
StorageLocation productionArea;
|
||||
switch (storageLocationRepository.findByStorageType(StorageType.PRODUCTION_AREA)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var locations) -> {
|
||||
var active = locations.stream().filter(StorageLocation::active).findFirst();
|
||||
if (active.isEmpty()) {
|
||||
return Result.failure(new ProductionOutputError.NoProductionArea(
|
||||
"No active PRODUCTION_AREA storage location found"));
|
||||
}
|
||||
productionArea = active.get();
|
||||
}
|
||||
}
|
||||
|
||||
var articleId = de.effigenix.domain.masterdata.article.ArticleId.of(cmd.articleId());
|
||||
var storageLocationId = productionArea.id();
|
||||
|
||||
return unitOfWork.executeAtomically(() -> {
|
||||
// 2. Find or create Stock for (articleId, storageLocationId)
|
||||
Stock stock;
|
||||
switch (stockRepository.findByArticleIdAndStorageLocationId(articleId, storageLocationId)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var opt) -> {
|
||||
if (opt.isPresent()) {
|
||||
stock = opt.get();
|
||||
} else {
|
||||
// Auto-create stock with no minimumLevel/minimumShelfLife
|
||||
var stockDraft = new StockDraft(
|
||||
cmd.articleId(), storageLocationId.value(), null, null, null);
|
||||
switch (Stock.create(stockDraft)) {
|
||||
case Result.Failure(var stockErr) -> {
|
||||
return Result.failure(new ProductionOutputError.StockCreationFailed(
|
||||
stockErr.message()));
|
||||
}
|
||||
case Result.Success(var newStock) -> stock = newStock;
|
||||
}
|
||||
switch (stockRepository.save(stock)) {
|
||||
case Result.Failure(var saveErr) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(
|
||||
saveErr.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Add batch to stock
|
||||
StockBatch stockBatch;
|
||||
var batchDraft = new StockBatchDraft(
|
||||
cmd.batchNumber(), "PRODUCED",
|
||||
cmd.quantityAmount(), cmd.quantityUnit(),
|
||||
cmd.bestBeforeDate());
|
||||
switch (stock.addBatch(batchDraft)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.BatchAdditionFailed(err.message()));
|
||||
}
|
||||
case Result.Success(var batch) -> stockBatch = batch;
|
||||
}
|
||||
|
||||
// 4. Save stock with new batch
|
||||
switch (stockRepository.save(stock)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
// 5. Record stock movement
|
||||
var movementDraft = new StockMovementDraft(
|
||||
stock.id().value(),
|
||||
cmd.articleId(),
|
||||
stockBatch.id().value(),
|
||||
cmd.batchNumber(),
|
||||
"PRODUCED",
|
||||
"PRODUCTION_OUTPUT",
|
||||
null,
|
||||
cmd.quantityAmount(),
|
||||
cmd.quantityUnit(),
|
||||
null,
|
||||
null,
|
||||
"SYSTEM"
|
||||
);
|
||||
StockMovement movement;
|
||||
switch (StockMovement.record(movementDraft)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.MovementRecordingFailed(err.message()));
|
||||
}
|
||||
case Result.Success(var mv) -> movement = mv;
|
||||
}
|
||||
|
||||
switch (stockMovementRepository.save(movement)) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new ProductionOutputError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
return Result.success(null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package de.effigenix.application.inventory.command;
|
||||
|
||||
public record BookProductionOutputCommand(
|
||||
String batchNumber,
|
||||
String articleId,
|
||||
String quantityAmount,
|
||||
String quantityUnit,
|
||||
String bestBeforeDate
|
||||
) {}
|
||||
|
|
@ -2,20 +2,30 @@ package de.effigenix.application.production;
|
|||
|
||||
import de.effigenix.application.production.command.CompleteBatchCommand;
|
||||
import de.effigenix.domain.production.*;
|
||||
import de.effigenix.domain.production.event.BatchCompleted;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import de.effigenix.shared.event.DomainEventPublisher;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
import de.effigenix.shared.security.ActorId;
|
||||
import de.effigenix.shared.security.AuthorizationPort;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public class CompleteBatch {
|
||||
|
||||
private final BatchRepository batchRepository;
|
||||
private final RecipeRepository recipeRepository;
|
||||
private final AuthorizationPort authorizationPort;
|
||||
private final DomainEventPublisher domainEventPublisher;
|
||||
private final UnitOfWork unitOfWork;
|
||||
|
||||
public CompleteBatch(BatchRepository batchRepository, AuthorizationPort authorizationPort, UnitOfWork unitOfWork) {
|
||||
public CompleteBatch(BatchRepository batchRepository, RecipeRepository recipeRepository,
|
||||
AuthorizationPort authorizationPort, DomainEventPublisher domainEventPublisher,
|
||||
UnitOfWork unitOfWork) {
|
||||
this.batchRepository = batchRepository;
|
||||
this.recipeRepository = recipeRepository;
|
||||
this.authorizationPort = authorizationPort;
|
||||
this.domainEventPublisher = domainEventPublisher;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
|
|
@ -59,6 +69,34 @@ public class CompleteBatch {
|
|||
}
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
// Load recipe to get articleId for the event
|
||||
Recipe recipe;
|
||||
switch (recipeRepository.findById(batch.recipeId())) {
|
||||
case Result.Failure(var err) -> {
|
||||
return Result.failure(new BatchError.RepositoryFailure(err.message()));
|
||||
}
|
||||
case Result.Success(var opt) -> {
|
||||
if (opt.isEmpty()) {
|
||||
return Result.failure(new BatchError.RepositoryFailure(
|
||||
"Recipe not found: " + batch.recipeId().value()));
|
||||
}
|
||||
recipe = opt.get();
|
||||
}
|
||||
}
|
||||
|
||||
domainEventPublisher.publish(new BatchCompleted(
|
||||
batch.id(),
|
||||
batch.batchNumber(),
|
||||
batch.recipeId(),
|
||||
recipe.articleId(),
|
||||
batch.actualQuantity(),
|
||||
batch.waste(),
|
||||
batch.bestBeforeDate(),
|
||||
batch.completedAt(),
|
||||
Instant.now()
|
||||
));
|
||||
|
||||
return Result.success(batch);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
package de.effigenix.domain.inventory;
|
||||
|
||||
public sealed interface ProductionOutputError {
|
||||
|
||||
String message();
|
||||
|
||||
record NoProductionArea(String message) implements ProductionOutputError {}
|
||||
record StockCreationFailed(String message) implements ProductionOutputError {}
|
||||
record BatchAdditionFailed(String message) implements ProductionOutputError {}
|
||||
record MovementRecordingFailed(String message) implements ProductionOutputError {}
|
||||
record RepositoryFailure(String message) implements ProductionOutputError {}
|
||||
}
|
||||
|
|
@ -1,17 +1,23 @@
|
|||
package de.effigenix.domain.production.event;
|
||||
|
||||
import de.effigenix.domain.production.BatchId;
|
||||
import de.effigenix.domain.production.BatchNumber;
|
||||
import de.effigenix.domain.production.RecipeId;
|
||||
import de.effigenix.shared.common.Quantity;
|
||||
import de.effigenix.shared.event.DomainEvent;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
/**
|
||||
* Stub – wird derzeit nicht publiziert.
|
||||
* Vorgesehen für spätere Event-Infrastruktur (Inventory stock-in, Audit, Tracing).
|
||||
*/
|
||||
public record BatchCompleted(
|
||||
BatchId batchId,
|
||||
BatchNumber batchNumber,
|
||||
RecipeId recipeId,
|
||||
String articleId,
|
||||
Quantity actualQuantity,
|
||||
Quantity waste,
|
||||
OffsetDateTime completedAt
|
||||
) {}
|
||||
LocalDate bestBeforeDate,
|
||||
OffsetDateTime completedAt,
|
||||
Instant occurredAt
|
||||
) implements DomainEvent {}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import de.effigenix.application.inventory.RecordCountItem;
|
|||
import de.effigenix.application.inventory.StartInventoryCount;
|
||||
import de.effigenix.application.inventory.GetStockMovement;
|
||||
import de.effigenix.application.inventory.ListStockMovements;
|
||||
import de.effigenix.application.inventory.BookProductionOutput;
|
||||
import de.effigenix.application.inventory.ConfirmReservation;
|
||||
import de.effigenix.application.inventory.RecordStockMovement;
|
||||
import de.effigenix.application.inventory.ActivateStorageLocation;
|
||||
|
|
@ -144,6 +145,14 @@ public class InventoryUseCaseConfiguration {
|
|||
return new ListStocksBelowMinimum(stockRepository, authorizationPort);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BookProductionOutput bookProductionOutput(StockRepository stockRepository,
|
||||
StorageLocationRepository storageLocationRepository,
|
||||
StockMovementRepository stockMovementRepository,
|
||||
UnitOfWork unitOfWork) {
|
||||
return new BookProductionOutput(stockRepository, storageLocationRepository, stockMovementRepository, unitOfWork);
|
||||
}
|
||||
|
||||
// ==================== StockMovement Use Cases ====================
|
||||
|
||||
@Bean
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import de.effigenix.domain.production.BatchRepository;
|
|||
import de.effigenix.domain.production.BatchTraceabilityService;
|
||||
import de.effigenix.domain.production.ProductionOrderRepository;
|
||||
import de.effigenix.domain.production.RecipeRepository;
|
||||
import de.effigenix.shared.event.DomainEventPublisher;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
import de.effigenix.shared.security.AuthorizationPort;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
|
@ -134,9 +135,10 @@ public class ProductionUseCaseConfiguration {
|
|||
}
|
||||
|
||||
@Bean
|
||||
public CompleteBatch completeBatch(BatchRepository batchRepository, AuthorizationPort authorizationPort,
|
||||
public CompleteBatch completeBatch(BatchRepository batchRepository, RecipeRepository recipeRepository,
|
||||
AuthorizationPort authorizationPort, DomainEventPublisher domainEventPublisher,
|
||||
UnitOfWork unitOfWork) {
|
||||
return new CompleteBatch(batchRepository, authorizationPort, unitOfWork);
|
||||
return new CompleteBatch(batchRepository, recipeRepository, authorizationPort, domainEventPublisher, unitOfWork);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
package de.effigenix.infrastructure.integration.event;
|
||||
|
||||
public record BatchCompletedIntegrationEvent(
|
||||
String batchId,
|
||||
String batchNumber,
|
||||
String articleId,
|
||||
String quantityAmount,
|
||||
String quantityUnit,
|
||||
String bestBeforeDate
|
||||
) {}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package de.effigenix.infrastructure.inventory.event;
|
||||
|
||||
import de.effigenix.application.inventory.BookProductionOutput;
|
||||
import de.effigenix.application.inventory.command.BookProductionOutputCommand;
|
||||
import de.effigenix.infrastructure.integration.event.BatchCompletedIntegrationEvent;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BatchCompletedInventoryListener {
|
||||
|
||||
private static final System.Logger logger = System.getLogger(BatchCompletedInventoryListener.class.getName());
|
||||
|
||||
private final BookProductionOutput bookProductionOutput;
|
||||
|
||||
public BatchCompletedInventoryListener(BookProductionOutput bookProductionOutput) {
|
||||
this.bookProductionOutput = bookProductionOutput;
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void on(BatchCompletedIntegrationEvent event) {
|
||||
var cmd = new BookProductionOutputCommand(
|
||||
event.batchNumber(),
|
||||
event.articleId(),
|
||||
event.quantityAmount(),
|
||||
event.quantityUnit(),
|
||||
event.bestBeforeDate()
|
||||
);
|
||||
switch (bookProductionOutput.execute(cmd)) {
|
||||
case Result.Failure(var err) ->
|
||||
logger.log(System.Logger.Level.WARNING,
|
||||
"Failed to book production output for batch {0}: {1}",
|
||||
event.batchNumber(), err.message());
|
||||
case Result.Success(var ignored) ->
|
||||
logger.log(System.Logger.Level.INFO,
|
||||
"Booked production output for batch {0}", event.batchNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package de.effigenix.infrastructure.production.event;
|
||||
|
||||
import de.effigenix.domain.production.event.BatchCompleted;
|
||||
import de.effigenix.infrastructure.integration.event.BatchCompletedIntegrationEvent;
|
||||
import de.effigenix.shared.event.DomainEvent;
|
||||
import de.effigenix.shared.event.DomainEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ProductionDomainEventPublisher implements DomainEventPublisher {
|
||||
|
||||
private final ApplicationEventPublisher publisher;
|
||||
|
||||
public ProductionDomainEventPublisher(ApplicationEventPublisher publisher) {
|
||||
this.publisher = publisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void publish(DomainEvent event) {
|
||||
switch (event) {
|
||||
case BatchCompleted bc -> publisher.publishEvent(
|
||||
new BatchCompletedIntegrationEvent(
|
||||
bc.batchId().value(),
|
||||
bc.batchNumber().value(),
|
||||
bc.articleId(),
|
||||
bc.actualQuantity().amount().toPlainString(),
|
||||
bc.actualQuantity().uom().name(),
|
||||
bc.bestBeforeDate().toString()
|
||||
)
|
||||
);
|
||||
default -> { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package de.effigenix.shared.event;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public interface DomainEvent {
|
||||
Instant occurredAt();
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package de.effigenix.shared.event;
|
||||
|
||||
public interface DomainEventPublisher {
|
||||
void publish(DomainEvent event);
|
||||
}
|
||||
|
|
@ -8,6 +8,8 @@ package de.effigenix.shared.security;
|
|||
*/
|
||||
public record ActorId(String value) {
|
||||
|
||||
public static final ActorId SYSTEM = new ActorId("SYSTEM");
|
||||
|
||||
public ActorId {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new IllegalArgumentException("ActorId cannot be null or empty");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,165 @@
|
|||
package de.effigenix.application.inventory;
|
||||
|
||||
import de.effigenix.application.inventory.command.BookProductionOutputCommand;
|
||||
import de.effigenix.domain.inventory.*;
|
||||
import de.effigenix.domain.masterdata.article.ArticleId;
|
||||
import de.effigenix.shared.common.RepositoryError;
|
||||
import de.effigenix.shared.common.Result;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
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.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@DisplayName("BookProductionOutput Use Case")
|
||||
class BookProductionOutputTest {
|
||||
|
||||
@Mock private StockRepository stockRepository;
|
||||
@Mock private StorageLocationRepository storageLocationRepository;
|
||||
@Mock private StockMovementRepository stockMovementRepository;
|
||||
@Mock private UnitOfWork unitOfWork;
|
||||
|
||||
private BookProductionOutput bookProductionOutput;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
bookProductionOutput = new BookProductionOutput(stockRepository, storageLocationRepository,
|
||||
stockMovementRepository, unitOfWork);
|
||||
lenient().when(unitOfWork.executeAtomically(any())).thenAnswer(inv -> ((Supplier<?>) inv.getArgument(0)).get());
|
||||
}
|
||||
|
||||
private StorageLocation activeProductionArea() {
|
||||
return StorageLocation.reconstitute(
|
||||
StorageLocationId.of("loc-prod"),
|
||||
StorageLocationName.of("Produktion").unsafeGetValue(),
|
||||
StorageType.PRODUCTION_AREA,
|
||||
null,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
private StorageLocation inactiveProductionArea() {
|
||||
return StorageLocation.reconstitute(
|
||||
StorageLocationId.of("loc-prod-inactive"),
|
||||
StorageLocationName.of("Produktion Alt").unsafeGetValue(),
|
||||
StorageType.PRODUCTION_AREA,
|
||||
null,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private Stock existingStock(String articleId, String locationId) {
|
||||
return Stock.reconstitute(
|
||||
StockId.of("stock-1"),
|
||||
ArticleId.of(articleId),
|
||||
StorageLocationId.of(locationId),
|
||||
null, null,
|
||||
List.of(), List.of()
|
||||
);
|
||||
}
|
||||
|
||||
private BookProductionOutputCommand validCommand() {
|
||||
return new BookProductionOutputCommand(
|
||||
"P-2026-03-01-001", "article-42", "95", "KILOGRAM", "2026-06-01");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should book production output when stock exists")
|
||||
void should_BookOutput_When_StockExists() {
|
||||
var location = activeProductionArea();
|
||||
var stock = existingStock("article-42", "loc-prod");
|
||||
when(storageLocationRepository.findByStorageType(StorageType.PRODUCTION_AREA))
|
||||
.thenReturn(Result.success(List.of(location)));
|
||||
when(stockRepository.findByArticleIdAndStorageLocationId(ArticleId.of("article-42"), StorageLocationId.of("loc-prod")))
|
||||
.thenReturn(Result.success(Optional.of(stock)));
|
||||
when(stockRepository.save(any())).thenReturn(Result.success(null));
|
||||
when(stockMovementRepository.save(any())).thenReturn(Result.success(null));
|
||||
|
||||
var result = bookProductionOutput.execute(validCommand());
|
||||
|
||||
assertThat(result.isSuccess()).isTrue();
|
||||
verify(stockRepository, times(1)).save(stock);
|
||||
verify(stockMovementRepository).save(any(StockMovement.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should auto-create stock when none exists")
|
||||
void should_CreateStock_When_NoneExists() {
|
||||
var location = activeProductionArea();
|
||||
when(storageLocationRepository.findByStorageType(StorageType.PRODUCTION_AREA))
|
||||
.thenReturn(Result.success(List.of(location)));
|
||||
when(stockRepository.findByArticleIdAndStorageLocationId(any(), any()))
|
||||
.thenReturn(Result.success(Optional.empty()));
|
||||
when(stockRepository.save(any())).thenReturn(Result.success(null));
|
||||
when(stockMovementRepository.save(any())).thenReturn(Result.success(null));
|
||||
|
||||
var result = bookProductionOutput.execute(validCommand());
|
||||
|
||||
assertThat(result.isSuccess()).isTrue();
|
||||
// save called twice: once for new stock creation, once after addBatch
|
||||
verify(stockRepository, times(2)).save(any(Stock.class));
|
||||
verify(stockMovementRepository).save(any(StockMovement.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when no active PRODUCTION_AREA exists")
|
||||
void should_Fail_When_NoProductionArea() {
|
||||
when(storageLocationRepository.findByStorageType(StorageType.PRODUCTION_AREA))
|
||||
.thenReturn(Result.success(List.of()));
|
||||
|
||||
var result = bookProductionOutput.execute(validCommand());
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOutputError.NoProductionArea.class);
|
||||
verify(stockRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when only inactive PRODUCTION_AREA exists")
|
||||
void should_Fail_When_OnlyInactiveProductionArea() {
|
||||
when(storageLocationRepository.findByStorageType(StorageType.PRODUCTION_AREA))
|
||||
.thenReturn(Result.success(List.of(inactiveProductionArea())));
|
||||
|
||||
var result = bookProductionOutput.execute(validCommand());
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOutputError.NoProductionArea.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail with RepositoryFailure when storage location lookup fails")
|
||||
void should_Fail_When_StorageLocationLookupFails() {
|
||||
when(storageLocationRepository.findByStorageType(StorageType.PRODUCTION_AREA))
|
||||
.thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost")));
|
||||
|
||||
var result = bookProductionOutput.execute(validCommand());
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOutputError.RepositoryFailure.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail with RepositoryFailure when stock lookup fails")
|
||||
void should_Fail_When_StockLookupFails() {
|
||||
var location = activeProductionArea();
|
||||
when(storageLocationRepository.findByStorageType(StorageType.PRODUCTION_AREA))
|
||||
.thenReturn(Result.success(List.of(location)));
|
||||
when(stockRepository.findByArticleIdAndStorageLocationId(any(), any()))
|
||||
.thenReturn(Result.failure(new RepositoryError.DatabaseError("read error")));
|
||||
|
||||
var result = bookProductionOutput.execute(validCommand());
|
||||
|
||||
assertThat(result.isFailure()).isTrue();
|
||||
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOutputError.RepositoryFailure.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,8 @@ 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.event.DomainEvent;
|
||||
import de.effigenix.shared.event.DomainEventPublisher;
|
||||
import de.effigenix.shared.security.ActorId;
|
||||
import de.effigenix.shared.security.AuthorizationPort;
|
||||
import de.effigenix.shared.persistence.UnitOfWork;
|
||||
|
|
@ -13,6 +15,7 @@ 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.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
|
|
@ -32,7 +35,9 @@ import static org.mockito.Mockito.*;
|
|||
class CompleteBatchTest {
|
||||
|
||||
@Mock private BatchRepository batchRepository;
|
||||
@Mock private RecipeRepository recipeRepository;
|
||||
@Mock private AuthorizationPort authPort;
|
||||
@Mock private DomainEventPublisher domainEventPublisher;
|
||||
@Mock private UnitOfWork unitOfWork;
|
||||
|
||||
private CompleteBatch completeBatch;
|
||||
|
|
@ -40,11 +45,30 @@ class CompleteBatchTest {
|
|||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
completeBatch = new CompleteBatch(batchRepository, authPort, unitOfWork);
|
||||
completeBatch = new CompleteBatch(batchRepository, recipeRepository, authPort, domainEventPublisher, unitOfWork);
|
||||
performedBy = ActorId.of("admin-user");
|
||||
lenient().when(unitOfWork.executeAtomically(any())).thenAnswer(inv -> ((Supplier<?>) inv.getArgument(0)).get());
|
||||
}
|
||||
|
||||
private Recipe activeRecipe(String recipeId) {
|
||||
return Recipe.reconstitute(
|
||||
RecipeId.of(recipeId),
|
||||
RecipeName.of("Test Recipe").unsafeGetValue(),
|
||||
1,
|
||||
RecipeType.FINISHED_PRODUCT,
|
||||
"desc",
|
||||
YieldPercentage.of(100).unsafeGetValue(),
|
||||
30,
|
||||
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
|
||||
"article-42",
|
||||
RecipeStatus.ACTIVE,
|
||||
List.of(),
|
||||
List.of(),
|
||||
OffsetDateTime.now(ZoneOffset.UTC),
|
||||
OffsetDateTime.now(ZoneOffset.UTC)
|
||||
);
|
||||
}
|
||||
|
||||
private Batch inProductionBatchWithConsumption(String id) {
|
||||
var batch = Batch.reconstitute(
|
||||
BatchId.of(id),
|
||||
|
|
@ -92,9 +116,11 @@ class CompleteBatchTest {
|
|||
void should_CompleteBatch_When_Valid() {
|
||||
var batchId = BatchId.of("batch-1");
|
||||
var batch = inProductionBatchWithConsumption("batch-1");
|
||||
var recipe = activeRecipe("recipe-1");
|
||||
when(authPort.can(performedBy, ProductionAction.BATCH_COMPLETE)).thenReturn(true);
|
||||
when(batchRepository.findById(batchId)).thenReturn(Result.success(Optional.of(batch)));
|
||||
when(batchRepository.save(any())).thenReturn(Result.success(null));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1"))).thenReturn(Result.success(Optional.of(recipe)));
|
||||
|
||||
var result = completeBatch.execute(validCommand("batch-1"), performedBy);
|
||||
|
||||
|
|
@ -106,6 +132,30 @@ class CompleteBatchTest {
|
|||
verify(batchRepository).save(batch);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should publish BatchCompleted event with articleId from recipe")
|
||||
void should_PublishEvent_When_BatchCompleted() {
|
||||
var batchId = BatchId.of("batch-1");
|
||||
var batch = inProductionBatchWithConsumption("batch-1");
|
||||
var recipe = activeRecipe("recipe-1");
|
||||
when(authPort.can(performedBy, ProductionAction.BATCH_COMPLETE)).thenReturn(true);
|
||||
when(batchRepository.findById(batchId)).thenReturn(Result.success(Optional.of(batch)));
|
||||
when(batchRepository.save(any())).thenReturn(Result.success(null));
|
||||
when(recipeRepository.findById(RecipeId.of("recipe-1"))).thenReturn(Result.success(Optional.of(recipe)));
|
||||
|
||||
completeBatch.execute(validCommand("batch-1"), performedBy);
|
||||
|
||||
var captor = ArgumentCaptor.forClass(DomainEvent.class);
|
||||
verify(domainEventPublisher).publish(captor.capture());
|
||||
var event = captor.getValue();
|
||||
assertThat(event).isInstanceOf(de.effigenix.domain.production.event.BatchCompleted.class);
|
||||
var batchCompleted = (de.effigenix.domain.production.event.BatchCompleted) event;
|
||||
assertThat(batchCompleted.articleId()).isEqualTo("article-42");
|
||||
assertThat(batchCompleted.batchId()).isEqualTo(batchId);
|
||||
assertThat(batchCompleted.actualQuantity().amount()).isEqualByComparingTo(new BigDecimal("95"));
|
||||
assertThat(batchCompleted.bestBeforeDate()).isEqualTo(LocalDate.of(2026, 6, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("should fail when batch not found")
|
||||
void should_Fail_When_BatchNotFound() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue