public async Task <IActionResult> PostLoan([FromBody] LoanAPI loanApi) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Item item = (await _itemRepo.GetAsync(i => i.Id == loanApi.ItemId)).FirstOrDefault(); Member member = (await _memberRepo.GetAsync(m => m.Ssn == loanApi.MemberSsn)).FirstOrDefault(); if (item != null && member != null) { if (member.Loans.Where(l => l.IsReturned == false).Count() + 1 > member.LoanRule.BookLimit) { return(BadRequest()); } if (item.RentStatus != RentStatus.AVAILABLE || item.ItemStatus != ItemStatus.RENTABLE || (item.ItemCondition != ItemCondition.DAMAGED && item.ItemCondition != ItemCondition.OK)) { return(BadRequest()); } item.RentStatus = RentStatus.UNAVAILABLE; await _itemRepo.UpdateAsync(item); Loan loan = new Loan(loanApi.ItemId, loanApi.MemberSsn); await _repository.AddAsync(loan); return(CreatedAtAction("GetLoan", new { id = loan.LoanID }, loan)); } return(BadRequest()); }
public void ReturnLoanLoanNoSuchCustomer() { var loanManagerMock = new Mock <ILoanManager>(); var customerManagerMock = new Mock <ICustomerManager>(); var bookManagerMock = new Mock <IBookManager>(); loanManagerMock.Setup(m => m.GetLoanByCustomerAndBook(It.IsAny <int>(), (It.IsAny <int>()))) .Returns(new Loan { CustomerID = 2, BookID = 2 }); bookManagerMock.Setup(m => m.GetBookByBookNumber(It.IsAny <int>())) .Returns(new Book { BookNumber = 2 }); var loanAPI = new LoanAPI(bookManagerMock.Object, loanManagerMock.Object, customerManagerMock.Object); var result = loanAPI.ReturnLoan(2, 2); Assert.AreEqual(ErrorCodesReturnBook.NoSuchCustomer, result); loanManagerMock.Verify(m => m.ReturnLoan(2, 2), Times.Never()); }
[TestMethod] //inte löst public void AddToLoanBookBelongsToActiveLoan() { var loanManagerMock = new Mock <ILoanManager>(); var bookManagerMock = new Mock <IBookManager>(); var customerManagerMock = new Mock <ICustomerManager>(); customerManagerMock.Setup(m => m.GetCustomerByCustomerID(It.IsAny <int>())) .Returns( new Customer { CustomerID = 4, } ); bookManagerMock.Setup(m => m.GetBookByBookNumber(It.IsAny <int>())) .Returns( new Book { BookNumber = 1, BookActiveLoan = true } );; var loanAPI = new LoanAPI(bookManagerMock.Object, null, customerManagerMock.Object); var successfull = loanAPI.AddLoan(1, 4); Assert.AreEqual(ErrorCodesLoan.BookBelongsToActiveLoan, successfull); loanManagerMock.Verify(m => m.AddLoan(3, 2, It.IsAny <DateTime>(), It.IsAny <DateTime>()), Times.Never()); }
public void MoveBookOk() { var bookManagerMock = new Mock <IBookManager>(); var shelfManagerMock = new Mock <IShelfManager>(); shelfManagerMock.Setup(m => m.GetShelfByShelfNumber(It.IsAny <int>())) .Returns(new Shelf { ShelfID = 2 }); bookManagerMock.Setup(m => m.GetBookByBookName(It.IsAny <string>())) .Returns(new Book { BookName = "Boken", Shelf = new Shelf() }); var loanAPI = new LoanAPI(bookManagerMock.Object, null, null, null, shelfManagerMock.Object, null); var result = loanAPI.MoveBook("Bok", 1); Assert.AreEqual(MoveBookError.Ok, result); bookManagerMock.Verify(m => m.MoveBook(It.IsAny <int>(), It.IsAny <int>()), Times.Once()); }
public async void Add_Loan_DAMAGED() { var memberSsn = 112233446; using (var context = GetContextWithData()) using (var controller = new LoansController(context)) { var itemId = AddItem(context, ItemCondition.DAMAGED); LoanAPI loanAPI = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn }; var result = await controller.PostLoan(loanAPI); var loans = await controller.GetLoans(); var loan = loans.Last(); output.WriteLine(loan.Item.RentStatus.ToString()); output.WriteLine(loan.Member.Ssn.ToString()); Assert.IsType <CreatedAtActionResult>(result); Assert.True(loan is Loan); Assert.Equal(RentStatus.UNAVAILABLE, loan.Item.RentStatus); } }
public async void Add_Unavailable_Loan() { var memberSsn = 112233446; var memberSsn2 = 556677889; using (var context = GetContextWithData()) using (var controller = new LoansController(context)) { var itemId = context.Items.FirstOrDefault().Id; var loanAPI = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn }; var result = await controller.PostLoan(loanAPI); var loanAPI2 = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn2 }; var result2 = await controller.PostLoan(loanAPI2); var loans = await controller.GetLoans(); var loanz = loans.Where(l => l.Item.Id == itemId && l.IsReturned == false); Assert.True(loanz.Count() == 1); } }
public async void Add_Loan_DAMAGED_Wrong_LOANLIMIT() { var memberSsn = 112233446; // already has 4 loans using (var context = GetContextWithData()) using (var controller = new LoansController(context)) { var itemId1 = AddItem(context); LoanAPI loanAPI1 = new LoanAPI() { ItemId = itemId1, MemberSsn = memberSsn }; await controller.PostLoan(loanAPI1); // 5 loans LIMIT var itemId = AddItem(context, ItemCondition.DAMAGED, RentStatus.AVAILABLE, ItemStatus.RENTABLE); LoanAPI loanAPI = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn }; var result = await controller.PostLoan(loanAPI); Assert.IsType <BadRequestResult>(result); } }
public void AddToLoanNoSuchBook() { var bookManagerMock = new Mock <IBookManager>(); var loanAPI = new LoanAPI(bookManagerMock.Object, null, null); var result = loanAPI.AddLoan(0, 0); Assert.AreEqual(ErrorCodesLoan.NoSuchBook, result); }
public void TestReturnNonExistingLoan() { var customerManagerMock = SetupMock(new Customer { CustomerName = "Erika Axelsson" }); var bookManagerMock = SetupMock(new Book { BookTitle = "ABC" }); var loanManagerMock = SetupMock((Loan)null); var loanAPI = new LoanAPI(loanManagerMock.Object, bookManagerMock.Object, customerManagerMock.Object); var successfull = loanAPI.ReturnLoanedBook("ABC", "Erika Axelsson", 4); Assert.AreEqual(LoanedBookStatusCodes.NoSuchLoan, successfull); loanManagerMock.Verify(m => m.ReturnLoanedBook(It.IsAny<int>()), Times.Never); }
private LoanedBookStatusCodes AddLoanNumberOne(Mock<ILoanManager> loanManagerMock) { var customerManagerMock = SetupMock(new Customer { CustomerName = "Erika Axelsson" }); var bookManagerMock = SetupMock(new Book { BookTitle = "Peter Pan", IsLoaned = false }); var loanAPI = new LoanAPI(loanManagerMock.Object, bookManagerMock.Object, customerManagerMock.Object); var successfull = loanAPI.AddLoan("Erika Axelsson", "Peter Pan"); return successfull; }
public void TestAddExistingLoanExtend() { Mock<ILoanManager> loanManagerMock = SetupMock(new Loan()); var customerManagerMock = SetupMock(new Customer { CustomerName = "Erika Axelsson" }); var bookManagerMock = SetupMock(new Book { BookTitle = "Peter Pan", IsLoaned = true }); var loanApi = new LoanAPI(loanManagerMock.Object, bookManagerMock.Object, customerManagerMock.Object); var result = loanApi.AddLoan("Erika Axelsson", "Peter Pan"); Assert.AreEqual(LoanedBookStatusCodes.ExtendLoan, result); }
public void TestCustomerHasToManyLoans() { Mock<ILoanManager> loanManagerMock = SetupMock(new Loan()); var customerManagerMock = SetupMock(new Customer { CustomerName = "Erika Axelsson" }); var bookManagerMock = SetupMock(new Book { BookID = 7, BookTitle = "Oliver Twist" }); loanManagerMock.Setup(m => m.GetLoanFromCustomer(It.IsAny<string>())) .Returns(new List<Loan> { new Loan { CustomerID = 1, BookID = 1 }, new Loan { CustomerID = 1, BookID = 2 }, new Loan { CustomerID = 1, BookID = 3 }, new Loan { CustomerID = 1, BookID = 4 }, new Loan { CustomerID = 1, BookID = 5 }, }); var loanApi = new LoanAPI(loanManagerMock.Object, bookManagerMock.Object, customerManagerMock.Object); var result = loanApi.AddLoan("Erika Axelsson", "Oliver Twist"); Assert.AreEqual(LoanedBookStatusCodes.CustomerHasToManyLoans, result); loanManagerMock.Verify( m => m.AddLoan(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.Is<string>(i => i == "Erika Axelsson"), It.Is<string>(i => i == "Oliver Twist")), Times.Never()); }
private static bool GetBookByBookName(Mock <IBookManager> bookManagerMock) { var bookLoanManagerMock = new Mock <IBookLoanManager>(); var bookShelfAisleManagerMock = new Mock <IBookshelfAisleManager>(); var customerManagerMock = new Mock <ICustomerManager>(); var shelfManagerMock = new Mock <IShelfManager>(); var moneyDebtManagerMock = new Mock <IMoneyDebtManager>(); var LoanAPI = new LoanAPI(bookManagerMock.Object, bookLoanManagerMock.Object, bookShelfAisleManagerMock.Object, customerManagerMock.Object, shelfManagerMock.Object, moneyDebtManagerMock.Object); var successfull = LoanAPI.AddBook("Boken", 0); return(successfull); }
public void TestReturnLoanedBook() { var customerManagerMock = SetupMock(new Customer { CustomerName = "Erika Axelsson" }); var bookManagerMock = SetupMock(new Book { BookTitle = "Peter Pan" }); var loanManagerMock = SetupMock(new Loan { LoanID = 1, Book = new Book() }); var loanAPI = new LoanAPI(loanManagerMock.Object, bookManagerMock.Object, customerManagerMock.Object); var successfull = loanAPI.ReturnLoanedBook("Peter Pan", "Erika Axelsson", 2); Assert.AreEqual(LoanedBookStatusCodes.Ok, successfull); loanManagerMock.Verify(m => m.ReturnLoanedBook(It.IsAny<int>()), Times.Once); }
public void AddToLoanNoSuchCustomer() { var bookManagerMock = new Mock <IBookManager>(); bookManagerMock.Setup(m => m.GetBookByBookNumber(It.IsAny <int>())) .Returns( new Book() ); var customerManager = new Mock <ICustomerManager>(); var loanAPI = new LoanAPI(bookManagerMock.Object, null, customerManager.Object); var result = loanAPI.AddLoan(0, 0); Assert.AreEqual(ErrorCodesLoan.NoSuchCustomer, result); }
public async void Add_Loan_DAMAGED_Wrong_MemberSSN() { var memberSsn = 1122334466; using (var context = GetContextWithData()) using (var controller = new LoansController(context)) { var itemId = AddItem(context, ItemCondition.DAMAGED, RentStatus.AVAILABLE, ItemStatus.NONRENTABLE); LoanAPI loanAPI = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn }; var result = await controller.PostLoan(loanAPI); Assert.IsType <BadRequestResult>(result); } }
public async void Add_Loan_OK_Wrong_ITEMID() { var memberSsn = 112233446; using (var context = GetContextWithData()) using (var controller = new LoansController(context)) { var itemId = new Guid(); LoanAPI loanAPI = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn }; var result = await controller.PostLoan(loanAPI); Assert.IsType <BadRequestResult>(result); } }
public void TestAddLoanNoSuchCustomer() { Mock<ILoanManager> loanManagerMock = SetupMock(new Loan()); var customerManagerMock = SetupMock((Customer)null); var bookManagerMock = SetupMock(new Book { BookID = 4, BookTitle = "Moby Dick" }); var loanApi = new LoanAPI(null, bookManagerMock.Object, customerManagerMock.Object); var result = loanApi.AddLoan("Bobo Krut", "Moby Dick"); Assert.AreEqual(LoanedBookStatusCodes.NoSuchCustomer, result); loanManagerMock.Verify( m => m.AddLoan(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.Is<string>(i => i == "Gunvor Gnut"), It.Is<string>(i => i == "Moby Dick")), Times.Never()); }
public void TestAddLoanNoSuchBook() { Mock<ILoanManager> loanManagerMock = SetupMock(new Loan()); var customerManagerMock = SetupMock(new Customer { CustomerID = 4, CustomerName = "Lovisa Lund" }); var bookManagerMock = SetupMock((Book)null); var loanApi = new LoanAPI(null, bookManagerMock.Object, customerManagerMock.Object); var result = loanApi.AddLoan("Lovisa Lund", "ABC Boken"); Assert.AreEqual(LoanedBookStatusCodes.NoSuchBook, result); loanManagerMock.Verify( m => m.AddLoan(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.Is<string>(i => i == "Lovisa Lund"), It.Is<string>(i => i == "ABC")), Times.Never()); }
public void TestAddExistingLoan() { var loanManagerMock = new Mock<ILoanManager>(); var customerManagerMock = SetupMock(new Customer { CustomerName = "Lovisa Lund"}); var bookManagerMock = SetupMock(new Book { BookTitle = "Peter Pan", IsLoaned = true }); var loanApi = new LoanAPI(loanManagerMock.Object, bookManagerMock.Object, customerManagerMock.Object); var result = loanApi.AddLoan("Lovisa Lund", "Peter Pan"); Assert.AreEqual(LoanedBookStatusCodes.BookIsLoaned, result); loanManagerMock.Verify( m => m.AddLoan(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.Is<string>(i => i == "Lovisa Lund"), It.Is<string>(i => i == "Peter Pan")), Times.Never()); }
public void ReturnLoanNoOpenLoan() { var bookManagerMock = new Mock <IBookManager>(); var loanManagerMock = new Mock <ILoanManager>(); var customerManagerMock = new Mock <ICustomerManager>(); bookManagerMock.Setup(m => m.GetBookByBookNumber(It.IsAny <int>())) .Returns( new Book { BookNumber = 2 } ); customerManagerMock.Setup(m => m.GetCustomerByCustomerID(It.IsAny <int>())) .Returns( new Customer { CustomerID = 4 } ); loanManagerMock.Setup(m => m.GetLoanByCustomerAndBook(It.Is <int>(i => i == 2), (It.Is <int>(i => i == 4)))) .Returns( new Loan { CustomerID = 4, BookID = 3 }); loanManagerMock.Setup(m => m.ReturnLoan(It.Is <int>(i => i == 3), It.Is <int>(i => i == 2))); var loanAPI = new LoanAPI(bookManagerMock.Object, loanManagerMock.Object, customerManagerMock.Object); var successfull = loanAPI.ReturnLoan(2, 4); Assert.AreEqual(ErrorCodesReturnBook.NoOpenLoan, successfull); loanManagerMock.Verify(m => m.ReturnLoan(It.IsAny <int>(), It.IsAny <int>()), Times.Never); }
public void TestExistingBook() { var bookManagerMock = new Mock <IBookManager>(); var loanAPI = SetUpMock(new Book()); var bookLoanManagerMock = new Mock <IBookLoanManager>(); var bookShelfAisleManagerMock = new Mock <IBookshelfAisleManager>(); var customerManagerMock = new Mock <ICustomerManager>(); var shelfManagerMock = new Mock <IShelfManager>(); var moneyDebtManagerMock = new Mock <IMoneyDebtManager>(); var LoanAPI = new LoanAPI(bookManagerMock.Object, bookLoanManagerMock.Object, bookShelfAisleManagerMock.Object, customerManagerMock.Object, shelfManagerMock.Object, moneyDebtManagerMock.Object); var successfull = LoanAPI.RemoveBook("Boken"); Assert.AreEqual(RemoveBookError.NoSuchBook, successfull); bookManagerMock.Verify( m => m.RemoveBook(It.IsAny <string>()), Times.Never()); }
public async void Add_Loan_Over_Limit(int books) { var memberSsn = 112233446; // already has 4 loans using (var context = GetContextWithData()) using (var controller = new LoansController(context)) { for (int i = 0; i < books; i++) { var itemId = AddItem(context); LoanAPI loanAPI = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn }; await controller.PostLoan(loanAPI); } var loans = context.Members.FirstOrDefault(m => m.Ssn == memberSsn).Loans; Assert.Equal(5, loans.Count); } }
public async Task <IActionResult> ReturnLoan([FromBody] LoanAPI loanApi, [FromRoute] int condition) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Item item = (await _itemRepo.GetAsync(i => i.Id == loanApi.ItemId)).FirstOrDefault(); Member member = (await _memberRepo.GetAsync(m => m.Ssn == loanApi.MemberSsn)).FirstOrDefault(); if (item != null && member != null) { if (item.RentStatus != RentStatus.UNAVAILABLE && item.ItemStatus == ItemStatus.RENTABLE) { return(BadRequest()); } var cond = (ItemCondition)condition; if (cond == ItemCondition.OK || cond == ItemCondition.DAMAGED) { item.RentStatus = RentStatus.AVAILABLE; item.ItemCondition = (ItemCondition)condition; await _itemRepo.UpdateAsync(item); } else { item.ItemCondition = (ItemCondition)condition; item.ItemStatus = ItemStatus.NONRENTABLE; await _itemRepo.UpdateAsync(item); } var loan = (await _repository.GetAsync(l => l.MemberSsn == loanApi.MemberSsn && l.ItemId == loanApi.ItemId && l.IsReturned == false)).FirstOrDefault(); loan.IsReturned = true; await _repository.UpdateAsync(loan); return(CreatedAtAction("ReturnedLoan", new { id = loan.LoanID }, loan)); } return(BadRequest()); }
[TestMethod] //inte löst public void ReturnLoanLoanOk() { var loanManagerMock = new Mock <ILoanManager>(); var bookManagerMock = new Mock <IBookManager>(); var customerManagerMock = new Mock <ICustomerManager>(); loanManagerMock.Setup(m => m.GetLoanByCustomerAndBook(It.IsAny <int>(), It.IsAny <int>())) .Returns(new Loan { LoanID = 2, Items = new Book() }); customerManagerMock.Setup(m => m.GetCustomerByCustomerID(It.IsAny <int>())) .Returns(new Customer { CustomerID = 2 }); bookManagerMock.Setup(m => m.GetBookByBookNumber(It.IsAny <int>())) .Returns( new Book { BookNumber = 1, BookActiveLoan = true } ); var loanAPI = new LoanAPI(bookManagerMock.Object, loanManagerMock.Object, customerManagerMock.Object); var result = loanAPI.ReturnLoan(2, 2); Assert.AreEqual(ErrorCodesReturnBook.Ok, result); loanManagerMock.Verify(m => m.ReturnLoan(It.IsAny <int>(), (It.IsAny <int>())), Times.Once()); }
public async void Update_ItemStatus(ItemStatus status, ItemCondition condition) { var memberSsn = 112233446; using (var context = GetContextWithData()) using (var controller = new LoansController(context)) { var itemId = context.Items.FirstOrDefault().Id; LoanAPI loanAPI = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn }; await controller.PostLoan(loanAPI); await controller.ReturnLoan(loanAPI, (int)condition); var loans = await controller.GetLoans(); var loan = loans.Where(l => l.Item.Id == itemId && l.Member.Ssn == memberSsn).FirstOrDefault(); Assert.Equal(status, loan.Item.ItemStatus); } }
public async void Return_Loan(ItemCondition condition) { var memberSsn = 112233445; using (var context = GetContextWithData()) using (var controller = new LoansController(context)) { var itemId = context.Items.FirstOrDefault().Id; LoanAPI loanAPI = new LoanAPI() { ItemId = itemId, MemberSsn = memberSsn }; await controller.PostLoan(loanAPI); await controller.ReturnLoan(loanAPI, (int)condition); var loans = await controller.GetLoans(); var loanz = loans.Where(l => l.Item.Id == itemId && l.Member.Ssn == memberSsn && l.IsReturned == false); output.WriteLine(loanz.Count().ToString()); Assert.True(loanz.Count() == 0); } }
public void addLoanOk() { var bookManagerMock = new Mock <IBookManager>(); var customerManagerMock = new Mock <ICustomerManager>(); var loanManagerMock = new Mock <ILoanManager>(); bookManagerMock.Setup(m => m.GetBookByBookNumber(It.IsAny <int>())) .Returns( new Book { BookNumber = 2 } ); customerManagerMock.Setup(m => m.GetCustomerByCustomerID(It.IsAny <int>())) .Returns( new Customer { CustomerID = 4 } ); loanManagerMock.Setup(m => m.GetLoanByCustomerAndBook(It.Is <int>(i => i == 2), (It.Is <int>(i => i == 4)))) .Returns( new Loan { CustomerID = 4, BookID = 1 } ); loanManagerMock.Setup(m => m.GetLoanByCustomers(It.IsAny <int>())) .Returns(new List <Loan> { new Loan { CustomerID = 1, BookID = 5 }, new Loan { CustomerID = 1, BookID = 3 }, new Loan { CustomerID = 1, BookID = 2 }, new Loan { CustomerID = 1, BookID = 6 }, } ); loanManagerMock.Setup(m => m.AddLoan(It.Is <int>(i => i == 3), It.Is <int>(i => i == 2), It.IsAny <DateTime>(), It.IsAny <DateTime>())); var loanAPI = new LoanAPI(bookManagerMock.Object, loanManagerMock.Object, customerManagerMock.Object); var successfull = loanAPI.AddLoan(4, 2); Assert.AreEqual(ErrorCodesLoan.Ok, successfull); loanManagerMock.Verify(m => m.AddLoan(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <DateTime>(), It.IsAny <DateTime>()), Times.Once); }