public async Task <IActionResult> PostAsync([FromBody] BasicAwardCalculationRequestModel data) { try { if (data == null) { ModelState.AddModelError("", "Bad payload provided; unable to parse the request , please review request data and try again"); _logger.LogError(400, $"Bad payload " + $"{ModelState}\n Request Data is null and could not be parsed"); return(BadRequest(ModelState)); } var validator = new BasicAwardCalculationRequestValidator(); if (!ModelState.IsValid || !validator.Validate(data).IsValid) { _logger.LogError((int)HttpStatusCode.BadRequest, $"Request model not valid " + $"{ModelState}\n Request Data {JsonConvert.SerializeObject(data)} \n Errors : " + $"{validator.Validate(data).Errors.GetErrorsAsString()} "); return(BadRequest(ModelState)); } var result = await _baService.PerformBasicAwardCalculationAsync(data, _options); _logger.LogInformation((int)HttpStatusCode.OK, $"Calculation performed successfully for the request data provided \n Request Data: {JsonConvert.SerializeObject(data)}"); return(Ok(result)); } catch (MissingConfigurationException exp) { _logger.LogError((int)HttpStatusCode.BadRequest, $"Calculation was not performed for the request data provided \n Request Data: {JsonConvert.SerializeObject(data)} Bad payload provided; {exp.Message}, please review request data and try again"); return(BadRequest($"Bad payload provided; {exp.Message}, please review request data and try again")); } }
public async Task <BasicAwardCalculationResponseDTO> PerformBasicAwardCalculationAsync( BasicAwardCalculationRequestModel data, IOptions <ConfigLookupRoot> options) { DateTime date = DateTime.Today; var taxRate = ConfigValueLookupHelper.GetTaxRate(options, date); var niThreshold = ConfigValueLookupHelper.GetNIThreshold(options, date); var niUpperThreshold = ConfigValueLookupHelper.GetNIUpperThreshold(options, date); var niRate = ConfigValueLookupHelper.GetNIRate(options, date); var niUpperRate = ConfigValueLookupHelper.GetNIUpperRate(options, date); decimal taxDeducted = Math.Round(await data.BasicAwardAmount.GetTaxDeducted(taxRate, data.IsTaxable), 2); decimal niDeducted = Math.Round(await data.BasicAwardAmount.GetNIDeducted(niThreshold, niUpperThreshold, niRate, niUpperRate, data.IsTaxable), 2); var response = new BasicAwardCalculationResponseDTO() { GrossEntitlement = Math.Round(data.BasicAwardAmount, 2), IsTaxable = data.IsTaxable, TaxDeducted = taxDeducted, NIDeducted = niDeducted, NetEntitlement = Math.Round(data.BasicAwardAmount, 2) - taxDeducted - niDeducted, PreferentialClaim = 0m, NonPreferentialClaim = Math.Round(data.BasicAwardAmount, 2) }; return(await Task.FromResult(response)); }
public async Task PerformCalculationAsync_WhenBasicAwardAmountIsGreaterThanUpperThreshold() { var request = new BasicAwardCalculationRequestModel { BasicAwardAmount = 1000m, IsTaxable = true }; var response = await _service.PerformBasicAwardCalculationAsync(request, _options); response.GrossEntitlement.Should().Be(request.BasicAwardAmount); response.IsTaxable.Should().Be(request.IsTaxable); response.TaxDeducted.Should().Be(200m); response.NIDeducted.Should().Be(94.24m); response.NetEntitlement.Should().Be(705.76m); response.PreferentialClaim.Should().Be(0m); response.NonPreferentialClaim.Should().Be(response.GrossEntitlement); }
public async Task PerformCalculationAsync_WhenBasicAwardAmountIsBetweenLowerAndUpperThreshold() { var request = new BasicAwardCalculationRequestModel { BasicAwardAmount = 500m, IsTaxable = true }; var response = await _service.PerformBasicAwardCalculationAsync(request, _options); response.GrossEntitlement.Should().Be(request.BasicAwardAmount); response.IsTaxable.Should().Be(request.IsTaxable); response.TaxDeducted.Should().Be(100m); response.NIDeducted.Should().Be(38.04m); response.NetEntitlement.Should().Be(361.96m); response.PreferentialClaim.Should().Be(0m); response.NonPreferentialClaim.Should().Be(response.GrossEntitlement); }
public async Task PerformCalculationAsync_WhenIsTaxableIsFalse() { var request = new BasicAwardCalculationRequestModel { BasicAwardAmount = 500m, IsTaxable = false }; var response = await _service.PerformBasicAwardCalculationAsync(request, _options); response.GrossEntitlement.Should().Be(request.BasicAwardAmount); response.IsTaxable.Should().Be(request.IsTaxable); response.TaxDeducted.Should().Be(0m); response.NIDeducted.Should().Be(0m); response.NetEntitlement.Should().Be(500m); response.PreferentialClaim.Should().Be(0m); response.NonPreferentialClaim.Should().Be(response.GrossEntitlement); }
public async Task PostAsync_FailsWithBadRequest_WhenThereIsAValidationError(BasicAwardCalculationRequestModel request, string expectedErrorMessage) { //Arrange var controller = new BasicAwardController(_service.Object, _mockLogger.Object, _confOptions); //Act var result = await controller.PostAsync(request); //Assert var badRequestObjectRequest = result.Should().BeOfType <BadRequestObjectResult>().Subject; badRequestObjectRequest.StatusCode.Should().Be((int)System.Net.HttpStatusCode.BadRequest); _mockLogger.Verify(x => x.Log( LogLevel.Error, It.IsAny <EventId>(), It.Is <object>(v => v.ToString().Contains(expectedErrorMessage)), null, It.IsAny <Func <object, Exception, string> >() )); }