|
| 1 | +package com.codedifferently.lesson16.chelseaogbonnia.bookshelf; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +public class BookshelfTest { |
| 11 | + private Bookshelf bookshelf; |
| 12 | + |
| 13 | + @BeforeEach |
| 14 | + public void setUp() { |
| 15 | + bookshelf = new Bookshelf("Chelsea", 10, true); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testAddBook() throws Exception { |
| 20 | + // Arrange |
| 21 | + Book book = new Book("Frankenstein", "Mary Shelly", Genre.HORROR, BookType.NOVEL); |
| 22 | + |
| 23 | + // Act |
| 24 | + bookshelf.addBook(book); |
| 25 | + |
| 26 | + // Assert |
| 27 | + assertTrue(bookshelf.hasBook("Frankenstein")); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testFindBookExists() throws Exception { |
| 32 | + // Arrange |
| 33 | + Book book = |
| 34 | + new Book("Brave New World", "Aldous Huxley", Genre.SCIENCE_FICTION, BookType.PAPERBACK); |
| 35 | + bookshelf.addBook(book); |
| 36 | + |
| 37 | + // Act |
| 38 | + Book foundBook = bookshelf.findBook("Brave New World"); |
| 39 | + |
| 40 | + // Assert |
| 41 | + assertEquals(book, foundBook); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testFindBookNotExists() { |
| 46 | + // Arrange |
| 47 | + String nonExistentBookTitle = "Non-Existent Book"; |
| 48 | + |
| 49 | + // Act & Assert |
| 50 | + Exception exception = |
| 51 | + assertThrows( |
| 52 | + BookNotFoundException.class, |
| 53 | + () -> { |
| 54 | + bookshelf.findBook(nonExistentBookTitle); |
| 55 | + }); |
| 56 | + assertEquals("Book titled \"" + nonExistentBookTitle + "\" not found.", exception.getMessage()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testCountBooksByGenre() throws Exception { |
| 61 | + // Arrange |
| 62 | + bookshelf.addBook(new Book("The Hobbit", "J.R.R. Tolkien", Genre.FANTASY, BookType.HARDCOVER)); |
| 63 | + bookshelf.addBook(new Book("Dune", "Frank Herbert", Genre.SCIENCE_FICTION, BookType.HARDCOVER)); |
| 64 | + |
| 65 | + // Act & Assert |
| 66 | + assertEquals(1, bookshelf.countBooksByGenre(Genre.SCIENCE_FICTION)); |
| 67 | + assertEquals(1, bookshelf.countBooksByGenre(Genre.FANTASY)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testAddBookToFullShelf() throws Exception { |
| 72 | + // Arrange |
| 73 | + for (int i = 0; i < 10; i++) { |
| 74 | + bookshelf.addBook(new Book("Book " + i, "Author " + i, Genre.FICTION, BookType.PAPERBACK)); |
| 75 | + } |
| 76 | + Book extraBook = new Book("Extra Book", "Author Extra", Genre.FICTION, BookType.HARDCOVER); |
| 77 | + |
| 78 | + // Act & Assert |
| 79 | + Exception exception = |
| 80 | + assertThrows( |
| 81 | + Exception.class, |
| 82 | + () -> { |
| 83 | + bookshelf.addBook(extraBook); |
| 84 | + }); |
| 85 | + assertEquals("Bookshelf is full. Cannot add more books.", exception.getMessage()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testCountBooksByGenreNoBooks() { |
| 90 | + // Arrange |
| 91 | + Genre genreToCheck = Genre.ADVENTURE; |
| 92 | + |
| 93 | + // Act & Assert |
| 94 | + assertEquals(0, bookshelf.countBooksByGenre(genreToCheck)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testListBooks() throws Exception { |
| 99 | + // Arrange |
| 100 | + bookshelf.addBook( |
| 101 | + new Book("The Adventures of Tintin", "Hergé", Genre.ADVENTURE, BookType.COMICS)); |
| 102 | + bookshelf.addBook(new Book("The Big Sleep", "Raymond Chandler", Genre.CRIME, BookType.NOVEL)); |
| 103 | + bookshelf.addBook( |
| 104 | + new Book("Don Quixote", "Miguel de Cervantes", Genre.ADVENTURE, BookType.NOVEL)); |
| 105 | + bookshelf.addBook(new Book("Sonnet 18", "William Shakespeare", Genre.ROMANCE, BookType.POEM)); |
| 106 | + |
| 107 | + // Act |
| 108 | + int bookCount = bookshelf.listBooks().size(); |
| 109 | + |
| 110 | + // Assert |
| 111 | + assertEquals(4, bookCount); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void checksMagazineBooks() throws Exception { |
| 116 | + // Arrange |
| 117 | + Book magazine = |
| 118 | + new Book("National Geographic", "Various", Genre.NON_FICTION, BookType.MAGAZINE); |
| 119 | + bookshelf.addBook(magazine); |
| 120 | + |
| 121 | + // Act |
| 122 | + boolean hasMagazine = bookshelf.hasBook("National Geographic"); |
| 123 | + int magazineCount = bookshelf.countBooksByGenre(Genre.NON_FICTION); |
| 124 | + |
| 125 | + // Assert |
| 126 | + assertTrue(hasMagazine, "Bookshelf should contain the magazine 'National Geographic'."); |
| 127 | + assertEquals( |
| 128 | + 1, magazineCount, "There should be 1 magazine in the bookshelf under NON_FICTION."); |
| 129 | + } |
| 130 | +} |
0 commit comments