Exemplo n.º 1
0
        public void TestOff()
        {
            // 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);

            // Test subject Action - off
            vendingMachineController.Off();

            // verify - off
            Assert.True(orderPanel.OffCalled);
            Assert.True(paymentReceiver.OffCalled);

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

            // verify the  message flow
            List <MessageCode> TestCatchedCodes = new List <MessageCode>
            {
                MessageCode.OutOfServise
            };

            Assert.Equal((IEnumerable <MessageCode>)TestCatchedCodes, (IEnumerable <MessageCode>)messageRepository.CatchedCodes);
            Assert.Equal((IEnumerable <string>)messageRepository.ReturnList, (IEnumerable <string>)displayPanel.DisplayList);
        }
Exemplo n.º 2
0
        public void TestOutOfStockFromOrderPanel()
        {
            // 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, 0, 0);
            // !!! setup order state
            vendingMachineController.VendingMachineState = VendingMachineState.Order;

            // Test subject Action - out of stock
            orderPanel.InvokeOrderAction(OrderCmdEvent.OutOfStock, null);

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

            // verify - out of stock
            Assert.True(orderPanel.OffCalled);
            Assert.Equal(0, productRepository.CountProduct(PrdA.Code));

            // verify the  message flow
            List <MessageCode> TestCatchedCodes = new List <MessageCode>
            {
                MessageCode.OutOfStock
            };

            Assert.Equal((IEnumerable <MessageCode>)TestCatchedCodes, (IEnumerable <MessageCode>)messageRepository.CatchedCodes);
            Assert.Equal((IEnumerable <string>)messageRepository.ReturnList, (IEnumerable <string>)displayPanel.DisplayList);
        }
Exemplo n.º 3
0
        public void TestSelectOrderFromOrderPanel()
        {
            // 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;

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

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

            // verify - select order as product A
            Assert.True(orderPanel.OffCalled);
            Assert.True(paymentReceiver.OnCalled);
            Assert.Equal(0, productRepository.CountProduct(PrdA.Code));

            // verify the  message flow
            List <MessageCode> TestCatchedCodes = new List <MessageCode>
            {
                MessageCode.Checkout
            };

            Assert.Equal(TestCatchedCodes, messageRepository.CatchedCodes);
            Assert.Equal(messageRepository.ReturnList, displayPanel.DisplayList);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public void TestSelect()
        {
            // setup the tested class instance and dependencies with the test case initialization.
            var mockDisplayPanel = new MockDisplayPanel();
            // !!! define the simulation key pad command
            var mockReadKeypadInput = new MockReadKeypadInput()
            {
                SimInputAs = 'A'
            };
            var mockVendingMessageRepository = new MockVendingMessageRepository();
            IProductRepository product       = new ProductRepository();

            product.RegisterOrUpdateProduct(PrdA);
            product.RegisterOrUpdateProduct(PrdB);
            product.AddToStock('a', 1);
            product.AddToStock('b', 1);
            var           orderPanel = new OrderPanel(mockDisplayPanel, mockReadKeypadInput, product, mockVendingMessageRepository);
            OrderCmdEvent cmd        = OrderCmdEvent.OutOfStock;
            Product       objPrd     = null;

            // !! define the action test listener
            orderPanel.OrderAction += (c, p) =>
            {
                cmd    = c;
                objPrd = p;
                // off after getting the event
                orderPanel.Off();
            };
            bool exception = false;

            orderPanel.FailtException += ex => exception = true;

            // Test subject Action - select a product
            orderPanel.On();
            Thread.Sleep(10000);

            // verify - selected product A
            Assert.False(exception);
            Assert.Equal(OrderCmdEvent.Select, cmd);
            Assert.Equal(objPrd, PrdA);

            // verify the  message flow
            List <MessageCode> TestCatchedCodes = new List <MessageCode>
            {
                MessageCode.MakeYourOrder,
                MessageCode.SelectOrderLine,
                MessageCode.SelectOrderLine,
                MessageCode.SelectOrder,
                MessageCode.Ok
            };

            Assert.Equal((IEnumerable <MessageCode>)TestCatchedCodes, (IEnumerable <MessageCode>)mockVendingMessageRepository.CatchedCodes);
            Assert.Equal((IEnumerable <string>)mockVendingMessageRepository.ReturnList, (IEnumerable <string>)mockDisplayPanel.DisplayList);
        }
        public void TestPayment()
        {
            // setup the tested class instance and dependencies with the test case initialization.
            var             mockDisplayPanel             = new MockDisplayPanel();
            var             mockReadKeypadInput          = new MockReadKeypadInput();
            var             mockVendingMessageRepository = new MockVendingMessageRepository();
            var             paymentPanel = new PaymentReceiver(mockDisplayPanel, mockReadKeypadInput, mockVendingMessageRepository);
            PaymentCmdEvent cmd          = PaymentCmdEvent.Cancel;
            INotionValue    objCoin      = null;
            // !!! define the simulation key pad command of payment
            var firstCoin = paymentPanel.MapToCoins.First();

            mockReadKeypadInput.SimInputAs = firstCoin.Key;
            // !! define the action test listener
            paymentPanel.CoinAction += (c, p) =>
            {
                cmd     = c;
                objCoin = p;
                // off after getting the event
                paymentPanel.Off();
            };
            bool exception = false;

            paymentPanel.FailtException += ex => exception = true;

            // Test subject Action - on payment receiver and get simulated payment
            paymentPanel.On();
            Thread.Sleep(10000);

            // verify - payment
            Assert.False(exception);
            Assert.Equal(PaymentCmdEvent.Payment, cmd);
            Assert.Equal(firstCoin.Value, objCoin);

            // verify the  message flow
            List <MessageCode> TestCatchedCodes = new List <MessageCode>
            {
                MessageCode.MakeYourPayment,
                MessageCode.Ok
            };

            Assert.Equal((IEnumerable <MessageCode>)TestCatchedCodes, (IEnumerable <MessageCode>)mockVendingMessageRepository.CatchedCodes);
            Assert.Equal((IEnumerable <string>)mockVendingMessageRepository.ReturnList, (IEnumerable <string>)mockDisplayPanel.DisplayList);
        }
Exemplo n.º 7
0
        public void TestFaultExceptionEvent1()
        {
            // 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);

            // Test subject Action - Exception orderPanel
            orderPanel.InvokeFailtException(new Exception());

            // verify - Exception orderPanel
            Assert.True(orderPanel.OffCalled);
            Assert.True(paymentReceiver.OffCalled);
            Assert.Equal(VendingMachineState.Fault, vendingMachineController.VendingMachineState);
        }