コード例 #1
0
        public async Task HasAllTemplateCalculationsBeenApproved_GivenResponseIsSuccess_ReturnsValue(bool expectedValue)
        {
            //Arrange
            BooleanResponseModel booleanResponseModel = new BooleanResponseModel {
                Value = expectedValue
            };

            ApiResponse <BooleanResponseModel> apiResponse = new ApiResponse <BooleanResponseModel>(HttpStatusCode.OK, booleanResponseModel);

            ICalculationsApiClient calculationsApiClient = CreateCalculationsApiClient();

            calculationsApiClient
            .CheckHasAllApprovedTemplateCalculationsForSpecificationId(Arg.Is(specificationId))
            .Returns(apiResponse);

            CalculationsService calculationsService = CreateCalculationsService(calculationsApiClient);

            //Act
            bool responseValue = await calculationsService.HaveAllTemplateCalculationsBeenApproved(specificationId);

            //Assert
            responseValue
            .Should()
            .Be(expectedValue);
        }
コード例 #2
0
        public async Task GetTemplateMapping_GivenFailedResponseFromCalculationsApi_ThrowsRetriableException()
        {
            //Arrange
            ApiResponse <TemplateMapping> apiResponse = new ApiResponse <TemplateMapping>(HttpStatusCode.NotFound);

            ICalculationsApiClient calculationsApiClient = CreateCalculationsApiClient();

            calculationsApiClient
            .GetTemplateMapping(Arg.Is(specificationId), Arg.Is(fundingStreamId))
            .Returns(apiResponse);

            ILogger logger = CreateLogger();

            string errorMessage = $"Failed to retrieve template mapping for specification id '{specificationId}' and  funding stream id '{fundingStreamId}'" +
                                  $" with status code '{apiResponse.StatusCode}'";

            CalculationsService calculationsService = CreateCalculationsService(calculationsApiClient, logger);

            //Act
            Func <Task> test = async() => await calculationsService.GetTemplateMapping(specificationId, fundingStreamId);

            //Assert
            test
            .Should()
            .ThrowExactly <RetriableException>()
            .Which
            .Message
            .Should()
            .Be(errorMessage);

            logger
            .Received(1)
            .Error(Arg.Is(errorMessage));
        }
コード例 #3
0
        public void HasAllTemplateCalculationsBeenApproved_GivenFailedResponseCheckingForApprovedTemplateCalcs_ThrowsRetriableException()
        {
            //Arrange
            ApiResponse <BooleanResponseModel> apiResponse = new ApiResponse <BooleanResponseModel>(HttpStatusCode.NotFound);

            ICalculationsApiClient calculationsApiClient = CreateCalculationsApiClient();

            calculationsApiClient
            .CheckHasAllApprovedTemplateCalculationsForSpecificationId(Arg.Is(specificationId))
            .Returns(apiResponse);

            ILogger logger = CreateLogger();

            string errorMessage = $"Failed to check spoecification with id '{specificationId}' " +
                                  $"for all approved template calculations with status code '{HttpStatusCode.NotFound}'";

            CalculationsService calculationsService = CreateCalculationsService(calculationsApiClient, logger);

            //Act
            Func <Task> test = async() => await calculationsService.HaveAllTemplateCalculationsBeenApproved(specificationId);

            //Assert
            test
            .Should()
            .ThrowExactly <RetriableException>()
            .Which
            .Message
            .Should()
            .Be(errorMessage);

            logger
            .Received(1)
            .Error(Arg.Is(errorMessage));
        }
コード例 #4
0
        public async Task GetCalculationMetadataForSpecification_GivenResponseIsSuccess_ReturnsCalculationMetadataValue()
        {
            //Arrange
            IEnumerable <CalculationMetadata> expectedCalculationMetadata = new List <CalculationMetadata>()
            {
                new CalculationMetadata()
            };
            ApiResponse <IEnumerable <CalculationMetadata> > apiResponse = new ApiResponse <IEnumerable <CalculationMetadata> >(HttpStatusCode.OK, expectedCalculationMetadata);

            ICalculationsApiClient calculationsApiClient = CreateCalculationsApiClient();

            calculationsApiClient
            .GetCalculationMetadataForSpecification(Arg.Is(specificationId))
            .Returns(apiResponse);

            ILogger logger = CreateLogger();

            CalculationsService calculationsService = CreateCalculationsService(calculationsApiClient, logger);

            //Act
            IEnumerable <CalculationMetadata> responseValue = await calculationsService.GetCalculationMetadataForSpecification(specificationId);

            //Assert
            responseValue
            .Should()
            .BeEquivalentTo(expectedCalculationMetadata);
        }
コード例 #5
0
        public async Task GetCalculationMetadataForSpecification_GivenFailedResponseFromCalculationsApi_ThrowsRetriableException()
        {
            //Arrange
            ApiResponse <IEnumerable <CalculationMetadata> > apiResponse = new ApiResponse <IEnumerable <CalculationMetadata> >(HttpStatusCode.NotFound);

            ICalculationsApiClient calculationsApiClient = CreateCalculationsApiClient();

            calculationsApiClient
            .GetCalculationMetadataForSpecification(Arg.Is(specificationId))
            .Returns(apiResponse);

            ILogger logger = CreateLogger();

            string errorMessage = $"Failed to retrieve calculation metadata for specification id '{specificationId}'" +
                                  $" with status code '{apiResponse.StatusCode}'";

            CalculationsService calculationsService = CreateCalculationsService(calculationsApiClient, logger);

            //Act
            Func <Task> test = async() => await calculationsService.GetCalculationMetadataForSpecification(specificationId);

            //Assert
            test
            .Should()
            .ThrowExactly <RetriableException>()
            .Which
            .Message
            .Should()
            .Be(errorMessage);

            logger
            .Received(1)
            .Error(Arg.Is(errorMessage));
        }
コード例 #6
0
        public async Task GetTemplateMapping_GivenResponseIsSuccess_ReturnsTempalteMappingValue()
        {
            //Arrange
            TemplateMapping expectedTemplateMapping = new TemplateMapping()
            {
                SpecificationId = specificationId, FundingStreamId = fundingStreamId
            };
            ApiResponse <TemplateMapping> apiResponse = new ApiResponse <TemplateMapping>(HttpStatusCode.OK, expectedTemplateMapping);

            ICalculationsApiClient calculationsApiClient = CreateCalculationsApiClient();

            calculationsApiClient
            .GetTemplateMapping(Arg.Is(specificationId), Arg.Is(fundingStreamId))
            .Returns(apiResponse);

            ILogger logger = CreateLogger();

            CalculationsService calculationsService = CreateCalculationsService(calculationsApiClient, logger);

            //Act
            TemplateMapping responseValue = await calculationsService.GetTemplateMapping(specificationId, fundingStreamId);

            //Assert
            responseValue
            .Should()
            .Be(expectedTemplateMapping);
        }
コード例 #7
0
        public async Task <DTO> GetAccountDto()
        {
            var dto         = new DTO();
            var accountDtos = new List <AccountDTO>();

            try
            {
                var accounts = await _context.Accounts.ToListAsync();

                var bills = await _context.Bills.ToListAsync();

                var payDeductionDict     = CalculationsService.GetPayDeductionDict(accounts, bills);
                var requiredSavingsDict  = CalculationsService.GetAccountRequiredSavingsDict(payDeductionDict, bills);
                var sumOfAccountBalances = 0.0m;
                var totalSurplus         = 0.0m;

                foreach (var account in accounts)
                {
                    var newDto = new AccountDTO();
                    newDto.Account = account;
                    newDto.Bills   = await bills.Where(b => b.AccountId == account.Id).ToListAsync();

                    newDto.BillSum = newDto.Bills.Sum(b => b.AmountDue);

                    payDeductionDict.TryGetValue(account.Id, out var payDeduction);

                    newDto.PayDeduction               = payDeduction;
                    newDto.PaycheckPercentage         = CalculationsService.GetPaycheckPercentage(payDeductionDict, payDeduction);
                    newDto.ExpensesBeforeNextPaycheck = 63.43m;

                    requiredSavingsDict.TryGetValue(account.Id, out var accountRequiredSavings);

                    newDto.RequiredSavings = accountRequiredSavings;

                    var accountSurplus = account.Balance - accountRequiredSavings;
                    newDto.BalanceSurplus = accountSurplus;

                    totalSurplus         += accountSurplus;
                    sumOfAccountBalances += account.Balance;


                    accountDtos.Add(newDto);
                }

                dto.AccountDtos          = accountDtos;
                dto.SumOfAccountBalances = accounts.Sum(a => a.Balance);
                dto.TotalSurplus         = totalSurplus;


                return(dto);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(null);
            }
        }
コード例 #8
0
        public void Setup()
        {
            _calculationsService = new CalculationsService();

            var filePath = Common.GetFilePath("valid file.csv");

            _numbersService = new NumbersService(filePath);

            _numbers = _numbersService.ReadNumbers();
        }
コード例 #9
0
        public async Task <DTO> GetExpenseDto()
        {
            var dto         = new DTO();
            var expenseDtos = new List <ExpenseDTO>();

            try
            {
                var accounts = await _context.Accounts.ToListAsync();

                var expenses = await _context.Expenses.ToListAsync();

                var unpaidExpenses = await expenses.Where(e => !e.Paid).ToListAsync();

                var payDeductionDict = CalculationsService.GetPayDeductionDict(accounts, expenses, "expenses");

                foreach (var expense in unpaidExpenses)
                {
                    var expenseDto = new ExpenseDTO();

                    decimal payDeduction = payDeductionDict.FirstOrDefault(e => e.Key == expense.Id).Value;
                    expenseDto.Expense            = expense;
                    expenseDto.PayDeduction       = payDeduction;
                    expenseDto.PaycheckPercentage = CalculationsService.GetPaycheckPercentage(payDeductionDict, payDeduction);
                    expenseDto.RequiredSavings    = CalculationsService.GetExpenseRequiredSavings(payDeductionDict, expense);

                    expenseDtos.Add(expenseDto);
                }

                dto.ExpenseDtos = expenseDtos;
                var costPerPaycheck = payDeductionDict.Sum(e => e.Value);
                dto.CostOfExpensesPerPayPeriod = costPerPaycheck;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }


            return(dto);
        }
コード例 #10
0
        public async Task <DTO> GetBillDto()
        {
            var dto      = new DTO();
            var billDtos = new List <BillDTO>();

            try
            {
                var bills = await _context.Bills.ToListAsync();

                var accounts = await _context.Accounts.ToListAsync();

                var payDeductionDict = CalculationsService.GetPayDeductionDict(accounts, bills, "bill");

                foreach (var bill in bills)
                {
                    var billDto = new BillDTO();
                    billDto.Bill = bill;

                    decimal payDeduction = payDeductionDict.FirstOrDefault(b => b.Key == bill.Id).Value;

                    billDto.PayDeduction       = payDeduction;
                    billDto.PaycheckPercentage = CalculationsService.GetPaycheckPercentage(payDeductionDict, payDeduction);
                    billDto.RequiredSavings    = CalculationsService.GetBillRequiredSavings(payDeductionDict, bill);

                    billDtos.Add(billDto);
                }

                dto.BillDtos = billDtos;
                var costPerPaycheck = payDeductionDict.Sum(b => b.Value);
                dto.CostOfBillsPerPayPeriod = costPerPaycheck;
                dto.MonthlyCostOfBills      = costPerPaycheck * 26 / 12;

                return(dto);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
コード例 #11
0
 public CalculationsServiceTests()
 {
     _calculationsService = new CalculationsService();
 }
コード例 #12
0
 public void Setup()
 {
     _calculationsService = new CalculationsService();
 }