using System.Net; using Big.Domain; using Big.Infrastructure; namespace Big.Tests; public class FlowableWorkflowClientTests { private static readonly Uri Base = new("http://flowable/flowable-rest/"); private static FlowableWorkflowClient Client(StubHandler handler) => new( new HttpClient(handler), new FlowableOptions { BaseUrl = Base, Username = "rest-admin", Password = "test", WorkerId = "worker-x" }); [Fact] public async Task Start_posts_the_registration_id_variable_and_returns_the_instance_id() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.Created, """{"id":"pi-1"}""")); var rid = RegistrationId.New(); var pid = await client.StartRegistrationProcessAsync(rid); Assert.Equal("pi-1", pid); Assert.Equal(HttpMethod.Post, capture.Seen!.Method); Assert.Equal("http://flowable/flowable-rest/service/runtime/process-instances", capture.Seen.RequestUri!.ToString()); Assert.Equal("Basic", capture.Seen.Headers.Authorization!.Scheme); Assert.Contains("\"processDefinitionKey\":\"registratie\"", capture.Body); Assert.Contains("\"name\":\"registrationId\"", capture.Body); Assert.Contains($"\"value\":\"{rid}\"", capture.Body); } [Fact] public async Task Acquire_posts_the_topic_and_parses_jobs_with_their_registration_id() { var rid = RegistrationId.New(); var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.OK, $$"""[{"id":"job-1","variables":[{"name":"registrationId","type":"string","value":"{{rid}}"}]}]""")); var jobs = await client.AcquireOpenZaakJobsAsync(3); var job = Assert.Single(jobs); Assert.Equal("job-1", job.JobId); Assert.Equal(rid, job.RegistrationId); Assert.Equal("http://flowable/flowable-rest/external-job-api/acquire/jobs", capture.Seen!.RequestUri!.ToString()); Assert.Contains("\"topic\":\"OpenZaakAanmaken\"", capture.Body); Assert.Contains("\"numberOfTasks\":3", capture.Body); Assert.Contains("\"workerId\":\"worker-x\"", capture.Body); Assert.Contains("\"lockDuration\":\"PT5M\"", capture.Body); } [Fact] public async Task Acquire_returns_empty_when_flowable_has_no_parked_jobs() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.OK, "[]")); var jobs = await client.AcquireOpenZaakJobsAsync(1); Assert.NotNull(capture.Seen); Assert.Empty(jobs); } [Fact] public async Task Complete_posts_the_zaak_url_variable_to_the_job_complete_endpoint() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.NoContent)); await client.CompleteOpenZaakJobAsync("job-1", new Uri("http://openzaak/zaken/api/v1/zaken/abc")); Assert.NotNull(capture.Seen); Assert.Equal(HttpMethod.Post, capture.Seen!.Method); Assert.Equal("http://flowable/flowable-rest/external-job-api/acquire/jobs/job-1/complete", capture.Seen.RequestUri!.ToString()); Assert.Contains("\"workerId\":\"worker-x\"", capture.Body); Assert.Contains("\"name\":\"zaakUrl\"", capture.Body); Assert.Contains("\"value\":\"http://openzaak/zaken/api/v1/zaken/abc\"", capture.Body); } [Fact] public async Task Start_throws_when_flowable_rejects_the_request() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.InternalServerError)); await Assert.ThrowsAsync( () => client.StartRegistrationProcessAsync(RegistrationId.New())); } }