using Big.Application; using Big.Domain; namespace Big.Tests; // S-10a (#102): the application handler behind the RegistratieVerlopen external-worker job. The 30-day // document-wait timer fired, so the correlated registration is expired to VERLOPEN. Mirrors // OpenZaakWorker — pure application logic over ports, idempotent under at-least-once delivery (§8.6). public class ExpireRegistrationWorkerTests { private const string Bsn = "123456782"; // By the time the 30-day document timer fires, the zaak was opened long ago (OpenZaakAanmaken runs // early in the flow), so a timed-out registration carries a zaak the worker can cancel. private static Registration Submitted(string processInstanceId = "proc-1", Uri? zaakUrl = null) { var registration = Registration.Submit(Bsn); registration.RecordProcessStarted(processInstanceId); registration.AttachZaak(zaakUrl ?? FakeAclClient.DefaultZaakUrl); return registration; } [Fact] public async Task Expires_the_registration_the_job_correlates_to() { var store = new FakeRegistrationStore(); var registration = Submitted(); store.Seed(registration); await new ExpireRegistrationWorker(store, new FakeAclClient()).HandleAsync( new RegistratieVerlopenJob("job-7", registration.Id)); var saved = await store.GetAsync(registration.Id); Assert.Equal(RegistrationStatus.Verlopen, saved!.Status); Assert.Equal(1, store.SaveCount); } [Fact] public async Task Cancels_the_zaak_via_the_acl_when_expiring_a_still_open_registration() { // S-10c: expiring the aggregate is not enough — the ZGW zaak must also be set to its // cancellation status, which the ACL owns (§8.1). The worker hands the ACL the zaak URL. var store = new FakeRegistrationStore(); var zaak = new Uri("http://openzaak/zaken/api/v1/zaken/timed-out"); var registration = Submitted(zaakUrl: zaak); store.Seed(registration); var acl = new FakeAclClient(); await new ExpireRegistrationWorker(store, acl).HandleAsync( new RegistratieVerlopenJob("job-7", registration.Id)); Assert.Equal(1, acl.CancelCallCount); Assert.Equal(zaak, acl.CancelledZaakUrl); } [Fact] public async Task An_already_verlopen_registration_is_not_persisted_again_and_the_zaak_is_not_recancelled() { // A redelivered job (§8.6) finds the aggregate already VERLOPEN: a no-op, not saved again — and // the ACL is not asked to cancel the zaak a second time (posting a second resultaat would 400). var store = new FakeRegistrationStore(); var registration = Submitted(); registration.Expire(); store.Seed(registration); var acl = new FakeAclClient(); await new ExpireRegistrationWorker(store, acl).HandleAsync( new RegistratieVerlopenJob("job-7", registration.Id)); Assert.Equal(0, store.SaveCount); Assert.Equal(0, acl.CancelCallCount); Assert.Equal(RegistrationStatus.Verlopen, (await store.GetAsync(registration.Id))!.Status); } [Fact] public async Task An_already_resolved_registration_is_left_alone_and_the_zaak_is_not_cancelled() { // Race with S-11: the citizen withdrew while parked at WachtOpDocumenten, so the aggregate is // already terminal (INGETROKKEN) when the timer's job arrives. Expiring it would violate the // aggregate's invariant; the worker must instead no-op (and let the job complete), not throw // into a redelivery loop — and it must not cancel the zaak of a registration it didn't expire. var store = new FakeRegistrationStore(); var registration = Submitted(); registration.Withdraw(); store.Seed(registration); var acl = new FakeAclClient(); await new ExpireRegistrationWorker(store, acl).HandleAsync( new RegistratieVerlopenJob("job-7", registration.Id)); Assert.Equal(0, store.SaveCount); Assert.Equal(0, acl.CancelCallCount); Assert.Equal(RegistrationStatus.Ingetrokken, (await store.GetAsync(registration.Id))!.Status); } [Fact] public async Task An_unknown_registration_throws_so_the_job_is_redelivered() { var store = new FakeRegistrationStore(); await Assert.ThrowsAsync(() => new ExpireRegistrationWorker(store, new FakeAclClient()).HandleAsync( new RegistratieVerlopenJob("job-7", RegistrationId.New()))); } [Fact] public async Task Rejects_a_null_job() => await Assert.ThrowsAsync(() => new ExpireRegistrationWorker(new FakeRegistrationStore(), new FakeAclClient()).HandleAsync(null!)); }