using System.Net; using System.Net.Http.Json; using System.Text.Json; using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; using StellaOps.SbomService.Models; using Xunit; namespace StellaOps.SbomService.Tests; public class SbomEventEndpointsTests : IClassFixture> { private readonly WebApplicationFactory _factory; public SbomEventEndpointsTests(WebApplicationFactory factory) { _factory = factory.WithWebHostBuilder(_ => { }); } [Fact] public async Task Backfill_publishes_version_created_events_once() { var client = _factory.CreateClient(); var backfillResponse = await client.PostAsync("/internal/sbom/events/backfill", content: null); backfillResponse.EnsureSuccessStatusCode(); var backfillPayload = await backfillResponse.Content.ReadFromJsonAsync(); backfillPayload.TryGetProperty("published", out var publishedProp).Should().BeTrue(); publishedProp.GetInt32().Should().BeGreaterOrEqualTo(1); var events = await client.GetFromJsonAsync>("/internal/sbom/events"); events.Should().NotBeNull(); events!.Should().HaveCount(1); events[0].SnapshotId.Should().Be("snap-001"); events[0].TenantId.Should().Be("tenant-a"); // Requesting the projection should not duplicate events. var projectionResponse = await client.GetAsync("/sboms/snap-001/projection?tenant=tenant-a"); projectionResponse.StatusCode.Should().Be(HttpStatusCode.OK); var eventsAfterProjection = await client.GetFromJsonAsync>("/internal/sbom/events"); eventsAfterProjection.Should().NotBeNull(); eventsAfterProjection!.Should().HaveCount(1); } }