예제 #1
0
 public void Setup()
 {
     _mockAccountRepository   = new Mock <IAccountRepository>();
     _mockNotificationService = new Mock <INotificationService>();
     _testTransferMoney       = new TransferMoney(_mockAccountRepository.Object, _mockNotificationService.Object);
     _testWithdrawMoney       = new WithdrawMoney(_mockAccountRepository.Object, _mockNotificationService.Object);
 }
예제 #2
0
        public void WithdrawMoney_InsufficientFundsTest()
        {
            var accountId = Guid.NewGuid();
            var user      = new User {
                Id = Guid.NewGuid(), Name = "User", Email = ""
            };
            var account = new Account {
                Id = accountId, Balance = 0, User = user
            };

            IAccountRepository accounts = new MockAccountRepository();

            accounts.Add(account);

            var notificationService = new MockNotificationService();

            WithdrawMoney withdrawMoneyFeature = new WithdrawMoney(accounts, notificationService);

            try
            {
                withdrawMoneyFeature.Execute(accountId, 100);
            }
            catch (InvalidOperationException ex)
            {
                if (ex.Message == "Insufficient funds to make transfer")
                {
                    return;
                }
            }
            Assert.Fail();
        }
예제 #3
0
        public void Execute_BalanceIsLessThanAmountToWithdraw_ThrowInvalidOperationException()
        {
            _fromAccount.Balance = 1m;
            var sut = new WithdrawMoney(_accountRepository.Object, _notificationService.Object);

            Assert.Throws <InvalidOperationException>(() => sut.Execute(_fromAccountId, 2m));
        }
        public void Execute_ShouldSendLowFundsNotification()
        {
            // Arrange
            decimal amount = 500m;

            var from = new Account {
                Balance = 999m
            };                                         // simply change the account's balance to 1000 to see this test fail

            // Since that the notification limit is 500, if the new balance falls under this limit
            // this test should pass. In order to test it, just change the balance and the amount
            // that is being send accordingly. If the new balance is equal to or more than 500 this test will fail.

            var mockAccountRepository   = new Mock <IAccountRepository>();
            var mockNotificationService = new Mock <INotificationService>();

            var fromID = Guid.NewGuid();

            from.User = new User {
                Email = "*****@*****.**", Id = fromID
            };

            var withdrawMoney = new WithdrawMoney(mockAccountRepository.Object, mockNotificationService.Object);

            mockAccountRepository.Setup(x => x.GetAccountById(It.Is <Guid>(y => y == fromID))).Returns(from);


            mockNotificationService.Setup(x => x.NotifyFundsLow(from.User.Email));

            // Act
            withdrawMoney.Execute(fromID, amount);

            // Assert
            mockNotificationService.Verify(x => x.NotifyFundsLow(from.User.Email));
        }
        public void Execute_ShouldThrowInsufficientFundsException()
        {
            // Arrange
            decimal amount = 500m;

            var from = new Account {
                Balance = 499m
            };                                            // Since that the the exception would be thrown if the new balance is less than 0 and we send 500,
                                                          // then if the value of account's balance changes to 500 or more this test will fail.

            var mockAccountRepository   = new Mock <IAccountRepository>();
            var mockNotificationService = new Mock <INotificationService>();

            var fromID = Guid.NewGuid();

            from.User = new User {
                Email = "*****@*****.**", Id = fromID
            };

            var withdrawMoney = new WithdrawMoney(mockAccountRepository.Object, mockNotificationService.Object);

            mockAccountRepository.Setup(x => x.GetAccountById(It.Is <Guid>(y => y == fromID))).Returns(from);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => withdrawMoney.Execute(fromID, amount));
        }
예제 #6
0
        public void WithdrawMoney_Ensure_that_account_repo_is_called_to_update_account()
        {
            var fromAccountGuid = new System.Guid("adc1c2b0-bb71-4205-bf95-91bdbda67d75");

            var user = new User()
            {
                Email = "*****@*****.**"
            };
            var fromAccount = new Account()
            {
                Balance = 1000m, User = user
            };


            var accountRepoMock         = new Mock <IAccountRepository>();
            var notificationServiceMock = new Mock <INotificationService>();

            accountRepoMock.Setup(xx => xx.GetAccountById(fromAccountGuid)).Returns(fromAccount);



            var sut = new WithdrawMoney(accountRepoMock.Object, notificationServiceMock.Object);

            sut.Execute(fromAccountGuid, 600.0m);

            Assert.AreEqual(400m, fromAccount.Balance);
            accountRepoMock.Verify(x => x.Update(It.IsAny <Account>()), Times.Once);
        }
        public void Execute_ShouldUpdateAccountSuccessfully()
        {
            // Arrange
            decimal amount = 500m;

            // As the withdrawal takes place succesfully, the account is being updated at the end.
            // However, if an exception is thrown, the Execute method will never reach
            // the update invocation.

            var from = new Account {
                Balance = 500m
            };                                         // simply change the account's balance to 499 or less to cause an insufficient funds exception and see this test failing.

            var mockAccountRepository   = new Mock <IAccountRepository>();
            var mockNotificationService = new Mock <INotificationService>();

            var fromID = Guid.NewGuid();

            from.User = new User {
                Email = "*****@*****.**", Id = fromID
            };

            var withdrawMoney = new WithdrawMoney(mockAccountRepository.Object, mockNotificationService.Object);

            mockAccountRepository.Setup(x => x.GetAccountById(It.Is <Guid>(y => y == fromID))).Returns(from);

            mockAccountRepository.Setup(y => y.Update(from));

            // Act
            withdrawMoney.Execute(fromID, amount);

            // Assert
            mockAccountRepository.Verify(y => y.Update(from));
        }
예제 #8
0
        public WithdrawMoneyTests()
        {
            this.account = new Account
            {
                Id        = Guid.NewGuid(),
                Balance   = 1000,
                PaidIn    = 0,
                Withdrawn = 0,
                User      = new User
                {
                    Id    = Guid.NewGuid(),
                    Email = "*****@*****.**",
                    Name  = "John Smith"
                }
            };
            this.accountRepositoryMock = new Mock <IAccountRepository>();
            this.accountRepositoryMock
            .Setup(r => r.GetAccountById(this.account.Id))
            .Returns(this.account);

            this.accountRepositoryMock.Setup(r => r.Update(this.account)).Callback <Account>(acc => this.updatedAccount = acc);

            this.notificationServiceMock = new Mock <INotificationService>();

            this.sut = new WithdrawMoney(this.accountRepositoryMock.Object, this.notificationServiceMock.Object);
        }
예제 #9
0
        public void WithdrawMoney_SufficientFundsTest()
        {
            var accountId = Guid.NewGuid();
            var user      = new User {
                Id = Guid.NewGuid(), Name = "User", Email = ""
            };
            var account = new Account {
                Id = accountId, Balance = 100, User = user
            };

            IAccountRepository accounts = new MockAccountRepository();

            accounts.Add(account);

            var notificationService = new MockNotificationService();

            WithdrawMoney withdrawMoneyFeature = new WithdrawMoney(accounts, notificationService);

            withdrawMoneyFeature.Execute(accountId, 100);

            account = accounts.GetAccountById(accountId);

            Assert.AreEqual(0, account.Balance);
            Assert.AreEqual(-100, account.Withdrawn);
        }
        public void Execute_Successful()
        {
            const decimal amount = 200m;

            var accountRepository   = mockAccountRepository.Object;
            var notificationService = mockNotificationService.Object;

            var account = new Account(notificationService)
            {
                Id      = fromAccountId,
                Balance = 1000m
            };

            mockAccountRepository.Setup(x => x.GetAccountById(It.IsAny <Guid>()))
            .Returns(account);
            mockAccountRepository.Setup(x => x.Update(It.IsAny <Account>()));

            withdrawMoney = new WithdrawMoney(accountRepository);

            withdrawMoney.Execute(fromAccountId, amount);

            mockAccountRepository.Verify(x => x.GetAccountById(fromAccountId));

            mockAccountRepository.Verify(x => x.Update(account));

            Assert.AreEqual(800m, account.Balance);
        }
예제 #11
0
        public void WithdrawMoney_ensure_withdrawn_is_updated_with_correct_amount()
        {
            var fromAccountGuid = new System.Guid("adc1c2b0-bb71-4205-bf95-91bdbda67d75");

            var user = new User()
            {
                Email = "*****@*****.**"
            };
            var fromAccount = new Account()
            {
                Balance = 1000m, User = user
            };


            var accountRepoMock         = new Mock <IAccountRepository>();
            var notificationServiceMock = new Mock <INotificationService>();

            accountRepoMock.Setup(xx => xx.GetAccountById(fromAccountGuid)).Returns(fromAccount);



            var sut = new WithdrawMoney(accountRepoMock.Object, notificationServiceMock.Object);

            sut.Execute(fromAccountGuid, 600.0m);


            Assert.AreEqual(-600m, fromAccount.Withdrawn);
        }
예제 #12
0
        public void WithdrawMoneyExecute_ExecutingWithdraw_RunsWithSuccess()
        {
            // Arrange

            var fromAccountId = Guid.NewGuid();

            var fromAccount = new Account();

            fromAccount.Balance   = 600m;
            fromAccount.Withdrawn = 500m;
            fromAccount.User      = new User();
            fromAccount.Id        = fromAccountId;

            var amount = 10;

            this.mockAccountRepository.Setup(x => x.GetAccountById(It.Is <Guid>(v => v.Equals(fromAccountId))))
            .Returns(fromAccount);


            var myWithdrawMoeny = new WithdrawMoney(this.mockAccountRepository.Object, this.mockNotificationService.Object);

            // Act

            myWithdrawMoeny.Execute(fromAccountId, amount);

            // Assert

            this.mockAccountRepository.Verify(x => x.GetAccountById(fromAccountId), Times.Once);

            this.mockNotificationService.Verify(x => x.NotifyApproachingPayInLimit(It.IsAny <string>()), Times.Never);
            this.mockNotificationService.Verify(x => x.NotifyFundsLow(It.IsAny <string>()), Times.Never);

            this.mockAccountRepository.Verify(x => x.Update(fromAccount), Times.Once);
        }
예제 #13
0
        public WithdrawMoneyTests()
        {
            this.accountRepositoryMock   = new Mock <IAccountRepository>();
            this.notificationServiceMock = new Mock <INotificationService>();

            transferMoneyFeature = new WithdrawMoney(accountRepositoryMock.Object, notificationServiceMock.Object);
        }
예제 #14
0
        public void WithdrawMoney_Given_From_account_balance_is_less_than_500m_after_withdrawal_fire_a_notification_NotifyFundsLow()
        {
            var fromAccountGuid = new System.Guid("adc1c2b0-bb71-4205-bf95-91bdbda67d75");

            var user = new User()
            {
                Email = "*****@*****.**"
            };
            var fromAccount = new Account()
            {
                Balance = 1000m, User = user
            };


            var accountRepoMock         = new Mock <IAccountRepository>();
            var notificationServiceMock = new Mock <INotificationService>();

            accountRepoMock.Setup(xx => xx.GetAccountById(fromAccountGuid)).Returns(fromAccount);



            var sut = new WithdrawMoney(accountRepoMock.Object, notificationServiceMock.Object);

            sut.Execute(fromAccountGuid, 600.0m);

            Assert.AreEqual(400m, fromAccount.Balance);

            notificationServiceMock.Verify(x => x.NotifyFundsLow(fromAccount.User.Email), Times.Once);
        }
        public void Withdraw_Sufficient_Funds()
        {
            var withdrawAmount = 1000m;

            var withdrawMoneyFeature = new WithdrawMoney(_mockAccountRepository.Object, _mockNotificationService.Object);

            withdrawMoneyFeature.Execute(this._fromAccount.Id, withdrawAmount);
        }
예제 #16
0
        public void TestInsufficientFunds()
        {
            PopulateData();
            var withdrawMoney = new WithdrawMoney(_accountRepository.Object, _notificationService.Object);
            var ex            = Assert.Throws <InvalidOperationException>(() => withdrawMoney.Execute(fromAccount.Id, 2500));

            Assert.That(ex.Message.Equals($"Insufficient funds for {fromAccount.User.Name}"));
        }
예제 #17
0
        public void SetUp()
        {
            mockAccountRepository = new Mock <IAccountRepository>();
            mockPayOutValidator   = new Mock <IPayoutValidator>();
            mockPayInValidator    = new Mock <IPayInValidator>();

            this.withdrawMoney = new WithdrawMoney(mockAccountRepository.Object);
        }
        public void Execute_DoesNotUpdateAccountInRepo_When_InsufficientFunds()
        {
            var withdrawMoney = new WithdrawMoney(accountRepository.Object, notificationService.Object);

            var exception = Assert.Throws <InvalidOperationException>(() => withdrawMoney.Execute(accountguid, 4001m));

            accountRepository.Verify(x => x.Update(account), Times.Never);
        }
        public void Execute_UpdatesAccountInRepo_When_SufficientFunds()
        {
            var withdrawMoney = new WithdrawMoney(accountRepository.Object, notificationService.Object);

            withdrawMoney.Execute(accountguid, 100);

            accountRepository.Verify(x => x.Update(account), Times.AtLeast(1));
        }
예제 #20
0
        public void ValidateSufficientWithdrawFundsSuccess()
        {
            var amountToWithdraw = 1001;

            var withdrawMoney = new WithdrawMoney(_accountRepositoryMock.Object);

            withdrawMoney.Execute(_fromAccountId, amountToWithdraw);
        }
        public void Withdraw_Insufficient_Funds()
        {
            var withdrawAmount = 1001m;

            var withdrawMoneyFeature = new WithdrawMoney(_mockAccountRepository.Object, _mockNotificationService.Object);

            Assert.Throws <InvalidOperationException>(() => withdrawMoneyFeature.Execute(this._fromAccount.Id, withdrawAmount));
        }
예제 #22
0
        public void TestFundsIsLow()
        {
            PopulateData();
            var withdrawMoney = new WithdrawMoney(_accountRepository.Object, _notificationService.Object);

            withdrawMoney.Execute(fromAccount.Id, 1800);
            _notificationService.Verify(x => x.NotifyFundsLow(fromAccount.User.Email));
            _accountRepository.Verify(x => x.Update(fromAccount));
        }
        public void WithdrawMoney()
        {
            Setup();
            WithdrawMoney withdrawMoney = new WithdrawMoney(accountRepository.Object, notifcationService.Object);

            accountRepository.Setup(m => m.GetAccountById(account.Id)).Returns((account));// => accounts.Dequeue());
            withdrawMoney.Execute(account.Id, 500);
            Assert.Equal(5500, account.Balance);
        }
        public void Execute_UpdatesWithdrawnAndBalance_When_SufficientFunds()
        {
            var withdrawMoney = new WithdrawMoney(accountRepository.Object, notificationService.Object);

            withdrawMoney.Execute(accountguid, 100);

            Assert.That(account.Withdrawn, Is.EqualTo(0));
            Assert.That(account.Balance, Is.EqualTo(900));
        }
예제 #25
0
        public void Setup()
        {
            _accountRepository   = new Mock <IAccountRepository>();
            _notificationService = new Mock <INotificationService>();

            var comand = new WithdrawCommand(_accountRepository.Object, _notificationService.Object);

            _subject = new WithdrawMoney(comand);
        }
예제 #26
0
        public void Execute_BalanceBecomesLowerThanThreshold500_SendNotification()
        {
            _fromAccount.Balance = 500m;
            var sut = new WithdrawMoney(_accountRepository.Object, _notificationService.Object);

            sut.Execute(_fromAccountId, 1m);

            _notificationService.Verify(n => n.NotifyFundsLow(_fromUser.Email));
        }
예제 #27
0
        public void WithdrawMoney_OnUninitializedBankAccount_Throws()
        {
            var bankAccount           = BankAccount.Rehydrate(DefaultBankAccountId, Enumerable.Empty <BankAccountEvent>());
            var bankAccountRepository = ConfigureBankAccountRepositoryForAccount(bankAccount);
            var systemUnderTest       = new WithdrawMoneyHandler(bankAccountRepository);
            var command = new WithdrawMoney(DefaultBankAccountId, Transaction.Of(Guid.Parse("996228f4-70cb-4e44-9716-8a2a3b27d18c")), new Money(200, Currency.Euro), TimeStamp.Of(3));

            systemUnderTest.Awaiting(it => it.Handle(command, CancellationToken.None)).Should().Throw <ConstraintViolationException>();
        }
예제 #28
0
        public void TestSufficentWithdraw()
        {
            decimal       startBalance         = this.testFromAccount.Balance;
            WithdrawMoney withdrawMoneyFeature = new WithdrawMoney(accountRepository.Object, notificationService.Object);

            withdrawMoneyFeature.Execute(testFromAccount.Id, 10.0m);

            Assert.AreEqual(this.testFromAccount.Balance, startBalance - 10.0m);
        }
예제 #29
0
        public void WithdrawMoney_WithLimitExceeded_Throws()
        {
            var bankAccount           = new BankAccountBuilder(DefaultBankAccountId).WithCurrency(Currency.Euro).WithDepositedMoney(100).Build();
            var bankAccountRepository = ConfigureBankAccountRepositoryForAccount(bankAccount);
            var systemUnderTest       = new WithdrawMoneyHandler(bankAccountRepository);
            var command = new WithdrawMoney(DefaultBankAccountId, Transaction.Of(Guid.Parse("996228f4-70cb-4e44-9716-8a2a3b27d18c")), new Money(200, Currency.Euro), TimeStamp.Of(3));

            systemUnderTest.Awaiting(it => it.Handle(command, CancellationToken.None)).Should().Throw <LimitExceededException>();
        }
예제 #30
0
        public void TestEventBalanceIsZero()
        {
            PopulateData();
            var withdrawMoney = new WithdrawMoney(_accountRepository.Object, _notificationService.Object);

            withdrawMoney.Execute(fromAccount.Id, 2000);
            var account = fromAccount.Events.OfType <Account>().SingleOrDefault();

            Assert.AreEqual(account.Balance, fromAccount.Balance);
        }
예제 #31
0
        public ActionResult WithdrawMoney(WithdrawMoney model)
        {
            ViewBag.message = "";

            if (!ModelState.IsValid)
            {
                return View(model);
            }

            string updateBy = AccountInfo.GetUserName(Request);
            string username = model.Username;
            int withDrawMoney = model.AmountOfMoney;
            int transactionType = 3;
            string transactionContent = "Rút tiền";

            UserinfoModel userinfo = new UserinfoModel();
            DataTable userInfoDataTable = new DataTable();
            UserInfoDetailTableAdapter userInfoDetailAdapter = new UserInfoDetailTableAdapter();
            UserInfoTableAdapter userInfoAdapter = new UserInfoTableAdapter();
            userInfoDataTable = userInfoAdapter.GetDataByUsername(username);
            //string userTypeName = null;
            //string email = null;
            //if (!string.IsNullOrEmpty(userInfoDataTable.Rows[0]["TypeShortName"].ToString()))
            //{
            //	userInfoDataTable = userInfoDetailAdapter.GetDataByUsername(username);
            //	userTypeName = (string)userInfoDataTable.Rows[0]["TypeName"];
            //	email = userInfoDataTable.Rows[0]["Email"].ToString();
            //}
            //else
            //{
            //	AccountTableAdapter accountAdapter = new AccountTableAdapter();
            //	DataTable accountDataTable = new DataTable();
            //	accountDataTable = accountAdapter.GetDataByUsername(username);
            //	email = accountDataTable.Rows[0]["Email"].ToString();
            //}
            DateTime date = DateTime.Parse(userInfoDataTable.Rows[0]["LastUpdatedMoney"].ToString());
            int amountOfMoney = (int)userInfoDataTable.Rows[0]["AmountOfMoney"];

            TransactionHistoryTableAdapter transactionAdapter = new TransactionHistoryTableAdapter();
            int? money = transactionAdapter.GetCurrentMoney(username, date);
            if (money == null)
            {
                money = 0;
            }

            amountOfMoney += money.Value;

            if (withDrawMoney > amountOfMoney)
            {
                Session["withDrawMoney"] = "Giao dịch thất bại! Số tiền trong tài khoản không đủ để thực hiện giao dịch";
                return View(model);
            }
            else
            {
                try
                {
                    string transactionID = transactionAdapter.RechargeMoneyScalar(username, transactionType, amountOfMoney, transactionContent, null, false, date, updateBy, date).ToString();
                    int id = int.Parse(transactionID);
                    XmlSync.SaveTransactionHistoryXml(id, username, transactionType, amountOfMoney, transactionContent, null, false, date, updateBy, date, null);
                    Session["withDrawMoney"] = "Giao dịch thành công!";
                }
                catch (Exception ex)
                {
                    Log.ErrorLog(ex.Message);
                    Session["withDrawMoney"] = "Giao dịch thất bại!";
                }

                return RedirectToAction("WithdrawMoney","Transaction");
            }
        }