Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mission run refactor #864

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ frontend/build/
.idea
*.log
*.vs
*.code-workspace

# Environment variables
*.env
Expand Down
5 changes: 4 additions & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ The access matrix looks like this:

| | **Read Only** | **User** | **Admin** |
| -------------------------- | ------------- | -------- | --------- |
| Asset Decks | Read | Read | CRUD |
| Area | Read | Read | CRUD |
| Deck | Read | Read | CRUD |
| Plant | Read | Read | CRUD |
| Installation | Read | Read | CRUD |
| Echo | Read | Read | CRUD |
| Missions | Read | Read | CRUD |
| Robots | Read | Read | CRUD |
Expand Down
226 changes: 185 additions & 41 deletions backend/api.test/EndpointTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,111 @@ public void Dispose()
GC.SuppressFinalize(this);
}

private async Task PopulateAreaDb(string installationCode, string plantCode, string deckName, string areaName)
{
string installationUrl = $"/installations";
string plantUrl = $"/plants";
string deckUrl = $"/decks";
string areaUrl = $"/areas";
var testPose = new Pose
{
Position = new Position
{
X = 1,
Y = 2,
Z = 2
},
Orientation = new Orientation
{
X = 0,
Y = 0,
Z = 0,
W = 1
}
};

var installationQuery = new CreateInstallationQuery
{
InstallationCode = installationCode,
Name = installationCode
};

var plantQuery = new CreatePlantQuery
{
InstallationCode = installationCode,
PlantCode = plantCode,
Name = plantCode
};

var deckQuery = new CreateDeckQuery
{
InstallationCode = installationCode,
PlantCode = plantCode,
Name = deckName
};

var areaQuery = new CreateAreaQuery
{
InstallationCode = installationCode,
PlantCode = plantCode,
DeckName = deckName,
AreaName = areaName,
DefaultLocalizationPose = testPose
};

var installationContent = new StringContent(
JsonSerializer.Serialize(installationQuery),
null,
"application/json"
);

var plantContent = new StringContent(
JsonSerializer.Serialize(plantQuery),
null,
"application/json"
);

var deckContent = new StringContent(
JsonSerializer.Serialize(deckQuery),
null,
"application/json"
);

var areaContent = new StringContent(
JsonSerializer.Serialize(areaQuery),
null,
"application/json"
);

// Act
var installationResponse = await _client.PostAsync(installationUrl, installationContent);
Assert.NotNull(installationResponse);
var plantResponse = await _client.PostAsync(plantUrl, plantContent);
Assert.NotNull(plantResponse);
var deckResponse = await _client.PostAsync(deckUrl, deckContent);
Assert.NotNull(deckResponse);
var areaResponse = await _client.PostAsync(areaUrl, areaContent);
Assert.NotNull(areaResponse);
}

#region MissionsController
[Fact]
public async Task MissionsTest()
{
string url = "/missions";
string url = "/missions/runs";
var response = await _client.GetAsync(url);
var missions = await response.Content.ReadFromJsonAsync<List<Mission>>(
var missionRuns = await response.Content.ReadFromJsonAsync<List<MissionRun>>(
_serializerOptions
);
Assert.True(response.IsSuccessStatusCode);
Assert.True(missions != null && missions.Count == 3);
Assert.True(missionRuns != null && missionRuns.Count == 3);
}

[Fact]
public async Task GetMissionById_ShouldReturnNotFound()
{
string missionId = "RandomString";
string url = "/missions/" + missionId;
string url = "/missions/runs/" + missionId;
var response = await _client.GetAsync(url);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
Expand All @@ -128,7 +215,7 @@ public async Task GetMissionById_ShouldReturnNotFound()
public async Task DeleteMission_ShouldReturnNotFound()
{
string missionId = "RandomString";
string url = "/missions/" + missionId;
string url = "/missions/runs/" + missionId;
var response = await _client.DeleteAsync(url);
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
Expand Down Expand Up @@ -177,45 +264,54 @@ public async Task GetRobotById_ShouldReturnRobot()
public async Task StartMissionTest()
{
// Arrange
string url = "/robots";
var response = await _client.GetAsync(url);
string robotUrl = "/robots";
string missionsUrl = "/missions";
var response = await _client.GetAsync(robotUrl);
Assert.True(response.IsSuccessStatusCode);
var robots = await response.Content.ReadFromJsonAsync<List<Robot>>(_serializerOptions);
Assert.True(robots != null);
var robot = robots[0];
string robotId = robot.Id;
string testInstallation = "TestInstallation";
string testArea = "testArea";
int echoMissionId = 95;

// Act
url = "/missions";
var query = new ScheduledMissionQuery
{
RobotId = robotId,
AssetCode = "test",
EchoMissionId = 95,
InstallationCode = testInstallation,
AreaName = testArea,
EchoMissionId = echoMissionId,
DesiredStartTime = DateTimeOffset.UtcNow
};
var content = new StringContent(
JsonSerializer.Serialize(query),
null,
"application/json"
);
response = await _client.PostAsync(url, content);
response = await _client.PostAsync(missionsUrl, content);

// Assert
Assert.True(response.IsSuccessStatusCode);
var mission = await response.Content.ReadFromJsonAsync<Mission>(_serializerOptions);
Assert.True(mission != null);
Assert.True(mission.Id != null);
Assert.True(mission.Status == MissionStatus.Pending);
var missionRun = await response.Content.ReadFromJsonAsync<MissionRun>(_serializerOptions);
Assert.True(missionRun != null);
Assert.True(missionRun.Id != null);
Assert.True(missionRun.Status == MissionStatus.Pending);
}

[Fact]
public async Task AssetDeckTest()
public async Task AreaTest()
{
// Arrange
string testAsset = "testAsset";
string testDeck = "testDeck";
string assetDeckUrl = $"/asset-decks";
string testInstallation = "TestInstallation";
string testPlant = "TestPlant";
string testDeck = "testDeck2";
string testArea = "testArea";
string installationUrl = $"/installations";
string plantUrl = $"/plants";
string deckUrl = $"/decks";
string areaUrl = $"/areas";
var testPose = new Pose
{
Position = new Position
Expand All @@ -233,35 +329,81 @@ public async Task AssetDeckTest()
}
};

var query = new CreateAssetDeckQuery
var installationQuery = new CreateInstallationQuery
{
InstallationCode = testInstallation,
Name = testInstallation
};

var plantQuery = new CreatePlantQuery
{
AssetCode = testAsset,
InstallationCode = testInstallation,
PlantCode = testPlant,
Name = testPlant
};

var deckQuery = new CreateDeckQuery
{
InstallationCode = testInstallation,
PlantCode = testPlant,
Name = testDeck
};

var areaQuery = new CreateAreaQuery
{
InstallationCode = testInstallation,
PlantCode = testPlant,
DeckName = testDeck,
AreaName = testArea,
DefaultLocalizationPose = testPose
};

var content = new StringContent(
JsonSerializer.Serialize(query),
var installationContent = new StringContent(
JsonSerializer.Serialize(installationQuery),
null,
"application/json"
);

var plantContent = new StringContent(
JsonSerializer.Serialize(plantQuery),
null,
"application/json"
);

var deckContent = new StringContent(
JsonSerializer.Serialize(deckQuery),
null,
"application/json"
);

var areaContent = new StringContent(
JsonSerializer.Serialize(areaQuery),
null,
"application/json"
);

// Act
var assetDeckResponse = await _client.PostAsync(assetDeckUrl, content);
var installationResponse = await _client.PostAsync(installationUrl, installationContent);
var plantResponse = await _client.PostAsync(plantUrl, plantContent);
var deckResponse = await _client.PostAsync(deckUrl, deckContent);
var areaResponse = await _client.PostAsync(areaUrl, areaContent);

// Assert
Assert.True(assetDeckResponse.IsSuccessStatusCode);
var assetDeck = await assetDeckResponse.Content.ReadFromJsonAsync<AssetDeck>(_serializerOptions);
Assert.True(assetDeck != null);
Assert.True(installationResponse.IsSuccessStatusCode);
Assert.True(plantResponse.IsSuccessStatusCode);
Assert.True(deckResponse.IsSuccessStatusCode);
Assert.True(areaResponse.IsSuccessStatusCode);
var area = await areaResponse.Content.ReadFromJsonAsync<Area>(_serializerOptions);
Assert.True(area != null);
}

[Fact]
public async Task SafePositionTest()
{
// Arrange - Add Safe Position
string testAsset = "testAsset";
string testDeck = "testDeck";
string addSafePositionUrl = $"/asset-decks/{testAsset}/{testDeck}/safe-position";
string testInstallation = "testInstallation";
string testArea = "testArea";
string addSafePositionUrl = $"/areas/{testInstallation}/{testArea}/safe-position";
var testPosition = new Position
{
X = 1,
Expand All @@ -284,10 +426,13 @@ public async Task SafePositionTest()
null,
"application/json"
);
var assetDeckResponse = await _client.PostAsync(addSafePositionUrl, content);
Assert.True(assetDeckResponse.IsSuccessStatusCode);
var assetDeck = await assetDeckResponse.Content.ReadFromJsonAsync<AssetDeck>(_serializerOptions);
Assert.True(assetDeck != null);

await PopulateAreaDb("testInstallation", "testPlant", "testDeck", "testArea");

var areaResponse = await _client.PostAsync(addSafePositionUrl, content);
Assert.True(areaResponse.IsSuccessStatusCode);
var area = await areaResponse.Content.ReadFromJsonAsync<Area>(_serializerOptions);
Assert.True(area != null);

// Arrange - Get a Robot
string url = "/robots";
Expand All @@ -299,15 +444,15 @@ public async Task SafePositionTest()
string robotId = robot.Id;

// Act
string goToSafePositionUrl = $"/robots/{robotId}/{testAsset}/{testDeck}/go-to-safe-position";
string goToSafePositionUrl = $"/robots/{robotId}/{testInstallation}/{testArea}/go-to-safe-position";
var missionResponse = await _client.PostAsync(goToSafePositionUrl, null);

// Assert
Assert.True(missionResponse.IsSuccessStatusCode);
var mission = await missionResponse.Content.ReadFromJsonAsync<Mission>(_serializerOptions);
Assert.True(mission != null);
var missionRun = await missionResponse.Content.ReadFromJsonAsync<MissionRun>(_serializerOptions);
Assert.True(missionRun != null);
Assert.True(
JsonSerializer.Serialize(mission.Tasks[0].RobotPose.Position) ==
JsonSerializer.Serialize(missionRun.Tasks[0].RobotPose.Position) ==
JsonSerializer.Serialize(testPosition)
);
}
Expand All @@ -322,11 +467,10 @@ public async Task GetMapMetadata()

foreach (string input in inputOutputPairs.Keys)
{
string assetDeckId = input;
string url = $"/asset-decks/{assetDeckId}/map-metadata";
string areaId = input;
string url = $"/areas/{areaId}/map-metadata";
var response = await _client.GetAsync(url);
Assert.Equal(inputOutputPairs[input], response.StatusCode);

}
}
}
Expand Down
Loading