예제 #1
0
 public async Task ProcessResource(Uri basUri, SiteResource siteResource)
 {
     foreach (var indexResourceProcessor in _indexResourceProcessors)
     {
         await indexResourceProcessor.ProcessResource(basUri, siteResource);
     }
 }
예제 #2
0
        public async Task ItShouldConvertAccountIdToUpper()
        {
            var idLower = _id.ToLower();

            MockContextBase.Setup(x => x.Request.RawUrl).Returns("https:/tempuri.org");

            var siteResource = new SiteResource {
                ResourceKey = SupportServiceResourceKey.EmployerAccountFinance, Challenge = SupportServiceResourceKey.EmployerAccountFinanceChallenge
            };

            MockPermissionsChecker.Setup(x => x.HasPermissions(It.IsAny <HttpRequestBase>(),
                                                               It.IsAny <HttpResponseBase>(),
                                                               It.IsAny <IPrincipal>(),
                                                               $"{_resourceKey.ToString()}/{_id}"))
            .Returns(false);

            ActionResultResponse = await Unit.Index(_resourceKey, idLower, _childId);

            MockManifestRepository.Setup(x => x.GetNav(It.Is <SupportServiceResourceKey>(rk => rk == _resourceKey), It.Is <string>(id => id == _id.ToUpper())));

            Assert.IsInstanceOf <RedirectToRouteResult>(ActionResultResponse);
            var result = (RedirectToRouteResult)ActionResultResponse;

            Assert.AreEqual(result.RouteValues["resourceid"], _id);
        }
예제 #3
0
        public async Task ItShouldRedirectToChallengeIfTheResourceDefinesAChallengeThatHasNotAlreadyBeenPassed()
        {
            MockContextBase.Setup(x => x.Request.RawUrl).Returns("https:/tempuri.org");

            var siteResource = new SiteResource {
                ResourceKey = SupportServiceResourceKey.EmployerAccountFinance, Challenge = SupportServiceResourceKey.EmployerAccountFinanceChallenge
            };

            MockPermissionsChecker.Setup(x => x.HasPermissions(It.IsAny <HttpRequestBase>(),
                                                               It.IsAny <HttpResponseBase>(),
                                                               It.IsAny <IPrincipal>(),
                                                               $"{_resourceKey.ToString()}/{_id}"))
            .Returns(false);

            ActionResultResponse = await Unit.Index(_resourceKey, _id, _childId);

            Assert.IsInstanceOf <RedirectToRouteResult>(ActionResultResponse);
            var result = (RedirectToRouteResult)ActionResultResponse;

            Assert.IsNotEmpty(result.RouteValues);
            Assert.AreEqual((int)siteResource.ResourceKey, result.RouteValues["resourceKey"]);
            Assert.AreEqual((int)siteResource.Challenge, result.RouteValues["challengeKey"]);
            Assert.AreEqual(_id, result.RouteValues["resourceId"]);
            Assert.AreEqual(MockContextBase.Object.Request.RawUrl, result.RouteValues["url"]);
        }
        private async Task IndexDocument(Uri baseUri, SiteResource siteResource, string newIndexName)
        {
            _queryTimer.Start();
            _logger.Info($" Downloading Index Records for type {typeof(T).Name}...");

            var    searchItemCountUri     = new Uri(baseUri, string.Format(siteResource.SearchTotalItemsUrl, 1));
            string totalSearchItemsString = null;
            var    retryCount             = 0;

            do
            {
                totalSearchItemsString = await _dataSource.Download(searchItemCountUri);
            }while (_dataSource.LastCode == HttpStatusCode.Unauthorized && ++retryCount < 3);

            ValidateDownResponse(_dataSource.LastCode);

            if (!int.TryParse(totalSearchItemsString, out int totalSearchItems))
            {
                var errorMsg = $"Get Total Search Item Count returned invalid data from : {totalSearchItemsString}";
                throw new InvalidCastException(errorMsg);
            }

            _logger.Info($"Estimated Total Search Items Count  for type {typeof(T).Name}  equals {totalSearchItems}");

            var pages = (int)Math.Ceiling(totalSearchItems / (double)_pageSize);

            for (int pageNumber = 1; pageNumber <= pages; pageNumber++)
            {
                var             searchUri = new Uri(baseUri, string.Format(siteResource.SearchItemsUrl, _pageSize, pageNumber));
                IEnumerable <T> searchItems;
                retryCount = 0;
                do
                {
                    searchItems = await _dataSource.Download <IEnumerable <T> >(searchUri);
                }while (_dataSource.LastCode == HttpStatusCode.Unauthorized && ++retryCount < 3);

                try
                {
                    ValidateDownResponse(_dataSource.LastCode);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, $" Error while retriving page {pageNumber} for type {typeof(T).Name}.");
                    // removed throw to allow index to be created when 1 or 2 pages fail to be retrieved.
                }

                _indexTimer.Start();
                _logger.Info($" Indexing Documents for type {typeof(T).Name}...page : {pageNumber}");
                _indexProvider.IndexDocuments(newIndexName, searchItems);
                _indexTimer.Stop();
                _logger.Info($"Indexing Time {_indexTimer.Elapsed} page : {pageNumber}");
            }

            _queryTimer.Stop();
            _logger.Info($"Query Elapse Time For {typeof(T).Name} : {_queryTimer.Elapsed}");
        }
        public async Task ProcessResource(Uri baseUri, SiteResource siteResource)
        {
            if (!ContinueProcessing(siteResource.SearchCategory))
            {
                return;
            }

            try
            {
                var newIndexName = _indexNameCreator.CreateNewIndexName(_searchSettings.IndexName, siteResource.SearchCategory);
                CreateIndex(newIndexName);

                try
                {
                    await IndexDocument(baseUri, siteResource, newIndexName);
                }
                catch (Exception)
                {
                    _logger.Info($"Deleting New Index {newIndexName} due to exception");
                    _indexProvider.DeleteIndex(newIndexName);
                    throw;
                }

                _logger.Info($"Creating Index Alias and Swapping from old to new index for type {typeof(T).Name}...");
                var indexAlias = _indexNameCreator.CreateIndexesAliasName(_searchSettings.IndexName, siteResource.SearchCategory);
                _indexProvider.CreateIndexAlias(newIndexName, indexAlias);

                _logger.Info($"Deleting Old Indexes for type {typeof(T).Name}...");
                _indexProvider.DeleteIndexes(_indexToRetain, indexAlias);
                _logger.Info($"Deleting Old Indexes Completed for type {typeof(T).Name}...");
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Exception while Indexing {typeof(T).Name}");
            }
        }
예제 #6
0
        public async Task ItShouldReturnTheSubviewIfTheResourceDefinesAChallengethatHasAlredyBeenPassed()
        {
            MockContextBase.Setup(x => x.Request.RawUrl).Returns("https:/tempuri.org");

            var siteResource = new SiteResource
            {
                ResourceKey = SupportServiceResourceKey.EmployerAccountFinance,
                Challenge   = SupportServiceResourceKey.EmployerAccountFinanceChallenge
            };

            MockPermissionsChecker
            .Setup(x => x.HasPermissions(It.IsAny <HttpRequestBase>(),
                                         It.IsAny <HttpResponseBase>(),
                                         It.IsAny <IPrincipal>(),
                                         $"{SupportServiceResourceKey.EmployerAccountFinanceChallenge}/{_id}"))
            .Returns(true);

            ActionResultResponse = await Unit.Index(_resourceKey, _id, _childId);

            Assert.IsInstanceOf <ViewResult>(ActionResultResponse);
            var result = (ViewResult)ActionResultResponse;

            Assert.AreEqual("Sub", result.ViewName);
        }
예제 #7
0
 public CmsServices()
 {
     _site         = new Site();
     _siteTemp     = new SiteTemp();
     _siteResource = new SiteResource();
 }
예제 #8
0
 public int Remove(SiteResource siteResource)
 {
     return(_siteResource.Delete(siteResource));
 }
예제 #9
0
 public int AddResource(SiteResource siteResource)
 {
     siteResource.Updatetime = DateTime.UtcNow;
     return(_siteResource.Add(siteResource));
 }
        protected void Initialise()
        {
            _baseUrl = new Uri("http://localhost");

            _accountModels = new List <AccountSearchModel>
            {
                new AccountSearchModel
                {
                    Account = "Valtech"
                }
            };


            _accountSiteResource = new SiteResource
            {
                SearchCategory      = SearchCategory.Account,
                SearchTotalItemsUrl = "localhost",
                SearchItemsUrl      = "localhost",
            };

            _userSiteResource = new SiteResource
            {
                SearchCategory      = SearchCategory.User,
                SearchTotalItemsUrl = "localhost",
                SearchItemsUrl      = "localhost",
            };


            _downloader    = new Mock <ISiteConnector>();
            _indexProvider = new Mock <IIndexProvider>();

            _searchSettings = new Mock <ISearchSettings>();
            _searchSettings.Setup(o => o.IndexName).Returns(_indexName);


            _logger           = new Mock <ILog>();
            _indexNameCreator = new Mock <IIndexNameCreator>();
            _elasticClient    = new Mock <IElasticsearchCustomClient>();
            _siteSettings     = new Mock <ISiteSettings>();

            _indexNameCreator
            .Setup(o => o.CreateNewIndexName(_indexName, SearchCategory.Account))
            .Returns(_indexName);

            _indexNameCreator
            .Setup(o => o.CreateIndexesAliasName(_indexName, SearchCategory.Account))
            .Returns("new_index_name_Alias");

            _elasticClient
            .Setup(o => o.IndexExists(_indexName, string.Empty))
            .Returns(false);

            _elasticClient
            .Setup(x => x.CreateIndex(_indexName, It.IsAny <Func <CreateIndexDescriptor, ICreateIndexRequest> >(), string.Empty))
            .Returns(new Common.Infrastucture.Elasticsearch.CreateIndexResponse {
                HttpStatusCode = (int)HttpStatusCode.OK
            });

            _downloader
            .Setup(o => o.Download <IEnumerable <AccountSearchModel> >(_baseUrl))
            .Returns(Task.FromResult(_accountModels));

            _downloader
            .Setup(o => o.Download(It.IsAny <Uri>()))
            .Returns(Task.FromResult("50"));

            _downloader
            .Setup(o => o.LastCode)
            .Returns(HttpStatusCode.OK);

            _indexProvider
            .Setup(o => o.DeleteIndex(_indexName));

            _indexProvider
            .Setup(o => o.CreateIndexAlias(_indexName, It.IsAny <string>()));

            _indexProvider
            .Setup(o => o.DeleteIndexes(_indexToRetain, It.IsAny <string>()));
        }