public async Task GetFundingPeriods_GivenFundingPeriodsNotInCacheAndFailedToGetFromCosomos_ReturnsInternalServerError()
        {
            // Arrange
            HttpRequest request = Substitute.For <HttpRequest>();

            ICacheProvider cacheProvider = CreateCacheProvider();

            cacheProvider
            .GetAsync <Period[]>(Arg.Is(CacheKeys.FundingPeriods))
            .Returns((Period[])null);

            ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository();

            IFundingService fundingService = CreateService(specificationsRepository: specificationsRepository, cacheProvider: cacheProvider);

            // Act
            IActionResult result = await fundingService.GetFundingPeriods(request);

            // Assert
            result
            .Should()
            .BeOfType <InternalServerErrorResult>();

            await
            cacheProvider
            .DidNotReceive()
            .SetAsync <Period[]>(Arg.Any <string>(), Arg.Any <Period[]>(), Arg.Any <TimeSpan>(), Arg.Any <bool>());
        }
        public async Task GetFundingPeriods_GivenFundingPeriodsInCache_ReturnsFromCache()
        {
            // Arrange
            HttpRequest request = Substitute.For <HttpRequest>();

            Period[] fundingPeriods =
            {
                new Period(),
                new Period()
            };

            ICacheProvider cacheProvider = CreateCacheProvider();

            cacheProvider
            .GetAsync <Period[]>(Arg.Is(CacheKeys.FundingPeriods))
            .Returns(fundingPeriods);

            ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository();

            IFundingService fundingService = CreateService(specificationsRepository: specificationsRepository, cacheProvider: cacheProvider);

            // Act
            IActionResult result = await fundingService.GetFundingPeriods(request);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            await
            specificationsRepository
            .DidNotReceive()
            .GetPeriods();
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetFundingPeriods(HttpRequest request)
        {
            IActionResult actionResult = await _fundingService.GetFundingPeriods(request);

            if (actionResult is OkObjectResult okObjectResult)
            {
                IEnumerable <CalculateFunding.Models.Specs.Period> periods = (IEnumerable <CalculateFunding.Models.Specs.Period>)okObjectResult.Value;
                List <Models.Period> mappedPeriods = _mapper.Map <List <Models.Period> >(periods);
                return(new OkObjectResult(mappedPeriods));
            }
            return(actionResult);
        }
        public async Task GetTimePeriods_WhenServiceReturns500InternalServerErrorResult_ShouldReturnErrorResult()
        {
            // Arrange
            IMapper mockMapper = Substitute.For <IMapper>();

            IFundingService mockFundingService = Substitute.For <IFundingService>();

            mockFundingService.GetFundingPeriods(Arg.Any <HttpRequest>()).Returns(new InternalServerErrorResult("Doesn't matter message`"));

            TimePeriodsService serviceUnderTest = new TimePeriodsService(mockFundingService, mockMapper);

            // Act
            IActionResult result = await serviceUnderTest.GetFundingPeriods(Substitute.For <HttpRequest>());

            // Assert
            result
            .Should().NotBeNull()
            .And
            .Subject.Should().BeOfType <InternalServerErrorResult>();
        }
        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>());
        }
        public async Task GetTimePeriods_WhenServiceReturns200OkResult_ShouldReturnOkResultWithFundingPeriods()
        {
            // Arrange
            Models.Specs.Period fundingPeriod1 = new Models.Specs.Period()
            {
                Id        = "AYCode",
                Name      = "AcademicYear",
                StartDate = DateTimeOffset.Now,
                EndDate   = DateTimeOffset.Now.AddYears(1)
            };
            Models.Specs.Period fundingPeriod2 = new Models.Specs.Period()
            {
                Id        = "FYCode",
                Name      = "FinalYear",
                StartDate = DateTimeOffset.Now,
                EndDate   = DateTimeOffset.Now.AddYears(1)
            };

            Mapper.Reset();
            MapperConfigurationExpression mappings = new MapperConfigurationExpression();

            mappings.AddProfile <ExternalApiMappingProfile>();
            Mapper.Initialize(mappings);
            IMapper mapper = Mapper.Instance;

            OkObjectResult specServiceOkObjectResult = new OkObjectResult(new List <Models.Specs.Period>
            {
                fundingPeriod1,
                fundingPeriod2
            });

            IFundingService mockFundingService = Substitute.For <IFundingService>();

            mockFundingService.GetFundingPeriods(Arg.Any <HttpRequest>()).Returns(specServiceOkObjectResult);

            TimePeriodsService serviceUnderTest = new TimePeriodsService(mockFundingService, mapper);

            // Act
            IActionResult result = await serviceUnderTest.GetFundingPeriods(Substitute.For <HttpRequest>());

            // Assert
            result
            .Should().NotBeNull()
            .And
            .Subject.Should().BeOfType <OkObjectResult>();

            OkObjectResult resultCasted = result as OkObjectResult;

            resultCasted.Value
            .Should().NotBeNull()
            .And
            .Subject.Should().BeOfType <List <V1.Models.Period> >();

            List <V1.Models.Period> resultPeriods = resultCasted.Value as List <V1.Models.Period>;

            resultPeriods
            .Count
            .Should()
            .Be(2);

            resultPeriods.ElementAt(0).Id.Should().Be("AYCode");
            resultPeriods.ElementAt(0).Name.Should().Be("AcademicYear");
            resultPeriods.ElementAt(0).StartYear.Should().Be(DateTimeOffset.Now.Year);
            resultPeriods.ElementAt(0).EndYear.Should().Be(DateTimeOffset.Now.Year + 1);
            resultPeriods.ElementAt(1).Id.Should().Be("FYCode");
            resultPeriods.ElementAt(1).Name.Should().Be("FinalYear");
            resultPeriods.ElementAt(1).StartYear.Should().Be(DateTimeOffset.Now.Year);
            resultPeriods.ElementAt(1).EndYear.Should().Be(DateTimeOffset.Now.Year + 1);
        }
 public async Task <IActionResult> RunGetFundingPeriods()
 {
     return(await _fundingService.GetFundingPeriods(ControllerContext.HttpContext.Request));
 }