-
Notifications
You must be signed in to change notification settings - Fork 567
Add tests to improve R4.Core tests coverage #5260
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
Open
rbans96
wants to merge
3
commits into
main
Choose a base branch
from
personal/ribans/test-coverage-r4-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
195 changes: 195 additions & 0 deletions
195
...ft.Health.Fhir.R4.Core.UnitTests/Extensions/OperationDefinitionMediatorExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // ------------------------------------------------------------------------------------------------- | ||
| // 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.Threading; | ||
| using System.Threading.Tasks; | ||
| using Hl7.Fhir.Model; | ||
| using MediatR; | ||
| using Microsoft.Health.Fhir.Core.Extensions; | ||
| using Microsoft.Health.Fhir.Core.Messages.Operation; | ||
| using Microsoft.Health.Fhir.Shared.Core.Extensions; | ||
| 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.R4.Core.UnitTests.Extensions | ||
| { | ||
| [Trait(Traits.OwningTeam, OwningTeam.Fhir)] | ||
| [Trait(Traits.Category, Categories.Operations)] | ||
| public class OperationDefinitionMediatorExtensionsTests | ||
| { | ||
| private readonly IMediator _mediator; | ||
|
|
||
| public OperationDefinitionMediatorExtensionsTests() | ||
| { | ||
| _mediator = Substitute.For<IMediator>(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("export")] | ||
| [InlineData("reindex")] | ||
| [InlineData("member-match")] | ||
| [InlineData("convert-data")] | ||
| [InlineData("purge-history")] | ||
| public async Task GivenVariousOperationNames_WhenGetOperationDefinitionAsync_ThenCorrectRequestIsSent(string operationName) | ||
| { | ||
| // Arrange | ||
| var operationDefinition = CreateTestOperationDefinition(operationName); | ||
| var response = new OperationDefinitionResponse(operationDefinition.ToResourceElement()); | ||
|
|
||
| _mediator.Send( | ||
| Arg.Any<OperationDefinitionRequest>(), | ||
| Arg.Any<CancellationToken>()) | ||
| .Returns(response); | ||
|
|
||
| // Act | ||
| await _mediator.GetOperationDefinitionAsync(operationName, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| await _mediator.Received(1).Send( | ||
| Arg.Is<OperationDefinitionRequest>(r => r.OperationName == operationName), | ||
| Arg.Any<CancellationToken>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GivenCancellationToken_WhenGetOperationDefinitionAsync_ThenCancellationTokenIsPassedThrough() | ||
| { | ||
| // Arrange | ||
| const string operationName = "export"; | ||
| var operationDefinition = CreateTestOperationDefinition(operationName); | ||
| var response = new OperationDefinitionResponse(operationDefinition.ToResourceElement()); | ||
| var cancellationToken = new CancellationToken(false); | ||
|
|
||
| _mediator.Send( | ||
| Arg.Any<OperationDefinitionRequest>(), | ||
| Arg.Any<CancellationToken>()) | ||
| .Returns(response); | ||
|
|
||
| // Act | ||
| await _mediator.GetOperationDefinitionAsync(operationName, cancellationToken); | ||
|
|
||
| // Assert | ||
| await _mediator.Received(1).Send( | ||
| Arg.Any<OperationDefinitionRequest>(), | ||
| Arg.Is<CancellationToken>(ct => ct == cancellationToken)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GivenNullMediator_WhenGetOperationDefinitionAsync_ThenArgumentNullExceptionIsThrown() | ||
| { | ||
| // Arrange | ||
| IMediator nullMediator = null; | ||
|
|
||
| // Act & Assert | ||
| await Assert.ThrowsAsync<ArgumentNullException>( | ||
| () => nullMediator.GetOperationDefinitionAsync("export", CancellationToken.None)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GivenNullOperationName_WhenGetOperationDefinitionAsync_ThenArgumentExceptionIsThrown() | ||
| { | ||
| // Act & Assert | ||
| await Assert.ThrowsAsync<ArgumentNullException>( | ||
| () => _mediator.GetOperationDefinitionAsync(null, CancellationToken.None)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| public async Task GivenInvalidOperationName_WhenGetOperationDefinitionAsync_ThenArgumentExceptionIsThrown(string operationName) | ||
| { | ||
| // Act & Assert | ||
| await Assert.ThrowsAsync<ArgumentException>( | ||
| () => _mediator.GetOperationDefinitionAsync(operationName, CancellationToken.None)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GivenMediatorThrowsException_WhenGetOperationDefinitionAsync_ThenExceptionIsNotCaught() | ||
| { | ||
| // Arrange | ||
| const string operationName = "export"; | ||
| _mediator.Send( | ||
| Arg.Any<OperationDefinitionRequest>(), | ||
| Arg.Any<CancellationToken>()) | ||
| .Returns<OperationDefinitionResponse>(x => throw new InvalidOperationException("Test exception")); | ||
|
|
||
| // Act & Assert | ||
| await Assert.ThrowsAsync<InvalidOperationException>( | ||
| () => _mediator.GetOperationDefinitionAsync(operationName, CancellationToken.None)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GivenCancellationRequested_WhenGetOperationDefinitionAsync_ThenOperationCanceledExceptionIsThrown() | ||
| { | ||
| // Arrange | ||
| const string operationName = "export"; | ||
| var cancellationTokenSource = new CancellationTokenSource(); | ||
| cancellationTokenSource.Cancel(); | ||
|
|
||
| _mediator.Send( | ||
| Arg.Any<OperationDefinitionRequest>(), | ||
| Arg.Any<CancellationToken>()) | ||
| .Returns<OperationDefinitionResponse>(x => throw new OperationCanceledException()); | ||
|
|
||
| // Act & Assert | ||
| await Assert.ThrowsAsync<OperationCanceledException>( | ||
| () => _mediator.GetOperationDefinitionAsync(operationName, cancellationTokenSource.Token)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GivenMultipleCalls_WhenGetOperationDefinitionAsync_ThenEachCallCreatesNewRequest() | ||
| { | ||
| // Arrange | ||
| const string operationName1 = "export"; | ||
| const string operationName2 = "reindex"; | ||
|
|
||
| var operationDefinition1 = CreateTestOperationDefinition(operationName1); | ||
| var operationDefinition2 = CreateTestOperationDefinition(operationName2); | ||
|
|
||
| _mediator.Send( | ||
| Arg.Is<OperationDefinitionRequest>(r => r.OperationName == operationName1), | ||
| Arg.Any<CancellationToken>()) | ||
| .Returns(new OperationDefinitionResponse(operationDefinition1.ToResourceElement())); | ||
|
|
||
| _mediator.Send( | ||
| Arg.Is<OperationDefinitionRequest>(r => r.OperationName == operationName2), | ||
| Arg.Any<CancellationToken>()) | ||
| .Returns(new OperationDefinitionResponse(operationDefinition2.ToResourceElement())); | ||
|
|
||
| // Act | ||
| var result1 = await _mediator.GetOperationDefinitionAsync(operationName1, CancellationToken.None); | ||
| var result2 = await _mediator.GetOperationDefinitionAsync(operationName2, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result1); | ||
| Assert.NotNull(result2); | ||
| await _mediator.Received(1).Send( | ||
| Arg.Is<OperationDefinitionRequest>(r => r.OperationName == operationName1), | ||
| Arg.Any<CancellationToken>()); | ||
| await _mediator.Received(1).Send( | ||
| Arg.Is<OperationDefinitionRequest>(r => r.OperationName == operationName2), | ||
| Arg.Any<CancellationToken>()); | ||
| } | ||
|
|
||
| private static OperationDefinition CreateTestOperationDefinition(string operationName) | ||
| { | ||
| return new OperationDefinition | ||
| { | ||
| Url = $"http://example.org/fhir/OperationDefinition/{operationName}", | ||
| Name = operationName, | ||
| Status = PublicationStatus.Active, | ||
| Kind = OperationDefinition.OperationKind.Operation, | ||
| Code = operationName, | ||
| System = false, | ||
| Type = true, | ||
| Instance = false, | ||
| Parameter = new System.Collections.Generic.List<OperationDefinition.ParameterComponent>(), | ||
| }; | ||
| } | ||
| } | ||
| } | ||
161 changes: 161 additions & 0 deletions
161
...t.Health.Fhir.R4.Core.UnitTests/Features/Definition/ComponentDefinitionExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // ------------------------------------------------------------------------------------------------- | ||
| // 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 Hl7.Fhir.Model; | ||
| using Microsoft.Health.Fhir.Core.Features.Definition; | ||
| using Microsoft.Health.Fhir.Tests.Common; | ||
| using Microsoft.Health.Test.Utilities; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Health.Fhir.R4.Core.UnitTests.Features.Definition | ||
| { | ||
| [Trait(Traits.OwningTeam, OwningTeam.Fhir)] | ||
| [Trait(Traits.Category, Categories.Operations)] | ||
| public class ComponentDefinitionExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void GivenAComponentWithValidDefinition_WhenCallingGetComponentDefinitionUri_ThenUriIsReturned() | ||
| { | ||
| // Arrange | ||
| var component = new SearchParameter.ComponentComponent | ||
| { | ||
| Definition = "http://hl7.org/fhir/SearchParameter/Patient-name", | ||
| }; | ||
|
|
||
| // Act | ||
| Uri result = component.GetComponentDefinitionUri(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Equal("http://hl7.org/fhir/SearchParameter/Patient-name", result.OriginalString); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenAComponentWithNullDefinition_WhenCallingGetComponentDefinitionUri_ThenNullIsReturned() | ||
| { | ||
| // Arrange | ||
| var component = new SearchParameter.ComponentComponent | ||
| { | ||
| Definition = null, | ||
| }; | ||
|
|
||
| // Act | ||
| Uri result = component.GetComponentDefinitionUri(); | ||
|
|
||
| // Assert | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenAComponentWithEmptyDefinition_WhenCallingGetComponentDefinitionUri_ThenNullIsReturned() | ||
| { | ||
| // Arrange | ||
| var component = new SearchParameter.ComponentComponent | ||
| { | ||
| Definition = string.Empty, | ||
| }; | ||
|
|
||
| // Act | ||
| Uri result = component.GetComponentDefinitionUri(); | ||
|
|
||
| // Assert | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenAComponentWithInvalidUri_WhenCallingGetComponentDefinitionUri_ThenUriFormatExceptionIsThrown() | ||
| { | ||
| // Arrange | ||
| var component = new SearchParameter.ComponentComponent | ||
| { | ||
| Definition = "not a valid uri", | ||
| }; | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<UriFormatException>(() => component.GetComponentDefinitionUri()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("http://hl7.org/fhir/SearchParameter/Patient-name")] | ||
| [InlineData("http://hl7.org/fhir/SearchParameter/Observation-code")] | ||
| [InlineData("https://example.com/custom/search/param")] | ||
| public void GivenVariousValidDefinitions_WhenCallingGetComponentDefinitionUri_ThenCorrectUriIsReturned(string definitionUrl) | ||
| { | ||
| // Arrange | ||
| var component = new SearchParameter.ComponentComponent | ||
| { | ||
| Definition = definitionUrl, | ||
| }; | ||
|
|
||
| // Act | ||
| Uri result = component.GetComponentDefinitionUri(); | ||
|
|
||
| // Assert - whitespace should return null, others should return valid URI | ||
| if (string.IsNullOrWhiteSpace(definitionUrl)) | ||
| { | ||
| Assert.Null(result); | ||
| } | ||
| else | ||
| { | ||
| Assert.NotNull(result); | ||
| Assert.Equal(definitionUrl, result.OriginalString); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("http://hl7.org/fhir/SearchParameter/Patient-birthdate")] | ||
| [InlineData("http://example.org/fhir/SearchParameter/custom-param")] | ||
| public void GivenResourceReferenceWithVariousUrls_WhenCallingGetComponentDefinition_ThenCorrectStringIsReturned(string urlString) | ||
| { | ||
| // Arrange | ||
| var reference = new ResourceReference | ||
| { | ||
| Url = new Uri(urlString), | ||
| }; | ||
|
|
||
| // Act | ||
| string result = reference.GetComponentDefinition(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Equal(urlString, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenAResourceReferenceWithNullUrl_WhenCallingGetComponentDefinition_ThenNullIsReturned() | ||
| { | ||
| // Arrange | ||
| var reference = new ResourceReference | ||
| { | ||
| Url = null, | ||
| }; | ||
|
|
||
| // Act | ||
| string result = reference.GetComponentDefinition(); | ||
|
|
||
| // Assert | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GivenAResourceReferenceWithEmptyUrl_WhenCallingGetComponentDefinition_ThenEmptyStringIsReturned() | ||
| { | ||
| // Arrange | ||
| var expectedUrl = string.Empty; | ||
| var reference = new ResourceReference | ||
| { | ||
| Url = new Uri(expectedUrl, UriKind.Relative), | ||
| }; | ||
|
|
||
| // Act | ||
| string result = reference.GetComponentDefinition(); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(result); | ||
| Assert.Empty(result); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Missing Dispose call on local IDisposable Warning