async public Task SaveFundingPeriods_GivenNoYamlWasProvidedButFileNameWas_ReturnsBadRequest() { // Arrange IHeaderDictionary headerDictionary = new HeaderDictionary(); headerDictionary .Add("yaml-file", new StringValues(yamlFile)); HttpRequest request = Substitute.For <HttpRequest>(); request .Headers .Returns(headerDictionary); ILogger logger = CreateLogger(); IFundingService fundingService = CreateService(logger: logger); // Act IActionResult result = await fundingService.SaveFundingPeriods(request); // Assert result .Should() .BeOfType <BadRequestObjectResult>(); logger .Received(1) .Error(Arg.Is($"Null or empty yaml provided for file: {yamlFile}")); }
async public Task SaveFundingPeriods_GivenValidYamlButSavingToDatabaseThrowsException_ReturnsInternalServerError() { // Arrange string yaml = CreateRawFundingPeriods(); byte[] byteArray = Encoding.UTF8.GetBytes(yaml); MemoryStream stream = new MemoryStream(byteArray); IHeaderDictionary headerDictionary = new HeaderDictionary(); headerDictionary .Add("yaml-file", new StringValues(yamlFile)); HttpRequest request = Substitute.For <HttpRequest>(); request .Headers .Returns(headerDictionary); request .Body .Returns(stream); ILogger logger = CreateLogger(); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .When(x => x.SavePeriods(Arg.Any <Period[]>())) .Do(x => { throw new Exception(); }); IFundingService fundingService = CreateService(logger: logger, specificationsRepository: specificationsRepository); // Act IActionResult result = await fundingService.SaveFundingPeriods(request); // Assert result .Should() .BeOfType <StatusCodeResult>(); StatusCodeResult statusCodeResult = (StatusCodeResult)result; statusCodeResult .StatusCode .Should() .Be(500); logger .Received(1) .Error(Arg.Any <Exception>(), Arg.Is($"Exception occurred writing to yaml file: {yamlFile} to cosmos db")); }
async public Task SaveFundingPeriods_GivenValidYamlAndSaveWasSuccesful_ReturnsOK() { // Arrange string yaml = CreateRawFundingPeriods(); byte[] byteArray = Encoding.UTF8.GetBytes(yaml); MemoryStream stream = new MemoryStream(byteArray); IHeaderDictionary headerDictionary = new HeaderDictionary(); headerDictionary .Add("yaml-file", new StringValues(yamlFile)); HttpRequest request = Substitute.For <HttpRequest>(); request .Headers .Returns(headerDictionary); request .Body .Returns(stream); ILogger logger = CreateLogger(); ICacheProvider cacheProvider = CreateCacheProvider(); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); IFundingService fundingService = CreateService(logger: logger, specificationsRepository: specificationsRepository, cacheProvider: cacheProvider); // Act IActionResult result = await fundingService.SaveFundingPeriods(request); // Assert result .Should() .BeOfType <OkResult>(); logger .Received(1) .Information(Arg.Is($"Successfully saved file: {yamlFile} to cosmos db")); await cacheProvider .Received(1) .SetAsync <Period[]>(Arg.Is(CacheKeys.FundingPeriods), Arg.Any <Period[]>(), Arg.Any <TimeSpan>(), Arg.Is(true)); }
async public Task SaveFundingPeriods_GivenNoYamlWasProvidedWithNoFileName_ReturnsBadRequest() { // Arrange HttpRequest request = Substitute.For <HttpRequest>(); ILogger logger = CreateLogger(); IFundingService fundingService = CreateService(logger: logger); // Act IActionResult result = await fundingService.SaveFundingPeriods(request); // Assert result .Should() .BeOfType <BadRequestObjectResult>(); logger .Received(1) .Error(Arg.Is($"Null or empty yaml provided for file: File name not provided")); }
async public Task SaveFundingPeriods_GivenNoYamlWasProvidedButIsInvalid_ReturnsBadRequest() { // Arrange string yaml = "invalid yaml"; byte[] byteArray = Encoding.UTF8.GetBytes(yaml); MemoryStream stream = new MemoryStream(byteArray); IHeaderDictionary headerDictionary = new HeaderDictionary(); headerDictionary .Add("yaml-file", new StringValues(yamlFile)); HttpRequest request = Substitute.For <HttpRequest>(); request .Headers .Returns(headerDictionary); request .Body .Returns(stream); ILogger logger = CreateLogger(); IFundingService fundingService = CreateService(logger: logger); // Act IActionResult result = await fundingService.SaveFundingPeriods(request); // Assert result .Should() .BeOfType <BadRequestObjectResult>(); logger .Received(1) .Error(Arg.Any <Exception>(), Arg.Is($"Invalid yaml was provided for file: {yamlFile}")); }
public async Task <IActionResult> RunSaveFundingPeriods() { return(await _fundingService.SaveFundingPeriods(ControllerContext.HttpContext.Request)); }