using System.Net.Http.Json; using FluentAssertions; public class APITests : IClassFixture> { private readonly HttpClient client; public APITests(StreetWebApplicationFactory factory) { client = factory.CreateClient(); } [Fact] public async Task GET_Non_Existing_Street_Should_Return_NotFound() { var response = await client.GetAsync("/api/streets/Haid-und-Neu-Straße"); response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound); } [Fact] public async Task GET_Street_Should_Return_POSTed_Street() { var street = new { name = "Kaiserstraße", capacity = 100, geometry = new[] { new { x = 10, y = 20 } } }; var post_response = await client.PostAsJsonAsync("/api/streets/", street); post_response.StatusCode.Should().Be(System.Net.HttpStatusCode.Created); var get_response = await client.GetAsync("/api/streets/Kaiserstraße"); get_response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK); var responseContent = await get_response.Content.ReadFromJsonAsync(); street.Should().BeEquivalentTo(responseContent); } [Fact] public async Task POST_Existing_Street_Should_Return_Conflict() { var street = new { name = "Adenauerring", capacity = 100, geometry = new[] { new { x = 10, y = 20 } } }; await client.PostAsJsonAsync("/api/streets/", street); var duplicate_street = new { name = "Adenauerring", capacity = 200, geometry = new[] { new { x = 30, y = 40 } } }; var response = await client.PostAsJsonAsync("/api/streets", duplicate_street); response.StatusCode.Should().Be(System.Net.HttpStatusCode.Conflict); } [Fact] public async Task DELETE_Non_Existing_Street_Should_Return_NotFound() { var response = await client.GetAsync("/api/streets/Englerstraße"); response.StatusCode.Should().Be(System.Net.HttpStatusCode.NoContent); } [Fact] public async Task DELETE_Existing_Street_Should_Delete_Street() { var street = new { name = "Moltkestraße", capacity = 100, geometry = new[] { new { x = 10, y = 20 } } }; await client.PostAsJsonAsync("/api/streets/", street); var delete_response = await client.DeleteAsync("/api/streets/Moltkestraße"); delete_response.StatusCode.Should().Be(System.Net.HttpStatusCode.NoContent); var get_response = await client.GetAsync("/api/streets/Moltkestraße"); get_response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound); } [Fact] public async Task PATCH_Non_Existing_Street_Should_Return_NotFound() { var update = new { Point = new { x = 10, y = 40 } }; var response = await client.PatchAsJsonAsync("/api/streets/Ludwig-Erhard-Allee", update); response.StatusCode.Should().Be(System.Net.HttpStatusCode.NotFound); } [Fact] public async Task PATCH_Existing_Street_Should_Update_Street() { var street = new { name = "Ettlingerstraße", capacity = 50, geometry = new[] { new { x = 5, y = 3 } } }; await client.PostAsJsonAsync("/api/streets/", street); var update_no_method = new { Point = new { x = 15, y = 30 } }; var patch_response_no_method = await client.PatchAsJsonAsync("/api/streets/Ettlingerstraße", update_no_method); patch_response_no_method.StatusCode.Should().Be(System.Net.HttpStatusCode.OK); var get_response_no_method = await client.GetAsync("/api/streets/Ettlingerstraße"); get_response_no_method.StatusCode.Should().Be(System.Net.HttpStatusCode.OK); var content_no_method = get_response_no_method.Content.ReadFromJsonAsync(); var expected_content_no_method = new { name = "Ettlingerstraße", capacity = 50, geometry = new[] { new { x = 5, y = 3 }, new { x = 15, y = 30 } } }; expected_content_no_method.Should().BeEquivalentTo(content_no_method); // Test with "Backend" method var update_backend = new { Point = new { x = 20, y = 35 }, Method = "Backend" }; var patch_response_backend = await client.PatchAsJsonAsync("/api/streets/Ettlingerstraße", update_backend); patch_response_backend.StatusCode.Should().Be(System.Net.HttpStatusCode.OK); // Test with "PostGIS" method var update_postgis = new { Point = new { x = 25, y = 40 }, Method = "PostGIS" }; var patch_response_postgis = await client.PatchAsJsonAsync("/api/streets/Ettlingerstraße", update_postgis); patch_response_postgis.StatusCode.Should().Be(System.Net.HttpStatusCode.OK); // Test with "Database" method var update_database = new { Point = new { x = 30, y = 45 }, Method = "Database" }; var patch_response_database = await client.PatchAsJsonAsync("/api/streets/Ettlingerstraße", update_database); patch_response_database.StatusCode.Should().Be(System.Net.HttpStatusCode.OK); var get_response_final = await client.GetAsync("/api/streets/Ettlingerstraße"); get_response_final.StatusCode.Should().Be(System.Net.HttpStatusCode.OK); var content_final = await get_response_final.Content.ReadFromJsonAsync(); var expected_content_final = new { name = "Ettlingerstraße", capacity = 50, geometry = new[] { new { x = 5, y = 3 }, new { x = 15, y = 30 }, new { x = 20, y = 35 }, new { x = 25, y = 40 }, new { x = 30, y = 45 } } }; expected_content_final.Should().BeEquivalentTo(content_final); } }