using System.Net; using System.Net.Http.Json; using System.Text; using System.Text.Json; using Microsoft.EntityFrameworkCore; using Xunit; namespace PatientProfile.AcceptanceTests; public sealed class CreatePatientTests(PatientApiFixture fixture) : IClassFixture { // This HTTP contract is independent of the service's implementation DTOs. public sealed record Profile(Guid PatientId, string Name, DateOnly DateOfBirth, string? ContactInfo); [Fact] public async Task Valid_profile_is_assigned_an_identifier_and_can_be_retrieved_after_restart() { using var response = await fixture.Client.PostAsJsonAsync("/api/patients", new { name = "Steffen Example", dateOfBirth = "1980-01-01", contactInfo = "steffen@example.test" }); Assert.Equal(HttpStatusCode.Created, response.StatusCode); var created = await response.Content.ReadFromJsonAsync(); Assert.NotNull(created); Assert.NotEqual(Guid.Empty, created.PatientId); Assert.Equal("Steffen Example", created.Name); Assert.Equal(new DateOnly(1980, 1, 1), created.DateOfBirth); Assert.Equal("steffen@example.test", created.ContactInfo); Assert.Equal($"/api/patients/{created.PatientId}", response.Headers.Location!.ToString()); await fixture.RestartHostsAsync(); using var retrieved = await fixture.Client.GetAsync(response.Headers.Location); Assert.Equal(HttpStatusCode.OK, retrieved.StatusCode); Assert.Equal(created, await retrieved.Content.ReadFromJsonAsync()); await using var db = fixture.CreateDbContext(); var stored = await db.Patients.SingleAsync(p => p.PatientId == created.PatientId); Assert.Equal(created.Name, stored.Name); Assert.Equal(created.DateOfBirth, stored.DateOfBirth); Assert.Equal(created.ContactInfo, stored.ContactInfo); Assert.NotEqual(default, stored.CreatedAt); Assert.Equal(stored.CreatedAt, stored.UpdatedAt); } [Theory] [InlineData("{\"dateOfBirth\":\"1980-01-01\"}", "Name")] [InlineData("{\"name\":null,\"dateOfBirth\":\"1980-01-01\"}", "Name")] [InlineData("{\"name\":\"\",\"dateOfBirth\":\"1980-01-01\"}", "Name")] [InlineData("{\"name\":\" \",\"dateOfBirth\":\"1980-01-01\"}", "Name")] [InlineData("{\"name\":\"Steffen Example\"}", "DateOfBirth")] [InlineData("{\"name\":\"Steffen Example\",\"dateOfBirth\":null}", "DateOfBirth")] public async Task Missing_required_information_is_rejected_without_persisting(string json, string field) { await using var db = fixture.CreateDbContext(); var countBefore = await db.Patients.CountAsync(); using var content = new StringContent(json, Encoding.UTF8, "application/json"); using var response = await fixture.Client.PostAsync("/api/patients", content); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); using var problem = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); Assert.True(problem.RootElement.GetProperty("errors").TryGetProperty(field, out _)); Assert.Equal(countBefore, await db.Patients.CountAsync()); } [Theory] [InlineData("{\"name\":\"Steffen Example\",\"dateOfBirth\":\"invalid\"}")] [InlineData("{\"name\":\"Steffen Example\",\"dateOfBirth\":\"1980-02-30\"}")] [InlineData("{")] public async Task Malformed_information_is_rejected(string json) { await using var db = fixture.CreateDbContext(); var countBefore = await db.Patients.CountAsync(); using var content = new StringContent(json, Encoding.UTF8, "application/json"); using var response = await fixture.Client.PostAsync("/api/patients", content); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Equal(countBefore, await db.Patients.CountAsync()); } [Fact] public async Task Separate_creations_have_distinct_identifiers_and_contact_information_is_optional() { var request = new { name = "Steffen Example", dateOfBirth = "1980-01-01" }; using var first = await fixture.Client.PostAsJsonAsync("/api/patients", request); using var second = await fixture.Client.PostAsJsonAsync("/api/patients", request); Assert.Equal(HttpStatusCode.Created, first.StatusCode); Assert.Equal(HttpStatusCode.Created, second.StatusCode); var firstProfile = await first.Content.ReadFromJsonAsync(); var secondProfile = await second.Content.ReadFromJsonAsync(); Assert.NotEqual(firstProfile!.PatientId, secondProfile!.PatientId); Assert.Null(firstProfile.ContactInfo); Assert.Equal(firstProfile, await fixture.Client.GetFromJsonAsync(first.Headers.Location)); } [Fact] public async Task Unknown_identifier_returns_not_found() { using var response = await fixture.Client.GetAsync($"/api/patients/{Guid.NewGuid()}"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); } }