public async Task <IActionResult> GetFundingPeriods(HttpRequest request) { IEnumerable <Period> fundingPeriods = await _cacheProvider.GetAsync <Period[]>(CacheKeys.FundingPeriods); if (fundingPeriods.IsNullOrEmpty()) { fundingPeriods = await _specificationsRepository.GetPeriods(); if (!fundingPeriods.IsNullOrEmpty()) { await _cacheProvider.SetAsync <Period[]>(CacheKeys.FundingPeriods, fundingPeriods.ToArraySafe(), TimeSpan.FromDays(100), true); } else { return(new InternalServerErrorResult("Failed to find any funding periods")); } } return(new OkObjectResult(fundingPeriods)); }
public async Task GetFundingPeriods_GivenFundingPeriodsNotInCacheButGetsFromCosmos_ReturnsFundingPeriods() { // Arrange HttpRequest request = Substitute.For <HttpRequest>(); Period[] fundingPeriods = { new Period(), new Period() }; ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <Period[]>(Arg.Is(CacheKeys.FundingPeriods)) .Returns((Period[])null); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .GetPeriods() .Returns(fundingPeriods); IFundingService fundingService = CreateService(specificationsRepository: specificationsRepository, cacheProvider: cacheProvider); // Act IActionResult result = await fundingService.GetFundingPeriods(request); // Assert result .Should() .BeOfType <OkObjectResult>(); await cacheProvider .Received(1) .SetAsync <Period[]>(Arg.Any <string>(), Arg.Any <Period[]>(), Arg.Any <TimeSpan>(), Arg.Any <bool>()); }