Skip to content

Commit 180bb1e

Browse files
committed
Add test for system available for mission logic
1 parent 225d546 commit 180bb1e

File tree

6 files changed

+274
-53
lines changed

6 files changed

+274
-53
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Threading.Tasks;
2+
using Api.Database.Models;
3+
using Api.Services;
4+
using Api.Services.Helpers;
5+
using Api.Test.Utilities;
6+
using Microsoft.Extensions.Logging;
7+
using Moq;
8+
using Xunit;
9+
10+
namespace Api.Test.Services.Helpers;
11+
12+
public class MissionSchedulingHelpersTests : IAsyncLifetime
13+
{
14+
public async Task InitializeAsync() => await Task.CompletedTask;
15+
16+
public Task DisposeAsync() => Task.CompletedTask;
17+
18+
[Theory]
19+
[InlineData(false, MissionRunType.Normal, RobotStatus.Available, true, false, true)]
20+
[InlineData(false, MissionRunType.Normal, RobotStatus.Busy, true, false, false)]
21+
[InlineData(false, MissionRunType.Normal, RobotStatus.Offline, true, false, false)]
22+
[InlineData(false, MissionRunType.Normal, RobotStatus.Blocked, true, false, false)]
23+
[InlineData(
24+
false,
25+
MissionRunType.Normal,
26+
RobotStatus.BlockedProtectiveStop,
27+
true,
28+
false,
29+
false
30+
)]
31+
[InlineData(true, MissionRunType.Normal, RobotStatus.Available, true, false, false)]
32+
[InlineData(true, MissionRunType.Emergency, RobotStatus.Available, true, false, true)]
33+
[InlineData(true, MissionRunType.ReturnHome, RobotStatus.Available, true, false, true)]
34+
[InlineData(false, MissionRunType.Normal, RobotStatus.Available, false, false, false)]
35+
[InlineData(false, MissionRunType.Normal, RobotStatus.Available, true, true, false)]
36+
public void CheckLogicOfSystemIsAvailableToRunAMissionFunction(
37+
bool missionQueueFrozen,
38+
MissionRunType missionRunType,
39+
RobotStatus robotStatus,
40+
bool isIsarConnected,
41+
bool isRobotDeprecated,
42+
bool expectedResult
43+
)
44+
{
45+
// Arrange
46+
var installation = TestObjectGenerator.NewInstallation();
47+
var plant = TestObjectGenerator.NewPlant(installation);
48+
var inspectionArea = TestObjectGenerator.NewInspectionArea(installation, plant);
49+
50+
var robot = TestObjectGenerator.NewRobot(
51+
currentInstallation: installation,
52+
currentInspectionArea: inspectionArea,
53+
robotStatus: robotStatus,
54+
isarConnected: isIsarConnected,
55+
deprecated: isRobotDeprecated,
56+
missionQueueFrozen: missionQueueFrozen
57+
);
58+
59+
var missionRun = TestObjectGenerator.NewMissionRun(
60+
installationCode: installation.InstallationCode,
61+
robot: robot,
62+
inspectionArea: inspectionArea,
63+
missionRunType: missionRunType
64+
);
65+
66+
// Act
67+
var isSystemAvailable = MissionSchedulingHelpers.TheSystemIsAvailableToRunAMission(
68+
robot,
69+
missionRun,
70+
new Mock<ILogger<MissionSchedulingService>>().Object
71+
);
72+
73+
// Assert
74+
Assert.Equal(expectedResult, isSystemAvailable);
75+
}
76+
}

backend/api.test/Services/MissionRunService.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Threading;
3-
using System.Threading.Tasks;
4-
using Api.Controllers.Models;
5-
using Api.Database.Models;
1+
using System.Threading.Tasks;
62
using Api.Services;
73
using Api.Test.Database;
84
using Microsoft.Extensions.DependencyInjection;

backend/api.test/Services/RobotService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using System.Linq;
1+
using System.Linq;
32
using System.Threading.Tasks;
4-
using Api.Controllers.Models;
53
using Api.Database.Models;
64
using Api.Services;
75
using Api.Test.Database;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Api.Database.Models;
4+
5+
namespace Api.Test.Utilities;
6+
7+
public static class TestObjectGenerator
8+
{
9+
public static Installation NewInstallation(
10+
string? id = null,
11+
string name = "TestInst",
12+
string installationCode = "TestCode"
13+
)
14+
{
15+
return new Installation
16+
{
17+
Id = id ?? Guid.NewGuid().ToString(),
18+
Name = name,
19+
InstallationCode = installationCode,
20+
};
21+
}
22+
23+
public static Plant NewPlant(
24+
Installation installation,
25+
string? id = null,
26+
string name = "TestPlant",
27+
string plantCode = "TestCode"
28+
)
29+
{
30+
return new Plant
31+
{
32+
Id = id ?? Guid.NewGuid().ToString(),
33+
Installation = installation,
34+
PlantCode = plantCode,
35+
Name = name,
36+
};
37+
}
38+
39+
public static InspectionArea NewInspectionArea(
40+
Installation installation,
41+
Plant plant,
42+
string? id = null,
43+
string name = "TestName"
44+
)
45+
{
46+
return new InspectionArea
47+
{
48+
Id = id ?? Guid.NewGuid().ToString(),
49+
Plant = plant,
50+
Installation = installation,
51+
Name = name,
52+
};
53+
}
54+
55+
public static Robot NewRobot(
56+
Installation currentInstallation,
57+
InspectionArea currentInspectionArea,
58+
RobotModel? robotModel = null,
59+
RobotStatus robotStatus = RobotStatus.Available,
60+
string name = "Test Robot",
61+
string? id = null,
62+
string? isarId = null,
63+
string serialNumber = "TestSerialNumber",
64+
float batteryLevel = 100,
65+
BatteryState batteryState = BatteryState.Normal,
66+
float pressureLevel = 90,
67+
string host = "localhost",
68+
int port = 8080,
69+
IList<RobotCapabilitiesEnum>? capabilities = null,
70+
bool isarConnected = true,
71+
bool deprecated = false,
72+
bool missionQueueFrozen = false,
73+
RobotFlotillaStatus flotillaStatus = RobotFlotillaStatus.Normal,
74+
Pose? pose = null,
75+
string? currentMissionId = null
76+
)
77+
{
78+
return new Robot
79+
{
80+
Id = id ?? Guid.NewGuid().ToString(),
81+
Name = name,
82+
IsarId = isarId ?? Guid.NewGuid().ToString(),
83+
Model = robotModel ?? new RobotModel(),
84+
SerialNumber = serialNumber,
85+
CurrentInstallation = currentInstallation,
86+
CurrentInspectionArea = currentInspectionArea,
87+
BatteryLevel = batteryLevel,
88+
BatteryState = batteryState,
89+
PressureLevel = pressureLevel,
90+
Host = host,
91+
Port = port,
92+
RobotCapabilities = capabilities ?? [],
93+
IsarConnected = isarConnected,
94+
Deprecated = deprecated,
95+
MissionQueueFrozen = missionQueueFrozen,
96+
Status = robotStatus,
97+
FlotillaStatus = flotillaStatus,
98+
Pose = pose ?? new Pose(),
99+
CurrentMissionId = currentMissionId,
100+
};
101+
}
102+
103+
public static MissionRun NewMissionRun(
104+
string installationCode,
105+
Robot robot,
106+
InspectionArea inspectionArea,
107+
string name = "TestName",
108+
string? id = null,
109+
string? missionId = null,
110+
MissionStatus missionStatus = MissionStatus.Pending,
111+
DateTime? startTime = null,
112+
IList<MissionTask>? tasks = null,
113+
MissionRunType missionRunType = MissionRunType.Normal,
114+
string? isarMissionId = null,
115+
bool isDeprecated = false
116+
)
117+
{
118+
return new MissionRun
119+
{
120+
Id = id ?? Guid.NewGuid().ToString(),
121+
MissionId = missionId ?? Guid.NewGuid().ToString(),
122+
Status = missionStatus,
123+
InstallationCode = installationCode,
124+
DesiredStartTime = startTime ?? DateTime.UtcNow,
125+
Robot = robot,
126+
Tasks = tasks ?? [],
127+
MissionRunType = missionRunType,
128+
IsarMissionId = isarMissionId ?? Guid.NewGuid().ToString(),
129+
InspectionArea = inspectionArea,
130+
Name = name,
131+
IsDeprecated = isDeprecated,
132+
};
133+
}
134+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Api.Database.Models;
2+
3+
namespace Api.Services.Helpers;
4+
5+
public static class MissionSchedulingHelpers
6+
{
7+
public static bool TheSystemIsAvailableToRunAMission(
8+
Robot robot,
9+
MissionRun missionRun,
10+
ILogger logger
11+
)
12+
{
13+
if (
14+
robot.MissionQueueFrozen
15+
&& !(missionRun.IsEmergencyMission() || missionRun.IsReturnHomeMission())
16+
)
17+
{
18+
logger.LogInformation(
19+
"Mission run {MissionRunId} was not started as the mission run queue for robot {RobotName} is frozen",
20+
missionRun.Id,
21+
robot.Name
22+
);
23+
return false;
24+
}
25+
26+
if (robot.Status is not RobotStatus.Available)
27+
{
28+
logger.LogInformation(
29+
"Mission run {MissionRunId} was not started as the robot is not available",
30+
missionRun.Id
31+
);
32+
return false;
33+
}
34+
if (!robot.IsarConnected)
35+
{
36+
logger.LogWarning(
37+
"Mission run {MissionRunId} was not started as the robots {RobotId} isar instance is disconnected",
38+
missionRun.Id,
39+
robot.Id
40+
);
41+
return false;
42+
}
43+
if (robot.Deprecated)
44+
{
45+
logger.LogWarning(
46+
"Mission run {MissionRunId} was not started as the robot {RobotId} is deprecated",
47+
missionRun.Id,
48+
robot.Id
49+
);
50+
return false;
51+
}
52+
return true;
53+
}
54+
}

backend/api/Services/MissionSchedulingService.cs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Api.Controllers.Models;
33
using Api.Database.Models;
44
using Api.Services.Events;
5+
using Api.Services.Helpers;
56
using Api.Services.Models;
67
using Api.Utilities;
78

@@ -76,7 +77,13 @@ public async Task StartNextMissionRunIfSystemIsAvailable(Robot robot)
7677
return;
7778
}
7879

79-
if (!TheSystemIsAvailableToRunAMission(robot, missionRun))
80+
if (
81+
!MissionSchedulingHelpers.TheSystemIsAvailableToRunAMission(
82+
robot,
83+
missionRun,
84+
logger
85+
)
86+
)
8087
{
8188
logger.LogInformation(
8289
"Mission {MissionRunId} was put on the queue as the system may not start a mission now",
@@ -583,50 +590,6 @@ await missionRunService.UpdateMissionRunProperty(
583590
return ongoingMissions;
584591
}
585592

586-
private bool TheSystemIsAvailableToRunAMission(Robot robot, MissionRun missionRun)
587-
{
588-
if (
589-
robot.MissionQueueFrozen
590-
&& !(missionRun.IsEmergencyMission() || missionRun.IsReturnHomeMission())
591-
)
592-
{
593-
logger.LogInformation(
594-
"Mission run {MissionRunId} was not started as the mission run queue for robot {RobotName} is frozen",
595-
missionRun.Id,
596-
robot.Name
597-
);
598-
return false;
599-
}
600-
601-
if (robot.Status is not RobotStatus.Available)
602-
{
603-
logger.LogInformation(
604-
"Mission run {MissionRunId} was not started as the robot is not available",
605-
missionRun.Id
606-
);
607-
return false;
608-
}
609-
if (!robot.IsarConnected)
610-
{
611-
logger.LogWarning(
612-
"Mission run {MissionRunId} was not started as the robots {RobotId} isar instance is disconnected",
613-
missionRun.Id,
614-
robot.Id
615-
);
616-
return false;
617-
}
618-
if (robot.Deprecated)
619-
{
620-
logger.LogWarning(
621-
"Mission run {MissionRunId} was not started as the robot {RobotId} is deprecated",
622-
missionRun.Id,
623-
robot.Id
624-
);
625-
return false;
626-
}
627-
return true;
628-
}
629-
630593
public async Task AbortActiveReturnToHomeMission(string robotId)
631594
{
632595
var activeReturnToHomeMission =

0 commit comments

Comments
 (0)