forked from equinor/flotilla
-
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.
Refactor database operations to utilities class
- Loading branch information
Showing
2 changed files
with
178 additions
and
125 deletions.
There are no files selected for viewing
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,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<ILogger<MissionRunService>>().Object); | ||
_robotModelService = new RobotModelService(context); | ||
_robotService = new RobotService(context, new Mock<ILogger<RobotService>>().Object, _robotModelService, new MockSignalRService()); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_robotService.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public async Task<MissionRun> 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<Installation> NewInstallation() | ||
{ | ||
var createInstallationQuery = new CreateInstallationQuery | ||
{ | ||
InstallationCode = "testInstallationCode", | ||
Name = "testInstallation" | ||
}; | ||
|
||
return await _installationService.Create(createInstallationQuery); | ||
} | ||
|
||
public async Task<Plant> NewPlant(string installationCode) | ||
{ | ||
var createPlantQuery = new CreatePlantQuery | ||
{ | ||
InstallationCode = installationCode, | ||
PlantCode = "testPlantCode", | ||
Name = "testPlant" | ||
}; | ||
|
||
return await _plantService.Create(createPlantQuery); | ||
} | ||
|
||
public async Task<Deck> NewDeck(string installationCode, string plantCode) | ||
{ | ||
var createDeckQuery = new CreateDeckQuery | ||
{ | ||
InstallationCode = installationCode, | ||
PlantCode = plantCode, | ||
Name = "testDeck" | ||
}; | ||
|
||
return await _deckService.Create(createDeckQuery); | ||
} | ||
|
||
public async Task<Area> 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<Robot> 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<CreateVideoStreamQuery>(), | ||
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); | ||
} | ||
} | ||
} |
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