using Big.Application; using Big.Domain; using Big.Infrastructure; using Microsoft.Extensions.Logging.Abstractions; namespace Big.Tests; public class OpenZaakJobProcessorTests { /// A fake external-worker client: scripts the jobs to acquire and records completions. private sealed class FakeExternalWorkerClient(params OpenZaakJob[] jobs) : IExternalWorkerClient { public int AcquireCount { get; private set; } public List<(string JobId, Uri ZaakUrl)> Completed { get; } = []; public Task> AcquireOpenZaakJobsAsync(int maxJobs, CancellationToken ct = default) { AcquireCount++; return Task.FromResult>(jobs.Take(maxJobs).ToList()); } public Task CompleteOpenZaakJobAsync(string jobId, Uri zaakUrl, CancellationToken ct = default) { Completed.Add((jobId, zaakUrl)); return Task.CompletedTask; } } private static OpenZaakJobProcessor Processor(IExternalWorkerClient client, IRegistrationStore store, FakeAclClient acl) => new(client, new OpenZaakWorker(store, acl), NullLogger.Instance); [Fact] public async Task Acquires_a_job_opens_the_zaak_and_completes_it() { var registration = Registration.Submit("123456782"); var store = new FakeRegistrationStore(); store.Seed(registration); var client = new FakeExternalWorkerClient(new OpenZaakJob("job-1", registration.Id)); var acl = new FakeAclClient(); var acquired = await Processor(client, store, acl).PumpOnceAsync(5); Assert.Equal(1, acquired); var completed = Assert.Single(client.Completed); Assert.Equal("job-1", completed.JobId); Assert.Equal(FakeAclClient.DefaultZaakUrl, completed.ZaakUrl); Assert.Equal(FakeAclClient.DefaultZaakUrl, (await store.GetAsync(registration.Id))!.ZaakUrl); } [Fact] public async Task A_failing_job_is_left_uncompleted_for_flowable_to_redeliver() { var store = new FakeRegistrationStore(); var acl = new FakeAclClient(); // The job correlates to a registration that is not in the store, so the worker throws. var client = new FakeExternalWorkerClient(new OpenZaakJob("job-1", RegistrationId.New())); var acquired = await Processor(client, store, acl).PumpOnceAsync(5); Assert.Equal(1, acquired); Assert.Empty(client.Completed); } [Fact] public async Task Does_nothing_but_poll_when_there_are_no_jobs() { var store = new FakeRegistrationStore(); var acl = new FakeAclClient(); var client = new FakeExternalWorkerClient(); var acquired = await Processor(client, store, acl).PumpOnceAsync(5); Assert.Equal(0, acquired); Assert.Equal(1, client.AcquireCount); Assert.Empty(client.Completed); } }