public void ScanBooksBookNotFound()
        {
            var member = CreateMockIMember();

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            _bookDao.GetBookByID(0).Returns((IBook)null);

            ctrl.bookScanned(0);

            _bookDao.Received().GetBookByID(0);

            borrowctrl.Received().DisplayErrorMessage("Book scanned was not found");

            Assert.Equal(EBorrowState.SCANNING_BOOKS, ctrl._state);
        }
        public void ScanBooksBookScanCountLessThanLoanLimit()
        {
            var member = CreateMockIMember();

            var book = Substitute.For<IBook>();
            book.State.Returns(BookState.AVAILABLE);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var loan = Substitute.For<Loan>(book, member, borrowDate, dueDate);

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            _bookDao.GetBookByID(0).Returns(book);
            _loanDao.CreateLoan(member, book, borrowDate, dueDate).Returns(loan);

            ctrl.bookScanned(0);

            _bookDao.Received().GetBookByID(0);
            _loanDao.Received().CreateLoan(member, book, borrowDate, dueDate);

            borrowctrl.Received().DisplayScannedBookDetails(book.ToString());

            borrowctrl.Received().DisplayPendingLoan(loan.ToString());

            Assert.Equal(1, ctrl.scanCount);
            Assert.NotNull(ctrl._loanList);
            Assert.NotEmpty(ctrl._loanList);
            Assert.Equal(1, ctrl._loanList.Count);

            Assert.Equal(loan, ctrl._loanList[0]);

            Assert.NotNull(ctrl._bookList);
            Assert.NotEmpty(ctrl._bookList);
            Assert.Equal(1, ctrl._bookList.Count);

            Assert.Equal(book, ctrl._bookList[0]);

            Assert.Equal(EBorrowState.SCANNING_BOOKS, ctrl._state);
        }
        public void ScanBookControlNotScanningBooks()
        {
            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            ctrl.initialise();

            var ex = Assert.Throws<InvalidOperationException>(() => { ctrl.bookScanned(0); });

            Assert.Equal("Control state must be set to 'Scanning Books'", ex.Message);
        }
        public void ScanBooksBookAlreadyScanned()
        {
            var member = CreateMockIMember();

            var book = Substitute.For<IBook>();
            book.State.Returns(BookState.AVAILABLE);

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            _bookDao.GetBookByID(0).Returns(book);

            ctrl._bookList.Add(book); // Add the book to the booklist.

            ctrl.bookScanned(0);

            _bookDao.Received().GetBookByID(0);

            borrowctrl.Received().DisplayErrorMessage("Book has already been scanned");

            Assert.Equal(EBorrowState.SCANNING_BOOKS, ctrl._state);
        }
        public void RunScenario()
        {
            // Some test data initialisation
            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var existingBook = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var existingLoan = _loanDao.CreateLoan(member, existingBook, borrowDate, dueDate);

            _loanDao.CommitLoan(existingLoan);

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming 2", "call number");

            // Set up
            var controller = new BorrowController(_display, _reader, _scanner, _printer,
                                                        _bookDao, _loanDao, _memberDao);

            controller.initialise();
            controller.cardSwiped(member.ID);
            controller.bookScanned(book.ID);
            controller.scansCompleted();

            // Test Pre-conditions
            Assert.True(_display.Display.IsEnabled);

            var borrowCtrl = ((BorrowControl)_display.Display);
            var confirmingLoansCtrl = borrowCtrl._controlDict.Single(c => c.Value is ConfirmLoanControl).Value as ConfirmLoanControl;
            var scanBookCtrl = borrowCtrl._controlDict.Single(c => c.Value is ScanBookControl).Value as ScanBookControl;

            Assert.NotNull(confirmingLoansCtrl);
            Assert.True(confirmingLoansCtrl.IsEnabled);
            Assert.True(confirmingLoansCtrl.cancelButton.IsEnabled);
            Assert.True(confirmingLoansCtrl.rejectButton.IsEnabled);

            Assert.True(!_reader.Enabled);
            Assert.True(!_scanner.Enabled);

            Assert.NotNull(controller._loanList);
            Assert.NotEmpty(controller._loanList);
            Assert.Equal(1, controller._loanList.Count);
            Assert.Equal(book, controller._loanList[0].Book);
            Assert.Equal(member, controller._loanList[0].Borrower);

            var loan = controller._loanList[0];

            Assert.NotNull(controller._bookList);
            Assert.NotEmpty(controller._bookList);
            Assert.Equal(1, controller._bookList.Count);

            Assert.Equal(book, controller._bookList[0]);

            Assert.Equal(EBorrowState.CONFIRMING_LOANS, controller._state);

            // Run use case
            controller.loansRejected();

            // Test Post-conditions

            Assert.NotNull(scanBookCtrl);
            Assert.True(scanBookCtrl.IsEnabled);
            Assert.True(scanBookCtrl.cancelButton.IsEnabled);
            Assert.True(scanBookCtrl.completeButton.IsEnabled);

            Assert.True(!_reader.Enabled);
            Assert.True(_scanner.Enabled);
            Assert.Equal(controller, _scanner.Listener);

            Assert.Equal(member.Loans.Count, controller.scanCount);
            Assert.Equal(member, controller._borrower);
            Assert.Equal(EBorrowState.SCANNING_BOOKS, controller._state);

            Assert.Equal(member.ID, scanBookCtrl.idLabel.Content);
            Assert.Equal($"{member.FirstName} {member.LastName}", scanBookCtrl.nameLabel.Content.ToString());
            Assert.Equal(member.Loans[0].ToString(), scanBookCtrl.existingLoanBox.Text);  // Test one existing loan is present

            Assert.Equal("", scanBookCtrl.pendingLoanBox.Text);
            Assert.Equal("", scanBookCtrl.currentbookBox.Text);

            Assert.NotNull(controller._bookList);
            Assert.Empty(controller._bookList);
            Assert.NotNull(controller._loanList);
            Assert.Empty(controller._loanList);
            Assert.Equal(1, controller.scanCount);
        }
        public void CanScanBook()
        {
            var member = CreateMockIMember();

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            InitialiseToScanBookPreConditions(ctrl, member);

            // Make the bookDao return a successful read
            _bookDao.GetBookByID(0).Returns(Substitute.For<IBook>());

            ctrl.bookScanned(0); // if we get this far we've worked.
        }
        public void ScanBooksBookScanCountLessThanLoanLimitClearsPreviousError()
        {
            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            ctrl.bookScanned(book.ID);

            // Expect the error message to be cleared
            borrowctrl.Received().DisplayErrorMessage("");
        }
        public void RunBookAlreadyScanned()
        {
            // Some test data initialisation
            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var existingBook = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var existingLoan = _loanDao.CreateLoan(member, existingBook, borrowDate, dueDate);

            _loanDao.CommitLoan(existingLoan);

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming 2", "call number");

            // Set up
            var controller = new BorrowController(_display, _reader, _scanner, _printer,
                                                        _bookDao, _loanDao, _memberDao);

            controller.initialise();
            controller.cardSwiped(member.ID);

            controller._bookList.Add(book);

            // Test Pre-conditions
            Assert.True(_display.Display.IsEnabled);

            var borrowCtrl = ((BorrowControl)_display.Display);
            var scanBookCtrl = borrowCtrl._controlDict.Single(c => c.Value is ScanBookControl).Value as ScanBookControl;

            Assert.NotNull(scanBookCtrl);
            Assert.True(scanBookCtrl.IsEnabled);
            Assert.True(scanBookCtrl.cancelButton.IsEnabled);
            Assert.True(scanBookCtrl.completeButton.IsEnabled);

            Assert.True(!_reader.Enabled);
            Assert.True(_scanner.Enabled);
            Assert.Equal(controller, _scanner.Listener);

            Assert.Equal(member.Loans.Count, controller.scanCount);
            Assert.Equal(member, controller._borrower);
            Assert.Equal(EBorrowState.SCANNING_BOOKS, controller._state);

            // Run use case
            controller.bookScanned(book.ID);

            // Test Post-conditions
            Assert.True(scanBookCtrl.IsEnabled);
            Assert.True(scanBookCtrl.cancelButton.IsEnabled);
            Assert.True(scanBookCtrl.completeButton.IsEnabled);

            Assert.True(!_reader.Enabled);
            Assert.True(_scanner.Enabled);

            Assert.Equal("Book has already been scanned", scanBookCtrl.errorMessage.Content);

            Assert.Equal(EBorrowState.SCANNING_BOOKS, controller._state);
        }
        public void ScanBooksBookScanCountEqualsLoanLimit()
        {
            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");
            var book2 = _bookDao.AddBook("Jim Tulip", "Adventures in Programming 2", "call number");

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            ctrl.scanCount = BookConstants.LOAN_LIMIT - 2;

            ctrl.bookScanned(book.ID);
            ctrl.bookScanned(book2.ID);

            borrowctrl.Received().DisplayScannedBookDetails(book.ToString());
            borrowctrl.Received().DisplayScannedBookDetails(book2.ToString());

            Assert.Equal(BookConstants.LOAN_LIMIT, ctrl.scanCount);
            Assert.NotNull(ctrl._loanList);
            Assert.NotEmpty(ctrl._loanList);
            Assert.Equal(2, ctrl._loanList.Count);

            borrowctrl.Received().DisplayPendingLoan(ctrl._loanList[0].ToString());
            borrowctrl.Received().DisplayPendingLoan(ctrl._loanList[1].ToString());

            borrowctrl.Received().DisplayConfirmingLoan(ctrl._loanList[0].ToString());
            borrowctrl.Received().DisplayConfirmingLoan(ctrl._loanList[1].ToString());

            Assert.NotNull(ctrl._bookList);
            Assert.NotEmpty(ctrl._bookList);
            Assert.Equal(2, ctrl._bookList.Count);

            Assert.Equal(book, ctrl._bookList[0]);

            Assert.True(!_scanner.Enabled);

            Assert.Equal(EBorrowState.CONFIRMING_LOANS, ctrl._state);
        }
        public void ScanBooksBookScanCountLessThanLoanLimit()
        {
            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            ctrl.bookScanned(book.ID);

            borrowctrl.Received().DisplayScannedBookDetails(book.ToString());

            Assert.Equal(1, ctrl.scanCount);
            Assert.NotNull(ctrl._loanList);
            Assert.NotEmpty(ctrl._loanList);
            Assert.Equal(1, ctrl._loanList.Count);

            borrowctrl.Received().DisplayPendingLoan(ctrl._loanList[0].ToString());

            Assert.NotNull(ctrl._bookList);
            Assert.NotEmpty(ctrl._bookList);
            Assert.Equal(1, ctrl._bookList.Count);

            Assert.Equal(book, ctrl._bookList[0]);

            Assert.Equal(EBorrowState.SCANNING_BOOKS, ctrl._state);
        }
        public void ScanBooksBookNotFound()
        {
            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            ctrl.bookScanned(0);

            borrowctrl.Received().DisplayErrorMessage("Book scanned was not found");

            Assert.Equal(EBorrowState.SCANNING_BOOKS, ctrl._state);
        }
        public void ScanBooksBookNotAvailable()
        {
            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");
            book.Dispose();

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            ctrl.bookScanned(book.ID);

            borrowctrl.Received().DisplayErrorMessage("Book is not available to be borrowed");

            Assert.Equal(EBorrowState.SCANNING_BOOKS, ctrl._state);
        }
        public void CanScanBook()
        {
            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            InitialiseToScanBookPreConditions(ctrl, member);

            ctrl.bookScanned(book.ID); // if we get this far we've worked.
        }
        public void ScanBooksBookScanCountLessThanLoanLimitClearsPreviousError()
        {
            var member = CreateMockIMember();

            var book = Substitute.For<IBook>();
            book.State.Returns(BookState.AVAILABLE);

            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var loan = Substitute.For<Loan>(book, member, borrowDate, dueDate);

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            // Set the UI to the mock so we can test
            var borrowctrl = Substitute.For<ABorrowControl>();
            ctrl._ui = borrowctrl;

            InitialiseToScanBookPreConditions(ctrl, member);

            _bookDao.GetBookByID(0).Returns(book);
            _loanDao.CreateLoan(member, book, borrowDate, dueDate).Returns(loan);

            ctrl.bookScanned(0);

            // Expect the error message to be cleared
            borrowctrl.Received().DisplayErrorMessage("");
        }
        public void ScanBooksScannerEnabled()
        {
            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            InitialiseToScanBookPreConditions(ctrl, member);

            ctrl.bookScanned(book.ID);

            Assert.True(_scanner.Enabled);
        }
        public void ScanBooksScannerEnabled()
        {
            var member = CreateMockIMember();

            var ctrl = new BorrowController(_display, _reader, _scanner, _printer, _bookDao, _loanDao, _memberDao);

            InitialiseToScanBookPreConditions(ctrl, member);

            // Make the bookDao return a successful read
            _bookDao.GetBookByID(0).Returns(Substitute.For<IBook>());

            ctrl.bookScanned(0);

            Assert.True(_scanner.Enabled);
        }
        public void RunScenario()
        {
            // Some test data initialisation
            var borrowDate = DateTime.Today;
            var dueDate = DateTime.Today.AddDays(7);

            var member = _memberDao.AddMember("Jim", "Tulip", "Phone", "Email");

            var existingBook = _bookDao.AddBook("Jim Tulip", "Adventures in Programming", "call number");

            var existingLoan = _loanDao.CreateLoan(member, existingBook, borrowDate, dueDate);

            _loanDao.CommitLoan(existingLoan);

            var book = _bookDao.AddBook("Jim Tulip", "Adventures in Programming 2", "call number");

            // Set up
            var controller = new BorrowController(_display, _reader, _scanner, _printer,
                                                        _bookDao, _loanDao, _memberDao);

            controller.initialise();
            controller.cardSwiped(member.ID);
            controller.bookScanned(book.ID);
            controller.scansCompleted();

            // Test Pre-conditions
            Assert.True(_display.Display.IsEnabled);

            var borrowCtrl = ((BorrowControl)_display.Display);
            var confirmingLoansCtrl = borrowCtrl._controlDict.Single(c => c.Value is ConfirmLoanControl).Value as ConfirmLoanControl;

            Assert.NotNull(confirmingLoansCtrl);
            Assert.True(confirmingLoansCtrl.IsEnabled);
            Assert.True(confirmingLoansCtrl.cancelButton.IsEnabled);
            Assert.True(confirmingLoansCtrl.rejectButton.IsEnabled);

            Assert.True(!_reader.Enabled);
            Assert.True(!_scanner.Enabled);

            Assert.NotNull(controller._loanList);
            Assert.NotEmpty(controller._loanList);
            Assert.Equal(1, controller._loanList.Count);
            Assert.Equal(book, controller._loanList[0].Book);
            Assert.Equal(member, controller._loanList[0].Borrower);

            var loan = controller._loanList[0];

            Assert.NotNull(controller._bookList);
            Assert.NotEmpty(controller._bookList);
            Assert.Equal(1, controller._bookList.Count);

            Assert.Equal(book, controller._bookList[0]);

            Assert.Equal(EBorrowState.CONFIRMING_LOANS, controller._state);

            // Run use case
            controller.loansConfirmed();

            // Test Post-conditions
            Assert.True(borrowCtrl.IsEnabled);

            Assert.Equal(LoanState.CURRENT, loan.State);
            Assert.Equal(loan.ToString() + "\n\n", ((Printer)_printer).printBox.Text);

            Assert.True(!_reader.Enabled);
            Assert.True(!_scanner.Enabled);
            Assert.Equal(EBorrowState.COMPLETED, controller._state);
        }