public void Create_ValidPaymentDetails_PaymentDetailsAppended() { //Arrange var confMock = new Mock <IConfiguration>(); confMock.Setup(x => x["PaymentInfoPath"]).Returns(@"E:\Logs.txt"); using (var writer = new StreamWriter(@"E:\Logs.txt")) { writer.Flush(); } var loggerMock = Mock.Of <ILogger <PaymentsController> >(); PaymentsController sut = new PaymentsController(loggerMock, confMock.Object); //Act PaymentInfo model1 = new PaymentInfo() { AccountName = "ABC XYZ", AccountNumber = "123456789", BSBNumber = "987654", Reference = 367, Amount = 3400 }; sut.Create(model1); PaymentInfo model2 = new PaymentInfo() { AccountName = "New Person", AccountNumber = "3432432423", BSBNumber = "456-656", Reference = 2322222, Amount = 670 }; sut.Create(model2); //Assert Assert.Contains("has been completed successfully.", sut.ViewBag.Message); using (var reader = new StreamReader(@"E:\Logs.txt")) { var fileData = reader.ReadToEnd(); Assert.Contains($"The below transaction was done successfully at {DateTime.Now.Date.ToString("M/d/yyyy")}", fileData); //file contains first set of payment data Assert.Contains("Account Name : ABC XYZ", fileData); Assert.Contains("Account Number : 123456789", fileData); Assert.Contains("BSB Number : 987654", fileData); Assert.Contains("Amount : 3400$", fileData); Assert.Contains("Reference : 367", fileData); //same file contains second set of payment data Assert.Contains("Account Name : New Person", fileData); Assert.Contains("Account Number : 3432432423", fileData); Assert.Contains("BSB Number : 456-656", fileData); Assert.Contains("Amount : 670", fileData); Assert.Contains("Reference : 2322222", fileData); } }
public void Create_WhenPaymentCreated_Returns201Created() { _repositoryMock .Setup(r => r.Add(It.IsAny <Payment>())) .Returns(Task.CompletedTask); var response = _paymentsController.Create(new Payment()).Result as CreatedAtRouteResult; Assert.NotNull(response); Assert.Equal(201, response.StatusCode); }
public void Create__when__Processor_returns_Success__should__return_201_and_PaymentId() { var request = new CreatePaymentRequest(); string paymentId = Guid.NewGuid().ToString(); paymentsProcessor.Setup(p => p.CreatePayment(It.IsAny <PaymentCreationData>())).Returns( new PaymentCreationResult { IsSuccess = true, PaymentId = paymentId } ); // act var result = controller.Create(request)?.Result as CreatedResult; result.StatusCode.Should().Be(201); var response = result.Value as CreatePaymentResponse; response.Should().NotBeNull(); response.PaymentId.Should().Be(paymentId); }
public void Create_PaymentFolderNotSet_ErrorMessageSet() { //Arrange var confMock = Mock.Of <IConfiguration>(); var loggerMock = Mock.Of <ILogger <PaymentsController> >(); PaymentsController sut = new PaymentsController(loggerMock, confMock); //Act PaymentInfo model = new PaymentInfo(); model.AccountName = null; sut.Create(model); //Assert Assert.EndsWith("ended in errors.", sut.ViewBag.Message); }
public async Task Create_invalid_returns_ErrorResponse() { var mockValidator = new Mock <IPaymentRequestValidator>(); mockValidator.Setup(a => a.Validate(It.IsAny <PaymentRequest>())).Returns(new DataValidationResult(ValidationErrors.empty_request, "No data was sent")); var controller = new PaymentsController(null, new PaymentMapper(new CardNumberGuard()), new ErrorToApiErrorCodeMapper(), mockValidator.Object); var result = await controller.Create(new CreatePaymentApiModel { }) as BadRequestObjectResult; Assert.NotNull(result); var errorResponse = result.Value as ErrorResponse; Assert.NotNull(errorResponse); Assert.Equal(ApiErrorCodes.empty_request, errorResponse.ErrorCode); }
public void Create_ValidPaymentFolder_SuccessMessageSet() { //Arrange var confMock = new Mock <IConfiguration>(); confMock.Setup(x => x["PaymentInfoPath"]).Returns(@"E:\Logs.txt"); var loggerMock = Mock.Of <ILogger <PaymentsController> >(); PaymentsController sut = new PaymentsController(loggerMock, confMock.Object); //Act PaymentInfo model = new PaymentInfo(); sut.Create(model); //Assert Assert.Contains("has been completed successfully.", sut.ViewBag.Message); }
public void Create_PaymentFolderNotAccessible_ErrorMessageSet() { //Arrange var confMock = new Mock <IConfiguration>(); confMock.Setup(x => x["PaymentInfoPath"]).Returns(@"C:\Logs.txt"); var loggerMock = Mock.Of <ILogger <PaymentsController> >(); PaymentsController sut = new PaymentsController(loggerMock, confMock.Object); //Act PaymentInfo model = new PaymentInfo(); model.AccountName = null; sut.Create(model); //Assert Assert.EndsWith("ended in errors.", sut.ViewBag.Message); }
public void Create_ValidPaymentFolder_PaymentDetailsUpdated() { //Arrange var confMock = new Mock <IConfiguration>(); confMock.Setup(x => x["PaymentInfoPath"]).Returns(@"E:\Logs.txt"); using (var writer = new StreamWriter(@"E:\Logs.txt")) { writer.Flush(); } var loggerMock = Mock.Of <ILogger <PaymentsController> >(); PaymentsController sut = new PaymentsController(loggerMock, confMock.Object); //Act PaymentInfo model = new PaymentInfo() { AccountName = "ABC XYZ", AccountNumber = "123456789", BSBNumber = "987654", Reference = 367, Amount = 3400, Description = "Credit Card Payment" }; sut.Create(model); //Assert Assert.Contains("has been completed successfully.", sut.ViewBag.Message); using (var reader = new StreamReader(@"E:\Logs.txt")) { var fileData = reader.ReadToEnd(); Assert.Contains($"The below transaction was done successfully at {DateTime.Now.Date.ToString("M/d/yyyy")}", fileData); Assert.Contains("Account Name : ABC XYZ", fileData); Assert.Contains("Account Number : 123456789", fileData); Assert.Contains("BSB Number : 987654", fileData); Assert.Contains("Amount : 3400$", fileData); Assert.Contains("Reference : 367", fileData); Assert.Contains("Description : Credit Card Payment", fileData); } }
public async Task Create_valid_returns_OkResponse() { var mockValidator = new Mock <IPaymentRequestValidator>(); mockValidator.Setup(a => a.Validate(It.IsAny <PaymentRequest>())).Returns(new DataValidationResult(ValidationErrors.none)); var mockPaymentManager = new Mock <IPaymentManager>(); mockPaymentManager.Setup(a => a.AddPayment(It.IsAny <PaymentRequest>())).Returns(Task.FromResult(new Payment() { Id = new Guid() })); var controller = new PaymentsController(mockPaymentManager.Object, new PaymentMapper(new CardNumberGuard()), new ErrorToApiErrorCodeMapper(), mockValidator.Object); var result = await controller.Create(new CreatePaymentApiModel { }) as OkObjectResult; Assert.NotNull(result); var response = result.Value as PaymentApiModel; Assert.NotNull(response); }