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

            ILogger logger = CreateLogger();

            CalculationService service = CreateCalculationService(logger: logger);

            //Act
            IActionResult result = await service.GetCurrentCalculationsForSpecification(request);

            //Assert
            result
            .Should()
            .BeOfType <BadRequestObjectResult>()
            .Which
            .Value
            .Should()
            .Be("Null or empty specificationId provided");

            logger
            .Received(1)
            .Warning(Arg.Is("No specificationId was provided to GetCalculationsForSpecification"));
        }
        async public Task GetCurrentCalculationsForSpecification_WhenEmptyListOfCalculationsFoundInCache_ThenCalculationsReturned()
        {
            //Arrange
            const string specificationId = "specid";

            List <CalculationCurrentVersion> calculations = new List <CalculationCurrentVersion>();

            IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues>
            {
                { "specificationId", new StringValues(specificationId) }
            });

            HttpRequest request = Substitute.For <HttpRequest>();

            request
            .Query
            .Returns(queryStringValues);

            ILogger logger = CreateLogger();

            ICalculationsRepository calculationsRepository = CreateCalculationsRepository();

            ICacheProvider cacheProvider = CreateCacheProvider();

            cacheProvider
            .GetAsync <List <CalculationCurrentVersion> >($"{CacheKeys.CurrentCalculationsForSpecification}{specificationId}")
            .Returns(calculations);

            CalculationService service = CreateCalculationService(logger: logger, calculationsRepository: calculationsRepository, cacheProvider: cacheProvider);

            //Act
            IActionResult result = await service.GetCurrentCalculationsForSpecification(request);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeAssignableTo <IEnumerable <CalculationCurrentVersion> >()
            .Which
            .Should()
            .HaveCount(calculations.Count);

            await calculationsRepository
            .Received(0)
            .GetCalculationsBySpecificationId(Arg.Is(specificationId));
        }
Ejemplo n.º 3
0
        async public Task GetCurrentCalculationsForSpecification_WhenEmptyListOfCalculationsFoundInCache_ThenCalculationsReturned()
        {
            //Arrange
            const string specificationId = "specid";

            List <CalculationResponseModel> calculations = new List <CalculationResponseModel>();

            ILogger logger = CreateLogger();

            ICalculationsRepository calculationsRepository = CreateCalculationsRepository();

            ICacheProvider cacheProvider = CreateCacheProvider();

            cacheProvider
            .GetAsync <List <CalculationResponseModel> >($"{CacheKeys.CurrentCalculationsForSpecification}{specificationId}")
            .Returns(calculations);

            CalculationService service = CreateCalculationService(logger: logger, calculationsRepository: calculationsRepository, cacheProvider: cacheProvider);

            //Act
            IActionResult result = await service.GetCurrentCalculationsForSpecification(CalculationId);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeAssignableTo <IEnumerable <CalculationResponseModel> >()
            .Which
            .Should()
            .HaveCount(calculations.Count);

            await calculationsRepository
            .Received(0)
            .GetCalculationsBySpecificationId(Arg.Is(specificationId));
        }
Ejemplo n.º 4
0
        public async Task GetCurrentCalculationsForSpecification_WhenCalculationsFoundInCache_ThenCalculationsReturnedFromCache()
        {
            //Arrange
            const string specificationId = "specid";

            List <CalculationResponseModel> calculations = new List <CalculationResponseModel>()
            {
                new CalculationResponseModel()
                {
                    Id              = "one",
                    Name            = "Calculation Name",
                    SourceCode      = "Return 10",
                    PublishStatus   = PublishStatus.Draft,
                    Author          = new Reference("userId", "User Name"),
                    CalculationType = CalculationType.Template,
                    Version         = 1,
                },
                new CalculationResponseModel()
                {
                    Id              = "two",
                    Name            = "Calculation Name Two",
                    SourceCode      = "Return 50",
                    PublishStatus   = PublishStatus.Approved,
                    Author          = new Reference("userId", "User Name"),
                    CalculationType = CalculationType.Template,
                    Version         = 5,
                }
            };

            ILogger logger = CreateLogger();

            ICalculationsRepository calculationsRepository = CreateCalculationsRepository();

            ICacheProvider cacheProvider = CreateCacheProvider();

            cacheProvider
            .GetAsync <List <CalculationResponseModel> >($"{CacheKeys.CurrentCalculationsForSpecification}{specificationId}")
            .Returns(calculations);

            CalculationService service = CreateCalculationService(logger: logger, calculationsRepository: calculationsRepository, cacheProvider: cacheProvider);

            //Act
            IActionResult result = await service.GetCurrentCalculationsForSpecification(specificationId);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeAssignableTo <IEnumerable <CalculationResponseModel> >()
            .Which
            .Should()
            .Contain(m => m.Id == "one" && m.Name == "Calculation Name" && m.SourceCode == "Return 10" && m.CalculationType == CalculationType.Template && m.Version == 1 && m.PublishStatus == PublishStatus.Draft)
            .And.Contain(m => m.Id == "two" && m.Name == "Calculation Name Two" && m.SourceCode == "Return 50" && m.PublishStatus == PublishStatus.Approved && m.CalculationType == CalculationType.Template && m.Version == 5);

            await calculationsRepository
            .Received(0)
            .GetCalculationsBySpecificationId(Arg.Is(specificationId));

            await cacheProvider
            .Received(1)
            .GetAsync <List <CalculationResponseModel> >($"{CacheKeys.CurrentCalculationsForSpecification}{specificationId}");
        }
        public async Task GetCurrentCalculationsForSpecification_WhenCalculationsFoundInCache_ThenCalculationsReturnedFromCache()
        {
            //Arrange
            const string specificationId = "specid";

            List <CalculationCurrentVersion> calculations = new List <CalculationCurrentVersion>()
            {
                new CalculationCurrentVersion()
                {
                    Id                       = "one",
                    Name                     = "Calculation Name",
                    SourceCode               = "Return 10",
                    PublishStatus            = PublishStatus.Draft,
                    Author                   = new Reference("userId", "User Name"),
                    CalculationSpecification = new Reference("specId", "Calculation Specification ID"),
                    CalculationType          = "Funding",
                    Version                  = 1,
                },
                new CalculationCurrentVersion()
                {
                    Id                       = "two",
                    Name                     = "Calculation Name Two",
                    SourceCode               = "Return 50",
                    PublishStatus            = PublishStatus.Approved,
                    Author                   = new Reference("userId", "User Name"),
                    CalculationSpecification = new Reference("specId", "Calculation Specification ID"),
                    CalculationType          = "Number",
                    Version                  = 5,
                }
            };

            IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues>
            {
                { "specificationId", new StringValues(specificationId) }
            });

            HttpRequest request = Substitute.For <HttpRequest>();

            request
            .Query
            .Returns(queryStringValues);

            ILogger logger = CreateLogger();

            ICalculationsRepository calculationsRepository = CreateCalculationsRepository();

            ICacheProvider cacheProvider = CreateCacheProvider();

            cacheProvider
            .GetAsync <List <CalculationCurrentVersion> >($"{CacheKeys.CurrentCalculationsForSpecification}{specificationId}")
            .Returns(calculations);

            CalculationService service = CreateCalculationService(logger: logger, calculationsRepository: calculationsRepository, cacheProvider: cacheProvider);

            //Act
            IActionResult result = await service.GetCurrentCalculationsForSpecification(request);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeAssignableTo <IEnumerable <CalculationCurrentVersion> >()
            .Which
            .Should()
            .Contain(m => m.Id == "one" && m.Name == "Calculation Name" && m.SourceCode == "Return 10" && m.CalculationType == "Funding" && m.Version == 1 && m.PublishStatus == PublishStatus.Draft)
            .And.Contain(m => m.Id == "two" && m.Name == "Calculation Name Two" && m.SourceCode == "Return 50" && m.PublishStatus == PublishStatus.Approved && m.CalculationType == "Number" && m.Version == 5);

            await calculationsRepository
            .Received(0)
            .GetCalculationsBySpecificationId(Arg.Is(specificationId));

            await cacheProvider
            .Received(1)
            .GetAsync <List <CalculationCurrentVersion> >($"{CacheKeys.CurrentCalculationsForSpecification}{specificationId}");
        }