public async Task CacheReloadServiceReloadIsSuccessfulForCreate() { // arrange const int NumerOfSummaryItems = 2; const int NumberOfDeletions = 3; var cancellationToken = new CancellationToken(false); var expectedValidContentPageModel = BuildValidContentPageModel(); var fakePagesSummaryItemModels = BuldFakePagesSummaryItemModel(NumerOfSummaryItems); var fakeCachedContentPageModels = BuldFakeContentPageModels(NumberOfDeletions); A.CallTo(() => fakeCmsApiService.GetSummaryAsync <CmsApiSummaryItemModel>()).Returns(fakePagesSummaryItemModels); A.CallTo(() => fakeCmsApiService.GetItemAsync <CmsApiDataModel>(A <Uri> .Ignored)).Returns(A.Fake <CmsApiDataModel>()); A.CallTo(() => fakeMapper.Map <ContentPageModel>(A <CmsApiDataModel> .Ignored)).Returns(expectedValidContentPageModel); A.CallTo(() => fakeEventMessageService.UpdateAsync(A <ContentPageModel> .Ignored)).Returns(HttpStatusCode.NotFound); A.CallTo(() => fakeEventMessageService.CreateAsync(A <ContentPageModel> .Ignored)).Returns(HttpStatusCode.Created); A.CallTo(() => fakeEventMessageService.GetAllCachedItemsAsync()).Returns(fakeCachedContentPageModels); A.CallTo(() => fakeEventMessageService.DeleteAsync(A <Guid> .Ignored)).Returns(HttpStatusCode.OK); A.CallTo(() => fakeContentCacheService.AddOrReplace(A <Guid> .Ignored, A <List <Guid> > .Ignored, A <string> .Ignored)); var cacheReloadService = new CacheReloadService(fakeLogger, fakeMapper, fakeEventMessageService, fakeCmsApiService, fakeContentCacheService, fakeAppRegistryService, fakeContentTypeMappingService, fakeApiCacheService); // act await cacheReloadService.Reload(cancellationToken).ConfigureAwait(false); // assert A.CallTo(() => fakeContentTypeMappingService.AddMapping(A <string> .Ignored, A <Type> .Ignored)).MustHaveHappenedOnceOrMore(); A.CallTo(() => fakeCmsApiService.GetSummaryAsync <CmsApiSummaryItemModel>()).MustHaveHappenedOnceExactly(); A.CallTo(() => fakeContentCacheService.Clear()).MustHaveHappenedOnceExactly(); A.CallTo(() => fakeCmsApiService.GetItemAsync <CmsApiDataModel>(A <Uri> .Ignored)).MustHaveHappened(NumerOfSummaryItems, Times.Exactly); A.CallTo(() => fakeMapper.Map <ContentPageModel>(A <CmsApiDataModel> .Ignored)).MustHaveHappened(NumerOfSummaryItems, Times.Exactly); A.CallTo(() => fakeEventMessageService.UpdateAsync(A <ContentPageModel> .Ignored)).MustHaveHappened(NumerOfSummaryItems, Times.Exactly); A.CallTo(() => fakeEventMessageService.CreateAsync(A <ContentPageModel> .Ignored)).MustHaveHappened(NumerOfSummaryItems, Times.Exactly); A.CallTo(() => fakeEventMessageService.GetAllCachedItemsAsync()).MustHaveHappenedTwiceExactly(); A.CallTo(() => fakeEventMessageService.DeleteAsync(A <Guid> .Ignored)).MustHaveHappened(NumberOfDeletions, Times.Exactly); A.CallTo(() => fakeContentCacheService.AddOrReplace(A <Guid> .Ignored, A <List <Guid> > .Ignored, A <string> .Ignored)).MustHaveHappened(NumerOfSummaryItems, Times.Exactly); A.CallTo(() => fakeAppRegistryService.PagesDataLoadAsync()).MustHaveHappenedOnceExactly(); }
public async Task <HttpStatusCode> ProcessContentAsync(Uri url, Guid contentId) { if (url == null) { throw new ArgumentNullException(nameof(url)); } url = Combine(url.ToString(), "true?checkAncestryById=true"); // This enables the multiDirectional support needed for page locations var apiDataModel = await cmsApiService.GetItemAsync <CmsApiDataModel>(url).ConfigureAwait(false); var contentPageModel = mapper.Map <ContentPageModel>(apiDataModel); if (contentPageModel == null) { return(HttpStatusCode.NoContent); } if (!TryValidateModel(contentPageModel)) { return(HttpStatusCode.BadRequest); } var existingContentPageModel = await contentPageService.GetByIdAsync(contentId).ConfigureAwait(false); var contentResult = await eventMessageService.UpdateAsync(contentPageModel).ConfigureAwait(false); if (contentResult == HttpStatusCode.NotFound) { contentResult = await eventMessageService.CreateAsync(contentPageModel).ConfigureAwait(false); } if (contentResult == HttpStatusCode.OK || contentResult == HttpStatusCode.Created) { await eventGridService.CompareAndSendEventAsync(existingContentPageModel, contentPageModel).ConfigureAwait(false); var contentItemIds = contentPageModel.AllContentItemIds; contentItemIds.AddRange(contentPageModel.AllPageLocationIds); contentCacheService.AddOrReplace(contentId, contentItemIds); } return(contentResult); }
public async Task <HttpStatusCode> ProcessContentAsync(Uri url, Guid contentId) { var apiDataModel = await cmsApiService.GetItemAsync <CmsApiDataModel>(url).ConfigureAwait(false); var contentPageModel = mapper.Map <ContentPageModel>(apiDataModel); if (contentPageModel == null) { return(HttpStatusCode.NoContent); } if (!TryValidateModel(contentPageModel)) { return(HttpStatusCode.BadRequest); } var existingContentPageModel = await contentPageService.GetByIdAsync(contentId).ConfigureAwait(false); var contentResult = await eventMessageService.UpdateAsync(contentPageModel).ConfigureAwait(false); if (contentResult == HttpStatusCode.NotFound) { contentResult = await eventMessageService.CreateAsync(contentPageModel).ConfigureAwait(false); } if (contentResult == HttpStatusCode.OK || contentResult == HttpStatusCode.Created) { await eventGridService.CompareAndSendEventAsync(existingContentPageModel, contentPageModel).ConfigureAwait(false); var contentItemIds = contentPageModel.AllContentItemIds; contentItemIds.AddRange(contentPageModel.AllPageLocationIds); contentCacheService.AddOrReplace(contentId, contentItemIds); } return(contentResult); }
public async Task GetAndSaveItemAsync(CmsApiSummaryItemModel item, CancellationToken stoppingToken) { _ = item ?? throw new ArgumentNullException(nameof(item)); try { logger.LogInformation($"Get details for {item.Title} - {item.Url}"); var apiDataModel = await cmsApiService.GetItemAsync <CmsApiDataModel>(item.Url !).ConfigureAwait(false); if (apiDataModel == null) { logger.LogWarning($"No details returned from {item.Title} - {item.Url}"); return; } if (stoppingToken.IsCancellationRequested) { logger.LogWarning("Process item get and save cancelled"); return; } var contentPageModel = mapper.Map <ContentPageModel>(apiDataModel); if (!TryValidateModel(contentPageModel)) { logger.LogError($"Validation failure for {item.Title} - {item.Url}"); return; } logger.LogInformation($"Updating cache with {item.Title} - {item.Url}"); var result = await eventMessageService.UpdateAsync(contentPageModel).ConfigureAwait(false); if (result == HttpStatusCode.NotFound) { logger.LogInformation($"Does not exist, creating cache with {item.Title} - {item.Url}"); result = await eventMessageService.CreateAsync(contentPageModel).ConfigureAwait(false); if (result == HttpStatusCode.Created) { logger.LogInformation($"Created cache with {item.Title} - {item.Url}"); } else { logger.LogError($"Cache create error status {result} from {item.Title} - {item.Url}"); } } else { logger.LogInformation($"Updated cache with {item.Title} - {item.Url}"); } var contentItemIds = contentPageModel.AllContentItemIds; contentItemIds.AddRange(contentPageModel.AllPageLocationIds); contentCacheService.AddOrReplace(contentPageModel.Id, contentItemIds); } catch (Exception ex) { logger.LogError(ex, $"Error in get and save for {item.Title} - {item.Url}"); } }