public void SuccessfullyMapsPaymentProcessingRequestModelToBankingApiRequestDto() { //Arrange var model = new ProcessPaymentRequestDto() { CardNumber = "5500000000000004", CardHolder = "Test Account", CardType = CardType.MasterCard, ExpirationMonth = DateTime.Now.ToString("MM"), ExpirationYear = DateTime.Now.AddYears(1).ToString("yy"), PaymentAmount = 100.00M, Currency = SupportedCurrencies.GBP, Cvv = "123" }; //Act var result = this._mapper.MapProcessPaymentRequestModelToBankDto(model); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <BankProcessPaymentRequestDto>(result); Assert.AreEqual(result.CardNumber, model.CardNumber); Assert.AreEqual(result.CardHolder, model.CardHolder); Assert.AreEqual(result.CardType, model.CardType); Assert.AreEqual(result.ExpirationMonth, model.ExpirationMonth); Assert.AreEqual(result.ExpirationYear, model.ExpirationYear); Assert.AreEqual(result.PaymentAmount, model.PaymentAmount); Assert.AreEqual(result.Currency, model.Currency); Assert.AreEqual(result.Cvv, model.Cvv); }
public BankProcessPaymentRequestDto MapProcessPaymentRequestModelToBankDto(ProcessPaymentRequestDto model) { if (model == null) { this._logger.LogError(Resources.Logging_DtoMapperNullInput, (typeof(ProcessPaymentRequestDto).Name)); throw new HttpException(HttpStatusCode.InternalServerError, Resources.ErrorCode_MappingError_PaymentApiToBankApi, Resources.ErrorMessage_MappingError_PaymentApiToBankApi); } var bankDto = new BankProcessPaymentRequestDto() { CardNumber = model.CardNumber, CardHolder = model.CardHolder, CardType = model.CardType, ExpirationMonth = model.ExpirationMonth, ExpirationYear = model.ExpirationYear, PaymentAmount = model.PaymentAmount, Currency = model.Currency, Cvv = model.Cvv }; return(bankDto); }
public async Task <ProcessPaymentResponse> ProcessPayment(ProcessPaymentRequestDto model) { var bankRequestDto = this._dtoMapper.MapProcessPaymentRequestModelToBankDto(model); var bankResponseDto = await this._bankingService.ProcessPayment(bankRequestDto); var processPaymentResponse = this._dtoMapper.MapBankApiPostResponseToDomainResponse(bankResponseDto); return(processPaymentResponse); }
public void Validate_WhenExpiryMonthIsNotValid_ShouldError(int expiryMonth) { var request = new ProcessPaymentRequestDto { CardExpiryMonth = expiryMonth }; var result = _validator.TestValidate(request); result.ShouldHaveValidationErrorFor(p => p.Amount); }
public void Validate_WhenAmountIsLessThanOrEqualsToZero_ShouldError(decimal amount) { var request = new ProcessPaymentRequestDto { Amount = amount }; var result = _validator.TestValidate(request); result.ShouldHaveValidationErrorFor(p => p.Amount); }
public void Validate_WhenCvvIsInvalid_ShouldError(int cvv) { var request = new ProcessPaymentRequestDto { Cvv = cvv }; var result = _validator.TestValidate(request); result.ShouldHaveValidationErrorFor(p => p.Cvv); }
public void ModelWithoutRequiredPropertiesShouldFailValidation(string propertyName) { //Arrange var model = new ProcessPaymentRequestDto(); //Act var results = ModelValidator.ValidateModel(model); //Assert Assert.IsTrue(results.Any(result => result.MemberNames.Contains(propertyName) && result.ErrorMessage.Contains("required"))); }
public async Task ProcessPaymentReturnsProcessPaymentResponse() { //Arrange var model = new ProcessPaymentRequestDto() { CardNumber = "5500000000000004", CardHolder = "Test Account", CardType = CardType.MasterCard, ExpirationMonth = DateTime.Now.ToString("MM"), ExpirationYear = DateTime.Now.AddYears(1).ToString("yy"), PaymentAmount = 100.00M, Currency = SupportedCurrencies.GBP, Cvv = "123" }; var bankingRequestDto = new BankProcessPaymentRequestDto() { CardNumber = "5500000000000004", CardHolder = "Test Account", CardType = CardType.MasterCard, ExpirationMonth = DateTime.Now.ToString("MM"), ExpirationYear = DateTime.Now.AddYears(1).ToString("yy"), PaymentAmount = 100.00M, Currency = SupportedCurrencies.GBP, Cvv = "123" }; var transactionId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; var bankingResponseDto = new BankProcessPaymentResponseDto() { TransactionId = transactionId, PaymentStatus = PaymentStatus.Success }; var guid = new Guid(transactionId); var processPaymentResponse = new ProcessPaymentResponse() { TransactionId = guid, PaymentStatus = PaymentStatus.Success }; this._dtoMapper.Setup(x => x.MapProcessPaymentRequestModelToBankDto(It.IsAny <ProcessPaymentRequestDto>())).Returns(bankingRequestDto); this._bankingService.Setup(x => x.ProcessPayment(It.IsAny <BankProcessPaymentRequestDto>())).ReturnsAsync(bankingResponseDto); this._dtoMapper.Setup(x => x.MapBankApiPostResponseToDomainResponse(It.IsAny <BankProcessPaymentResponseDto>())).Returns(processPaymentResponse); //Act var result = await this._paymentProcessingService.ProcessPayment(model); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <ProcessPaymentResponse>(result); Assert.AreEqual(guid, result.TransactionId); Assert.AreEqual(PaymentStatus.Success, result.PaymentStatus); }
public void Validate_WhenValuesAreNull_ShouldError() { var request = new ProcessPaymentRequestDto(); var result = _validator.TestValidate(request); result.ShouldHaveValidationErrorFor(p => p.Amount); result.ShouldHaveValidationErrorFor(p => p.Currency); result.ShouldHaveValidationErrorFor(p => p.Cvv); result.ShouldHaveValidationErrorFor(p => p.CardNumber); result.ShouldHaveValidationErrorFor(p => p.CardExpiryMonth); result.ShouldHaveValidationErrorFor(p => p.CardExpiryYear); }
public void ValidateCurrency(int currency, bool shouldFailValidation) { //Arrange var model = new ProcessPaymentRequestDto() { Currency = (SupportedCurrencies)currency }; //Act var results = ModelValidator.ValidateModel(model); //Assert Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("Currency"))); }
//The tests for the custom validation logic which require the current date are in the CustomAttributes folder. public void ValidatePaymentAmount(decimal paymentAmount, bool shouldFailValidation) { //Arrange var model = new ProcessPaymentRequestDto() { PaymentAmount = paymentAmount }; //Act var results = ModelValidator.ValidateModel(model); //Assert Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("PaymentAmount"))); }
//The tests for the custom validation logic which require the current date are in the CustomAttributes folder. public void ValidateExpirationYearFormat(string expirationYear, bool shouldFailValidation) { //Arrange var model = new ProcessPaymentRequestDto() { ExpirationYear = expirationYear }; //Act var results = ModelValidator.ValidateModel(model); //Assert Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("ExpirationYear"))); }
public void ValidateCvv(string cvv, bool shouldFailValidation) { //Arrange var model = new ProcessPaymentRequestDto() { Cvv = cvv }; //Act var results = ModelValidator.ValidateModel(model); //Assert Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("Cvv"))); }
public void ValidateCardType(int cardType, bool shouldFailValidation) { //Arrange var model = new ProcessPaymentRequestDto() { CardType = (CardType)cardType }; //Act var results = ModelValidator.ValidateModel(model); //Assert Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("CardType"))); }
public void ValidateExpirationMonth(string expirationMonth, bool shouldFailValidation) { //Arrange var model = new ProcessPaymentRequestDto() { ExpirationMonth = expirationMonth, ExpirationYear = DateTime.Now.AddYears(1).ToString("yy") //The tests for the custom validation logic are in the CustomAttributes folder. }; //Act var results = ModelValidator.ValidateModel(model); //Assert Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("ExpirationMonth"))); }
public void AttributeShouldOnlyTreatValuesOneToTwelveAsValidForFutureYears(string inputMonth, bool expectedOutput) { //Arrange var expirationYear = DateTime.Now.AddYears(1).ToString("yy"); var model = new ProcessPaymentRequestDto() { ExpirationYear = expirationYear }; var validationContext2 = new ValidationContext(model); //Act var result = IsValid(inputMonth, validationContext2); //Assert Assert.AreEqual(expectedOutput, string.IsNullOrEmpty(result?.ErrorMessage)); }
public void Validate_WhenValuesAreValid_ShouldNotError() { var request = new ProcessPaymentRequestDto { Currency = Currency.USD, CardNumber = "1111222233334444", CardExpiryMonth = 12, CardExpiryYear = 2020, Cvv = 123, Amount = 12 }; var result = _validator.TestValidate(request); result.ShouldNotHaveValidationErrorFor(p => p.Amount); result.ShouldNotHaveValidationErrorFor(p => p.Currency); result.ShouldNotHaveValidationErrorFor(p => p.Cvv); result.ShouldNotHaveValidationErrorFor(p => p.CardNumber); result.ShouldNotHaveValidationErrorFor(p => p.CardExpiryMonth); result.ShouldNotHaveValidationErrorFor(p => p.CardExpiryYear); }
public void AttributeShouldOnlyTreatCurrentOrFurtureMonthsAsValidForFutureYears(string inputMonth) { //Arrange var expirationYear = DateTime.Now.ToString("yy"); var model = new ProcessPaymentRequestDto() { ExpirationYear = expirationYear }; var validationContext2 = new ValidationContext(model); int inputMonthAsInt; int.TryParse(inputMonth, out inputMonthAsInt); var expectedOutput = inputMonthAsInt >= DateTime.Now.Month && inputMonthAsInt <= 12; //Act var result = IsValid(inputMonth, validationContext2); //Assert Assert.AreEqual(expectedOutput, string.IsNullOrEmpty(result?.ErrorMessage)); }
public async Task <ActionResult <ProcessPaymentResponseDto> > ProcessPayment( [FromBody] ProcessPaymentRequestDto paymentRequestDto) { var user = await _userManager.FindByNameAsync(User.Identity.Name); var paymentResult = await _paymentProcessor.ProcessAsync(new PaymentRequest { Amount = paymentRequestDto.Amount.Value, Currency = paymentRequestDto.Currency.Value, CardCvv = paymentRequestDto.Cvv.Value, CardNumber = paymentRequestDto.CardNumber, CardExpiryMonth = paymentRequestDto.CardExpiryMonth.Value, CardExpiryYear = paymentRequestDto.CardExpiryYear.Value, User = user }); return(Ok(new ProcessPaymentResponseDto { PaymentId = paymentResult.Payment.Id, Success = paymentResult.Payment.Success })); }
public async Task ProcessPaymentShouldReturn200() { //Arrange var model = new ProcessPaymentRequestDto(); var guid = new Guid(); var serviceResponse = new ProcessPaymentResponse() { TransactionId = guid, PaymentStatus = PaymentStatus.Success }; this._paymentProcessingService.Setup(x => x.ProcessPayment(It.IsAny <ProcessPaymentRequestDto>())) .ReturnsAsync(serviceResponse); //Act var result = await this._controller.ProcessPayment(model); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <OkObjectResult>(result); var resultAsOkObject = result as OkObjectResult; Assert.IsTrue(resultAsOkObject.StatusCode == 200); Assert.IsNotNull(resultAsOkObject.Value); Assert.IsInstanceOf <ResponseBaseDto>(resultAsOkObject.Value); var resultValue = resultAsOkObject.Value as ResponseBaseDto; Assert.IsTrue(resultValue.StatusCode == HttpStatusCode.OK); Assert.IsNotNull(resultValue.Data); Assert.IsInstanceOf <ProcessPaymentResponse>(resultValue.Data); var resultData = resultValue.Data as ProcessPaymentResponse; Assert.AreEqual(guid, resultData.TransactionId); Assert.AreEqual(PaymentStatus.Success, resultData.PaymentStatus); }