-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from aau-giraf/Testing-Environment
Testing environment, Citizens, Organizations, Users
- Loading branch information
Showing
19 changed files
with
1,501 additions
and
33 deletions.
There are no files selected for viewing
Empty file.
337 changes: 314 additions & 23 deletions
337
Giraf.IntegrationTests/Endpoints/CitizensEndpointTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,321 @@ | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using FluentAssertions; | ||
using System.Threading.Tasks; | ||
using Giraf.IntegrationTests.Utils; | ||
using Giraf.IntegrationTests.Utils.DbSeeders; | ||
using GirafAPI.Data; | ||
using GirafAPI.Entities.Citizens.DTOs; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Giraf.IntegrationTests.Endpoints; | ||
|
||
public class CitizensEndpointTests | ||
namespace Giraf.IntegrationTests.Endpoints | ||
{ | ||
[Fact] | ||
public async Task PostCitizenWithValidParameters() | ||
public class CitizensEndpointTests | ||
{ | ||
// Arrange | ||
var application = new GirafWebApplicationFactory(); | ||
CreateCitizenDTO citizen = new CreateCitizenDTO("Hans", "Hansen"); | ||
|
||
var client = application.CreateClient(); | ||
|
||
// Act | ||
var response = await client.PostAsJsonAsync("/citizens", citizen); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
|
||
var createdCitizen = await response.Content.ReadFromJsonAsync<CitizenDTO>(); | ||
createdCitizen?.Id.Should().Be(1); | ||
createdCitizen?.FirstName.Should().Be("Hans"); | ||
createdCitizen?.LastName.Should().Be("Hansen"); | ||
#region Get All Citizens Tests | ||
|
||
// Testing GET /citizens, with multiple existing citizens in the DB | ||
[Fact] | ||
public async Task GetAllCitizens_ReturnsListOfCitizens() | ||
{ | ||
// Arrange | ||
var seeder = new MultipleCitizensSeeder(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// Act | ||
var response = await client.GetAsync("/citizens"); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var citizens = await response.Content.ReadFromJsonAsync<List<CitizenDTO>>(); | ||
Assert.NotNull(citizens); | ||
Assert.Equal(3, citizens.Count); | ||
} | ||
|
||
// Test GET /citizens when there are no citizens. | ||
[Fact] | ||
public async Task GetAllCitizens_ReturnsEmptyList_WhenNoCitizens() | ||
{ | ||
// Arrange | ||
var seeder = new EmptyDb(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// Act | ||
var response = await client.GetAsync("/citizens"); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var citizens = await response.Content.ReadFromJsonAsync<List<CitizenDTO>>(); | ||
Assert.NotNull(citizens); | ||
Assert.Empty(citizens); | ||
} | ||
|
||
#endregion | ||
|
||
#region Get Citizen by ID Tests | ||
|
||
// Test GET /citizens/{id:int} when the citizen exists. | ||
[Fact] | ||
public async Task GetCitizenById_ReturnsCitizen_WhenCitizenExists() | ||
{ | ||
// Arrange | ||
var seeder = new BasicCitizenSeeder(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// First, get the list of citizens to obtain the ID | ||
var citizensResponse = await client.GetAsync("/citizens"); | ||
citizensResponse.EnsureSuccessStatusCode(); | ||
var citizens = await citizensResponse.Content.ReadFromJsonAsync<List<CitizenDTO>>(); | ||
Assert.NotNull(citizens); | ||
|
||
var citizenId = citizens[0].Id; | ||
|
||
// Act | ||
var response = await client.GetAsync($"/citizens/{citizenId}"); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var citizen = await response.Content.ReadFromJsonAsync<CitizenDTO>(); | ||
Assert.NotNull(citizen); | ||
Assert.Equal(citizenId, citizen.Id); | ||
Assert.Equal("Anders", citizen.FirstName); | ||
Assert.Equal("And", citizen.LastName); | ||
} | ||
|
||
// Test GET /citizens/{id:int} when the citizen does not exist. | ||
[Fact] | ||
public async Task GetCitizenById_ReturnsNotFound_WhenCitizenDoesNotExist() | ||
{ | ||
// Arrange | ||
var seeder = new EmptyDb(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// Act | ||
var response = await client.GetAsync("/citizens/999"); | ||
|
||
// Assert | ||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
|
||
#endregion | ||
|
||
#region Update Citizen Tests | ||
|
||
// Test PUT /citizens/{id:int} when updating an existing citizen. | ||
[Fact] | ||
public async Task UpdateCitizen_ReturnsOk_WhenCitizenExists() | ||
{ | ||
// Arrange | ||
var seeder = new BasicCitizenSeeder(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// Get the citizen's ID | ||
var citizensResponse = await client.GetAsync("/citizens"); | ||
citizensResponse.EnsureSuccessStatusCode(); | ||
var citizens = await citizensResponse.Content.ReadFromJsonAsync<List<CitizenDTO>>(); | ||
Assert.NotNull(citizens); | ||
Assert.Single(citizens); | ||
|
||
var citizenId = citizens[0].Id; | ||
|
||
var updateCitizenDto = new UpdateCitizenDTO("UpdatedFirstName", "UpdatedLastName"); | ||
|
||
// Act | ||
var response = await client.PutAsJsonAsync($"/citizens/{citizenId}", updateCitizenDto); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
|
||
// Verify that the citizen was updated | ||
var getResponse = await client.GetAsync($"/citizens/{citizenId}"); | ||
getResponse.EnsureSuccessStatusCode(); | ||
var updatedCitizen = await getResponse.Content.ReadFromJsonAsync<CitizenDTO>(); | ||
Assert.NotNull(updatedCitizen); | ||
Assert.Equal("UpdatedFirstName", updatedCitizen.FirstName); | ||
Assert.Equal("UpdatedLastName", updatedCitizen.LastName); | ||
} | ||
|
||
// Test PUT /citizens/{id:int} when the citizen does not exist. | ||
[Fact] | ||
public async Task UpdateCitizen_ReturnsNotFound_WhenCitizenDoesNotExist() | ||
{ | ||
// Arrange | ||
var seeder = new EmptyDb(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
var updateCitizenDto = new UpdateCitizenDTO("FirstName", "LastName"); | ||
|
||
// Act | ||
var response = await client.PutAsJsonAsync("/citizens/999", updateCitizenDto); | ||
|
||
// Assert | ||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
|
||
#endregion | ||
|
||
#region Add Citizen to Organization Tests | ||
|
||
// Test POST /citizens/{id}/add-citizen when the organization exists. | ||
[Fact] | ||
public async Task AddCitizen_ReturnsOk_WhenOrganizationExists() | ||
{ | ||
// Arrange | ||
var seeder = new BasicOrganizationSeeder(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// Get the organization ID | ||
using (var scope = factory.Services.CreateScope()) | ||
{ | ||
var dbContext = scope.ServiceProvider.GetRequiredService<GirafDbContext>(); | ||
var organization = await dbContext.Organizations.FirstOrDefaultAsync(); | ||
Assert.NotNull(organization); | ||
var organizationId = organization.Id; | ||
|
||
var createCitizenDto = new CreateCitizenDTO("New", "Citizen"); | ||
|
||
// Act | ||
var response = await client.PostAsJsonAsync($"/citizens/{organizationId}/add-citizen", createCitizenDto); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
|
||
// Verify that the citizen was added | ||
var getCitizensResponse = await client.GetAsync("/citizens"); | ||
getCitizensResponse.EnsureSuccessStatusCode(); | ||
var citizens = await getCitizensResponse.Content.ReadFromJsonAsync<List<CitizenDTO>>(); | ||
Assert.NotNull(citizens); | ||
Assert.Single(citizens); | ||
Assert.Equal("New", citizens[0].FirstName); | ||
Assert.Equal("Citizen", citizens[0].LastName); | ||
} | ||
} | ||
|
||
// Test POST /citizens/{id}/add-citizen when the organization does not exist. | ||
[Fact] | ||
public async Task AddCitizen_ReturnsNotFound_WhenOrganizationDoesNotExist() | ||
{ | ||
// Arrange | ||
var seeder = new EmptyDb(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
var createCitizenDto = new CreateCitizenDTO("New", "Citizen"); | ||
|
||
// Act | ||
var response = await client.PostAsJsonAsync("/citizens/999/add-citizen", createCitizenDto); | ||
|
||
// Assert | ||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
|
||
#endregion | ||
|
||
#region Remove Citizen from Organization Tests | ||
|
||
// Test DELETE /citizens/{id}/remove-citizen/{citizenId} when the citizen exists in the organization. | ||
[Fact] | ||
public async Task RemoveCitizen_ReturnsNoContent_WhenCitizenExistsInOrganization() | ||
{ | ||
// Arrange | ||
var seeder = new CitizenWithOrganizationSeeder(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// Get the organization ID and citizen ID | ||
using (var scope = factory.Services.CreateScope()) | ||
{ | ||
var dbContext = scope.ServiceProvider.GetRequiredService<GirafDbContext>(); | ||
var organization = await dbContext.Organizations.FirstOrDefaultAsync(); | ||
Assert.NotNull(organization); | ||
var organizationId = organization.Id; | ||
|
||
var citizen = await dbContext.Citizens.FirstOrDefaultAsync(); | ||
Assert.NotNull(citizen); | ||
var citizenId = citizen.Id; | ||
|
||
// Act | ||
var response = await client.DeleteAsync($"/citizens/{organizationId}/remove-citizen/{citizenId}"); | ||
|
||
// Assert | ||
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); | ||
|
||
// Verify that the citizen was removed | ||
var getCitizensResponse = await client.GetAsync("/citizens"); | ||
getCitizensResponse.EnsureSuccessStatusCode(); | ||
var citizens = await getCitizensResponse.Content.ReadFromJsonAsync<List<CitizenDTO>>(); | ||
Assert.NotNull(citizens); | ||
Assert.Empty(citizens); | ||
} | ||
} | ||
|
||
// Test DELETE /citizens/{id}/remove-citizen/{citizenId} when the citizen does not exist. | ||
[Fact] | ||
public async Task RemoveCitizen_ReturnsNotFound_WhenCitizenDoesNotExist() | ||
{ | ||
// Arrange | ||
var seeder = new BasicOrganizationSeeder(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// Get the organization ID | ||
using (var scope = factory.Services.CreateScope()) | ||
{ | ||
var dbContext = scope.ServiceProvider.GetRequiredService<GirafDbContext>(); | ||
var organization = await dbContext.Organizations.FirstOrDefaultAsync(); | ||
Assert.NotNull(organization); | ||
var organizationId = organization.Id; | ||
|
||
// Act | ||
var response = await client.DeleteAsync($"/citizens/{organizationId}/remove-citizen/999"); | ||
|
||
// Assert | ||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); | ||
} | ||
} | ||
|
||
// Test DELETE /citizens/{id}/remove-citizen/{citizenId} when the citizen does not belong to the organization. | ||
[Fact] | ||
public async Task RemoveCitizen_ReturnsBadRequest_WhenCitizenNotInOrganization() | ||
{ | ||
// Arrange | ||
var seeder = new MultipleOrganizationsAndCitizensSeeder(); | ||
var factory = new GirafWebApplicationFactory(seeder); | ||
var client = factory.CreateClient(); | ||
|
||
// Get the organization ID and a citizen ID from a different organization | ||
using (var scope = factory.Services.CreateScope()) | ||
{ | ||
var dbContext = scope.ServiceProvider.GetRequiredService<GirafDbContext>(); | ||
|
||
var organizations = await dbContext.Organizations.ToListAsync(); | ||
Assert.True(organizations.Count >= 2); | ||
|
||
var organization1 = organizations[0]; | ||
var organization2 = organizations[1]; | ||
|
||
var citizenNotInOrg = await dbContext.Citizens.FirstOrDefaultAsync(c => c.Organization.Id == organization2.Id); | ||
Assert.NotNull(citizenNotInOrg); | ||
var citizenId = citizenNotInOrg.Id; | ||
|
||
// Act | ||
var response = await client.DeleteAsync($"/citizens/{organization1.Id}/remove-citizen/{citizenId}"); | ||
|
||
// Assert | ||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} | ||
} |
Oops, something went wrong.