public async Task GetContractRemindersAsync_ReturnsExpectedResultTest()
        {
            //Arrange
            int reminderInterval      = 14;
            int pageNumber            = 1;
            int pageSize              = 1;
            ContractSortOptions sort  = ContractSortOptions.LastUpdatedAt;
            SortDirection       order = SortDirection.Asc;
            DateTime            currentDateTimeMinusNumberOfDays = DateTime.UtcNow.Date.AddDays(-reminderInterval).AddHours(23).AddMinutes(59);
            const string        ContractNumber = "expected-contract-number";
            var createdAt             = DateTime.UtcNow.AddDays(-30);
            var lastEmailReminderSent = DateTime.UtcNow.AddDays(-15);

            var contracts = new List <Contract>()
            {
                new Contract {
                    Id = 1, ContractNumber = ContractNumber, ContractVersion = 1, LastEmailReminderSent = null, Status = 0, FundingType = 1, CreatedAt = createdAt
                },
                new Contract {
                    Id = 2, ContractNumber = ContractNumber, ContractVersion = 2, LastEmailReminderSent = lastEmailReminderSent, Status = 0, FundingType = 1, CreatedAt = createdAt
                },
                new Contract {
                    Id = 3, ContractNumber = ContractNumber, ContractVersion = 3, LastEmailReminderSent = lastEmailReminderSent, Status = 0, FundingType = 1, CreatedAt = createdAt
                },
                new Contract {
                    Id = 4, ContractNumber = ContractNumber, ContractVersion = 4, LastEmailReminderSent = null, Status = 0, FundingType = 1, CreatedAt = createdAt
                },
            };

            var contractsExpected = new List <Contract>()
            {
                new Contract {
                    Id = 1, ContractNumber = ContractNumber, ContractVersion = 1, LastEmailReminderSent = null, Status = 0, FundingType = 1, CreatedAt = createdAt
                },
            };
            var pagedListExpected = new PagedList <Contract>(contractsExpected, 4, pageNumber, pageSize);

            var mockDbSet = new DbSetMock <Contract>(contracts, (c, _) => c.Id, true);

            var mockUnitOfWork = Mock.Of <IUnitOfWork>(MockBehavior.Strict);
            var mockRepo       = Mock.Of <IRepository <Contract> >(MockBehavior.Strict);

            Mock.Get(mockRepo)
            .Setup(r => r.GetMany(It.IsAny <Expression <Func <Contract, bool> > >()))
            .Returns(mockDbSet.Object);

            //Act
            var contractRepo = new ContractRepository(mockRepo, mockUnitOfWork, _mockLogger.Object);
            var actual       = await contractRepo.GetContractRemindersAsync(currentDateTimeMinusNumberOfDays, pageNumber, pageSize, sort, order);

            //Assert
            actual.Should().BeEquivalentTo(pagedListExpected);
            Mock.Get(mockRepo)
            .Verify(r => r.GetMany(q => ((q.LastEmailReminderSent == null && currentDateTimeMinusNumberOfDays >= q.CreatedAt) || (q.LastEmailReminderSent != null && currentDateTimeMinusNumberOfDays >= q.LastEmailReminderSent)) && q.Status == (int)ContractStatus.PublishedToProvider), Times.Exactly(1));
            _mockLogger.VerifyAll();
        }
 /// <inheritdoc/>
 public async Task <ContractReminders> GetContractRemindersAsync(
     uint reminderInterval    = 14,
     uint page                = 1,
     uint count               = 10,
     ContractSortOptions sort = ContractSortOptions.LastUpdatedAt,
     SortDirection order      = SortDirection.Asc)
 {
     _logger.LogInformation($"Retrieving a contract reminders for reminderInterval : {reminderInterval}, pageNumber : {page}, pageSize : {count}, sort : {sort} and order : {order}");
     return(await Get <ContractReminders>($"/api/contractReminders?reminderInterval={reminderInterval}&page={page}&count={count}&sort={sort}&order={order}"));
 }
コード例 #3
0
        public async Task GetContractRemindersAsync_MockHttp()
        {
            // Arrange
            int reminderInterval      = 14;
            int pageNumber            = 1;
            int pageSize              = 2;
            ContractSortOptions sort  = ContractSortOptions.LastUpdatedAt;
            SortDirection       order = SortDirection.Asc;

            Mock.Get(_contractsDataLogger)
            .Setup(p => p.LogInformation(It.IsAny <string>(), It.IsAny <object[]>()));

            var contractList = new List <ContractReminderItem>()
            {
                new ContractReminderItem()
                {
                    Id = 1, ContractVersion = 1, ContractNumber = "Test1"
                },
                new ContractReminderItem()
                {
                    Id = 2, ContractVersion = 1, ContractNumber = "Test2"
                }
            };
            var expectedContractReminders = new ContractReminders {
                Contracts = contractList
            };

            expectedContractReminders.Paging = new Paging()
            {
                CurrentPage = pageNumber, PageSize = pageSize, TotalCount = 2, TotalPages = 1, HasNextPage = false, HasPreviousPage = false, NextPageUrl = string.Empty, PreviousPageUrl = string.Empty
            };

            string jsonString = JsonSerializer.Serialize(expectedContractReminders);

            _mockHttpMessageHandler.Expect(TestBaseAddress + $"/api/contractReminders?reminderInterval={reminderInterval}&page={pageNumber}&count={pageSize}&sort={sort}&order={order}").Respond("application/json", jsonString);

            ContractsDataService contractsDataService = CreateContractsDataService();

            //Act
            var result = await contractsDataService.GetContractRemindersAsync((uint)reminderInterval, (uint)pageNumber, (uint)pageSize, sort, order);

            // Assert
            result.Should().BeEquivalentTo(expectedContractReminders);
            _mockHttpMessageHandler.VerifyNoOutstandingExpectation();
            VerifyAllMocks();
        }
        public async Task <ActionResult <ContractReminderResponse <IEnumerable <ContractReminderItem> > > > GetContractReminders(
            int reminderInterval     = 14,
            int page                 = 1,
            int count                = 10,
            ContractSortOptions sort = ContractSortOptions.LastUpdatedAt,
            SortDirection order      = SortDirection.Asc)
        {
            _logger.LogInformation($"Get contract reminders called with reminder interval: {reminderInterval},  page: {page}, count: {count}, sort: {sort}, order: {order}");

            // This creates a template of the query string so that ONLY the page can be replaced
            // further down the process.
            string routeTemplateUrl = Request.Path.Value + $"?reminderInterval={reminderInterval}&page={{page}}&count={count}&sort={sort}&order={order}";

            var results = await _contractService.GetContractRemindersAsync(reminderInterval, page, count, sort, order, routeTemplateUrl);

            if (results is null || (results.Paging.TotalCount > 0 && page > results.Paging.TotalPages))
            {
                return(NotFound());
            }
        public void OrderByDynamic_UKPRN_Desc_Test()
        {
            //Arrange
            ContractSortOptions sort  = ContractSortOptions.UKPRN;
            SortDirection       order = SortDirection.Desc;
            var contracts             = GetContracts().AsQueryable();

            var mockDbSet = new DbSetMock <Contract>(contracts, (c, _) => c.Id, true);

            var mockRepo = Mock.Of <IRepository <Contract> >(MockBehavior.Strict);

            Mock.Get(mockRepo)
            .Setup(r => r.GetMany(It.IsAny <Expression <Func <Contract, bool> > >()))
            .Returns(mockDbSet.Object);

            //Act
            var result = mockDbSet.Object.OrderByDynamic(sort.ToString(), order).ToList();

            //Assert
            result.FirstOrDefault().Id.Should().Be(10);
        }
コード例 #6
0
 /// <inheritdoc/>
 public async Task <ContractReminderResponse <IEnumerable <ContractReminderItem> > > GetContractRemindersAsync(uint reminderInterval, uint pageNumber, uint pageSize, ContractSortOptions sort, SortDirection order)
 {
     _logger.LogInformation($"Retrieving a contract reminders for reminderInterval : {reminderInterval}, pageNumber : {pageNumber}, pageSize : {pageSize}, sort : {sort} and order : {order}");
     return(await Get <ContractReminderResponse <IEnumerable <ContractReminderItem> > >($"/api/contractReminders?reminderInterval={reminderInterval}&page={pageNumber}&count={pageSize}&sort={sort}&order={order}"));
 }
        /// <inheritdoc/>
        public async Task <IPagedList <Contract> > GetContractRemindersAsync(DateTime currentDateTimeMinusNumberOfDays, int pageNumber, int pageSize, ContractSortOptions sort, SortDirection order)
        {
            //return all contracts that need reminders sent.
            var query = _repository
                        .GetMany(q =>
                                 ((q.LastEmailReminderSent == null && currentDateTimeMinusNumberOfDays >= q.CreatedAt) ||
                                  (q.LastEmailReminderSent != null && currentDateTimeMinusNumberOfDays >= q.LastEmailReminderSent)) &&
                                 q.Status == (int)ContractStatus.PublishedToProvider);

            var sortedContracts = query.OrderByDynamic(sort.ToString(), order);

            return(await sortedContracts.ToPagedList(pageNumber, pageSize));
        }
        public async Task GetContractRemindersAsync_ReturnsExpectedTest()
        {
            //Arrange
            SetMapperHelper();
            int reminderInterval               = 14;
            int pageNumber                     = 1;
            int pageSize                       = 1;
            ContractSortOptions sort           = ContractSortOptions.LastUpdatedAt;
            SortDirection       order          = SortDirection.Asc;
            string       baseUrl               = $"https://localhost:5001";
            string       routeTemplateUrl      = $"/api/contractReminders?reminderInterval={reminderInterval}&pageNumber={pageNumber}&pageSize={pageSize}&sort={sort}&order={order}";
            const string contractNumber        = "Test-Contract-Number";
            const string title                 = "Test Title";
            DateTime     lastEmailReminderSent = DateTime.UtcNow;

            var working = new List <DataModels.Contract>
            {
                new DataModels.Contract {
                    Id = 1, Title = title, ContractNumber = contractNumber, ContractVersion = 1, Ukprn = 12345678, LastEmailReminderSent = lastEmailReminderSent, Status = (int)ContractStatus.PublishedToProvider, FundingType = (int)ContractFundingType.AdvancedLearnerLoans
                },
                new DataModels.Contract {
                    Id = 2, Title = title, ContractNumber = contractNumber, ContractVersion = 2, Ukprn = 12345678, LastEmailReminderSent = lastEmailReminderSent.AddDays(-14), Status = (int)ContractStatus.PublishedToProvider, FundingType = (int)ContractFundingType.AdvancedLearnerLoans
                },
                new DataModels.Contract {
                    Id = 3, Title = title, ContractNumber = contractNumber, ContractVersion = 3, Ukprn = 12345678, LastEmailReminderSent = lastEmailReminderSent.AddDays(-15), Status = (int)ContractStatus.PublishedToProvider, FundingType = (int)ContractFundingType.AdvancedLearnerLoans
                },
            };

            var expectedList = new List <ContractReminderItem>
            {
                new ContractReminderItem {
                    Id = 2, Title = title, ContractNumber = contractNumber, ContractVersion = 2, Ukprn = 12345678, Status = ContractStatus.PublishedToProvider, FundingType = ContractFundingType.AdvancedLearnerLoans
                }
            };

            var expected = new ContractReminderResponse <IEnumerable <ContractReminderItem> >(expectedList)
            {
                Paging = new Metadata()
                {
                    CurrentPage     = pageNumber,
                    HasNextPage     = true,
                    HasPreviousPage = false,
                    NextPageUrl     = baseUrl + $"/api/contractReminders?reminderInterval={reminderInterval}&pageNumber={pageNumber}&pageSize={pageSize}&sort={sort}&order={order}",
                    PageSize        = pageSize,
                    PreviousPageUrl = string.Empty,
                    TotalCount      = 2,
                    TotalPages      = 2
                }
            };

            ILoggerAdapter <ContractService>    logger     = new LoggerAdapter <ContractService>(new Logger <ContractService>(new LoggerFactory()));
            ILoggerAdapter <ContractRepository> loggerRepo = new LoggerAdapter <ContractRepository>(new Logger <ContractRepository>(new LoggerFactory()));

            MockAuditService();

            var inMemPdsDbContext = HelperExtensions.GetInMemoryPdsDbContext();
            var repo         = new Repository <DataModels.Contract>(inMemPdsDbContext);
            var work         = new SingleUnitOfWorkForRepositories(inMemPdsDbContext);
            var contractRepo = new ContractRepository(repo, work, loggerRepo);
            var uriService   = new UriService(baseUrl);
            var asposeDocumentManagementContractService = GetDocumentService();
            var contractValidationService = GetContractValidationService();
            var mediator = BuildMediator();
            var contractDocumentService = BuildContractDocumentService();
            var service = new ContractService(contractRepo, _mapper, uriService, logger, _mockAuditService.Object, _semaphoreOnEntity, asposeDocumentManagementContractService, contractValidationService, mediator, contractDocumentService);

            foreach (var item in working)
            {
                await repo.AddAsync(item);
            }

            await work.CommitAsync();

            //Act
            var result = await service.GetContractRemindersAsync(reminderInterval, pageNumber, pageSize, sort, order, routeTemplateUrl);

            //Assert
            result.Should().BeEquivalentTo(expected);
        }
        /// <inheritdoc/>
        public async Task <ContractReminderResponse <IEnumerable <ContractReminderItem> > > GetContractRemindersAsync(int reminderInterval, int pageNumber, int pageSize, ContractSortOptions sort, SortDirection order, string templatedQueryString)
        {
            DateTime currentDateTimeMinusNumberOfDays = DateTime.UtcNow.Date.AddDays(-reminderInterval).AddHours(23).AddMinutes(59);

            _logger.LogInformation($"Get contract reminder by reminder interval : {reminderInterval} and cut off datetime {currentDateTimeMinusNumberOfDays}. - Current utc: {DateTime.UtcNow}");

            var contracts = await _repository.GetContractRemindersAsync(currentDateTimeMinusNumberOfDays, pageNumber, pageSize, sort, order);

            var metadata = new Metadata
            {
                TotalCount      = contracts.TotalCount,
                PageSize        = contracts.PageSize,
                CurrentPage     = contracts.CurrentPage,
                TotalPages      = contracts.TotalPages,
                HasNextPage     = contracts.HasNextPage,
                HasPreviousPage = contracts.HasPreviousPage,
                NextPageUrl     = contracts.HasNextPage ? _uriService.GetUri(SetPageValue(templatedQueryString, pageNumber + 1)).ToString() : string.Empty,
                PreviousPageUrl = contracts.HasPreviousPage ? _uriService.GetUri(SetPageValue(templatedQueryString, pageNumber - 1)).ToString() : string.Empty
            };

            var contractReminders = _mapper.Map <IEnumerable <ContractReminderItem> >(contracts.Items);

            var apiResponse = new ContractReminderResponse <IEnumerable <ContractReminderItem> >(contractReminders)
            {
                Paging = metadata
            };

            return(apiResponse);
        }