Пример #1
0
        public void GivenASearchResult_WhenCreateSearchBundle_ThenCorrectBundleShouldBeReturned()
        {
            _urlResolver.ResolveResourceWrapperUrl(Arg.Any <ResourceWrapper>()).Returns(x => new Uri(string.Format(_resourceUrlFormat, x.ArgAt <ResourceWrapper>(0).ResourceId)));
            _urlResolver.ResolveRouteUrl(_unsupportedSearchParameters).Returns(_selfUrl);

            ResourceElement observation1 = Samples.GetDefaultObservation().UpdateId("123");
            ResourceElement observation2 = Samples.GetDefaultObservation().UpdateId("abc");

            var resourceWrappers = new SearchResultEntry[]
            {
                new SearchResultEntry(CreateResourceWrapper(observation1, HttpMethod.Post)),
                new SearchResultEntry(CreateResourceWrapper(observation2, HttpMethod.Post)),
            };

            var searchResult = new SearchResult(resourceWrappers, continuationToken: null, sortOrder: null, unsupportedSearchParameters: _unsupportedSearchParameters);

            ResourceElement actual = null;

            using (Mock.Property(() => ClockResolver.UtcNowFunc, () => _dateTime))
            {
                actual = _bundleFactory.CreateSearchBundle(searchResult);
            }

            // Since there is no continuation token, there should not be next link.
            Assert.Null(actual.Scalar <string>("Bundle.link.where(relation='next').url"));
            Assert.Collection(
                actual.ToPoco <Bundle>().Entry,
                async e => await ValidateEntry(observation1.ToPoco <Observation>(), e),
                async e => await ValidateEntry(observation2.ToPoco <Observation>(), e));

            async Task ValidateEntry(Observation expected, Bundle.EntryComponent actualEntry)
            {
                Assert.NotNull(actualEntry);

                var raw = actualEntry as RawBundleEntryComponent;

                using (var ms = new MemoryStream())
                    using (var sr = new StreamReader(ms))
                    {
                        await raw?.ResourceElement?.SerializeToStreamAsUtf8Json(ms);

                        ms.Seek(0, SeekOrigin.Begin);
                        var resourceData = await sr.ReadToEndAsync();

                        Assert.NotNull(resourceData);

                        Resource resource;
                        resource = new FhirJsonParser().Parse(resourceData) as Resource;

                        Assert.Equal(expected.Id, resource.Id);
                        Assert.Equal(string.Format(_resourceUrlFormat, expected.Id), raw.FullUrl);
                        Assert.NotNull(raw.Search);
                        Assert.Equal(Bundle.SearchEntryMode.Match, raw.Search.Mode);
                    }
            }
        }
Пример #2
0
        public ResourceElement CreateSearchBundle(SearchResult result)
        {
            return(CreateBundle(result, Bundle.BundleType.Searchset, r =>
            {
                var resource = new RawBundleEntryComponent(r.Resource);

                resource.FullUrlElement = new FhirUri(_urlResolver.ResolveResourceWrapperUrl(r.Resource));
                resource.Search = new Bundle.SearchComponent
                {
                    Mode = r.SearchEntryMode == SearchEntryMode.Match ? Bundle.SearchEntryMode.Match : Bundle.SearchEntryMode.Include,
                };

                return resource;
            }));
        }