-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for list definitions endpoint
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
backend/api.test/Controllers/MissionDefinitionControllerTests.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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Api.Controllers.Models; | ||
using Api.Database.Models; | ||
using Api.Services; | ||
using Api.Test.Database; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Testcontainers.PostgreSql; | ||
using Xunit; | ||
|
||
namespace Api.Test.Controllers | ||
{ | ||
public class MissionDefinitionControllerTests : IAsyncLifetime | ||
{ | ||
public required DatabaseUtilities DatabaseUtilities; | ||
public required PostgreSqlContainer Container; | ||
public required HttpClient Client; | ||
public required JsonSerializerOptions SerializerOptions; | ||
|
||
public required IMissionDefinitionService MissionDefinitionService; | ||
public required ISourceService SourceService; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
(Container, string connectionString, var connection) = | ||
await TestSetupHelpers.ConfigurePostgreSqlDatabase(); | ||
var factory = TestSetupHelpers.ConfigureWebApplicationFactory( | ||
postgreSqlConnectionString: connectionString | ||
); | ||
var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); | ||
|
||
Client = TestSetupHelpers.ConfigureHttpClient(factory); | ||
SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions(); | ||
|
||
DatabaseUtilities = new DatabaseUtilities( | ||
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) | ||
); | ||
|
||
MissionDefinitionService = | ||
serviceProvider.GetRequiredService<IMissionDefinitionService>(); | ||
SourceService = serviceProvider.GetRequiredService<ISourceService>(); | ||
} | ||
|
||
public async Task DisposeAsync() => await Task.CompletedTask; | ||
|
||
[Fact] | ||
public async Task CheckThatListAllMissionDefinitionsEndpointReturnsSuccess() | ||
{ | ||
// Arrange | ||
var installation = await DatabaseUtilities.NewInstallation(); | ||
var plant = await DatabaseUtilities.NewPlant(installation.InstallationCode); | ||
var inspectionArea = await DatabaseUtilities.NewInspectionArea( | ||
installation.InstallationCode, | ||
plant.PlantCode | ||
); | ||
|
||
var source = await SourceService.CreateSourceIfDoesNotExist([]); | ||
|
||
var missionDefinition = new MissionDefinition() | ||
{ | ||
Source = source, | ||
InstallationCode = installation.InstallationCode, | ||
Name = "Test Mission Definition", | ||
InspectionArea = inspectionArea, | ||
IsDeprecated = false, | ||
}; | ||
|
||
_ = await MissionDefinitionService.Create(missionDefinition); | ||
|
||
// Act | ||
var response = await Client.GetAsync("missions/definitions"); | ||
|
||
// Assert | ||
var missionDefinitions = await response.Content.ReadFromJsonAsync< | ||
List<MissionDefinitionResponse> | ||
>(SerializerOptions); | ||
|
||
Assert.Single(missionDefinitions!); | ||
} | ||
} | ||
} |