|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Api.Controllers.Models; |
| 6 | +using Api.Database.Models; |
| 7 | +using Api.Services; |
| 8 | +using Api.Test.Database; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Testcontainers.PostgreSql; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Api.Test.Services |
| 14 | +{ |
| 15 | + public class InspectionAreaServiceTest : IAsyncLifetime |
| 16 | + { |
| 17 | + public required DatabaseUtilities DatabaseUtilities; |
| 18 | + public required PostgreSqlContainer Container; |
| 19 | + public required IMissionRunService MissionRunService; |
| 20 | + public required IInspectionAreaService InspectionAreaService; |
| 21 | + |
| 22 | + public async Task InitializeAsync() |
| 23 | + { |
| 24 | + (Container, string connectionString, var connection) = |
| 25 | + await TestSetupHelpers.ConfigurePostgreSqlDatabase(); |
| 26 | + var factory = TestSetupHelpers.ConfigureWebApplicationFactory( |
| 27 | + postgreSqlConnectionString: connectionString |
| 28 | + ); |
| 29 | + var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory); |
| 30 | + |
| 31 | + DatabaseUtilities = new DatabaseUtilities( |
| 32 | + TestSetupHelpers.ConfigurePostgreSqlContext(connectionString) |
| 33 | + ); |
| 34 | + MissionRunService = serviceProvider.GetRequiredService<IMissionRunService>(); |
| 35 | + InspectionAreaService = serviceProvider.GetRequiredService<IInspectionAreaService>(); |
| 36 | + } |
| 37 | + |
| 38 | + public Task DisposeAsync() => Task.CompletedTask; |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task TestTasksInsidePolygon() |
| 42 | + { |
| 43 | + var installation = await DatabaseUtilities.NewInstallation(); |
| 44 | + var plant = await DatabaseUtilities.NewPlant(installation.InstallationCode); |
| 45 | + var inspectionArea = await DatabaseUtilities.NewInspectionArea( |
| 46 | + installation.InstallationCode, |
| 47 | + plant.PlantCode |
| 48 | + ); |
| 49 | + inspectionArea.AreaPolygonJson = |
| 50 | + @"{ |
| 51 | + ""zmin"": 0, |
| 52 | + ""zmax"": 10, |
| 53 | + ""positions"": [ |
| 54 | + { ""x"": 0, ""y"": 0 }, |
| 55 | + { ""x"": 0, ""y"": 10 }, |
| 56 | + { ""x"": 10, ""y"": 10 }, |
| 57 | + { ""x"": 10, ""y"": 0 } |
| 58 | + ] |
| 59 | + }"; |
| 60 | + |
| 61 | + List<MissionTask> missionTasks = |
| 62 | + [ |
| 63 | + new(new Pose(1, 1, 1, 0, 0, 0, 1), MissionTaskType.Inspection), |
| 64 | + new(new Pose(2, 2, 2, 0, 0, 0, 1), MissionTaskType.ReturnHome), |
| 65 | + ]; |
| 66 | + |
| 67 | + var testBool = InspectionAreaService.MissionTasksAreInsideInspectionAreaPolygon( |
| 68 | + missionTasks, |
| 69 | + inspectionArea |
| 70 | + ); |
| 71 | + Assert.True(testBool); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public async Task TestTasksOutsidePolygon() |
| 76 | + { |
| 77 | + var installation = await DatabaseUtilities.NewInstallation(); |
| 78 | + var plant = await DatabaseUtilities.NewPlant(installation.InstallationCode); |
| 79 | + var inspectionArea = await DatabaseUtilities.NewInspectionArea( |
| 80 | + installation.InstallationCode, |
| 81 | + plant.PlantCode |
| 82 | + ); |
| 83 | + inspectionArea.AreaPolygonJson = |
| 84 | + @"{ |
| 85 | + ""zmin"": 0, |
| 86 | + ""zmax"": 10, |
| 87 | + ""positions"": [ |
| 88 | + { ""x"": 0, ""y"": 0 }, |
| 89 | + { ""x"": 0, ""y"": 10 }, |
| 90 | + { ""x"": 10, ""y"": 10 }, |
| 91 | + { ""x"": 10, ""y"": 0 } |
| 92 | + ] |
| 93 | + }"; |
| 94 | + List<MissionTask> missionTasks = |
| 95 | + [ |
| 96 | + new(new Pose(1, 1, 1, 0, 0, 0, 1), MissionTaskType.ReturnHome), |
| 97 | + new(new Pose(11, 11, 11, 0, 0, 0, 1), MissionTaskType.ReturnHome), |
| 98 | + ]; |
| 99 | + |
| 100 | + var testBool = InspectionAreaService.MissionTasksAreInsideInspectionAreaPolygon( |
| 101 | + missionTasks, |
| 102 | + inspectionArea |
| 103 | + ); |
| 104 | + Assert.False(testBool); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments