From 7d891568206dd9e5cc10389ef6fc54e0a10937a3 Mon Sep 17 00:00:00 2001 From: marcioassuncao Date: Tue, 22 Aug 2023 08:12:01 -0400 Subject: [PATCH] finish exercise --- .../java/com/redhat/training/Library.java | 1 + .../com/redhat/training/BookStatsTest.java | 9 ++++- .../java/com/redhat/training/LibraryTest.java | 36 +++++++++++++++++++ .../LibraryWithMockedInventoryTest.java | 31 ++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/school-library/src/main/java/com/redhat/training/Library.java b/school-library/src/main/java/com/redhat/training/Library.java index 9c0f196e6..4c13e603e 100644 --- a/school-library/src/main/java/com/redhat/training/Library.java +++ b/school-library/src/main/java/com/redhat/training/Library.java @@ -21,6 +21,7 @@ public Library(Inventory inventory) { } public Book checkOut(String studentId, String isbn) throws BookNotAvailableException { + if (!inventory.isBookAvailable(isbn)) { throw new BookNotAvailableException(isbn); } diff --git a/school-library/src/test/java/com/redhat/training/BookStatsTest.java b/school-library/src/test/java/com/redhat/training/BookStatsTest.java index 615a3fe90..7717bf94a 100644 --- a/school-library/src/test/java/com/redhat/training/BookStatsTest.java +++ b/school-library/src/test/java/com/redhat/training/BookStatsTest.java @@ -22,6 +22,13 @@ public void countingWordsOfEmptyBookReturnsZero() { @Test public void countingWordsReturnsNumberOfWordsInBook() { - assertEquals(0, 1); // Replace this line with the actual test code... + // Given + Book book = new Book("someISBN", "this is the content"); + + // When + double wordCount = BookStats.countWords(book); + + // Then + assertEquals(4, wordCount); } } diff --git a/school-library/src/test/java/com/redhat/training/LibraryTest.java b/school-library/src/test/java/com/redhat/training/LibraryTest.java index 37bfe5fbb..f4628188a 100644 --- a/school-library/src/test/java/com/redhat/training/LibraryTest.java +++ b/school-library/src/test/java/com/redhat/training/LibraryTest.java @@ -24,4 +24,40 @@ public void setUp() { } // Add tests here... + @Test + public void checkingOutDecreasesNumberOfBookCopiesFromInventory() + throws BookNotAvailableException { + // Given + inventory.add(new Book("book1")); + inventory.add(new Book("book1")); + + // When + library.checkOut("someStudentId", "book1"); + + // Then + assertEquals(1, inventory.countCopies("book1")); + } + + @Test + public void checkingOutUnavailableBookThrowsException() + throws BookNotAvailableException { + // Given + inventory.add(new Book("book1")); + inventory.add(new Book("book1")); + + library.checkOut("student1", "book1"); + library.checkOut("student2", "book1"); + + // When + final BookNotAvailableException exception = assertThrows( + BookNotAvailableException.class, + () -> { + library.checkOut("student3", "book1"); + } + ); + + // Then + assertTrue(exception.getMessage().matches("Book book1 is not available")); + } + } diff --git a/school-library/src/test/java/com/redhat/training/LibraryWithMockedInventoryTest.java b/school-library/src/test/java/com/redhat/training/LibraryWithMockedInventoryTest.java index 7510e9122..e73d024dc 100644 --- a/school-library/src/test/java/com/redhat/training/LibraryWithMockedInventoryTest.java +++ b/school-library/src/test/java/com/redhat/training/LibraryWithMockedInventoryTest.java @@ -24,4 +24,35 @@ public void setUp() { } // Add tests here... + @Test + public void checkingOutWithdrawsFromInventoryWhenBookIsAvailable() + throws BookNotAvailableException { + // Given + when(inventory.isBookAvailable("book1")).thenReturn(true); + + // When + library.checkOut("student1", "book1"); + + // Then + verify(inventory).withdraw("book1"); + } + + @Test + public void checkingOutDoesNotWithdrawFromInventoryWhenBookIsUnavailable() + throws BookNotAvailableException { + // Given + when(inventory.isBookAvailable("book1")).thenReturn(false); + + // When + try { + library.checkOut("student1", "book1"); + } catch(BookNotAvailableException e) {} + + // Then + verify(inventory, times(0)).withdraw("book1"); + + } + + + }