public async Task GetFundingConfigurationsByFundingStreamId_GivenFundingConfigurationsNotInCacheAndNotInDatabase_ReturnsNotFound() { // Arrange List <FundingConfiguration> fundingConfigs = null; ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <List <FundingConfiguration> >(Arg.Is(fundingConfigurationsCacheKey)) .Returns(fundingConfigs); IPolicyRepository policyRepository = CreatePolicyRepository(); policyRepository .GetFundingConfigurationsByFundingStreamId(Arg.Is(fundingStreamId)) .Returns(fundingConfigs); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService(cacheProvider: cacheProvider, policyRepository: policyRepository); // Act IActionResult result = await fundingConfigurationsService.GetFundingConfigurationsByFundingStreamId(fundingStreamId); // Assert result .Should() .BeOfType <NotFoundResult>(); }
public async Task GetFundingConfigurations_GivenSuccessResponse_ReturnsDictionary() { //Arrange SpecificationSummary specificationSummary = CreateSpecificationSummary(); IPoliciesApiClient policiesApiClient = CreatePoliciesApiClient(); policiesApiClient .GetFundingConfiguration(Arg.Any <string>(), Arg.Any <string>()) .Returns(new ApiResponse <FundingConfiguration>(System.Net.HttpStatusCode.OK, new FundingConfiguration())); FundingConfigurationService fundingConfigurationService = CreateService(policiesApiClient); //Act IDictionary <string, FundingConfiguration> results = await fundingConfigurationService.GetFundingConfigurations(specificationSummary); //Assert results .Should() .HaveCount(3); results["fs-1"] .Should() .NotBeNull(); results["fs-2"] .Should() .NotBeNull(); results["fs-3"] .Should() .NotBeNull(); }
public async Task GetFundingConfigurationsByFundingStreamId_GivenEmptyFundingStreamId_ReturnsBadRequestRequest() { // Arrange string fundingStreamId = string.Empty; ILogger logger = CreateLogger(); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService(logger: logger); // Act IActionResult result = await fundingConfigurationsService.GetFundingConfigurationsByFundingStreamId(fundingStreamId); // Assert result .Should() .BeOfType <BadRequestObjectResult>() .Which .Value .Should() .Be("Null or empty funding stream Id provided"); logger .Received(1) .Error(Arg.Is("No funding stream Id was provided to GetFundingConfigurationsByFundingStreamId")); }
public async Task GetFundingConfigurationsByFundingStreamId_GivenFundingConfigurationsInCache_ReturnsFromCache() { // Arrange List <FundingConfiguration> fundingConfigs = new List <FundingConfiguration> { new FundingConfiguration() }; ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <List <FundingConfiguration> >(Arg.Is(fundingConfigurationsCacheKey)) .Returns(fundingConfigs); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService(cacheProvider: cacheProvider); // Act IActionResult result = await fundingConfigurationsService.GetFundingConfigurationsByFundingStreamId(fundingStreamId); // Assert result .Should() .BeOfType <OkObjectResult>() .Which .Value .Should() .Be(fundingConfigs); }
public async Task GetFundingConfiguration__GivenFundingConfigurationWasFound_ReturnsSuccess(string fundingStreamId, string fundingPeriodId) { // Arrange FundingStream fundingStream = new FundingStream { Id = fundingStreamId }; FundingPeriod fundingPeriod = new FundingPeriod { Id = fundingPeriodId }; string configId = $"config-{fundingStreamId}-{fundingPeriodId}"; FundingConfiguration fundingConfiguration = new FundingConfiguration { Id = configId }; IPolicyRepository policyRepository = CreatePolicyRepository(); policyRepository .GetFundingStreamById(Arg.Is(fundingStreamId)) .Returns(fundingStream); policyRepository .GetFundingPeriodById(Arg.Is(fundingPeriodId)) .Returns(fundingPeriod); policyRepository .GetFundingConfiguration(Arg.Is(configId)) .Returns(fundingConfiguration); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService(policyRepository: policyRepository); // Act IActionResult result = await fundingConfigurationsService.GetFundingConfiguration(fundingStreamId, fundingPeriodId); // Assert result .Should() .BeOfType <OkObjectResult>() .Which .Value .Should() .Be(fundingConfiguration); FundingConfiguration fundingConfigurationResult = ((OkObjectResult)result).Value.As <FundingConfiguration>(); fundingConfigurationResult.ProviderSource.Should().Be(CalculateFunding.Models.Providers.ProviderSource.CFS); fundingConfigurationResult.PaymentOrganisationSource.Should().Be(PaymentOrganisationSource.PaymentOrganisationAsProvider); }
async public Task SaveFundingConfiguration_GivenValidConfigurationButFailedToSaveToDatabase_ReturnsStatusCode(string fundingStreamId, string fundingPeriodId) { //Arrange FundingStream fundingStream = new FundingStream { Id = fundingStreamId }; FundingPeriod fundingPeriod = new FundingPeriod { Id = fundingPeriodId }; ILogger logger = CreateLogger(); HttpStatusCode statusCode = HttpStatusCode.BadRequest; IPolicyRepository policyRepository = CreatePolicyRepository(); policyRepository .GetFundingStreamById(Arg.Is(fundingStreamId)) .Returns(fundingStream); policyRepository .GetFundingPeriodById(Arg.Is(fundingPeriodId)) .Returns(fundingPeriod); policyRepository .SaveFundingConfiguration(Arg.Is <FundingConfiguration>(x => x.FundingStreamId == fundingStreamId && x.FundingPeriodId == fundingPeriodId)) .Returns(statusCode); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService(logger: logger, policyRepository: policyRepository); FundingConfigurationViewModel fundingConfigurationViewModel = CreateConfigurationModel(); //Act IActionResult result = await fundingConfigurationsService.SaveFundingConfiguration("Action", "Controller", fundingConfigurationViewModel, fundingStreamId, fundingPeriodId); //Assert result .Should() .BeOfType <InternalServerErrorResult>(); InternalServerErrorResult statusCodeResult = (InternalServerErrorResult)result; statusCodeResult .StatusCode .Should() .Be(500); logger .Received(1) .Error(Arg.Is($"Failed to save configuration file for funding stream id: {fundingStreamId} and period id: {fundingPeriodId} to cosmos db with status 400")); }
public async Task GetFundingConfiguration__GivenFundingConfigurationAlreadyInCache_ReturnsSuccessWithConfigurationFromCache(string fundingStreamId, string fundingPeriodId) { // Arrange FundingStream fundingStream = new FundingStream { Id = fundingStreamId }; FundingPeriod fundingPeriod = new FundingPeriod { Id = fundingPeriodId }; string configId = $"config-{fundingStreamId}-{fundingPeriodId}"; FundingConfiguration fundingConfiguration = new FundingConfiguration { Id = configId }; IPolicyRepository policyRepository = CreatePolicyRepository(); policyRepository .GetFundingStreamById(Arg.Is(fundingStreamId)) .Returns(fundingStream); policyRepository .GetFundingPeriodById(Arg.Is(fundingPeriodId)) .Returns(fundingPeriod); string cacheKey = $"{CacheKeys.FundingConfig}{fundingStreamId}-{fundingPeriodId}"; ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <FundingConfiguration>(Arg.Is(cacheKey)) .Returns(fundingConfiguration); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService(policyRepository: policyRepository, cacheProvider: cacheProvider); // Act IActionResult result = await fundingConfigurationsService.GetFundingConfiguration(fundingStreamId, fundingPeriodId); // Assert result .Should() .BeOfType <OkObjectResult>() .Which .Value .Should() .Be(fundingConfiguration); }
public async Task GetFundingConfiguration_GivenFundingConfigurationDoesNotExist_ReturnsNotFoundRequest(string fundingStreamId, string fundingPeriodId) { // Arrange ILogger logger = CreateLogger(); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService(logger: logger); // Act IActionResult result = await fundingConfigurationsService.GetFundingConfiguration(fundingStreamId, fundingPeriodId); // Assert result .Should() .BeOfType <NotFoundResult>(); logger .Received(1) .Error(Arg.Is($"No funding Configuration was found for funding stream id : {fundingStreamId} and funding period id : {fundingPeriodId}")); }
public async Task GetFundingConfigurationsByFundingStreamId_GivenFundingConfigurationsNotInCacheBuInDatabase_ReturnsOKSetsInCache() { // Arrange List <FundingConfiguration> fundingConfigs = new List <FundingConfiguration> { new FundingConfiguration() }; ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <List <FundingConfiguration> >(Arg.Is(fundingConfigurationsCacheKey)) .Returns((List <FundingConfiguration>)null); IPolicyRepository policyRepository = CreatePolicyRepository(); policyRepository .GetFundingConfigurationsByFundingStreamId(Arg.Is(fundingStreamId)) .Returns(fundingConfigs); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService(cacheProvider: cacheProvider, policyRepository: policyRepository); // Act IActionResult result = await fundingConfigurationsService.GetFundingConfigurationsByFundingStreamId(fundingStreamId); // Assert result .Should() .BeOfType <OkObjectResult>() .Which .Value .Should() .Be(fundingConfigs); await cacheProvider .Received(1) .SetAsync(Arg.Is(fundingConfigurationsCacheKey), Arg.Any <List <FundingConfiguration> >()); }
public void GetFundingConfigurations_GivenApiResponseWithInvalidStatusCode_ThrowsException() { //Arrange SpecificationSummary specificationSummary = CreateSpecificationSummary(); IPoliciesApiClient policiesApiClient = CreatePoliciesApiClient(); policiesApiClient .GetFundingConfiguration(Arg.Any <string>(), Arg.Any <string>()) .Returns( new ApiResponse <FundingConfiguration>(System.Net.HttpStatusCode.OK, new FundingConfiguration()), new ApiResponse <FundingConfiguration>(System.Net.HttpStatusCode.BadRequest, null)); FundingConfigurationService fundingConfigurationService = CreateService(policiesApiClient); //Act Func <Task> test = () => fundingConfigurationService.GetFundingConfigurations(specificationSummary); //Assert test .Should() .ThrowExactly <Exception>(); }
async public Task SaveFundingConfiguration_GivenValidConfiguration_ReturnsStatusCode( string fundingStreamId, string fundingPeriodId) { FundingStream fundingStream = NewFundingStream(_ => _.WithId(fundingStreamId)); FundingPeriod fundingPeriod = NewFundingPeriod(_ => _.WithId(fundingPeriodId)); ILogger logger = CreateLogger(); HttpStatusCode statusCode = HttpStatusCode.OK; ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .RemoveAsync <List <FundingConfiguration> >($"{CacheKeys.FundingConfig}{fundingStreamId}") .Returns(Task.CompletedTask); cacheProvider .SetAsync( $"{CacheKeys.FundingConfig}{fundingStreamId}-{fundingPeriodId}", Arg.Is <FundingConfiguration>(_ => _.FundingStreamId == fundingStreamId && _.FundingPeriodId == fundingPeriodId)) .Returns(Task.CompletedTask); IPolicyRepository policyRepository = CreatePolicyRepository(); policyRepository .GetFundingStreamById(Arg.Is(fundingStreamId)) .Returns(fundingStream); policyRepository .GetFundingPeriodById(Arg.Is(fundingPeriodId)) .Returns(fundingPeriod); policyRepository .SaveFundingConfiguration(Arg.Is <FundingConfiguration>(x => x.FundingStreamId == fundingStreamId && x.FundingPeriodId == fundingPeriodId)) .Returns(statusCode); FundingConfigurationService fundingConfigurationsService = CreateFundingConfigurationService( logger: logger, policyRepository: policyRepository, cacheProvider: cacheProvider); FundingConfigurationViewModel fundingConfigurationViewModel = CreateConfigurationModel(); //Act IActionResult result = await fundingConfigurationsService.SaveFundingConfiguration("Action", "Controller", fundingConfigurationViewModel, fundingStreamId, fundingPeriodId); //Assert result .Should() .BeOfType <CreatedAtActionResult>(); await cacheProvider .Received(1) .SetAsync( $"{CacheKeys.FundingConfig}{fundingStreamId}-{fundingPeriodId}", Arg.Is <FundingConfiguration>(_ => _.FundingStreamId == fundingStreamId && _.FundingPeriodId == fundingPeriodId)); await cacheProvider .Received(1) .RemoveAsync <List <FundingConfiguration> >($"{CacheKeys.FundingConfig}{fundingStreamId}"); }