1
0
Fork 0
mirror of https://github.com/s-frick/effigenix.git synced 2026-03-28 12:09:35 +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:
Sebastian Frick 2026-03-19 14:04:36 +01:00
parent e9f2948e61
commit aa7ac785bb
16 changed files with 797 additions and 117 deletions

View file

@ -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);
}
}

View file

@ -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() {