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

Validate patient id is a relative reference #117

Merged
merged 4 commits into from
Jul 7, 2021
Merged
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
24 changes: 22 additions & 2 deletions src/lib/Microsoft.Health.Extensions.Fhir.R4/ModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Text.RegularExpressions;

namespace Microsoft.Health.Extensions.Fhir
{
public static class ModelExtensions
{
private static Regex _idMatcherRegex = new Regex(@"^[A-Za-z0-9_.\-~#]+$", RegexOptions.Compiled);

public static Hl7.Fhir.Model.ResourceReference ToReference<TResource>(this TResource resource)
where TResource : Hl7.Fhir.Model.Resource
{
Expand Down Expand Up @@ -39,9 +42,26 @@ public static Hl7.Fhir.Model.ResourceReference ToReference<TResource>(this strin
/// <returns>The id for the specified resource type if it exists, null otherwise.</returns>
public static string GetId<TResource>(this Hl7.Fhir.Model.ResourceReference reference)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have some tests for just the GetId extension method? Would be good to verify for the reference types we support today (Device & Patient)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added ModelExtensionsTests

{
string id;
var referenceType = $"{typeof(TResource).Name}/";
return reference?.Reference == null || !reference.Reference.StartsWith(referenceType, StringComparison.InvariantCultureIgnoreCase)
? null : reference.Reference.Substring(referenceType.Length);

if (reference?.Reference == null || !reference.Reference.StartsWith(referenceType, StringComparison.InvariantCultureIgnoreCase))
{
return null;
}

// Reference should be in the form: ResourceType/Identifier
var relativeReferenceIdentifier = reference.Reference.Substring(referenceType.Length);
var matches = _idMatcherRegex.Match(relativeReferenceIdentifier);
if (matches?.Groups?.Count != 1)
{
return null;
}
else
{
id = matches?.Groups?[0].Value;
return id;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using Xunit;
using Model = Hl7.Fhir.Model;

namespace Microsoft.Health.Extensions.Fhir.R4.UnitTests
{
public class ModelExtensionsTests
{
[Fact]
public void GivenDeviceWithValidPatientReference_GetId_ThenReferenceIdentifierIsReturned_Test()
{
var device = new Model.Device
{
Id = "1",
Patient = new Model.ResourceReference("Patient/123"),
};

var patientIdentifier = device.Patient?.GetId<Model.Patient>();
Assert.Equal("123", patientIdentifier);
}

[Fact]
public void GivenDeviceWithInvalidPatientReference_GetId_ThenNullIsReturned_Test()
{
var device = new Model.Device
{
Id = "1",
Patient = new Model.ResourceReference("Not a reference in the form of: ResourceName/Identifier"),
};

var patientReference = device.Patient?.GetId<Model.Patient>();
Assert.Null(patientReference);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,31 @@ public async void GivenDeviceWithNotPatientReference_WhenResolveResourceIdentiti

await resourceService.Received(1).GetResourceByIdentityAsync<Model.Device>(fhirClient, "deviceId", null);
}

[Fact]
public async void GivenDeviceWithPatientReferenceUnsupportedCharacters_WhenResolveResourceIdentitiesAsync_ThenNotSupportedExceptionThrown_Test()
{
var fhirClient = Utilities.CreateMockFhirClient();
var resourceService = Substitute.For<ResourceManagementService>();
var device = new Model.Device
{
Id = "1",
Patient = new Model.ResourceReference("Not a reference in the form of: /ResourceName/Identifier"),
};

var mg = Substitute.For<IMeasurementGroup>();
mg.DeviceId.Returns("deviceId");

resourceService.GetResourceByIdentityAsync<Model.Device>(Arg.Any<FhirClient>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Task.FromResult(device));

using (var idSrv = new R4DeviceAndPatientLookupIdentityService(fhirClient, resourceService))
{
var ex = await Assert.ThrowsAsync<FhirResourceNotFoundException>(async () => await idSrv.ResolveResourceIdentitiesAsync(mg));
Assert.Equal(ResourceType.Patient, ex.FhirResourceType);
}

await resourceService.Received(1).GetResourceByIdentityAsync<Model.Device>(fhirClient, "deviceId", null);
}
}
}