public async Task GivenAResourceWithCorrectId_WhenUpsertingConditionallyWithOneMatch_ThenTheServerShouldReturnTheUpdatedResourceSuccessfully()
        {
            string id      = Guid.NewGuid().ToString();
            string version = Guid.NewGuid().ToString();

            var mockResultEntry = new SearchResultEntry(CreateMockResourceWrapper(Samples.GetDefaultObservation().UpdateId(id), false));

            mockResultEntry.Resource.Version.Returns(version);

            ConditionalUpsertResourceRequest message = SetupConditionalUpdate(
                SaveOutcomeType.Updated,
                Samples.GetDefaultObservation().UpdateId(id),
                mockResultEntry);

            UpsertResourceResponse result = await _mediator.Send <UpsertResourceResponse>(message);

            Assert.Equal(SaveOutcomeType.Updated, result.Outcome.Outcome);

            await _fhirDataStore.Received().UpsertAsync(
                Arg.Is <ResourceWrapper>(x => x.ResourceId == id),
                Arg.Is <WeakETag>(x => x.VersionId == version),
                true,
                true,
                Arg.Any <CancellationToken>());
        }
        public async Task GivenAResource_WhenUpsertingConditionallyWithNoIdAndNoExisting_ThenTheServerShouldReturnTheUpdatedResourceSuccessfully()
        {
            ConditionalUpsertResourceRequest message = SetupConditionalUpdate(SaveOutcomeType.Created, Samples.GetDefaultObservation());

            UpsertResourceResponse result = await _mediator.Send <UpsertResourceResponse>(message);

            Assert.Equal(SaveOutcomeType.Created, result.Outcome.Outcome);
            var deserialized = result.Outcome.RawResourceElement.ToPoco <Observation>(Deserializers.ResourceDeserializer).ToResourceElement();
            await _fhirDataStore.Received().UpsertAsync(Arg.Is <ResourceWrapper>(x => x.ResourceId == deserialized.Id), null, true, true, Arg.Any <CancellationToken>());
        }
        public async Task GivenAResourceWithIncorrectId_WhenUpsertingConditionallyWithOneMatch_TheServerShouldFail()
        {
            var mockResultEntry = new SearchResultEntry(CreateMockResourceWrapper(Samples.GetDefaultObservation().UpdateId(Guid.NewGuid().ToString()), false));

            ConditionalUpsertResourceRequest message = SetupConditionalUpdate(
                SaveOutcomeType.Updated,
                Samples.GetDefaultObservation().UpdateId(Guid.NewGuid().ToString()),
                mockResultEntry);

            await Assert.ThrowsAsync <BadRequestException>(() => _mediator.Send <UpsertResourceResponse>(message));
        }
        public async Task GivenAResource_WhenUpsertingConditionallyWithAnIdAndNoExisting_ThenTheServerShouldReturnTheCreatedResourceSuccessfully()
        {
            string id = Guid.NewGuid().ToString();

            ConditionalUpsertResourceRequest message = SetupConditionalUpdate(SaveOutcomeType.Created, Samples.GetDefaultObservation().UpdateId(id));

            UpsertResourceResponse result = await _mediator.Send <UpsertResourceResponse>(message);

            Assert.Equal(SaveOutcomeType.Created, result.Outcome.Outcome);

            await _fhirDataStore.Received().UpsertAsync(Arg.Is <ResourceWrapper>(x => x.ResourceId == id), null, true, true, Arg.Any <CancellationToken>());
        }
        public async Task GivenAResource_WhenUpsertingConditionallyWithMultipleMatches_TheServerShouldFail()
        {
            ResourceWrapper mockResourceWrapper1 = CreateMockResourceWrapper(Samples.GetDefaultObservation().UpdateId(Guid.NewGuid().ToString()), false);
            ResourceWrapper mockResourceWrapper2 = CreateMockResourceWrapper(Samples.GetDefaultObservation().UpdateId(Guid.NewGuid().ToString()), false);

            ConditionalUpsertResourceRequest message = SetupConditionalUpdate(
                SaveOutcomeType.Updated,
                Samples.GetDefaultObservation(),
                mockResourceWrapper1,
                mockResourceWrapper2);

            await Assert.ThrowsAsync <PreconditionFailedException>(() => _mediator.Send <UpsertResourceResponse>(message));
        }
        private ConditionalUpsertResourceRequest SetupConditionalUpdate(SaveOutcomeType outcomeType, ResourceElement requestResource, params SearchResultEntry[] searchResults)
        {
            IReadOnlyList <Tuple <string, string> > list = new[] { Tuple.Create("_tag", Guid.NewGuid().ToString()) };

            _searchService.SearchAsync(Arg.Any <string>(), list, CancellationToken.None)
            .Returns(new SearchResult(searchResults, Enumerable.Empty <Tuple <string, string> >().ToArray(), Enumerable.Empty <(string, string)>().ToArray(), null));

            _fhirDataStore.UpsertAsync(Arg.Any <ResourceWrapper>(), Arg.Any <WeakETag>(), true, true, Arg.Any <CancellationToken>())
            .Returns(x => new UpsertOutcome(x.ArgAt <ResourceWrapper>(0), outcomeType));

            var message = new ConditionalUpsertResourceRequest(requestResource, list);

            return(message);
        }
示例#7
0
 public async Task <UpsertResourceResponse> Handle(ConditionalUpsertResourceRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <UpsertResourceResponse> next)
 => await GenericHandle(next, cancellationToken);