forked from equinor/flotilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotModelControllerTests.cs
More file actions
168 lines (140 loc) · 6.02 KB
/
RobotModelControllerTests.cs
File metadata and controls
168 lines (140 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
using Api.Controllers.Models;
using Api.Database.Models;
using Api.Services;
using Api.Test.Database;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;
namespace Api.Test.Controllers;
public class RobotModelControllerTests : IAsyncLifetime
{
public required DatabaseUtilities DatabaseUtilities;
public required PostgreSqlContainer Container;
public required HttpClient Client;
public required JsonSerializerOptions SerializerOptions;
public required IRobotModelService RobotModelService;
public async Task InitializeAsync()
{
(Container, var connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory);
Client = TestSetupHelpers.ConfigureHttpClient(factory);
SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions();
DatabaseUtilities = new DatabaseUtilities(
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString)
);
RobotModelService = serviceProvider.GetRequiredService<IRobotModelService>();
}
public Task DisposeAsync() => Task.CompletedTask;
[Fact]
public async Task CheckThatListAllRobotModelsReturnsSuccess()
{
var response = await Client.GetAsync("/robot-models");
var robotModels = await response.Content.ReadFromJsonAsync<List<RobotModel>>(
SerializerOptions
);
// Seven models are added by default to the database
// This number must be changed if new robots are introduced
Assert.Equal(7, robotModels!.Count);
}
[Fact]
public async Task CheckThatLookupRobotModelByRobotTypeReturnsSuccess()
{
const RobotType RobotType = RobotType.Robot;
var response = await Client.GetAsync("/robot-models/type/" + RobotType);
var robotModel = await response.Content.ReadFromJsonAsync<RobotModel>(SerializerOptions);
Assert.Equal(RobotType, robotModel!.Type);
}
[Fact]
public async Task CheckThatLookupRobotModelByIdReturnsSuccess()
{
var robotModel = await RobotModelService.ReadByRobotType(RobotType.Robot);
var response = await Client.GetAsync("/robot-models/" + robotModel!.Id);
var robotModelFromResponse = await response.Content.ReadFromJsonAsync<RobotModel>(
SerializerOptions
);
Assert.Equal(robotModel.Id, robotModelFromResponse!.Id);
Assert.Equal(robotModel.Type, robotModelFromResponse!.Type);
}
[Fact]
public async Task CheckThatCreateRobotModelReturnsSuccess()
{
// Arrange
var modelBefore = await RobotModelService.ReadByRobotType(RobotType.Robot);
_ = await RobotModelService.Delete(modelBefore!.Id);
var query = new CreateRobotModelQuery { RobotType = RobotType.Robot };
var content = new StringContent(JsonSerializer.Serialize(query), null, "application/json");
// Act
var response = await Client.PostAsync("/robot-models", content);
// Assert
var modelAfter = await RobotModelService.ReadByRobotType(RobotType.Robot);
Assert.True(response.IsSuccessStatusCode);
Assert.NotEqual(modelBefore!.Id, modelAfter!.Id);
}
[Theory]
[InlineData(10, 90, 10, 50, true)]
[InlineData(-1, -10, 10, 50, false)]
[InlineData(110, 1000, 10, 900, false)]
[InlineData(0, 9000, 0, 100, true)]
public async Task CheckThatUpdatingRobotBasedOnIdIsSuccessful(
int batteryWarningThreshold,
int upperPressureWarningThreshold,
int lowerPressureWarningThreshold,
int batteryMissionStartThreshold,
bool valuesShouldHaveBeenChanged
)
{
// Arrange
var modelBefore = await RobotModelService.ReadByRobotType(RobotType.Robot);
var query = new UpdateRobotModelQuery
{
BatteryWarningThreshold = batteryWarningThreshold,
UpperPressureWarningThreshold = upperPressureWarningThreshold,
LowerPressureWarningThreshold = lowerPressureWarningThreshold,
BatteryMissionStartThreshold = batteryMissionStartThreshold,
};
var content = new StringContent(JsonSerializer.Serialize(query), null, "application/json");
// Act
_ = await Client.PutAsync("/robot-models/" + modelBefore!.Id, content);
// Assert
var modelAfter = await RobotModelService.ReadByRobotType(RobotType.Robot);
if (valuesShouldHaveBeenChanged)
{
Assert.Equal(modelAfter!.BatteryWarningThreshold, batteryWarningThreshold);
Assert.Equal(modelAfter!.UpperPressureWarningThreshold, upperPressureWarningThreshold);
Assert.Equal(modelAfter!.LowerPressureWarningThreshold, lowerPressureWarningThreshold);
Assert.Equal(modelAfter!.BatteryMissionStartThreshold, batteryMissionStartThreshold);
}
else
{
Assert.Equal(modelAfter!.BatteryWarningThreshold, modelBefore!.BatteryWarningThreshold);
Assert.Equal(
modelAfter!.UpperPressureWarningThreshold,
modelBefore!.UpperPressureWarningThreshold
);
Assert.Equal(
modelAfter!.LowerPressureWarningThreshold,
modelBefore!.LowerPressureWarningThreshold
);
Assert.Equal(
modelAfter!.BatteryMissionStartThreshold,
modelBefore!.BatteryMissionStartThreshold
);
}
}
[Fact]
public async Task UpdateRobotModelByIdFailsWithValidId()
{
var testId = "550e8400-e29b-41d4-a716-446655440000";
var response = await Client.GetAsync("/robot-models/" + testId);
response.EnsureSuccessStatusCode();
}
}