Пример #1
0
        public async Task GetFundingStreamById_StringParam_GivenFundingStreamnWasFound_ReturnsSuccess()
        {
            // Arrange
            ILogger logger = CreateLogger();

            FundingStream fundingStream = new FundingStream
            {
                Id = FundingStreamId
            };

            ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository();

            specificationsRepository
            .GetFundingStreamById(Arg.Is(FundingStreamId))
            .Returns(fundingStream);

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

            // Act
            IActionResult result = await fundingService.GetFundingStreamById(FundingStreamId);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();
        }
Пример #2
0
        public async Task GetFundingStreamById_StringParam_GivenFundingStreamnWasNotFound_ReturnsNotFound()
        {
            // Arrange
            ILogger logger = CreateLogger();

            ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository();

            specificationsRepository
            .GetFundingStreamById(Arg.Is(FundingStreamId))
            .Returns((FundingStream)null);

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

            // Act
            IActionResult result = await fundingService.GetFundingStreamById(FundingStreamId);

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

            logger
            .Received(1)
            .Error(Arg.Is($"No funding stream was found for funding stream id : {FundingStreamId}"));
        }
Пример #3
0
        public async Task <IActionResult> GetFundingStreamById(string fundingStreamId)
        {
            if (string.IsNullOrWhiteSpace(fundingStreamId))
            {
                _logger.Error("No funding stream Id was provided to GetFundingStreamById");

                return(new BadRequestObjectResult("Null or empty funding stream Id provided"));
            }

            FundingStream fundingStream = await _specificationsRepository.GetFundingStreamById(fundingStreamId);

            if (fundingStream == null)
            {
                _logger.Error($"No funding stream was found for funding stream id : {fundingStreamId}");

                return(new NotFoundResult());
            }

            return(new OkObjectResult(fundingStream));
        }
Пример #4
0
        public async Task GetFundingStreamById_GivenFundingStreamnWasFound_ReturnsSuccess()
        {
            // Arrange
            IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues>
            {
                { "fundingStreamId", new StringValues(FundingStreamId) }
            });

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

            request
            .Query
            .Returns(queryStringValues);

            ILogger logger = CreateLogger();

            FundingStream fundingStream = new FundingStream
            {
                Id = FundingStreamId
            };

            ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository();

            specificationsRepository
            .GetFundingStreamById(Arg.Is(FundingStreamId))
            .Returns(fundingStream);

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

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

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();
        }
Пример #5
0
        public async Task GetFundingStreamById_GivenFundingStreamnWasNotFound_ReturnsNotFound()
        {
            // Arrange
            IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues>
            {
                { "fundingStreamId", new StringValues(FundingStreamId) }
            });

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

            request
            .Query
            .Returns(queryStringValues);

            ILogger logger = CreateLogger();

            ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository();

            specificationsRepository
            .GetFundingStreamById(Arg.Is(FundingStreamId))
            .Returns((FundingStream)null);

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

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

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

            logger
            .Received(1)
            .Error(Arg.Is($"No funding stream was found for funding stream id : {FundingStreamId}"));
        }
        public async Task SpecificationsService_CreateSpecification_WhenValidInputProvided_ThenSpecificationIsCreated()
        {
            // Arrange
            const string fundingStreamId = "fs1";
            const string fundingPeriodId = "fp1";


            ISpecificationsRepository specificationsRepository      = CreateSpecificationsRepository();
            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            IMapper mapper = CreateImplementedMapper();
            IVersionRepository <SpecificationVersion> versionRepository = CreateVersionRepository();

            SpecificationsService specificationsService = CreateService(
                specificationsRepository: specificationsRepository,
                searchRepository: searchRepository,
                mapper: mapper,
                specificationVersionRepository: versionRepository);

            SpecificationCreateModel specificationCreateModel = new SpecificationCreateModel()
            {
                Name             = "Specification Name",
                Description      = "Specification Description",
                FundingPeriodId  = "fp1",
                FundingStreamIds = new List <string>()
                {
                    fundingStreamId
                },
            };

            string json = JsonConvert.SerializeObject(specificationCreateModel);

            byte[]       byteArray = Encoding.UTF8.GetBytes(json);
            MemoryStream stream    = new MemoryStream(byteArray);

            ClaimsPrincipal principle = new ClaimsPrincipal(new[]
            {
                new ClaimsIdentity(new [] { new Claim(ClaimTypes.Sid, UserId), new Claim(ClaimTypes.Name, Username) })
            });

            HttpContext context = Substitute.For <HttpContext>();

            context
            .User
            .Returns(principle);

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

            request
            .Body
            .Returns(stream);

            request
            .HttpContext
            .Returns(context);

            specificationsRepository
            .GetSpecificationByQuery(Arg.Any <Expression <Func <Specification, bool> > >())
            .Returns((Specification)null);

            Period fundingPeriod = new Period()
            {
                Id   = fundingPeriodId,
                Name = "Funding Period 1"
            };

            specificationsRepository
            .GetPeriodById(Arg.Is(fundingPeriodId))
            .Returns(fundingPeriod);

            FundingStream fundingStream = new FundingStream()
            {
                Id              = fundingStreamId,
                Name            = "Funding Stream 1",
                AllocationLines = new List <AllocationLine>(),
            };

            specificationsRepository
            .GetFundingStreamById(Arg.Is(fundingStreamId))
            .Returns(fundingStream);

            DateTime createdDate = new DateTime(2018, 1, 2, 5, 6, 2);

            SpecificationVersion specificationVersion = new SpecificationVersion()
            {
                Description    = "Specification Description",
                FundingPeriod  = new Reference("fp1", "Funding Period 1"),
                Date           = createdDate,
                PublishStatus  = Models.Versioning.PublishStatus.Draft,
                FundingStreams = new List <Reference>()
                {
                    new Reference(FundingStreamId, "Funding Stream 1")
                },
                Name            = "Specification Name",
                Version         = 1,
                SpecificationId = SpecificationId
            };

            versionRepository
            .CreateVersion(Arg.Any <SpecificationVersion>())
            .Returns(specificationVersion);

            DocumentEntity <Specification> createdSpecification = new DocumentEntity <Specification>()
            {
                Content = new Specification()
                {
                    Name    = "Specification Name",
                    Id      = "createdSpec",
                    Current = specificationVersion
                },
            };

            specificationsRepository
            .CreateSpecification(Arg.Is <Specification>(
                                     s => s.Name == specificationCreateModel.Name &&
                                     s.Current.Description == specificationCreateModel.Description &&
                                     s.Current.FundingPeriod.Id == fundingPeriodId))
            .Returns(createdSpecification);

            // Act
            IActionResult result = await specificationsService.CreateSpecification(request);

            // Assert
            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeOfType <SpecificationCurrentVersion>()
            .And
            .NotBeNull();

            await specificationsRepository
            .Received(1)
            .CreateSpecification(Arg.Is <Specification>(
                                     s => s.Name == specificationCreateModel.Name &&
                                     s.Current.Description == specificationCreateModel.Description &&
                                     s.Current.FundingPeriod.Id == fundingPeriodId));

            await searchRepository
            .Received(1)
            .Index(Arg.Is <List <SpecificationIndex> >(c =>
                                                       c.Count() == 1 &&
                                                       !string.IsNullOrWhiteSpace(c.First().Id) &&
                                                       c.First().Name == specificationCreateModel.Name
                                                       ));

            await versionRepository
            .Received(1)
            .SaveVersion(Arg.Is <SpecificationVersion>(
                             m => !string.IsNullOrWhiteSpace(m.EntityId) &&
                             m.PublishStatus == Models.Versioning.PublishStatus.Draft &&
                             m.Description == "Specification Description" &&
                             m.FundingPeriod.Id == "fp1" &&
                             m.FundingPeriod.Name == "Funding Period 1" &&
                             m.FundingStreams.Any() &&
                             m.Name == "Specification Name" &&
                             m.Version == 1
                             ));
        }
        public async Task SpecificationsService_CreateSpecification_WhenFundingStreamIDIsProvidedButDoesNotExist_ThenPreConditionFailedReturned()
        {
            // Arrange
            const string fundingStreamId         = "fs1";
            const string fundingStreamNotFoundId = "notfound";

            const string fundingPeriodId = "fp1";


            ISpecificationsRepository specificationsRepository      = CreateSpecificationsRepository();
            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            IMapper mapper = CreateImplementedMapper();

            SpecificationsService specificationsService = CreateService(
                specificationsRepository: specificationsRepository,
                searchRepository: searchRepository,
                mapper: mapper);

            SpecificationCreateModel specificationCreateModel = new SpecificationCreateModel()
            {
                Name             = "Specification Name",
                Description      = "Specification Description",
                FundingPeriodId  = "fp1",
                FundingStreamIds = new List <string>()
                {
                    fundingStreamId, fundingStreamNotFoundId,
                },
            };

            string json = JsonConvert.SerializeObject(specificationCreateModel);

            byte[]       byteArray = Encoding.UTF8.GetBytes(json);
            MemoryStream stream    = new MemoryStream(byteArray);

            ClaimsPrincipal principle = new ClaimsPrincipal(new[]
            {
                new ClaimsIdentity(new [] { new Claim(ClaimTypes.Sid, UserId), new Claim(ClaimTypes.Name, Username) })
            });

            HttpContext context = Substitute.For <HttpContext>();

            context
            .User
            .Returns(principle);

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

            request
            .Body
            .Returns(stream);

            request
            .HttpContext
            .Returns(context);

            specificationsRepository
            .GetSpecificationByQuery(Arg.Any <Expression <Func <Specification, bool> > >())
            .Returns((Specification)null);

            Period fundingPeriod = new Period()
            {
                Id   = fundingPeriodId,
                Name = "Funding Period 1"
            };

            specificationsRepository
            .GetPeriodById(Arg.Is(fundingPeriodId))
            .Returns(fundingPeriod);

            FundingStream fundingStream = new FundingStream()
            {
                Id              = fundingStreamId,
                Name            = "Funding Stream 1",
                AllocationLines = new List <AllocationLine>(),
            };

            specificationsRepository
            .GetFundingStreamById(Arg.Is(fundingStreamId))
            .Returns(fundingStream);

            specificationsRepository
            .GetFundingStreamById(Arg.Is(fundingStreamNotFoundId))
            .Returns((FundingStream)null);

            // Act
            IActionResult result = await specificationsService.CreateSpecification(request);

            // Assert
            result
            .Should()
            .BeOfType <PreconditionFailedResult>()
            .Which
            .Value
            .Should()
            .Be("Unable to find funding stream with ID 'notfound'.");

            await specificationsRepository
            .Received(1)
            .GetFundingStreamById(Arg.Is(fundingStreamNotFoundId));

            await specificationsRepository
            .Received(1)
            .GetFundingStreamById(Arg.Is(fundingStreamId));
        }