Files
register-referentie/services/domain/Big.Tests/WithdrawRegistrationTests.cs
Niek Otten 2a6874ef6d test(domain): withdrawal cancels the workflow via the Beoordelen task's execution (refs #12)
The WithdrawRegistration handler delivers the withdrawal message to the open Beoordelen task's

execution; the Workflow Client PUTs messageEventReceived (RegistratieIngetrokken). BeoordelingTask

now carries its executionId.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-16 11:29:56 +02:00

96 lines
3.7 KiB
C#

using Big.Application;
using Big.Domain;
namespace Big.Tests;
public class WithdrawRegistrationTests
{
private static FakeUserTaskClient NoTasks() => new([]);
[Fact]
public async Task Withdrawing_marks_the_registration_ingetrokken_and_persists_it()
{
var store = new FakeRegistrationStore();
var registration = Registration.Submit("123456782");
store.Seed(registration);
var handler = new WithdrawRegistration(store, NoTasks());
await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id));
var saved = await store.GetAsync(registration.Id);
Assert.Equal(RegistrationStatus.Ingetrokken, saved!.Status);
Assert.Equal(1, store.SaveCount);
}
[Fact]
public async Task Withdrawing_cancels_the_open_beoordelen_task_via_its_execution()
{
var store = new FakeRegistrationStore();
var registration = Registration.Submit("123456782");
store.Seed(registration);
var tasks = new FakeUserTaskClient([new BeoordelingTask("task-1", "exec-1", registration.Id)]);
var handler = new WithdrawRegistration(store, tasks);
await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id));
// The withdrawal message is delivered to the Beoordelen task's execution, tripping the
// boundary event that ends the process (ADR-0014).
Assert.Equal("exec-1", tasks.WithdrawnExecutionId);
}
[Fact]
public async Task Withdrawing_with_no_open_task_still_marks_ingetrokken()
{
// If the process has not parked at Beoordelen yet (or already ended), the withdrawal stands:
// the aggregate is INGETROKKEN and nothing is cancelled.
var store = new FakeRegistrationStore();
var registration = Registration.Submit("123456782");
store.Seed(registration);
var tasks = NoTasks();
var handler = new WithdrawRegistration(store, tasks);
await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id));
Assert.Equal(RegistrationStatus.Ingetrokken, (await store.GetAsync(registration.Id))!.Status);
Assert.Null(tasks.WithdrawnExecutionId);
}
[Fact]
public async Task Rejects_a_null_command_without_touching_the_store()
{
var store = new FakeRegistrationStore();
var handler = new WithdrawRegistration(store, NoTasks());
await Assert.ThrowsAsync<ArgumentNullException>(() => handler.HandleAsync(null!));
Assert.Equal(0, store.SaveCount);
}
[Fact]
public async Task Withdrawing_an_unknown_registration_throws()
{
var store = new FakeRegistrationStore();
var handler = new WithdrawRegistration(store, NoTasks());
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
() => handler.HandleAsync(new WithdrawRegistrationCommand(RegistrationId.New())));
Assert.Contains("No registration", ex.Message);
Assert.Equal(0, store.SaveCount);
}
[Fact]
public async Task Re_withdrawing_an_already_ingetrokken_registration_is_idempotent()
{
var store = new FakeRegistrationStore();
var registration = Registration.Submit("123456782");
store.Seed(registration);
var handler = new WithdrawRegistration(store, NoTasks());
await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id));
await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id));
// The second withdrawal is a no-op: the aggregate is not persisted again.
Assert.Equal(1, store.SaveCount);
Assert.Equal(RegistrationStatus.Ingetrokken, (await store.GetAsync(registration.Id))!.Status);
}
}