From df1ce89b84e88e10b76c63970fa847464249d86e Mon Sep 17 00:00:00 2001 From: aestene Date: Mon, 20 Nov 2023 10:44:54 +0100 Subject: [PATCH] Refactor database operations to utilities class --- .../api.test/Database/DatabaseUtilities.cs | 141 +++++++++++++++ .../EventHandlers/TestMissionEventHandler.cs | 162 ++++-------------- 2 files changed, 178 insertions(+), 125 deletions(-) create mode 100644 backend/api.test/Database/DatabaseUtilities.cs diff --git a/backend/api.test/Database/DatabaseUtilities.cs b/backend/api.test/Database/DatabaseUtilities.cs new file mode 100644 index 000000000..b7bba6db7 --- /dev/null +++ b/backend/api.test/Database/DatabaseUtilities.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Api.Controllers.Models; +using Api.Database.Context; +using Api.Database.Models; +using Api.Services; +using Microsoft.Extensions.Logging; +using Moq; +namespace Api.Test.Database +{ + public class DatabaseUtilities : IDisposable + { + private readonly AreaService _areaService; + private readonly DeckService _deckService; + private readonly InstallationService _installationService; + private readonly MissionRunService _missionRunService; + private readonly PlantService _plantService; + private readonly RobotModelService _robotModelService; + private readonly RobotService _robotService; + + public DatabaseUtilities(FlotillaDbContext context) + { + var defaultLocalizationPoseService = new DefaultLocalizationPoseService(context); + + _installationService = new InstallationService(context); + _plantService = new PlantService(context, _installationService); + _deckService = new DeckService(context, defaultLocalizationPoseService, _installationService, _plantService); + _areaService = new AreaService(context, _installationService, _plantService, _deckService, defaultLocalizationPoseService); + _missionRunService = new MissionRunService(context, new MockSignalRService(), new Mock>().Object); + _robotModelService = new RobotModelService(context); + _robotService = new RobotService(context, new Mock>().Object, _robotModelService, new MockSignalRService()); + } + + public void Dispose() + { + _robotService.Dispose(); + GC.SuppressFinalize(this); + } + + public async Task NewMissionRun( + string installationCode, + Robot robot, + Area? area, + bool writeToDatabase = true + ) + { + var missionRun = new MissionRun + { + Name = "testMission", + Robot = robot, + MissionId = null, + MissionRunPriority = MissionRunPriority.Normal, + Status = MissionStatus.Pending, + DesiredStartTime = DateTime.Now, + Area = area, + Map = new MapMetadata(), + InstallationCode = installationCode + }; + if (writeToDatabase) + { + return await _missionRunService.Create(missionRun); + } + return missionRun; + } + + public async Task NewInstallation() + { + var createInstallationQuery = new CreateInstallationQuery + { + InstallationCode = "testInstallationCode", + Name = "testInstallation" + }; + + return await _installationService.Create(createInstallationQuery); + } + + public async Task NewPlant(string installationCode) + { + var createPlantQuery = new CreatePlantQuery + { + InstallationCode = installationCode, + PlantCode = "testPlantCode", + Name = "testPlant" + }; + + return await _plantService.Create(createPlantQuery); + } + + public async Task NewDeck(string installationCode, string plantCode) + { + var createDeckQuery = new CreateDeckQuery + { + InstallationCode = installationCode, + PlantCode = plantCode, + Name = "testDeck" + }; + + return await _deckService.Create(createDeckQuery); + } + + public async Task NewArea(string installationCode, string plantCode, string deckName) + { + var createAreaQuery = new CreateAreaQuery + { + InstallationCode = installationCode, + PlantCode = plantCode, + DeckName = deckName, + AreaName = "testArea", + DefaultLocalizationPose = new Pose() + }; + + return await _areaService.Create(createAreaQuery); + } + + public async Task NewRobot(RobotStatus status, Area area) + { + var createRobotQuery = new CreateRobotQuery + { + Name = "TestBot", + IsarId = Guid.NewGuid().ToString(), + RobotType = RobotType.Robot, + SerialNumber = "0001", + CurrentInstallation = "kaa", + CurrentArea = area, + VideoStreams = new List(), + Host = "localhost", + Port = 3000, + Enabled = true, + Status = status + }; + + var robotModel = await _robotModelService.ReadByRobotType(createRobotQuery.RobotType); + var robot = new Robot(createRobotQuery) + { + Model = robotModel! + }; + return await _robotService.Create(robot); + } + } +} diff --git a/backend/api.test/EventHandlers/TestMissionEventHandler.cs b/backend/api.test/EventHandlers/TestMissionEventHandler.cs index dae5aadf5..496ee3643 100644 --- a/backend/api.test/EventHandlers/TestMissionEventHandler.cs +++ b/backend/api.test/EventHandlers/TestMissionEventHandler.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using Api.Controllers; using Api.Controllers.Models; using Api.Database.Context; @@ -12,12 +11,14 @@ using Api.Mqtt.MessageModels; using Api.Services; using Api.Services.Models; +using Api.Test.Database; using Api.Test.Mocks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Moq; using Xunit; + namespace Api.Test.EventHandlers { [Collection("Database collection")] @@ -44,6 +45,7 @@ public class TestMissionEventHandler : IDisposable private readonly RobotModelService _robotModelService; private readonly RobotService _robotService; private readonly ISignalRService _signalRService; + private readonly DatabaseUtilities _databaseUtilities; public TestMissionEventHandler(DatabaseFixture fixture) { @@ -74,6 +76,8 @@ public TestMissionEventHandler(DatabaseFixture fixture) _missionSchedulingService = new MissionSchedulingService(missionSchedulingServiceLogger, _missionRunService, _robotService, _robotControllerMock.Mock.Object, _areaService, _isarServiceMock); + _databaseUtilities = new DatabaseUtilities(_context); + var mockServiceProvider = new Mock(); // Mock services and controllers that are passed through the mocked service injector @@ -110,108 +114,16 @@ public void Dispose() GC.SuppressFinalize(this); } - private async Task NewMissionRun(string installationCode, Robot robot, Area? area, bool writeToDatabase = true) - { - var missionRun = new MissionRun - { - Name = "testMission", - Robot = robot, - MissionId = null, - MissionRunPriority = MissionRunPriority.Normal, - Status = MissionStatus.Pending, - DesiredStartTime = DateTime.Now, - Area = area, - Map = new MapMetadata(), - InstallationCode = installationCode - }; - if (writeToDatabase) { return await _missionRunService.Create(missionRun); } - return missionRun; - } - - private async Task NewInstallation() - { - var createInstallationQuery = new CreateInstallationQuery - { - InstallationCode = "testInstallationCode", - Name = "testInstallation" - }; - - return await _installationService.Create(createInstallationQuery); - } - - private async Task NewPlant(string installationCode) - { - var createPlantQuery = new CreatePlantQuery - { - InstallationCode = installationCode, - PlantCode = "testPlantCode", - Name = "testPlant" - }; - - return await _plantService.Create(createPlantQuery); - } - - private async Task NewDeck(string installationCode, string plantCode) - { - var createDeckQuery = new CreateDeckQuery - { - InstallationCode = installationCode, - PlantCode = plantCode, - Name = "testDeck" - }; - - return await _deckService.Create(createDeckQuery); - } - - private async Task NewArea(string installationCode, string plantCode, string deckName) - { - var createAreaQuery = new CreateAreaQuery - { - InstallationCode = installationCode, - PlantCode = plantCode, - DeckName = deckName, - AreaName = "testArea", - DefaultLocalizationPose = new Pose() - }; - - return await _areaService.Create(createAreaQuery); - } - - private async Task NewRobot(RobotStatus status, Area area) - { - var createRobotQuery = new CreateRobotQuery - { - Name = "TestBot", - IsarId = Guid.NewGuid().ToString(), - RobotType = RobotType.Robot, - SerialNumber = "0001", - CurrentInstallation = "kaa", - CurrentArea = area, - VideoStreams = new List(), - Host = "localhost", - Port = 3000, - Enabled = true, - Status = status - }; - - var robotModel = await _robotModelService.ReadByRobotType(createRobotQuery.RobotType); - var robot = new Robot(createRobotQuery) - { - Model = robotModel! - }; - return await _robotService.Create(robot); - } - [Fact] public async void ScheduledMissionStartedWhenSystemIsAvailable() { // Arrange - var installation = await NewInstallation(); - var plant = await NewPlant(installation.InstallationCode); - var deck = await NewDeck(installation.InstallationCode, plant.PlantCode); - var area = await NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); - var robot = await NewRobot(RobotStatus.Available, area); - var missionRun = await NewMissionRun(installation.InstallationCode, robot, area, false); + var installation = await _databaseUtilities.NewInstallation(); + var plant = await _databaseUtilities.NewPlant(installation.InstallationCode); + var deck = await _databaseUtilities.NewDeck(installation.InstallationCode, plant.PlantCode); + var area = await _databaseUtilities.NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); + var robot = await _databaseUtilities.NewRobot(RobotStatus.Available, area); + var missionRun = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false); SetupMocksForRobotController(robot, missionRun); @@ -227,13 +139,13 @@ public async void ScheduledMissionStartedWhenSystemIsAvailable() public async void SecondScheduledMissionQueuedIfRobotIsBusy() { // Arrange - var installation = await NewInstallation(); - var plant = await NewPlant(installation.InstallationCode); - var deck = await NewDeck(installation.InstallationCode, plant.PlantCode); - var area = await NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); - var robot = await NewRobot(RobotStatus.Available, area); - var missionRunOne = await NewMissionRun(installation.InstallationCode, robot, area, false); - var missionRunTwo = await NewMissionRun(installation.InstallationCode, robot, area, false); + var installation = await _databaseUtilities.NewInstallation(); + var plant = await _databaseUtilities.NewPlant(installation.InstallationCode); + var deck = await _databaseUtilities.NewDeck(installation.InstallationCode, plant.PlantCode); + var area = await _databaseUtilities.NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); + var robot = await _databaseUtilities.NewRobot(RobotStatus.Available, area); + var missionRunOne = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false); + var missionRunTwo = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false); SetupMocksForRobotController(robot, missionRunOne); @@ -252,12 +164,12 @@ public async void SecondScheduledMissionQueuedIfRobotIsBusy() public async void NewMissionIsStartedWhenRobotBecomesAvailable() { // Arrange - var installation = await NewInstallation(); - var plant = await NewPlant(installation.InstallationCode); - var deck = await NewDeck(installation.InstallationCode, plant.PlantCode); - var area = await NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); - var robot = await NewRobot(RobotStatus.Busy, area); - var missionRun = await NewMissionRun(installation.InstallationCode, robot, area, false); + var installation = await _databaseUtilities.NewInstallation(); + var plant = await _databaseUtilities.NewPlant(installation.InstallationCode); + var deck = await _databaseUtilities.NewDeck(installation.InstallationCode, plant.PlantCode); + var area = await _databaseUtilities.NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); + var robot = await _databaseUtilities.NewRobot(RobotStatus.Busy, area); + var missionRun = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false); SetupMocksForRobotController(robot, missionRun); @@ -289,11 +201,11 @@ public async void NewMissionIsStartedWhenRobotBecomesAvailable() public async void NoMissionIsStartedIfQueueIsEmptyWhenRobotBecomesAvailable() { // Arrange - var installation = await NewInstallation(); - var plant = await NewPlant(installation.InstallationCode); - var deck = await NewDeck(installation.InstallationCode, plant.PlantCode); - var area = await NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); - var robot = await NewRobot(RobotStatus.Busy, area); + var installation = await _databaseUtilities.NewInstallation(); + var plant = await _databaseUtilities.NewPlant(installation.InstallationCode); + var deck = await _databaseUtilities.NewDeck(installation.InstallationCode, plant.PlantCode); + var area = await _databaseUtilities.NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); + var robot = await _databaseUtilities.NewRobot(RobotStatus.Busy, area); var mqttEventArgs = new MqttReceivedArgs( new IsarRobotStatusMessage @@ -329,14 +241,14 @@ public async void NoMissionIsStartedIfQueueIsEmptyWhenRobotBecomesAvailable() public async void MissionRunIsStartedForOtherAvailableRobotIfOneRobotHasAnOngoingMissionRun() { // Arrange - var installation = await NewInstallation(); - var plant = await NewPlant(installation.InstallationCode); - var deck = await NewDeck(installation.InstallationCode, plant.PlantCode); - var area = await NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); - var robotOne = await NewRobot(RobotStatus.Available, area); - var robotTwo = await NewRobot(RobotStatus.Available, area); - var missionRunOne = await NewMissionRun(installation.InstallationCode, robotOne, area, false); - var missionRunTwo = await NewMissionRun(installation.InstallationCode, robotTwo, area, false); + var installation = await _databaseUtilities.NewInstallation(); + var plant = await _databaseUtilities.NewPlant(installation.InstallationCode); + var deck = await _databaseUtilities.NewDeck(installation.InstallationCode, plant.PlantCode); + var area = await _databaseUtilities.NewArea(installation.InstallationCode, plant.PlantCode, deck.Name); + var robotOne = await _databaseUtilities.NewRobot(RobotStatus.Available, area); + var robotTwo = await _databaseUtilities.NewRobot(RobotStatus.Available, area); + var missionRunOne = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robotOne, area, false); + var missionRunTwo = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robotTwo, area, false); SetupMocksForRobotController(robotOne, missionRunOne);