public void ExceptionsWalletTest()
        {
            WalletRepository wallet = new WalletRepository();

            Assert.Throws <InvalidDataException>(() => { wallet.AddToWallet(p10, -1); });
            Assert.Throws <ArgumentNullException>(() => { wallet.AddToWallet(null, 1); });
            Assert.Throws <ArgumentNullException>(() => { wallet.AddToWallet(null, 0); });
            Assert.Throws <ArgumentNullException>(() => { wallet.AddToWallet(null, -1); });

            Assert.Throws <InvalidDataException>(() => { wallet.RemoveFromWallet(p10, 1); });
            wallet.AddToWallet(p10, 1);
            Assert.Throws <InvalidDataException>(() => { wallet.RemoveFromWallet(p10, -1); });
            Assert.Throws <InvalidDataException>(() => { wallet.RemoveFromWallet(p10, 2); });
            wallet.RemoveFromWallet(p10, 1);
            Assert.Throws <InvalidDataException>(() => { wallet.RemoveFromWallet(p10, 1); });


            Assert.Throws <ArgumentNullException>(() => { wallet.RemoveFromWallet(null, -1); });
            Assert.Throws <ArgumentNullException>(() => { wallet.RemoveFromWallet(null, 0); });
            Assert.Throws <ArgumentNullException>(() => { wallet.RemoveFromWallet(null, 1); });

            Assert.Throws <NotImplementedException>(() => { wallet.ClearMoney(); });

            try
            {
                PaymentWalletRepository pwallet = new PaymentWalletRepository();
                pwallet.ClearMoney();
            }
            catch
            {
                Assert.True(false);
            }
        }
        public void AddWalletTest()
        {
            WalletRepository wallet = new WalletRepository();

            // Add
            wallet.AddToWallet(p5, 1);
            wallet.AddToWallet(p10, 2);
            wallet.AddToWallet(p20, 3);
            wallet.AddToWallet(p50, 4);
            wallet.AddToWallet(p100, 5);

            // verify
            var content = wallet.WalletList;

            Assert.Single(content.Where(c => c.Item1.MoneyValue == p5.MoneyValue));
            Assert.Single(content.Where(c => c.Item1.MoneyValue == p10.MoneyValue));
            Assert.Single(content.Where(c => c.Item1.MoneyValue == p20.MoneyValue));
            Assert.Single(content.Where(c => c.Item1.MoneyValue == p50.MoneyValue));
            Assert.Single(content.Where(c => c.Item1.MoneyValue == p100.MoneyValue));


            Assert.Equal(1, content.Single(c => c.Item1.Nominal == p5.Nominal).Item2);
            Assert.Equal(2, content.Single(c => c.Item1.Nominal == p10.Nominal).Item2);
            Assert.Equal(3, content.Single(c => c.Item1.Nominal == p20.Nominal).Item2);
            Assert.Equal(4, content.Single(c => c.Item1.Nominal == p50.Nominal).Item2);
            Assert.Equal(5, content.Single(c => c.Item1.Nominal == p100.Nominal).Item2);
        }
Example #3
0
        public void TestOrderPaymentChangeGivenFromOrderPanel()
        {
            // setup the tested class instance and dependencies with the test case initialization.
            MockDisplayPanel             displayPanel      = new MockDisplayPanel();
            MockOrderPanel               orderPanel        = new MockOrderPanel();
            MockPaymentReceiver          paymentReceiver   = new MockPaymentReceiver();
            MockVendingMessageRepository messageRepository = new MockVendingMessageRepository();
            ProductRepository            productRepository = new ProductRepository();
            WalletRepository             walletRepository  = new WalletRepository();
            SoldRecord saleRecord = new SoldRecord();
            VendingMachineController vendingMachineController = new VendingMachineController(displayPanel, paymentReceiver, orderPanel, productRepository, walletRepository, saleRecord, messageRepository);

            InitProductRepository(productRepository, 1, 1);
            // !!! setup order state
            vendingMachineController.VendingMachineState = VendingMachineState.Order;

            // setup wallet repository
            walletRepository.AddToWallet(new Coin(5), 1);
            walletRepository.AddToWallet(new Coin(10), 1);
            walletRepository.AddToWallet(new Coin(20), 1);
            walletRepository.AddToWallet(new Coin(50), 1);
            var before = walletRepository.WalletList.Aggregate(Money.Zero, (m, p) => m + new Money(0, (int)p.Item1.Nominal * p.Item2));

            // Test subject Action - select order as product A
            orderPanel.InvokeOrderAction(OrderCmdEvent.Select, PrdA);

            Assert.Equal(VendingMachineState.Payment, vendingMachineController.VendingMachineState);

            // verify reserved product
            Assert.Equal(PrdA, vendingMachineController.OrderedProducts[0]);

            // complete payment without change
            paymentReceiver.InvokeCoinAction(PaymentCmdEvent.Payment, new Coin(100));

            // verify switch back to order state
            Assert.True(paymentReceiver.OffCalled);
            Assert.True(orderPanel.OnCalled);
            Assert.Equal(VendingMachineState.Order, vendingMachineController.VendingMachineState);

            // verify money in the wallet
            var total = walletRepository.WalletList.Aggregate(Money.Zero, (m, p) => m + new Money(0, (int)p.Item1.Nominal * p.Item2));

            Assert.Equal(before + PrdA.Price, total);

            // verify the  message flow
            List <MessageCode> TestCatchedCodes = new List <MessageCode>
            {
                MessageCode.Checkout,
                MessageCode.BalancePayment,
                MessageCode.GivenChange,
                MessageCode.GivenChange,
                MessageCode.GivenChange,
                MessageCode.CollectYourPurchase,
                MessageCode.ReadyToService,
            };

            Assert.Equal((IEnumerable <MessageCode>)TestCatchedCodes, (IEnumerable <MessageCode>)messageRepository.CatchedCodes);
            Assert.Equal((IEnumerable <string>)messageRepository.ReturnList, (IEnumerable <string>)displayPanel.DisplayList);
        }
        public void RemoveWalletTest()
        {
            WalletRepository wallet = new WalletRepository();

            // Add
            wallet.AddToWallet(p5, 1);
            wallet.AddToWallet(p10, 2);
            wallet.AddToWallet(p20, 3);
            wallet.AddToWallet(p50, 4);
            wallet.AddToWallet(p100, 5);

            // remove

            wallet.RemoveFromWallet(p5, 1);
            wallet.RemoveFromWallet(p10, 2);
            wallet.RemoveFromWallet(p20, 3);
            wallet.RemoveFromWallet(p50, 4);
            wallet.RemoveFromWallet(p100, 5);

            // verify
            var content = wallet.WalletList;

            Assert.Empty(content);
        }