-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathServerProvideProfileValidationTests.cs
More file actions
488 lines (406 loc) · 21.4 KB
/
Copy pathServerProvideProfileValidationTests.cs
File metadata and controls
488 lines (406 loc) · 21.4 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using MediatR;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.Health.Extensions.DependencyInjection;
using Microsoft.Health.Fhir.Core.Configs;
using Microsoft.Health.Fhir.Core.Extensions;
using Microsoft.Health.Fhir.Core.Features.Persistence;
using Microsoft.Health.Fhir.Core.Features.Search;
using Microsoft.Health.Fhir.Core.Features.Validation;
using Microsoft.Health.Fhir.Core.Models;
using Microsoft.Health.Fhir.Tests.Common;
using Microsoft.Health.Test.Utilities;
using NSubstitute;
using Xunit;
using Task = System.Threading.Tasks.Task;
namespace Microsoft.Health.Fhir.Core.UnitTests.Features.Validation
{
[Trait(Traits.OwningTeam, OwningTeam.Fhir)]
[Trait(Traits.Category, Categories.Validate)]
public class ServerProvideProfileValidationTests : IDisposable
{
private readonly IHostApplicationLifetime _hostApplicationLifetime;
private readonly ISearchService _searchService;
private readonly IScoped<ISearchService> _scopedSearchService;
private readonly Func<IScoped<ISearchService>> _searchServiceFactory;
private readonly IMediator _mediator;
private readonly IOptions<ValidateOperationConfiguration> _options;
private readonly ServerProvideProfileValidation _serverProvideProfileValidation;
public ServerProvideProfileValidationTests()
{
_hostApplicationLifetime = Substitute.For<IHostApplicationLifetime>();
_hostApplicationLifetime.ApplicationStopping.Returns(CancellationToken.None);
_searchService = Substitute.For<ISearchService>();
_scopedSearchService = Substitute.For<IScoped<ISearchService>>();
_scopedSearchService.Value.Returns(_searchService);
_searchServiceFactory = () => _scopedSearchService;
_mediator = Substitute.For<IMediator>();
var config = new ValidateOperationConfiguration
{
CacheDurationInSeconds = 300, // 5 minutes
BackgroundProfileStatusDelayedStartInSeconds = 1,
BackgroundProfileStatusCheckIntervalInSeconds = 5,
};
_options = Options.Create(config);
_serverProvideProfileValidation = new ServerProvideProfileValidation(
_searchServiceFactory,
_options,
_mediator,
_hostApplicationLifetime,
NullLogger<ServerProvideProfileValidation>.Instance);
}
[Fact]
public void GivenServerProvideProfileValidation_WhenGettingProfileTypes_ThenCorrectTypesAreReturned()
{
// Act
var profileTypes = _serverProvideProfileValidation.GetProfilesTypes();
// Assert
Assert.NotNull(profileTypes);
Assert.Equal(3, profileTypes.Count);
Assert.Contains("ValueSet", profileTypes);
Assert.Contains("StructureDefinition", profileTypes);
Assert.Contains("CodeSystem", profileTypes);
}
[Fact]
public async Task GivenNoStructureDefinitions_WhenGettingSupportedProfiles_ThenEmptyListIsReturned()
{
// Arrange
SetupSearchServiceWithNoResults();
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Assert
Assert.NotNull(profiles);
Assert.Empty(profiles);
Assert.False(_serverProvideProfileValidation.IsSyncRequested());
}
[Fact]
public async Task GivenStructureDefinitionsExist_WhenGettingSupportedProfiles_ThenMatchingProfilesAreReturned()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient");
SetupSearchServiceWithResults("StructureDefinition", patientProfile);
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Assert
Assert.NotNull(profiles);
Assert.Single(profiles);
Assert.Contains("http://example.org/fhir/StructureDefinition/custom-patient", profiles);
}
[Fact]
public async Task GivenVersionedStructureDefinition_WhenGettingSupportedProfiles_ThenVersionedCanonicalIsReturned()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient", "3.0.0");
SetupSearchServiceWithResults("StructureDefinition", patientProfile);
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Assert
Assert.Single(profiles);
Assert.Contains("http://example.org/fhir/StructureDefinition/custom-patient|3.0.0", profiles);
}
[Fact]
public async Task GivenVersionedStructureDefinition_WhenResolvingByCanonicalUriWithVersion_ThenMatchingProfileIsReturned()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient", "3.0.0");
SetupSearchServiceWithResults("StructureDefinition", patientProfile);
// Act
var profile = await _serverProvideProfileValidation.ResolveByCanonicalUriAsync("http://example.org/fhir/StructureDefinition/custom-patient|3.0.0");
// Assert
Assert.NotNull(profile);
var structureDefinition = Assert.IsType<StructureDefinition>(profile);
Assert.Equal("http://example.org/fhir/StructureDefinition/custom-patient", structureDefinition.Url);
Assert.Equal("3.0.0", structureDefinition.Version);
}
[Fact]
public async Task GivenANewStructureDefinition_WhenBackgroundLoopRuns_ThenSyncIsRequested()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient");
SetupSearchServiceWithResults("StructureDefinition", patientProfile);
// Wait for background refresh to complete
await Task.Delay(TimeSpan.FromSeconds(10));
// Sync should be requested after profile is added.
Assert.True(_serverProvideProfileValidation.IsSyncRequested());
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
_serverProvideProfileValidation.MarkSyncCompleted();
// Assert
Assert.NotNull(profiles);
Assert.Single(profiles);
Assert.Contains("http://example.org/fhir/StructureDefinition/custom-patient", profiles);
Assert.False(_serverProvideProfileValidation.IsSyncRequested());
}
[Fact]
public async Task GivenMultipleNewStructureDefinitions_WhenBackgroundLoopRuns_ThenSyncIsRequested()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient");
SetupSearchServiceWithResults("StructureDefinition", patientProfile);
// Wait for background refresh to complete
await Task.Delay(TimeSpan.FromSeconds(10));
// Sync should be requested after profile is added.
Assert.True(_serverProvideProfileValidation.IsSyncRequested());
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
_serverProvideProfileValidation.MarkSyncCompleted();
// Assert
Assert.NotNull(profiles);
Assert.Single(profiles);
Assert.Contains("http://example.org/fhir/StructureDefinition/custom-patient", profiles);
Assert.False(_serverProvideProfileValidation.IsSyncRequested());
var observationProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-observation", "Observation");
SetupSearchServiceWithResults("StructureDefinition", patientProfile, observationProfile);
// Refreshing the profiles to reset cache expiration time.
// This is something that would be done by the dependent services in a real scenario.
_serverProvideProfileValidation.Refresh();
// Wait for background refresh to complete
await Task.Delay(TimeSpan.FromSeconds(10));
// Sync should be requested after profile is added.
Assert.True(_serverProvideProfileValidation.IsSyncRequested());
// Act
profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Observation", CancellationToken.None);
_serverProvideProfileValidation.MarkSyncCompleted();
// Assert
Assert.NotNull(profiles);
Assert.Single(profiles);
Assert.Contains("http://example.org/fhir/StructureDefinition/custom-observation", profiles);
Assert.False(_serverProvideProfileValidation.IsSyncRequested());
}
[Fact]
public async Task GivenMultipleStructureDefinitions_WhenGettingSupportedProfiles_ThenOnlyMatchingResourceTypeIsReturned()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient");
var observationProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-observation", "Observation");
SetupSearchServiceWithResults("StructureDefinition", patientProfile, observationProfile);
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Assert
Assert.NotNull(profiles);
Assert.Single(profiles);
Assert.Contains("http://example.org/fhir/StructureDefinition/custom-patient", profiles);
Assert.DoesNotContain("http://example.org/fhir/StructureDefinition/custom-observation", profiles);
}
[Fact]
public async Task GivenCachedResults_WhenGettingSupportedProfiles_ThenCacheIsUsed()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient");
SetupSearchServiceWithResults("StructureDefinition", patientProfile);
// Act - First call
await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Act - Second call (should use cache)
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None, disableCacheRefresh: true);
// Assert
Assert.NotNull(profiles);
// Verify search was only called once (second call used cache)
await _searchService.Received(1).SearchAsync(
"StructureDefinition",
Arg.Any<List<Tuple<string, string>>>(),
Arg.Any<CancellationToken>());
}
[Fact]
public void GivenServerProvideProfileValidation_WhenRefreshIsCalled_ThenCacheIsMarkedForRefresh()
{
// Act
_serverProvideProfileValidation.Refresh();
// Assert - No exception should be thrown
Assert.NotNull(_serverProvideProfileValidation);
}
[Fact]
public async Task GivenStructureDefinitionWithoutType_WhenGettingSupportedProfiles_ThenItIsNotIncluded()
{
// Arrange - Create a malformed StructureDefinition without Type property
var malformedProfile = new StructureDefinition
{
Id = Guid.NewGuid().ToString("N").Substring(0, 16), // ID is required for ResourceWrapper
Url = "http://example.org/fhir/StructureDefinition/malformed",
Name = "MalformedProfile",
Status = PublicationStatus.Active,
Kind = StructureDefinition.StructureDefinitionKind.Resource,
Abstract = false,
// Type property intentionally not set
};
SetupSearchServiceWithResults("StructureDefinition", malformedProfile);
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Assert
Assert.NotNull(profiles);
Assert.Empty(profiles);
}
[Fact]
public async Task GivenPaginatedResults_WhenGettingSupportedProfiles_ThenAllPagesAreProcessed()
{
// Arrange
var profile1 = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/patient-1", "Patient");
var profile2 = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/patient-2", "Patient");
// Setup first page
SetupSearchServiceWithPaginatedResults("StructureDefinition", "page2token", profile1);
// Setup second page
var searchResult2 = CreateSearchResult(null, profile2);
_searchService.SearchAsync(
"StructureDefinition",
Arg.Is<List<Tuple<string, string>>>(list =>
list != null && list.Any(t => t.Item1 == "ct")),
Arg.Any<CancellationToken>())
.Returns(searchResult2);
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Assert
Assert.NotNull(profiles);
Assert.Equal(2, profiles.Count());
Assert.Contains("http://example.org/fhir/StructureDefinition/patient-1", profiles);
Assert.Contains("http://example.org/fhir/StructureDefinition/patient-2", profiles);
}
[Fact]
public async Task GivenValueSetResources_WhenGettingSupportedProfiles_ThenTheyAreNotIncluded()
{
// Arrange
var valueSet = new ValueSet
{
Id = Guid.NewGuid().ToString("N").Substring(0, 16), // ID is required for ResourceWrapper
Url = "http://example.org/fhir/ValueSet/test",
Name = "TestValueSet",
Status = PublicationStatus.Active,
};
SetupSearchServiceWithResults("ValueSet", valueSet);
// Act
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Assert
Assert.NotNull(profiles);
Assert.Empty(profiles);
}
[Fact]
public async Task GivenCaseInsensitiveResourceType_WhenGettingSupportedProfiles_ThenMatchingProfilesAreReturned()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient");
SetupSearchServiceWithResults("StructureDefinition", patientProfile);
// Act - Query with lowercase
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("patient", CancellationToken.None);
// Assert
Assert.NotNull(profiles);
Assert.Single(profiles);
Assert.Contains("http://example.org/fhir/StructureDefinition/custom-patient", profiles);
}
[Fact]
public async Task GivenDisableCacheRefresh_WhenGettingSupportedProfiles_ThenCacheIsNotRefreshed()
{
// Arrange
var patientProfile = CreateStructureDefinition("http://example.org/fhir/StructureDefinition/custom-patient", "Patient");
SetupSearchServiceWithResults("StructureDefinition", patientProfile);
// First call
await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None);
// Mark for refresh
_serverProvideProfileValidation.Refresh();
// Act - Call with cache refresh disabled
var profiles = await _serverProvideProfileValidation.GetSupportedProfilesAsync("Patient", CancellationToken.None, disableCacheRefresh: true);
// Assert - Should still return results from initial cache
Assert.NotNull(profiles);
Assert.Single(profiles);
}
public void Dispose()
{
_serverProvideProfileValidation?.Dispose();
}
private static StructureDefinition CreateStructureDefinition(string url, string type, string version = null)
{
return new StructureDefinition
{
Id = Guid.NewGuid().ToString("N").Substring(0, 16), // Generate valid FHIR ID
Url = url,
Version = version,
Name = $"{type}Profile",
Status = PublicationStatus.Active,
Kind = StructureDefinition.StructureDefinitionKind.Resource,
Abstract = false,
Type = type,
BaseDefinition = $"http://hl7.org/fhir/StructureDefinition/{type}",
Derivation = StructureDefinition.TypeDerivationRule.Constraint,
};
}
private void SetupSearchServiceWithNoResults()
{
var emptyResult = new SearchResult(
new List<SearchResultEntry>(),
null,
null,
new List<Tuple<string, string>>());
_searchService.SearchAsync(
Arg.Any<string>(),
Arg.Any<List<Tuple<string, string>>>(),
Arg.Any<CancellationToken>())
.Returns(emptyResult);
}
private void SetupSearchServiceWithResults(string resourceType, params Resource[] resources)
{
var searchEntries = resources.Select(r => CreateSearchResultEntry(r)).ToList();
var searchResult = new SearchResult(searchEntries, null, null, new List<Tuple<string, string>>()) { TotalCount = searchEntries.Count };
_searchService.SearchAsync(
resourceType,
Arg.Any<IReadOnlyList<Tuple<string, string>>>(),
Arg.Any<CancellationToken>())
.Returns(searchResult);
// Setup for other resource types to return empty
foreach (string type in new[] { "ValueSet", "CodeSystem", "StructureDefinition" }.Where(type => type != resourceType))
{
_searchService.SearchAsync(
type,
Arg.Any<IReadOnlyList<Tuple<string, string>>>(),
Arg.Any<CancellationToken>())
.Returns(new SearchResult(new List<SearchResultEntry>(), null, null, new List<Tuple<string, string>>()) { TotalCount = 0 });
}
}
private void SetupSearchServiceWithPaginatedResults(string resourceType, string continuationToken, params Resource[] resources)
{
var searchEntries = resources.Select(r => CreateSearchResultEntry(r)).ToList();
var searchResult = new SearchResult(searchEntries, continuationToken, null, new List<Tuple<string, string>>());
_searchService.SearchAsync(
resourceType,
Arg.Is<List<Tuple<string, string>>>(list => list != null && !list.Any(t => t.Item1 == "ct")),
Arg.Any<CancellationToken>())
.Returns(searchResult);
// Setup empty results for other types
foreach (var type in new[] { "ValueSet", "CodeSystem" })
{
_searchService.SearchAsync(
type,
Arg.Any<List<Tuple<string, string>>>(),
Arg.Any<CancellationToken>())
.Returns(new SearchResult(new List<SearchResultEntry>(), null, null, new List<Tuple<string, string>>()));
}
}
private static SearchResult CreateSearchResult(string continuationToken, params Resource[] resources)
{
var searchEntries = resources.Select(r => CreateSearchResultEntry(r)).ToList();
return new SearchResult(searchEntries, continuationToken, null, new List<Tuple<string, string>>());
}
private static SearchResultEntry CreateSearchResultEntry(Resource resource)
{
var json = new FhirJsonSerializer().SerializeToString(resource);
var rawResource = new RawResource(json, FhirResourceFormat.Json, false);
var resourceElement = resource.ToResourceElement();
var wrapper = new ResourceWrapper(
resourceElement,
rawResource,
new ResourceRequest("GET"),
false,
null,
null,
null);
return new SearchResultEntry(wrapper);
}
}
}