-
Notifications
You must be signed in to change notification settings - Fork 580
Expand file tree
/
Copy pathSearchParameterStatusManagerTests.cs
More file actions
258 lines (219 loc) · 12.5 KB
/
SearchParameterStatusManagerTests.cs
File metadata and controls
258 lines (219 loc) · 12.5 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
// -------------------------------------------------------------------------------------------------
// 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 System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Health.Core;
using Microsoft.Health.Fhir.Core.Extensions;
using Microsoft.Health.Fhir.Core.Features.Definition;
using Microsoft.Health.Fhir.Core.Features.Search.Parameters;
using Microsoft.Health.Fhir.Core.Features.Search.Registry;
using Microsoft.Health.Fhir.Core.Messages.Search;
using Microsoft.Health.Fhir.Core.Models;
using Microsoft.Health.Fhir.Tests.Common;
using Microsoft.Health.Fhir.ValueSets;
using Microsoft.Health.Test.Utilities;
using NSubstitute;
using Xunit;
namespace Microsoft.Health.Fhir.Core.UnitTests.Features.Search.Registry
{
[Trait(Traits.OwningTeam, OwningTeam.Fhir)]
[Trait(Traits.Category, Categories.Search)]
public class SearchParameterStatusManagerTests
{
private static readonly string ResourceId = "http://hl7.org/fhir/SearchParameter/Resource-id";
private static readonly string ResourceLastupdated = "http://hl7.org/fhir/SearchParameter/Resource-lastUpdated";
private static readonly string ResourceProfile = "http://hl7.org/fhir/SearchParameter/Resource-profile";
private static readonly string ResourceSecurity = "http://hl7.org/fhir/SearchParameter/Resource-security";
private static readonly string ResourceQuery = "http://hl7.org/fhir/SearchParameter/Resource-query";
private static readonly string ResourceSource = "http://hl7.org/fhir/SearchParameter/Resource-source";
private readonly SearchParameterStatusManager _manager;
private readonly ISearchParameterStatusDataStore _searchParameterStatusDataStore;
private readonly ISearchParameterDefinitionManager _searchParameterDefinitionManager;
private readonly IMediator _mediator;
private readonly SearchParameterInfo[] _searchParameterInfos;
private readonly SearchParameterInfo _queryParameter;
private readonly ISearchParameterSupportResolver _searchParameterSupportResolver;
private readonly ResourceSearchParameterStatus[] _resourceSearchParameterStatuses;
public SearchParameterStatusManagerTests()
{
_searchParameterStatusDataStore = Substitute.For<ISearchParameterStatusDataStore>();
_searchParameterDefinitionManager = Substitute.For<ISearchParameterDefinitionManager>();
_searchParameterSupportResolver = Substitute.For<ISearchParameterSupportResolver>();
_mediator = Substitute.For<IMediator>();
_manager = new SearchParameterStatusManager(
_searchParameterStatusDataStore,
_searchParameterDefinitionManager,
_searchParameterSupportResolver,
_mediator,
NullLogger<SearchParameterStatusManager>.Instance);
_resourceSearchParameterStatuses = new[]
{
new ResourceSearchParameterStatus
{
Status = SearchParameterStatus.Enabled,
Uri = new Uri(ResourceId),
LastUpdated = Clock.UtcNow,
},
new ResourceSearchParameterStatus
{
Status = SearchParameterStatus.Enabled,
Uri = new Uri(ResourceLastupdated),
IsPartiallySupported = true,
LastUpdated = Clock.UtcNow,
},
new ResourceSearchParameterStatus
{
Status = SearchParameterStatus.Unsupported,
Uri = new Uri(ResourceProfile),
LastUpdated = Clock.UtcNow,
},
new ResourceSearchParameterStatus
{
Status = SearchParameterStatus.Supported,
Uri = new Uri(ResourceSecurity),
LastUpdated = Clock.UtcNow,
},
new ResourceSearchParameterStatus
{
Status = SearchParameterStatus.Disabled,
Uri = new Uri(ResourceSource),
LastUpdated = Clock.UtcNow,
},
};
_searchParameterStatusDataStore.GetSearchParameterStatuses(Arg.Any<CancellationToken>()).Returns(_resourceSearchParameterStatuses);
List<string> baseResourceTypes = new List<string>() { "Resource" };
List<string> targetResourceTypes = new List<string>() { "Patient" };
_queryParameter = new SearchParameterInfo("_query", "_query", SearchParamType.Token, new Uri(ResourceQuery));
_searchParameterInfos = new[]
{
new SearchParameterInfo("_id", "_id", SearchParamType.Token, new Uri(ResourceId), targetResourceTypes: targetResourceTypes, baseResourceTypes: baseResourceTypes),
new SearchParameterInfo("_lastUpdated", "_lastUpdated", SearchParamType.Token, new Uri(ResourceLastupdated), targetResourceTypes: targetResourceTypes, baseResourceTypes: baseResourceTypes),
new SearchParameterInfo("_profile", "_profile", SearchParamType.Token, new Uri(ResourceProfile), targetResourceTypes: targetResourceTypes),
new SearchParameterInfo("_security", "_security", SearchParamType.Token, new Uri(ResourceSecurity), targetResourceTypes: targetResourceTypes),
_queryParameter,
new SearchParameterInfo("_source", "_source", SearchParamType.Uri, new Uri(ResourceSource), targetResourceTypes: targetResourceTypes),
};
_searchParameterDefinitionManager.GetSearchParameters("Account")
.Returns(_searchParameterInfos);
_searchParameterDefinitionManager.AllSearchParameters
.Returns(_searchParameterInfos);
_searchParameterDefinitionManager.GetSearchParameter(ResourceQuery)
.Returns(_queryParameter);
_searchParameterSupportResolver
.IsSearchParameterSupported(Arg.Any<SearchParameterInfo>())
.Returns((false, false));
_searchParameterSupportResolver
.IsSearchParameterSupported(Arg.Is(_searchParameterInfos[4]))
.Returns((true, false));
_searchParameterSupportResolver
.IsSearchParameterSupported(Arg.Is(_searchParameterInfos[5]))
.Returns((true, false));
}
[Fact]
public async Task GivenASPStatusManager_WhenInitializing_ThenSearchParameterIsUpdatedFromRegistry()
{
await _manager.EnsureInitializedAsync(CancellationToken.None);
var list = _searchParameterDefinitionManager.GetSearchParameters("Account").ToList();
Assert.True(list[0].IsSearchable);
Assert.True(list[0].IsSupported);
Assert.False(list[0].IsPartiallySupported);
Assert.True(list[1].IsSearchable);
Assert.True(list[1].IsSupported);
Assert.True(list[1].IsPartiallySupported);
Assert.False(list[2].IsSearchable);
Assert.False(list[2].IsSupported);
Assert.False(list[2].IsPartiallySupported);
Assert.False(list[3].IsSearchable);
Assert.True(list[3].IsSupported);
Assert.False(list[3].IsPartiallySupported);
Assert.False(list[4].IsSearchable);
Assert.True(list[4].IsSupported);
Assert.False(list[4].IsPartiallySupported);
Assert.False(list[5].IsSearchable);
Assert.False(list[5].IsSupported); // Disabled Search Params show as unsupported
Assert.False(list[5].IsPartiallySupported);
Assert.Equal(SearchParameterStatus.Disabled, list[5].SearchParameterStatus);
}
[Fact]
public async Task GivenASPStatusManager_WhenInitializing_ThenUpdatedSearchParametersInNotification()
{
await _manager.EnsureInitializedAsync(CancellationToken.None);
// Id should not be modified in this test case
var modifiedItems = _searchParameterInfos.Skip(1).ToArray();
await _mediator
.Received()
.Publish(
Arg.Is<SearchParametersUpdatedNotification>(x => modifiedItems.Except(x.SearchParameters).Any() == false),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task GivenASPStatusManager_WhenInitializing_ThenRegistryShouldNotUpdateNewlyFoundParameters()
{
await _manager.EnsureInitializedAsync(CancellationToken.None);
await _searchParameterStatusDataStore
.DidNotReceive()
.UpsertStatuses(Arg.Any<List<ResourceSearchParameterStatus>>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GivenASPStatusManager_WhenInitializingAndResolverThrowsException_ThenCatchItAndReturnFalse()
{
_searchParameterSupportResolver
.IsSearchParameterSupported(Arg.Is(_searchParameterInfos[2]))
.Returns(x => throw new FormatException("Unable to resolve"));
await _manager.EnsureInitializedAsync(CancellationToken.None);
var list = _searchParameterDefinitionManager.GetSearchParameters("Account").ToList();
_searchParameterSupportResolver.Received(1).IsSearchParameterSupported(Arg.Is(_searchParameterInfos[2]));
Assert.False(list[2].IsSearchable);
Assert.False(list[2].IsSupported);
Assert.False(list[2].IsPartiallySupported);
}
[Fact]
public async Task GivenASPStatusManager_WhenUpdatingStatus_ThenInMemoryCacheIsNotMutatedAndMediatorIsNotPublished()
{
// Arrange - Initialize so search parameters have known in-memory state
await _manager.EnsureInitializedAsync(CancellationToken.None);
var list = _searchParameterDefinitionManager.GetSearchParameters("Account").ToList();
// Capture initial in-memory state for the Enabled parameter (index 0: ResourceId)
bool initialIsSearchable = list[0].IsSearchable;
bool initialIsSupported = list[0].IsSupported;
bool initialIsPartiallySupported = list[0].IsPartiallySupported;
SortParameterStatus initialSortStatus = list[0].SortStatus;
// Clear any mediator calls from initialization
_mediator.ClearReceivedCalls();
// Act - Call UpdateSearchParameterStatusAsync to change status to Supported
await _manager.UpdateSearchParameterStatusAsync(
new[] { ResourceId },
SearchParameterStatus.Supported,
CancellationToken.None);
// Assert - DB write occurred
await _searchParameterStatusDataStore
.Received(1)
.UpsertStatuses(
Arg.Is<List<ResourceSearchParameterStatus>>(statuses =>
statuses.Count == 1 &&
statuses[0].Uri.OriginalString == ResourceId &&
statuses[0].Status == SearchParameterStatus.Supported),
Arg.Any<CancellationToken>());
// Assert - In-memory SearchParameterInfo was NOT modified
// Re-fetch from the definition manager to make the assertion intent explicit
var refreshedList = _searchParameterDefinitionManager.GetSearchParameters("Account").ToList();
Assert.Equal(initialIsSearchable, refreshedList[0].IsSearchable);
Assert.Equal(initialIsSupported, refreshedList[0].IsSupported);
Assert.Equal(initialIsPartiallySupported, refreshedList[0].IsPartiallySupported);
Assert.Equal(initialSortStatus, refreshedList[0].SortStatus);
// Assert - Mediator was NOT called (no SearchParametersUpdatedNotification)
await _mediator
.DidNotReceive()
.Publish(
Arg.Any<SearchParametersUpdatedNotification>(),
Arg.Any<CancellationToken>());
}
}
}