-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTenantServiceTestSuite.cs
More file actions
330 lines (267 loc) · 14.8 KB
/
Copy pathTenantServiceTestSuite.cs
File metadata and controls
330 lines (267 loc) · 14.8 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using Automation.UI.Models.ApiHealth;
using Automation.UI.Services.ApiHealth.Seeding;
using LantanaGroup.Link.Sdk.Clients;
using LantanaGroup.Link.Shared.Application.Enums;
using LantanaGroup.Link.Shared.Application.Models.Tenant;
using StepNames = Automation.UI.Services.ApiHealth.TestSuites.ApiEndPointLibrary.TenantSteps;
namespace Automation.UI.Services.ApiHealth.TestSuites;
/// <summary>
/// Exercises Tenant facility and Vendor service endpoints via LinkSdk.
/// Tests the full CRUD lifecycle plus soft-delete/restore, search, and error paths.
/// </summary>
public sealed class TenantServiceTestSuite : ServiceTestSuiteBase
{
private readonly IFacilityServiceClient _client;
private readonly IReportServiceClient _reportClient;
private readonly IApiHealthSeedContextAccessor _seedContext;
public override string ServiceName => "Tenant";
public TenantServiceTestSuite(
IFacilityServiceClient client,
IReportServiceClient reportClient,
IApiHealthSeedContextAccessor seedContext)
{
_client = client;
_reportClient = reportClient;
_seedContext = seedContext;
}
public override IReadOnlyList<ApiHealthSeedRequirement> GetSeedRequirements() =>
[
ApiHealthSeedRequirement.ReportSchedule
];
public override IReadOnlyList<ApiEndpointDefinition> GetEndpointDefinitions() =>
ApiEndPointLibrary.GetServiceEndpoints(ServiceName);
public override async Task<IReadOnlyList<ApiTestRunResult>> ExecuteAsync(CancellationToken ct = default)
{
var results = new List<ApiTestRunResult>();
var testVendor = new VendorModel { Name = $"ApiHealth-Vendor-{Guid.NewGuid():N}" };
FacilityModel BuildFacility(
string id,
string? name = null,
VendorModel? vendor = null,
bool allowNullName = false) => new()
{
FacilityId = id,
FacilityName = allowNullName ? name : name ?? id,
TimeZone = "America/Chicago",
Vendor = vendor,
ScheduledReports = new TenantScheduledReportConfig { Daily = [], Weekly = [], Monthly = [] }
};
AdHocReportRequest BuildAdhoc(
List<string>? reportTypes,
DateTime? startDate,
DateTime? endDate) => new()
{
ReportTypes = reportTypes ?? [],
StartDate = startDate,
EndDate = endDate
};
var facilityId = $"ApiHealth-Tenant-{Guid.NewGuid():N}";
var fakeFacilityId = $"ApiHealth-Tenant-Fake-{Guid.NewGuid():N}";
var created = false;
var vendorName = $"ApiHealth-Vendor-{Guid.NewGuid():N}";
var vendorCreated = false;
var vendorId = Guid.Empty;
var seededFacilityId = _seedContext.Current?.Report?.FacilityId;
var seededScheduleId = _seedContext.Current?.Report?.ScheduleId;
string? seededReportType = null;
const string seededUnavailable = "Seeded facility/report metadata was unavailable for Tenant seeded success-path checks.";
if (!string.IsNullOrWhiteSpace(seededScheduleId))
{
var seededSchedule = await _reportClient.GetScheduleAsync(seededScheduleId, ct);
if (seededSchedule.IsSuccessStatusCode)
seededReportType = seededSchedule.Body?.ReportTypes?.FirstOrDefault();
}
try
{
// === POST /api/Facility ===
// Create → 201
results.Add(await RunStepAsync(StepNames.Create201, 201, async () =>
{
var model = BuildFacility(facilityId, vendor: testVendor);
var result = await _client.CreateAsync(model, ct);
if (result.IsSuccessStatusCode) created = true;
return result;
}, ct: ct));
// Create → 400 (duplicate)
results.Add(await RunStepAsync(StepNames.Create400Duplicate, 400, async () =>
await _client.CreateAsync(BuildFacility(facilityId, vendor: testVendor), ct), ct: ct));
// Create → 400 (no name)
results.Add(await RunStepAsync(StepNames.Create400NoName, 400, async () =>
await _client.CreateAsync(BuildFacility($"ApiHealth-NoName-{Guid.NewGuid():N}", name: null, allowNullName: true, vendor: testVendor), ct), ct: ct));
// === POST /api/Vendor ===
// Vendor POST → 201
results.Add(await RunStepAsync(StepNames.VendorPost201, 201, async () =>
{
var response = await _client.CreateVendorAsync(new CreateVendorModel { Name = vendorName }, ct);
if (response.IsSuccessStatusCode)
{
vendorId = response.Body?.Id ?? Guid.Empty;
vendorCreated = vendorId != Guid.Empty;
}
return response;
}, ct: ct));
// Vendor POST → 409 (duplicate)
results.Add(await RunStepAsync(StepNames.VendorPost409, 409, async () =>
await _client.CreateVendorAsync(new CreateVendorModel { Name = vendorName }, ct), ct: ct));
// Vendor GET → 200
results.Add(await RunStepAsync(StepNames.VendorGet200, 200, async () =>
{
if (vendorId == Guid.Empty)
throw new InvalidOperationException("Expected a vendor id after creating the Vendor.");
return await _client.GetVendorAsync(vendorId, ct);
}, ct: ct));
// Vendors GET → 200
results.Add(await RunStepAsync(StepNames.VendorsGet200, 200, async () =>
await _client.GetVendorsAsync(ct), ct: ct));
// === GET /api/Facility (search) ===
// Search → 200
results.Add(await RunStepAsync(StepNames.Search200, 200, async () =>
await _client.SearchFacilitiesAsync(facilityId, cancellationToken: ct), ct: ct));
// Search → 204 (no results)
results.Add(await RunStepAsync(StepNames.Search204NoResults, 204, async () =>
await _client.SearchFacilitiesAsync($"NonExistent-{Guid.NewGuid():N}", cancellationToken: ct), ct: ct));
// === GET /api/Facility/list ===
results.Add(await RunStepAsync(StepNames.List200, 200, async () =>
await _client.GetFacilityListAsync(search: facilityId, includeDeleted: false, cancellationToken: ct), ct: ct));
results.Add(await RunStepAsync(StepNames.List204, 204, async () =>
await _client.GetFacilityListAsync(search: $"NonExistent-{Guid.NewGuid():N}", includeDeleted: false, cancellationToken: ct), ct: ct));
// === GET /api/Facility/{id} ===
// Get → 200
results.Add(await RunStepAsync(StepNames.Get200, 200, async () =>
await _client.GetAsync(facilityId, ct), ct: ct));
// Get → 404
results.Add(await RunStepAsync(StepNames.Get404, 404, async () =>
await _client.GetAsync(fakeFacilityId, ct), ct: ct));
// === PUT /api/Facility/{id} ===
// Update → 200
results.Add(await RunStepAsync(StepNames.Update200, 200, async () =>
{
var updated = new FacilityModel
{
FacilityId = facilityId,
FacilityName = facilityId + "-Updated",
TimeZone = "America/Chicago",
Vendor = new VendorModel { Name = "Epic" },
ScheduledReports = new TenantScheduledReportConfig { Daily = [], Weekly = [], Monthly = [] }
};
return await _client.UpdateAsync(facilityId, updated, ct);
}, ct: ct));
// Update → 404 (non-existent)
results.Add(await RunStepAsync(StepNames.Update404NonExistent, 404, async () =>
await _client.UpdateAsync(fakeFacilityId, BuildFacility(fakeFacilityId), ct), ct: ct));
// === GET /api/Facility/{id} (exists check) ===
// CheckExists → 200
results.Add(await RunStepAsync(StepNames.CheckExists200, 200, async () =>
await _client.CheckFacilityExistsAsync(facilityId, ct), ct: ct));
// CheckExists → 404
results.Add(await RunStepAsync(StepNames.CheckExists404, 404, async () =>
await _client.CheckFacilityExistsAsync(fakeFacilityId, ct), ct: ct));
// === DELETE /api/Facility/softDelete/{id} ===
// SoftDelete → 204
results.Add(await RunStepAsync(StepNames.SoftDelete204, 204, async () =>
await _client.SoftDeleteAsync(facilityId, ct), ct: ct));
// SoftDelete → 204 (idempotent — already soft-deleted)
results.Add(await RunStepAsync(StepNames.SoftDelete204Idempotent, 204, async () =>
await _client.SoftDeleteAsync(facilityId, ct), ct: ct));
// SoftDelete → 404 (non-existent)
results.Add(await RunStepAsync(StepNames.SoftDelete404NonExistent, 404, async () =>
await _client.SoftDeleteAsync(fakeFacilityId, ct), ct: ct));
// === PATCH /api/Facility/restore/{id} ===
// Restore → 204
results.Add(await RunStepAsync(StepNames.Restore204, 204, async () =>
await _client.RestoreAsync(facilityId, ct), ct: ct));
// Restore → 400 (not deleted)
results.Add(await RunStepAsync(StepNames.Restore400NotDeleted, 400, async () =>
await _client.RestoreAsync(facilityId, ct), ct: ct));
// Restore → 404 (non-existent)
results.Add(await RunStepAsync(StepNames.Restore404NonExistent, 404, async () =>
await _client.RestoreAsync(fakeFacilityId, ct), ct: ct));
// === POST /api/Facility/{id}/AdHocReport ===
if (!string.IsNullOrWhiteSpace(seededFacilityId) && !string.IsNullOrWhiteSpace(seededReportType))
{
results.Add(await RunStepAsync(StepNames.AdHoc200Seeded, 200, async () =>
await _client.GenerateAdhocReportAsync(
seededFacilityId,
BuildAdhoc([seededReportType], DateTime.UtcNow.AddDays(-1), DateTime.UtcNow),
ct), ct: ct));
}
else
{
results.Add(SkipStepAsync(StepNames.AdHoc200Seeded, seededUnavailable));
}
// AdHocReport → 400 (no measures)
results.Add(await RunStepAsync(StepNames.AdHoc400NoMeasures, 400, async () =>
await _client.GenerateAdhocReportAsync(facilityId, BuildAdhoc([], DateTime.UtcNow.AddDays(-30), DateTime.UtcNow), ct), ct: ct));
// AdHocReport → 400 (no start date)
results.Add(await RunStepAsync(StepNames.AdHoc400NoStartDate, 400, async () =>
await _client.GenerateAdhocReportAsync(facilityId, BuildAdhoc(["SomeMeasure"], null, DateTime.UtcNow), ct), ct: ct));
// AdHocReport → 400 (end before start)
results.Add(await RunStepAsync(StepNames.AdHoc400NoEndDate, 400, async () =>
await _client.GenerateAdhocReportAsync(facilityId, BuildAdhoc(["SomeMeasure"], DateTime.UtcNow.AddDays(-30), null), ct), ct: ct));
// AdHocReport → 400 (end before start)
results.Add(await RunStepAsync(StepNames.AdHoc400EndBeforeStart, 400, async () =>
await _client.GenerateAdhocReportAsync(facilityId, BuildAdhoc(["SomeMeasure"], DateTime.UtcNow, DateTime.UtcNow.AddDays(-30)), ct), ct: ct));
// AdHocReport → 404 (non-existent facility)
results.Add(await RunStepAsync(StepNames.AdHoc404NonExistent, 404, async () =>
await _client.GenerateAdhocReportAsync(fakeFacilityId, BuildAdhoc(["SomeMeasure"], DateTime.UtcNow.AddDays(-30), DateTime.UtcNow), ct), ct: ct));
// === POST /api/Facility/{id}/RegenerateReport ===
if (!string.IsNullOrWhiteSpace(seededFacilityId) && !string.IsNullOrWhiteSpace(seededScheduleId))
{
results.Add(await RunStepAsync(StepNames.Regenerate200Seeded, 200, async () =>
await _client.RegenerateReportAsync(seededFacilityId, new RegenerateReportRequest
{
ReportId = seededScheduleId
}, ct), ct: ct));
}
else
{
results.Add(SkipStepAsync(StepNames.Regenerate200Seeded, seededUnavailable));
}
// RegenerateReport → 400 (no report id)
results.Add(await RunStepAsync(StepNames.Regenerate400NoReportId, 400, async () =>
await _client.RegenerateReportAsync(facilityId, new RegenerateReportRequest
{
ReportId = ""
}, ct), ct: ct));
// RegenerateReport → 404 (non-existent facility)
results.Add(await RunStepAsync(StepNames.Regenerate404NonExistentFacility, 404, async () =>
await _client.RegenerateReportAsync(fakeFacilityId, new RegenerateReportRequest
{
ReportId = Guid.NewGuid().ToString()
}, ct), ct: ct));
// RegenerateReport → 404 (non-existent report)
results.Add(await RunStepAsync(StepNames.Regenerate404NonExistentReport, 404, async () =>
await _client.RegenerateReportAsync(facilityId, new RegenerateReportRequest
{
ReportId = Guid.NewGuid().ToString()
}, ct), ct: ct));
// Vendor DELETE → 204
results.Add(await RunStepAsync(StepNames.VendorDelete204, 204, async () =>
{
if (vendorId == Guid.Empty)
throw new InvalidOperationException("Expected a vendor id before deleting the Vendor.");
var response = await _client.DeleteVendorAsync(vendorId, ct);
if (response.IsSuccessStatusCode)
vendorCreated = false;
return response;
}, ct: ct));
// === DELETE /api/Facility/{id} (hard delete) ===
// Delete → 204
results.Add(await RunStepAsync(StepNames.Delete204, 204, async () =>
{
var resp = await _client.DeleteAsync(facilityId, ct);
if (resp.IsSuccessStatusCode) created = false;
return resp;
}, ct: ct));
// Delete → 404 (non-existent)
results.Add(await RunStepAsync(StepNames.Delete404NonExistent, 404, async () =>
await _client.DeleteAsync(fakeFacilityId, ct), ct: ct));
}
finally
{
if (vendorCreated) await TryCleanupAsync(() => _client.DeleteVendorAsync(vendorId, ct));
if (created) await TryCleanupAsync(() => _client.DeleteAsync(facilityId, ct));
}
return results;
}
}