Skip to content

Commit

Permalink
Remove area from robot query
Browse files Browse the repository at this point in the history
since area should only be set during locaization
  • Loading branch information
tsundvoll committed Dec 12, 2023
1 parent a46a11e commit 255bc5b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 77 deletions.
42 changes: 0 additions & 42 deletions backend/api.test/Client/RobotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,47 +76,6 @@ public async Task GetRobotById_ShouldReturnRobot()
Assert.Equal(robot.Id, robotId);
}

[Fact]
public async Task RobotIsCreatedWithCurrentArea()
{
// Area
string areaUrl = "/areas";
var areaResponse = await _client.GetAsync(areaUrl);
Assert.True(areaResponse.IsSuccessStatusCode);
var areas = await areaResponse.Content.ReadFromJsonAsync<List<AreaResponse>>(_serializerOptions);
Assert.True(areas != null);
var area = areas[0];

// Arrange - Create robot
var robotQuery = new CreateRobotQuery
{
IsarId = Guid.NewGuid().ToString(),
Name = "RobotGetNextRun",
SerialNumber = "GetNextRun",
RobotType = RobotType.Robot,
Status = RobotStatus.Available,
Enabled = true,
Host = "localhost",
Port = 3000,
CurrentInstallationCode = area.InstallationCode,
CurrentAreaName = area.AreaName,
VideoStreams = new List<CreateVideoStreamQuery>()
};

string robotUrl = "/robots";
var content = new StringContent(
JsonSerializer.Serialize(robotQuery),
null,
"application/json"
);
var response = await _client.PostAsync(robotUrl, content);
Assert.True(response != null, $"Failed to post to {robotUrl}. Null returned");
Assert.True(response.IsSuccessStatusCode, $"Failed to post to {robotUrl}. Status code: {response.StatusCode}");
var robot = await response.Content.ReadFromJsonAsync<RobotResponse>(_serializerOptions);
Assert.True(robot != null, $"No object returned from post to {robotUrl}");
Assert.True(robot.CurrentArea?.Id == area.Id);

}

[Fact]
public async Task RobotIsNotCreatedWithAreaNotInInstallation()
Expand Down Expand Up @@ -161,7 +120,6 @@ public async Task RobotIsNotCreatedWithAreaNotInInstallation()
Host = "localhost",
Port = 3000,
CurrentInstallationCode = wrongInstallation.InstallationCode,
CurrentAreaName = area.AreaName,
VideoStreams = new List<CreateVideoStreamQuery>()
};

Expand Down
5 changes: 2 additions & 3 deletions backend/api.test/Database/DatabaseUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DatabaseUtilities(FlotillaDbContext context)
_areaService = new AreaService(context, _installationService, _plantService, _deckService, defaultLocalizationPoseService, _accessRoleService);
_missionRunService = new MissionRunService(context, new MockSignalRService(), new Mock<ILogger<MissionRunService>>().Object, _accessRoleService);
_robotModelService = new RobotModelService(context);
_robotService = new RobotService(context, new Mock<ILogger<RobotService>>().Object, _robotModelService, new MockSignalRService(), _accessRoleService, _installationService, _areaService);
_robotService = new RobotService(context, new Mock<ILogger<RobotService>>().Object, _robotModelService, new MockSignalRService(), _accessRoleService, _installationService);
}

public void Dispose()
Expand Down Expand Up @@ -116,7 +116,7 @@ public async Task<Area> NewArea(string installationCode, string plantCode, strin
return await _areaService.Create(createAreaQuery);
}

public async Task<Robot> NewRobot(RobotStatus status, Area area, Installation installation)
public async Task<Robot> NewRobot(RobotStatus status, Installation installation)
{
var createRobotQuery = new CreateRobotQuery
{
Expand All @@ -125,7 +125,6 @@ public async Task<Robot> NewRobot(RobotStatus status, Area area, Installation in
RobotType = RobotType.Robot,
SerialNumber = "0001",
CurrentInstallationCode = installation.InstallationCode,
CurrentAreaName = area.Name,
VideoStreams = new List<CreateVideoStreamQuery>(),
Host = "localhost",
Port = 3000,
Expand Down
14 changes: 7 additions & 7 deletions backend/api.test/EventHandlers/TestMissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public TestMissionEventHandler(DatabaseFixture fixture)
_plantService = new PlantService(_context, _installationService, _accessRoleService);
_deckService = new DeckService(_context, _defaultLocalisationPoseService, _installationService, _plantService, _accessRoleService);
_areaService = new AreaService(_context, _installationService, _plantService, _deckService, _defaultLocalisationPoseService, _accessRoleService);
_robotService = new RobotService(_context, robotServiceLogger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
_robotService = new RobotService(_context, robotServiceLogger, _robotModelService, _signalRService, _accessRoleService, _installationService);
_missionSchedulingService = new MissionSchedulingService(missionSchedulingServiceLogger, _missionRunService, _robotService, _robotControllerMock.Mock.Object, _areaService,
_isarServiceMock);

Expand Down Expand Up @@ -125,7 +125,7 @@ public async void ScheduledMissionStartedWhenSystemIsAvailable()
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, installation);
var robot = await _databaseUtilities.NewRobot(RobotStatus.Available, installation);
var missionRun = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false);

SetupMocksForRobotController(robot, missionRun);
Expand All @@ -146,7 +146,7 @@ public async void SecondScheduledMissionQueuedIfRobotIsBusy()
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, installation);
var robot = await _databaseUtilities.NewRobot(RobotStatus.Available, installation);
var missionRunOne = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false);
var missionRunTwo = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false);

Expand All @@ -171,7 +171,7 @@ public async void NewMissionIsStartedWhenRobotBecomesAvailable()
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, installation);
var robot = await _databaseUtilities.NewRobot(RobotStatus.Busy, installation);
var missionRun = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false);

SetupMocksForRobotController(robot, missionRun);
Expand Down Expand Up @@ -208,7 +208,7 @@ public async void NoMissionIsStartedIfQueueIsEmptyWhenRobotBecomesAvailable()
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, installation);
var robot = await _databaseUtilities.NewRobot(RobotStatus.Busy, installation);

var mqttEventArgs = new MqttReceivedArgs(
new IsarRobotStatusMessage
Expand Down Expand Up @@ -248,8 +248,8 @@ public async void MissionRunIsStartedForOtherAvailableRobotIfOneRobotHasAnOngoin
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, installation);
var robotTwo = await _databaseUtilities.NewRobot(RobotStatus.Available, area, installation);
var robotOne = await _databaseUtilities.NewRobot(RobotStatus.Available, installation);
var robotTwo = await _databaseUtilities.NewRobot(RobotStatus.Available, installation);
var missionRunOne = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robotOne, area, false);
var missionRunTwo = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robotTwo, area, false);

Expand Down
2 changes: 1 addition & 1 deletion backend/api.test/Services/MissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task Create()
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, installation);
var robot = await _databaseUtilities.NewRobot(RobotStatus.Available, installation);
var missionRun = await _databaseUtilities.NewMissionRun(installation.InstallationCode, robot, area, false);

await _missionRunService.Create(missionRun);
Expand Down
16 changes: 4 additions & 12 deletions backend/api.test/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public class RobotServiceTest : IDisposable
private readonly ISignalRService _signalRService;
private readonly IAccessRoleService _accessRoleService;
private readonly IInstallationService _installationService;
private readonly IPlantService _plantService;
private readonly IDefaultLocalizationPoseService _defaultLocalizationPoseService;
private readonly IDeckService _deckService;
private readonly IAreaService _areaService;

public RobotServiceTest(DatabaseFixture fixture)
{
Expand All @@ -34,10 +30,6 @@ public RobotServiceTest(DatabaseFixture fixture)
_signalRService = new MockSignalRService();
_accessRoleService = new AccessRoleService(_context, new HttpContextAccessor());
_installationService = new InstallationService(_context, _accessRoleService);
_plantService = new PlantService(_context, _installationService, _accessRoleService);
_defaultLocalizationPoseService = new DefaultLocalizationPoseService(_context);
_deckService = new DeckService(_context, _defaultLocalizationPoseService, _installationService, _plantService, _accessRoleService);
_areaService = new AreaService(_context, _installationService, _plantService, _deckService, _defaultLocalizationPoseService, _accessRoleService);
}

public void Dispose()
Expand All @@ -49,7 +41,7 @@ public void Dispose()
[Fact]
public async Task ReadAll()
{
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService);
var robots = await robotService.ReadAll();

Assert.True(robots.Any());
Expand All @@ -58,7 +50,7 @@ public async Task ReadAll()
[Fact]
public async Task Read()
{
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService);
var robots = await robotService.ReadAll();
var firstRobot = robots.First();
var robotById = await robotService.ReadById(firstRobot.Id);
Expand All @@ -69,15 +61,15 @@ public async Task Read()
[Fact]
public async Task ReadIdDoesNotExist()
{
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService);
var robot = await robotService.ReadById("some_id_that_does_not_exist");
Assert.Null(robot);
}

[Fact]
public async Task Create()
{
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService, _areaService);
var robotService = new RobotService(_context, _logger, _robotModelService, _signalRService, _accessRoleService, _installationService);
var installationService = new InstallationService(_context, _accessRoleService);

var installation = await installationService.Create(new CreateInstallationQuery
Expand Down
2 changes: 0 additions & 2 deletions backend/api/Controllers/Models/CreateRobotQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public struct CreateRobotQuery

public string CurrentInstallationCode { get; set; }

public string CurrentAreaName { get; set; }

public IList<CreateVideoStreamQuery> VideoStreams { get; set; }

public string Host { get; set; }
Expand Down
12 changes: 2 additions & 10 deletions backend/api/Services/RobotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public class RobotService(FlotillaDbContext context,
IRobotModelService robotModelService,
ISignalRService signalRService,
IAccessRoleService accessRoleService,
IInstallationService installationService,
IAreaService areaService) : IRobotService, IDisposable
IInstallationService installationService) : IRobotService, IDisposable
{
private readonly Semaphore _robotSemaphore = new(1, 1);

Expand Down Expand Up @@ -70,14 +69,7 @@ public async Task<Robot> CreateFromQuery(CreateRobotQuery robotQuery)
throw new DbUpdateException($"Could not create new robot in database as installation {robotQuery.CurrentInstallationCode} doesn't exist");
}

var area = await areaService.ReadByInstallationAndName(robotQuery.CurrentInstallationCode, robotQuery.CurrentAreaName);
if (area is null && robotQuery.CurrentAreaName is not null)
{
logger.LogError("Area '{AreaName}' does not exist in installation {CurrentInstallation}", robotQuery.CurrentAreaName, robotQuery.CurrentInstallationCode);
throw new DbUpdateException($"Could not create new robot in database as area '{robotQuery.CurrentAreaName}' does not exist in installation {robotQuery.CurrentInstallationCode}");
}

var newRobot = new Robot(robotQuery, installation, area)
var newRobot = new Robot(robotQuery, installation)
{
Model = robotModel
};
Expand Down

0 comments on commit 255bc5b

Please sign in to comment.