Ejemplo n.º 1
0
        private async Task <bool> IsFormatSupportedAsync(ResourceFormat resourceFormat)
        {
            if (_supportedFormats.TryGetValue(resourceFormat, out var isSupported))
            {
                return(isSupported);
            }

            var typedStatement = await _conformanceProvider.GetCapabilityStatementAsync();

            CapabilityStatement statement = typedStatement.ToPoco <CapabilityStatement>();

            return(_supportedFormats.GetOrAdd(resourceFormat, format =>
            {
                switch (resourceFormat)
                {
                case ResourceFormat.Json:
                    return statement.Format.Any(f => f.Contains("json", StringComparison.Ordinal));

                case ResourceFormat.Xml:
                    return statement.Format.Any(f => f.Contains("xml", StringComparison.Ordinal));

                default:
                    return false;
                }
            }));
        }
        private async Task <bool> IsFormatSupportedAsync(ResourceFormat resourceFormat)
        {
            if (_supportedFormats.TryGetValue(resourceFormat, out var isSupported))
            {
                return(isSupported);
            }

            ResourceElement typedStatement = await _conformanceProvider.GetCapabilityStatementAsync();

            IEnumerable <string> formats = typedStatement.Select("CapabilityStatement.format").Select(x => (string)x.Value);

            return(_supportedFormats.GetOrAdd(resourceFormat, format =>
            {
                switch (resourceFormat)
                {
                case ResourceFormat.Json:
                    return formats.Any(f => f.Contains("json", StringComparison.Ordinal));

                case ResourceFormat.Xml:
                    return formats.Any(f => f.Contains("xml", StringComparison.Ordinal));

                default:
                    return false;
                }
            }));
        }
Ejemplo n.º 3
0
        public FhirRepositoryTests()
        {
            _dataStore           = Substitute.For <IDataStore>();
            _conformanceProvider = Substitute.For <ConformanceProviderBase>();

            // TODO: FhirRepository instantiate ResourceDeserializer class directly
            // which will try to deserialize the raw resource. We should mock it as well.
            _rawResourceFactory     = Substitute.For <RawResourceFactory>(new FhirJsonSerializer());
            _resourceWrapperFactory = Substitute.For <IResourceWrapperFactory>();
            _resourceWrapperFactory
            .Create(Arg.Any <Resource>(), Arg.Any <bool>())
            .Returns(x => CreateResourceWrapper(x.ArgAt <Resource>(0), x.ArgAt <bool>(1)));

            _conformanceStatement = CapabilityStatementMock.GetMockedCapabilityStatement();
            CapabilityStatementMock.SetupMockResource(_conformanceStatement, ResourceType.Observation, null);
            var observationResource = _conformanceStatement.Rest.First().Resource.Find(x => x.Type == ResourceType.Observation);

            observationResource.ReadHistory  = false;
            observationResource.UpdateCreate = true;
            observationResource.Versioning   = CapabilityStatement.ResourceVersionPolicy.Versioned;

            CapabilityStatementMock.SetupMockResource(_conformanceStatement, ResourceType.Patient, null);
            var patientResource = _conformanceStatement.Rest.First().Resource.Find(x => x.Type == ResourceType.Patient);

            patientResource.ReadHistory  = true;
            patientResource.UpdateCreate = true;
            patientResource.Versioning   = CapabilityStatement.ResourceVersionPolicy.VersionedUpdate;

            _conformanceProvider.GetCapabilityStatementAsync().Returns(_conformanceStatement);
            _repository = new FhirRepository(_dataStore, new Lazy <IConformanceProvider>(() => _conformanceProvider), _resourceWrapperFactory);
        }
Ejemplo n.º 4
0
        public ResourceHandlerTests()
        {
            _fhirDataStore       = Substitute.For <IFhirDataStore>();
            _conformanceProvider = Substitute.For <ConformanceProviderBase>();
            _searchService       = Substitute.For <ISearchService>();

            // TODO: FhirRepository instantiate ResourceDeserializer class directly
            // which will try to deserialize the raw resource. We should mock it as well.
            _rawResourceFactory     = Substitute.For <RawResourceFactory>(new FhirJsonSerializer());
            _resourceWrapperFactory = Substitute.For <IResourceWrapperFactory>();
            _resourceWrapperFactory
            .Create(Arg.Any <ResourceElement>(), Arg.Any <bool>(), Arg.Any <bool>())
            .Returns(x => CreateResourceWrapper(x.ArgAt <ResourceElement>(0), x.ArgAt <bool>(1)));

            _conformanceStatement = CapabilityStatementMock.GetMockedCapabilityStatement();
            CapabilityStatementMock.SetupMockResource(_conformanceStatement, ResourceType.Observation, null);
            var observationResource = _conformanceStatement.Rest.First().Resource.Find(x => x.Type == ResourceType.Observation);

            observationResource.ReadHistory       = false;
            observationResource.UpdateCreate      = true;
            observationResource.ConditionalCreate = true;
            observationResource.ConditionalUpdate = true;
            observationResource.Versioning        = CapabilityStatement.ResourceVersionPolicy.Versioned;

            CapabilityStatementMock.SetupMockResource(_conformanceStatement, ResourceType.Patient, null);
            var patientResource = _conformanceStatement.Rest.First().Resource.Find(x => x.Type == ResourceType.Patient);

            patientResource.ReadHistory       = true;
            patientResource.UpdateCreate      = true;
            patientResource.ConditionalCreate = true;
            patientResource.ConditionalUpdate = true;
            patientResource.Versioning        = CapabilityStatement.ResourceVersionPolicy.VersionedUpdate;

            _conformanceProvider.GetCapabilityStatementAsync().Returns(_conformanceStatement.ToTypedElement().ToResourceElement());
            var lazyConformanceProvider = new Lazy <IConformanceProvider>(() => _conformanceProvider);

            var collection = new ServiceCollection();

            // an auth service that allows all.
            _authorizationService = Substitute.For <IFhirAuthorizationService>();
            _authorizationService.CheckAccess(Arg.Any <DataActions>()).Returns(ci => ci.Arg <DataActions>());

            var referenceResolver = new ResourceReferenceResolver(_searchService, new TestQueryStringParser());

            _resourceIdProvider = new ResourceIdProvider();
            collection.Add(x => _mediator).Singleton().AsSelf();
            collection.Add(x => new CreateResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, _resourceIdProvider, referenceResolver, _authorizationService)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new UpsertResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, _resourceIdProvider, _authorizationService, ModelInfoProvider.Instance)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new ConditionalCreateResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, _searchService, x.GetService <IMediator>(), _resourceIdProvider, _authorizationService)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new ConditionalUpsertResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, _searchService, x.GetService <IMediator>(), _resourceIdProvider, _authorizationService)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new GetResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, _resourceIdProvider, _authorizationService)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new DeleteResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, _resourceIdProvider, _authorizationService)).Singleton().AsSelf().AsImplementedInterfaces();

            ServiceProvider provider = collection.BuildServiceProvider();

            _mediator = new Mediator(type => provider.GetService(type));

            _deserializer = new ResourceDeserializer(
                (FhirResourceFormat.Json, new Func <string, string, DateTimeOffset, ResourceElement>((str, version, lastUpdated) => _fhirJsonParser.Parse(str).ToResourceElement())));
        }
Ejemplo n.º 5
0
        public async Task <GetCapabilitiesResponse> Handle(GetCapabilitiesRequest message, CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(message, nameof(message));

            var response = await _provider.GetCapabilityStatementAsync(cancellationToken);

            return(new GetCapabilitiesResponse(response.ToResourceElement()));
        }
        public ValidateCapabilityPreProcessorTests()
        {
            var statement = CapabilityStatementMock.GetMockedCapabilityStatement();

            CapabilityStatementMock.SetupMockResource(statement, ResourceType.Observation, interactions: new[] { TypeRestfulInteraction.Read });

            _conformanceProvider = Substitute.For <ConformanceProviderBase>();
            _conformanceProvider.GetCapabilityStatementAsync().Returns(statement.ToTypedElement().ToResourceElement());
        }
Ejemplo n.º 7
0
        public ValidateContentTypeFilterAttributeTests()
        {
            _statement = new CapabilityStatement
            {
                Format = new List <string> {
                    "json"
                },
            };

            _conformanceProvider = Substitute.For <IConformanceProvider>();
            _conformanceProvider.GetCapabilityStatementAsync().Returns(_statement.ToTypedElement().ToResourceElement());
        }
Ejemplo n.º 8
0
        public ResourceHandlerTests()
        {
            _fhirDataStore       = Substitute.For <IFhirDataStore>();
            _conformanceProvider = Substitute.For <ConformanceProviderBase>();
            _searchService       = Substitute.For <ISearchService>();

            // TODO: FhirRepository instantiate ResourceDeserializer class directly
            // which will try to deserialize the raw resource. We should mock it as well.
            _rawResourceFactory     = Substitute.For <RawResourceFactory>(new FhirJsonSerializer());
            _resourceWrapperFactory = Substitute.For <IResourceWrapperFactory>();
            _resourceWrapperFactory
            .Create(Arg.Any <ResourceElement>(), Arg.Any <bool>())
            .Returns(x => CreateResourceWrapper(x.ArgAt <ResourceElement>(0), x.ArgAt <bool>(1)));

            _conformanceStatement = CapabilityStatementMock.GetMockedCapabilityStatement();
            CapabilityStatementMock.SetupMockResource(_conformanceStatement, ResourceType.Observation, null);
            var observationResource = _conformanceStatement.Rest.First().Resource.Find(x => x.Type == ResourceType.Observation);

            observationResource.ReadHistory       = false;
            observationResource.UpdateCreate      = true;
            observationResource.ConditionalCreate = true;
            observationResource.ConditionalUpdate = true;
            observationResource.Versioning        = CapabilityStatement.ResourceVersionPolicy.Versioned;

            CapabilityStatementMock.SetupMockResource(_conformanceStatement, ResourceType.Patient, null);
            var patientResource = _conformanceStatement.Rest.First().Resource.Find(x => x.Type == ResourceType.Patient);

            patientResource.ReadHistory       = true;
            patientResource.UpdateCreate      = true;
            patientResource.ConditionalCreate = true;
            patientResource.ConditionalUpdate = true;
            patientResource.Versioning        = CapabilityStatement.ResourceVersionPolicy.VersionedUpdate;

            _conformanceProvider.GetCapabilityStatementAsync().Returns(_conformanceStatement.ToTypedElement().ToResourceElement());
            var lazyConformanceProvider = new Lazy <IConformanceProvider>(() => _conformanceProvider);

            var collection = new ServiceCollection();

            collection.Add(x => _mediator).Singleton().AsSelf();
            collection.Add(x => new CreateResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new UpsertResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new ConditionalCreateResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, _searchService, x.GetService <IMediator>(), () => true)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new ConditionalUpsertResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, _searchService, x.GetService <IMediator>(), () => true)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new GetResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory, Deserializers.ResourceDeserializer)).Singleton().AsSelf().AsImplementedInterfaces();
            collection.Add(x => new DeleteResourceHandler(_fhirDataStore, lazyConformanceProvider, _resourceWrapperFactory)).Singleton().AsSelf().AsImplementedInterfaces();

            ServiceProvider provider = collection.BuildServiceProvider();

            _mediator = new Mediator(type => provider.GetService(type));
        }