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); }
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)); }
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)); }
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); }
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)); }
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); }
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); } }
public void Setup() { _calculationsService = new CalculationsService(); var filePath = Common.GetFilePath("valid file.csv"); _numbersService = new NumbersService(filePath); _numbers = _numbersService.ReadNumbers(); }
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); }
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; } }
public CalculationsServiceTests() { _calculationsService = new CalculationsService(); }
public void Setup() { _calculationsService = new CalculationsService(); }