PTV-Street/tests/APITests.cs

203 lines
6.5 KiB
C#

using System.Net.Http.Json;
using FluentAssertions;
using FluentAssertions.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class APITests : IClassFixture<StreetWebApplicationFactory<Program>>
{
private readonly HttpClient client;
public APITests(StreetWebApplicationFactory<Program> 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 }, new { x = 15, y = 25 } }
};
Console.WriteLine(JsonConvert.SerializeObject(street));
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 expected_street = JObject.Parse(JsonConvert.SerializeObject(street));
var response_street = JObject.Parse(await get_response.Content.ReadAsStringAsync());
response_street.Should().BeEquivalentTo(expected_street);
}
[Fact]
public async Task POST_Existing_Street_Should_Return_Conflict()
{
var street = new
{
name = "Adenauerring",
capacity = 100,
geometry = new[] { new { x = 10, y = 20 }, new { x = 50, y = 70 } }
};
await client.PostAsJsonAsync("/api/streets/", street);
var duplicate_street = new
{
name = "Adenauerring",
capacity = 200,
geometry = new[] { new { x = 30, y = 40 }, new { x = 100, y = 150 } }
};
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.NotFound);
}
[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 }, new { x = 50, y = 100 } }
};
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 }, new { x = 6, y = 4 } }
};
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 expected_street_no_method = new
{
name = "Ettlingerstraße",
capacity = 50,
geometry = new[] { new { x = 5, y = 3 }, new { x = 6, y = 4 }, new { x = 15, y = 30 } }
};
var expected_street_json = JObject.Parse(JsonConvert.SerializeObject(expected_street_no_method));
var response_street_no_method = JObject.Parse(await get_response_no_method.Content.ReadAsStringAsync());
response_street_no_method.Should().BeEquivalentTo(expected_street_json);
// Test with "Backend" method
var update_backend = new
{
Point = new { x = 20, y = 35 },
usePostGIS = false,
};
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 },
usePostGIS = true,
};
var patch_response_postgis = await client.PatchAsJsonAsync("/api/streets/Ettlingerstraße", update_postgis);
patch_response_postgis.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 expected_street_final = new
{
name = "Ettlingerstraße",
capacity = 50,
geometry = new[]
{
new { x = 5, y = 3 },
new { x = 6, y = 4 },
new { x = 15, y = 30 },
new { x = 20, y = 35 },
new { x = 25, y = 40 },
}
};
var expected_street = JObject.Parse(JsonConvert.SerializeObject(expected_street_final));
var response_street = JObject.Parse(await get_response_final.Content.ReadAsStringAsync());
response_street.Should().BeEquivalentTo(expected_street);
}
}