Ejemplo n.º 1
0
        public async Task ExecuteAsync(Container collection)
        {
            EnsureArg.IsNotNull(collection, nameof(collection));

            // Detect if registry has been initialized
            var partitionKey = new PartitionKey(SearchParameterStatusWrapper.SearchParameterStatusPartitionKey);
            var query        = _queryFactory.Create <dynamic>(
                collection,
                new CosmosQueryContext(
                    new QueryDefinition($"SELECT TOP 1 * FROM c where c.{KnownDocumentProperties.PartitionKey} = '{SearchParameterStatusWrapper.SearchParameterStatusPartitionKey}'"),
                    new QueryRequestOptions {
                PartitionKey = partitionKey
            }));

            var results = await query.ExecuteNextAsync();

            if (!results.Any())
            {
                var statuses = await _filebasedRegistry.GetSearchParameterStatuses();

                foreach (var batch in statuses.TakeBatch(100))
                {
                    TransactionalBatch transaction = collection.CreateTransactionalBatch(partitionKey);

                    foreach (SearchParameterStatusWrapper status in batch.Select(x => x.ToSearchParameterStatusWrapper()))
                    {
                        transaction.CreateItem(status);
                    }

                    await transaction.ExecuteAsync();
                }
            }
        }
Ejemplo n.º 2
0
        public async Task ExecuteAsync(IDocumentClient client, DocumentCollection collection, Uri relativeCollectionUri)
        {
            EnsureArg.IsNotNull(client, nameof(client));
            EnsureArg.IsNotNull(collection, nameof(collection));
            EnsureArg.IsNotNull(relativeCollectionUri, nameof(relativeCollectionUri));

            // Detect if registry has been initialized
            var query = _queryFactory.Create <dynamic>(
                client,
                new CosmosQueryContext(
                    relativeCollectionUri,
                    new SqlQuerySpec($"SELECT TOP 1 * FROM c where c.{KnownDocumentProperties.PartitionKey} = '{SearchParameterStatusWrapper.SearchParameterStatusPartitionKey}'")));

            var results = await query.ExecuteNextAsync();

            if (!results.Any())
            {
                var statuses = await _filebasedRegistry.GetSearchParameterStatuses();

                foreach (SearchParameterStatusWrapper status in statuses.Select(x => x.ToSearchParameterStatusWrapper()))
                {
                    await client.UpsertDocumentAsync(relativeCollectionUri, status);
                }
            }
        }
        public async Task EnsureInitialized()
        {
            var updated       = new List <SearchParameterInfo>();
            var newParameters = new List <ResourceSearchParameterStatus>();

            var parameters = (await _searchParameterRegistry.GetSearchParameterStatuses())
                             .ToDictionary(x => x.Uri);

            // Set states of known parameters
            foreach (var p in _searchParameterDefinitionManager.AllSearchParameters)
            {
                if (parameters.TryGetValue(p.Url, out ResourceSearchParameterStatus result))
                {
                    bool isSearchable = result.Status == SearchParameterStatus.Enabled;
                    bool isSupported  = result.Status != SearchParameterStatus.Disabled;

                    if (result.Status == SearchParameterStatus.Disabled)
                    {
                        // Re-check if this parameter is now supported.
                        isSupported = _searchParameterSupportResolver.IsSearchParameterSupported(p);
                    }

                    if (p.IsSearchable != isSearchable ||
                        p.IsSupported != isSupported ||
                        p.IsPartiallySupported != result.IsPartiallySupported)
                    {
                        p.IsSearchable         = isSearchable;
                        p.IsSupported          = isSupported;
                        p.IsPartiallySupported = result.IsPartiallySupported;

                        updated.Add(p);
                    }
                }
                else
                {
                    newParameters.Add(new ResourceSearchParameterStatus
                    {
                        Uri         = p.Url,
                        LastUpdated = Clock.UtcNow,
                        Status      = SearchParameterStatus.Supported,
                    });

                    p.IsSearchable = false;

                    // Check if this parameter is now supported.
                    p.IsSupported = _searchParameterSupportResolver.IsSearchParameterSupported(p);

                    updated.Add(p);
                }
            }

            if (newParameters.Any())
            {
                await _searchParameterRegistry.UpdateStatuses(newParameters);
            }

            await _mediator.Publish(new SearchParametersUpdated(updated));
        }
        public async Task EnsureInitialized()
        {
            var updated = new List <SearchParameterInfo>();

            var parameters = (await _searchParameterRegistry.GetSearchParameterStatuses())
                             .ToDictionary(x => x.Uri);

            // Set states of known parameters
            foreach (var p in _searchParameterDefinitionManager.AllSearchParameters)
            {
                if (parameters.TryGetValue(p.Url, out ResourceSearchParameterStatus result))
                {
                    bool isSearchable         = result.Status == SearchParameterStatus.Enabled;
                    bool isSupported          = result.Status != SearchParameterStatus.Disabled;
                    bool isPartiallySupported = result.IsPartiallySupported;

                    if (result.Status == SearchParameterStatus.Disabled)
                    {
                        // Re-check if this parameter is now supported.
                        (bool Supported, bool IsPartiallySupported)supportedResult = _searchParameterSupportResolver.IsSearchParameterSupported(p);
                        isSupported          = supportedResult.Supported;
                        isPartiallySupported = supportedResult.IsPartiallySupported;
                    }

                    if (p.IsSearchable != isSearchable ||
                        p.IsSupported != isSupported ||
                        p.IsPartiallySupported != isPartiallySupported)
                    {
                        p.IsSearchable         = isSearchable;
                        p.IsSupported          = isSupported;
                        p.IsPartiallySupported = isPartiallySupported;

                        updated.Add(p);
                    }
                }
                else
                {
                    p.IsSearchable = false;

                    // Check if this parameter is now supported.
                    (bool Supported, bool IsPartiallySupported)supportedResult = _searchParameterSupportResolver.IsSearchParameterSupported(p);
                    p.IsSupported          = supportedResult.Supported;
                    p.IsPartiallySupported = supportedResult.IsPartiallySupported;

                    updated.Add(p);
                }
            }

            var searchParameterHashValue = SearchHelperUtilities.CalculateSearchParameterHash(parameters.Values);
            await _mediator.Publish(new SearchParametersHashUpdated(searchParameterHashValue));

            await _mediator.Publish(new SearchParametersUpdated(updated));
        }
Ejemplo n.º 5
0
        public CosmosDbStatusRegistryInitializerTests()
        {
            ISearchParameterRegistry searchParameterRegistry = Substitute.For <ISearchParameterRegistry>();

            _cosmosDocumentQueryFactory = Substitute.For <ICosmosDocumentQueryFactory>();

            _initializer = new CosmosDbStatusRegistryInitializer(
                () => searchParameterRegistry,
                _cosmosDocumentQueryFactory);

            _testParameterUri = new Uri("/test", UriKind.Relative);
            searchParameterRegistry
            .GetSearchParameterStatuses()
            .Returns(new[]
            {
                new ResourceSearchParameterStatus
                {
                    Uri                  = _testParameterUri,
                    Status               = SearchParameterStatus.Enabled,
                    LastUpdated          = Clock.UtcNow,
                    IsPartiallySupported = false,
                },
            });
        }
        public SearchParameterStatusManagerTests()
        {
            _searchParameterRegistry          = Substitute.For <ISearchParameterRegistry>();
            _searchParameterDefinitionManager = Substitute.For <ISearchParameterDefinitionManager>();
            _searchParameterSupportResolver   = Substitute.For <ISearchParameterSupportResolver>();
            _mediator = Substitute.For <IMediator>();

            _manager = new SearchParameterStatusManager(
                _searchParameterRegistry,
                _searchParameterDefinitionManager,
                _searchParameterSupportResolver,
                _mediator);

            _searchParameterRegistry.GetSearchParameterStatuses()
            .Returns(new[]
            {
                new ResourceSearchParameterStatus
                {
                    Status = SearchParameterStatus.Enabled,
                    Uri    = new Uri(ResourceId),
                },
                new ResourceSearchParameterStatus
                {
                    Status = SearchParameterStatus.Enabled,
                    Uri    = new Uri(ResourceLastupdated),
                    IsPartiallySupported = true,
                },
                new ResourceSearchParameterStatus
                {
                    Status = SearchParameterStatus.Disabled,
                    Uri    = new Uri(ResourceProfile),
                },
                new ResourceSearchParameterStatus
                {
                    Status = SearchParameterStatus.Supported,
                    Uri    = new Uri(ResourceSecurity),
                },
            });

            _queryParameter       = new SearchParameterInfo("_query", SearchParamType.Token, new Uri(ResourceQuery));
            _searchParameterInfos = new[]
            {
                new SearchParameterInfo("_id", SearchParamType.Token, new Uri(ResourceId)),
                new SearchParameterInfo("_lastUpdated", SearchParamType.Token, new Uri(ResourceLastupdated)),
                new SearchParameterInfo("_profile", SearchParamType.Token, new Uri(ResourceProfile)),
                new SearchParameterInfo("_security", SearchParamType.Token, new Uri(ResourceSecurity)),
                _queryParameter,
            };

            _searchParameterDefinitionManager.GetSearchParameters("Account")
            .Returns(_searchParameterInfos);

            _searchParameterDefinitionManager.AllSearchParameters
            .Returns(_searchParameterInfos);

            _searchParameterDefinitionManager.GetSearchParameter(new Uri(ResourceQuery))
            .Returns(_queryParameter);

            _searchParameterSupportResolver
            .IsSearchParameterSupported(Arg.Any <SearchParameterInfo>())
            .Returns((false, false));

            _searchParameterSupportResolver
            .IsSearchParameterSupported(Arg.Is(_searchParameterInfos[4]))
            .Returns((true, false));
        }
Ejemplo n.º 7
0
        public SearchParameterDefinitionManagerTests()
        {
            _searchParameterSupportResolver = Substitute.For <ISearchParameterSupportResolver>();
            _mediator = Substitute.For <IMediator>();
            _searchParameterRegistry          = Substitute.For <ISearchParameterRegistry>();
            _searchParameterDefinitionManager = new SearchParameterDefinitionManager(ModelInfoProvider.Instance);
            _fhirRequestContextAccessor       = Substitute.For <IFhirRequestContextAccessor>();
            _fhirRequestContextAccessor.FhirRequestContext.Returns(_fhirRequestContext);

            _manager = new SearchParameterStatusManager(
                _searchParameterRegistry,
                _searchParameterDefinitionManager,
                _searchParameterSupportResolver,
                _mediator);

            _searchParameterRegistry.GetSearchParameterStatuses()
            .Returns(new[]
            {
                new ResourceSearchParameterStatus
                {
                    Status = SearchParameterStatus.Enabled,
                    Uri    = new Uri(ResourceId),
                },
                new ResourceSearchParameterStatus
                {
                    Status = SearchParameterStatus.Enabled,
                    Uri    = new Uri(ResourceLastUpdated),
                    IsPartiallySupported = true,
                },
                new ResourceSearchParameterStatus
                {
                    Status = SearchParameterStatus.Disabled,
                    Uri    = new Uri(ResourceProfile),
                },
                new ResourceSearchParameterStatus
                {
                    Status = SearchParameterStatus.Supported,
                    Uri    = new Uri(ResourceSecurity),
                },
            });

            _queryParameter       = new SearchParameterInfo("_query", SearchParamType.Token, new Uri(ResourceQuery));
            _searchParameterInfos = new[]
            {
                new SearchParameterInfo("_id", SearchParamType.Token, new Uri(ResourceId)),
                new SearchParameterInfo("_lastUpdated", SearchParamType.Token, new Uri(ResourceLastUpdated)),
                new SearchParameterInfo("_profile", SearchParamType.Token, new Uri(ResourceProfile)),
                new SearchParameterInfo("_security", SearchParamType.Token, new Uri(ResourceSecurity)),
                _queryParameter,
            };

            _testSearchParamInfo = new SearchParameterInfo("_test", SearchParamType.Special, new Uri(ResourceTest));

            _searchParameterSupportResolver
            .IsSearchParameterSupported(Arg.Any <SearchParameterInfo>())
            .Returns((false, false));

            _searchParameterSupportResolver
            .IsSearchParameterSupported(Arg.Is(_searchParameterInfos[4]))
            .Returns((true, false));

            _searchParameterDefinitionManager.Start();
        }
Ejemplo n.º 8
0
        public async Task EnsureInitialized()
        {
            var updated = new List <SearchParameterInfo>();
            var resourceTypeSearchParamStatusMap = new Dictionary <string, List <ResourceSearchParameterStatus> >();

            var parameters = (await _searchParameterRegistry.GetSearchParameterStatuses())
                             .ToDictionary(x => x.Uri);

            // Set states of known parameters
            foreach (SearchParameterInfo p in _searchParameterDefinitionManager.AllSearchParameters)
            {
                if (parameters.TryGetValue(p.Url, out ResourceSearchParameterStatus result))
                {
                    bool isSearchable         = result.Status == SearchParameterStatus.Enabled;
                    bool isSupported          = result.Status != SearchParameterStatus.Disabled;
                    bool isPartiallySupported = result.IsPartiallySupported;

                    if (result.Status == SearchParameterStatus.Disabled)
                    {
                        // Re-check if this parameter is now supported.
                        (bool Supported, bool IsPartiallySupported)supportedResult = _searchParameterSupportResolver.IsSearchParameterSupported(p);
                        isSupported          = supportedResult.Supported;
                        isPartiallySupported = supportedResult.IsPartiallySupported;
                    }

                    if (p.IsSearchable != isSearchable ||
                        p.IsSupported != isSupported ||
                        p.IsPartiallySupported != isPartiallySupported)
                    {
                        p.IsSearchable         = isSearchable;
                        p.IsSupported          = isSupported;
                        p.IsPartiallySupported = isPartiallySupported;

                        updated.Add(p);
                    }
                }
                else
                {
                    p.IsSearchable = false;

                    // Check if this parameter is now supported.
                    (bool Supported, bool IsPartiallySupported)supportedResult = _searchParameterSupportResolver.IsSearchParameterSupported(p);
                    p.IsSupported          = supportedResult.Supported;
                    p.IsPartiallySupported = supportedResult.IsPartiallySupported;

                    updated.Add(p);
                }

                // We need to keep track of supported or partially supported parameters.
                // These parameters will be used to calculate the search parameter hash below.
                if (p.IsPartiallySupported || p.IsSupported)
                {
                    if (result == null)
                    {
                        result = new ResourceSearchParameterStatus()
                        {
                            Uri         = p.Url,
                            Status      = SearchParameterStatus.Supported,
                            LastUpdated = Clock.UtcNow,
                        };
                    }

                    if (p.TargetResourceTypes != null)
                    {
                        foreach (string resourceType in p.TargetResourceTypes)
                        {
                            if (resourceTypeSearchParamStatusMap.ContainsKey(resourceType))
                            {
                                resourceTypeSearchParamStatusMap[resourceType].Add(result);
                            }
                            else
                            {
                                resourceTypeSearchParamStatusMap.Add(resourceType, new List <ResourceSearchParameterStatus>()
                                {
                                    result
                                });
                            }
                        }
                    }

                    if (p.BaseResourceTypes != null)
                    {
                        foreach (string resourceType in p.BaseResourceTypes)
                        {
                            if (resourceTypeSearchParamStatusMap.ContainsKey(resourceType))
                            {
                                resourceTypeSearchParamStatusMap[resourceType].Add(result);
                            }
                            else
                            {
                                resourceTypeSearchParamStatusMap.Add(resourceType, new List <ResourceSearchParameterStatus>()
                                {
                                    result
                                });
                            }
                        }
                    }
                }
            }

            var resourceHashMap = new Dictionary <string, string>();

            foreach (KeyValuePair <string, List <ResourceSearchParameterStatus> > kvp in resourceTypeSearchParamStatusMap)
            {
                string searchParamHash = SearchHelperUtilities.CalculateSearchParameterHash(kvp.Value);
                resourceHashMap.Add(kvp.Key, searchParamHash);
            }

            await _mediator.Publish(new SearchParametersHashUpdated(resourceHashMap));

            await _mediator.Publish(new SearchParametersUpdated(updated));
        }