using System.Net; using System.Text; using BigRegister.Api.Zgw; namespace BigRegister.Tests; /// /// Exercises the OpenZaak read source against a stub HttpMessageHandler (no live server, no /// mocking library) — the guarantee that it follows ZGW pagination, resolves + caches /// zaaktype labels, and always sends a Bearer token. /// public class OpenZaakZaakSourceTests { private const string ZrcBase = "https://oz.example/zaken/api/v1"; private const string ZtBase = "https://oz.example/catalogi/api/v1"; private static string Page1 => $$""" { "count": 2, "next": "{{ZrcBase}}/zaken?page=2", "results": [ { "url": "{{ZrcBase}}/zaken/uuid-1", "identificatie": "ZAAK-1", "zaaktype": "{{ZtBase}}/zaaktypen/zt-1", "startdatum": "2026-03-01", "einddatum": null, "registratiedatum": "2026-03-01" } ] } """; private static string Page2 => $$""" { "count": 2, "next": null, "results": [ { "url": "{{ZrcBase}}/zaken/uuid-2", "identificatie": "ZAAK-2", "zaaktype": "{{ZtBase}}/zaaktypen/zt-1", "startdatum": "2026-01-01", "einddatum": "2026-02-01", "registratiedatum": "2026-01-01" } ] } """; private const string Zaaktype = """{ "omschrijving": "Herregistratie arts" }"""; [Fact] public void Follows_pagination_caches_zaaktype_and_sends_bearer_token() { var handler = new StubHandler(url => url switch { _ when url == $"{ZrcBase}/zaken" => Page1, _ when url == $"{ZrcBase}/zaken?page=2" => Page2, _ when url == $"{ZtBase}/zaaktypen/zt-1" => Zaaktype, _ => throw new InvalidOperationException($"unexpected ZGW GET {url}"), }); var options = new ZgwOptions { ZrcBaseUrl = ZrcBase, ZtcBaseUrl = ZtBase, ClientId = "c", Secret = "s" }; var source = new OpenZaakZaakSource(new HttpClient(handler), new ZgwTokenProvider(options), options); var cases = source.ListCases(DateTimeOffset.UtcNow); // Both pages accumulated. Assert.Equal(2, cases.Count); Assert.Equal(new[] { "uuid-1", "uuid-2" }, cases.Select(c => c.Id)); Assert.All(cases, c => Assert.Equal("Herregistratie arts", c.Type)); Assert.Equal("InBehandeling", cases[0].Status.Tag); // open Assert.Equal("Goedgekeurd", cases[1].Status.Tag); // closed // Zaaktype resolved once despite two zaken sharing it (cache). Assert.Single(handler.Requests, r => r.Contains("zaaktypen")); // Every outbound request carried a Bearer token. Assert.All(handler.AuthSchemes, s => Assert.Equal("Bearer", s)); } private sealed class StubHandler(Func respond) : HttpMessageHandler { public List Requests { get; } = new(); public List AuthSchemes { get; } = new(); protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var url = request.RequestUri!.ToString(); Requests.Add(url); AuthSchemes.Add(request.Headers.Authorization?.Scheme); return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(respond(url), Encoding.UTF8, "application/json"), }); } } }