From 06dd4eea2adba3344a09493b0cba864a6a976126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D1=83=D0=BD=D0=BE=D0=B2?= Date: Tue, 16 May 2023 11:30:27 +0400 Subject: [PATCH] third labWork --- build.gradle | 7 + .../demo/Books/Controller/BookController.java | 56 +++++++ .../Books/Controller/GenreController.java | 43 ++++++ .../com/example/demo/Books/Models/Author.java | 61 ++++++++ .../com/example/demo/Books/Models/Book.java | 73 +++++++++ .../com/example/demo/Books/Models/Genre.java | 36 +++++ .../demo/Books/Service/AuthorService.java | 76 ++++++++++ .../demo/Books/Service/BookService.java | 122 +++++++++++++++ .../demo/Books/Service/GenreService.java | 65 ++++++++ src/main/resources/application.properties | 12 +- .../java/com/example/demo/JpaAuthorsTest.java | 92 ++++++++++++ .../java/com/example/demo/JpaBooksTest.java | 139 ++++++++++++++++++ .../java/com/example/demo/JpaGenresTest.java | 76 ++++++++++ 13 files changed, 857 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/demo/Books/Controller/BookController.java create mode 100644 src/main/java/com/example/demo/Books/Controller/GenreController.java create mode 100644 src/main/java/com/example/demo/Books/Models/Author.java create mode 100644 src/main/java/com/example/demo/Books/Models/Book.java create mode 100644 src/main/java/com/example/demo/Books/Models/Genre.java create mode 100644 src/main/java/com/example/demo/Books/Service/AuthorService.java create mode 100644 src/main/java/com/example/demo/Books/Service/BookService.java create mode 100644 src/main/java/com/example/demo/Books/Service/GenreService.java create mode 100644 src/test/java/com/example/demo/JpaAuthorsTest.java create mode 100644 src/test/java/com/example/demo/JpaBooksTest.java create mode 100644 src/test/java/com/example/demo/JpaGenresTest.java diff --git a/build.gradle b/build.gradle index bd92341..282aa9c 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,13 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' + + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + + implementation 'com.h2database:h2:2.1.210' + + implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' + testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/com/example/demo/Books/Controller/BookController.java b/src/main/java/com/example/demo/Books/Controller/BookController.java new file mode 100644 index 0000000..05fe0b7 --- /dev/null +++ b/src/main/java/com/example/demo/Books/Controller/BookController.java @@ -0,0 +1,56 @@ +package com.example.demo.Books.Controller; + +import com.example.demo.Books.Service.BookService; +import com.example.demo.Books.Models.Book; +import org.springframework.web.bind.annotation.*; + + +import java.util.List; + +@RestController +@RequestMapping("/book") +public class BookController { + private final BookService bookService; + + + public BookController(BookService bookService) { + this.bookService = bookService; + } + + @GetMapping("/test") + public String test() { + return "Test request"; + } + + + @GetMapping("/{id}") + public Book getBook(@PathVariable Long id) { + return bookService.findBook(id); + } + + @GetMapping("/") + public List getBooks() { + return bookService.findAllBooks(); + } + + @PostMapping("/") + public Book createBook(@RequestParam String name) { + return bookService.addBook(name); + } + + @PatchMapping("/{id}") + public Book updateBook(@PathVariable Long id, + @RequestParam("firstName") String name) { + return bookService.updateBook(id, name); + } + + @PatchMapping("/add_genre/{id}") + public Book addGenre(@PathVariable Long id, @RequestParam Long genre_id) { + return bookService.addGenre(id, genre_id); + } + + @DeleteMapping("/{id}") + public Book deleteBook(@PathVariable Long id) { + return bookService.deleteBook(id); + } +} diff --git a/src/main/java/com/example/demo/Books/Controller/GenreController.java b/src/main/java/com/example/demo/Books/Controller/GenreController.java new file mode 100644 index 0000000..a6eaeef --- /dev/null +++ b/src/main/java/com/example/demo/Books/Controller/GenreController.java @@ -0,0 +1,43 @@ +package com.example.demo.Books.Controller; + +import com.example.demo.Books.Service.GenreService; +import com.example.demo.Books.Models.Genre; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/genre") +public class GenreController { + private final GenreService genreService; + + public GenreController(GenreService genreService) { + this.genreService = genreService; + } + + @GetMapping("/{id}") + public Genre getGenre(@PathVariable Long id) { + return genreService.findGenre(id); + } + + @GetMapping("/") + public List getGenres() { + return genreService.findAllGenres(); + } + + @PostMapping("/") + public Genre createGenre(@RequestParam("name") String name) { + return genreService.addGenre(name); + } + + @PatchMapping("/{id}") + public Genre updateGenre(@PathVariable Long id, + @RequestParam("name") String name) { + return genreService.updateGenre(id, name); + } + + @DeleteMapping("/{id}") + public Genre deleteGenre(@PathVariable Long id) { + return genreService.deleteGenre(id); + } +} diff --git a/src/main/java/com/example/demo/Books/Models/Author.java b/src/main/java/com/example/demo/Books/Models/Author.java new file mode 100644 index 0000000..7e9ef9c --- /dev/null +++ b/src/main/java/com/example/demo/Books/Models/Author.java @@ -0,0 +1,61 @@ +package com.example.demo.Books.Models; + +import jakarta.persistence.Entity; + +import jakarta.persistence.*; +import java.util.List; + +@Entity +public class Author { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String name; + private String surname; + + @ManyToMany(fetch = FetchType.EAGER, mappedBy = "authors") + private List books; + + public Author() {} + + public Author(String name, String surname) { + this.name = name; + this.surname = surname; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public void setName(String name) { + this.name = name; + } + + public List getBooks() { + return books; + } + + @Override + public String toString() { + String res = "\nBooks{" + + "id: " + id + "," + + "name: " + name + "," + + "surname: " + name + "," + + "books:" + (books == null ? "[]" : books.toString()) + "}" + ; + return res; + } +} + diff --git a/src/main/java/com/example/demo/Books/Models/Book.java b/src/main/java/com/example/demo/Books/Models/Book.java new file mode 100644 index 0000000..b08567c --- /dev/null +++ b/src/main/java/com/example/demo/Books/Models/Book.java @@ -0,0 +1,73 @@ +package com.example.demo.Books.Models; + +import jakarta.persistence.*; +import org.hibernate.annotations.LazyCollection; +import org.hibernate.annotations.LazyCollectionOption; +import java.util.List; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String name; + + @ManyToMany + @LazyCollection(LazyCollectionOption.FALSE) + @JoinTable(name="books_genres", + joinColumns = @JoinColumn(name="book_id"), + inverseJoinColumns = @JoinColumn(name="genre_id") + ) + private List genres; + + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name="authors_books", + joinColumns = @JoinColumn(name="book_id"), + inverseJoinColumns = @JoinColumn(name="authors_id") + ) + private List authors; + + public Book() {} + + public Book(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void addGenre(Genre g) { + genres.add(g); + } + + public void addAuthor(Author author) { + authors.add(author); + } + + public List getAuthors() { + return authors; + } + + public List getGenres() { + return genres; + } + + @Override + public String toString() { + String res = "\nBook{" + + "id: " + id + "," + + "name: " + name + "," + + "genres:" + (genres == null ? "[]" : genres.toString()) + "}" + ; + return res; + } +} diff --git a/src/main/java/com/example/demo/Books/Models/Genre.java b/src/main/java/com/example/demo/Books/Models/Genre.java new file mode 100644 index 0000000..21eb0cb --- /dev/null +++ b/src/main/java/com/example/demo/Books/Models/Genre.java @@ -0,0 +1,36 @@ +package com.example.demo.Books.Models; + +import jakarta.persistence.*; + +@Entity +public class Genre { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String name; + + public Genre() {} + public Genre(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Genre{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/src/main/java/com/example/demo/Books/Service/AuthorService.java b/src/main/java/com/example/demo/Books/Service/AuthorService.java new file mode 100644 index 0000000..8cb1345 --- /dev/null +++ b/src/main/java/com/example/demo/Books/Service/AuthorService.java @@ -0,0 +1,76 @@ +package com.example.demo.Books.Service; + +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import java.util.List; +import org.springframework.transaction.annotation.Transactional; +import com.example.demo.Books.Models.Author; +import jakarta.persistence.*; + + +@Service +public class AuthorService { + + @PersistenceContext + private EntityManager em; + + @Transactional + public Author addAuthor(String name, String surname) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Author name is null or empty"); + } + + Author author = new Author(name, surname); + + em.persist(author); + return em.find(Author.class, author.getId()); + } + @Transactional + public Author addAuthor(String name, String surname, String path) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Author name is null or empty"); + } + + Author author = new Author(name, surname); + em.persist(author); + return em.find(Author.class, author.getId()); + } + + @Transactional(readOnly = true) + public Author findAuthor(Long id) { + final Author author = em.find(Author.class, id); + if (author == null) { + throw new EntityNotFoundException(String.format("Author with id [%s] is not found", id)); + } + return author; + } + + @Transactional(readOnly = true) + public List findAllAuthors() { + return em.createQuery("select f from Author f", Author.class) + .getResultList(); + } + + @Transactional + public Author updateAuthor(Long id, String name, String surname) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Author name is null or empty"); + } + final Author currentAuthor = findAuthor(id); + if(name != null) currentAuthor.setName(name); + if(surname != null) currentAuthor.setSurname(surname); + return em.merge(currentAuthor); + } + + @Transactional + public Author deleteAuthor(Long id) { + final Author currentAuthor = findAuthor(id); + em.remove(currentAuthor); + return currentAuthor; + } + + @Transactional + public void deleteAllAuthors() { + em.createQuery("delete from Author").executeUpdate(); + } +} diff --git a/src/main/java/com/example/demo/Books/Service/BookService.java b/src/main/java/com/example/demo/Books/Service/BookService.java new file mode 100644 index 0000000..2dbefd3 --- /dev/null +++ b/src/main/java/com/example/demo/Books/Service/BookService.java @@ -0,0 +1,122 @@ +package com.example.demo.Books.Service; + +import jakarta.persistence.*; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; +import com.example.demo.Books.Models.Author; +import com.example.demo.Books.Models.Book; +import com.example.demo.Books.Models.Genre; +import java.util.List; + +@Service +public class BookService { + + @PersistenceContext + private EntityManager em; + + @Transactional + public Book addBook(String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Book name is null or empty"); + } + + Book book = new Book(name); + + em.persist(book); + return em.find(Book.class, book.getId()); + } + + @Transactional + public Book addGenre(Long bookId, Long genreId) { + final Book book = em.find(Book.class, bookId); + + if (book == null) { + throw new EntityNotFoundException(String.format("Book with id [%s] is not found", bookId)); + } + + final Genre genre = em.find(Genre.class, genreId); + if (genre == null) { + throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", genreId)); + } + + book.addGenre(genre); + return em.merge(book); + } + + @Transactional + public Book addAuthor(Long bookId, Long authorId) { + final Book book = em.find(Book.class, bookId); + if (book == null) { + throw new EntityNotFoundException(String.format("Book with id [%s] is not found", bookId)); + } + + final Author author = em.find(Author.class, authorId); + if (author == null) { + throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", authorId)); + } + + book.addAuthor(author); + return em.merge(book); + } + + @Transactional(readOnly = true) + public Book findBook(Long id) { + final Book book = em.find(Book.class, id); + if (book == null) { + throw new EntityNotFoundException(String.format("Book with id [%s] is not found", id)); + } + return book; + } + + @Transactional(readOnly = true) + public List findBookByGenre(Long id, Long genreId) { + Query query = em.createQuery("select f from Book f " + + "join f.genres g " + + "where g.id = :genreId", Book.class) + .setParameter("genreId", genreId); + + List result = query.getResultList(); + return result; + } + + @Transactional(readOnly = true) + public List findBookByAuthor(Long id, String name) { + Query query = em.createQuery("select f from Book f " + + "join f.authors a " + + "where a.name like :authorName", Book.class) + .setParameter("authorName", '%' + name + '%'); + + List result = query.getResultList(); + return result; + } + + + @Transactional(readOnly = true) + public List findAllBooks() { + return em.createQuery("select f from Book f", Book.class) + .getResultList(); + } + + @Transactional + public Book updateBook(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Book name is null or empty"); + } + final Book currentBook = findBook(id); + currentBook.setName(name); + return em.merge(currentBook); + } + + @Transactional + public Book deleteBook(Long id) { + final Book currentBook = findBook(id); + em.remove(currentBook); + return currentBook; + } + + @Transactional + public void deleteAllBooks() { + em.createQuery("delete from Book").executeUpdate(); + } +} diff --git a/src/main/java/com/example/demo/Books/Service/GenreService.java b/src/main/java/com/example/demo/Books/Service/GenreService.java new file mode 100644 index 0000000..7cf4983 --- /dev/null +++ b/src/main/java/com/example/demo/Books/Service/GenreService.java @@ -0,0 +1,65 @@ +package com.example.demo.Books.Service; + +import com.example.demo.Books.Models.Genre; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import jakarta.persistence.*; +import java.util.List; + +@Service +public class GenreService { + + @PersistenceContext + private EntityManager em; + + @Transactional + public Genre addGenre(String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Genre name is null or empty"); + } + + Genre genre = new Genre(name); + + em.persist(genre); + return em.find(Genre.class, genre.getId()); + } + + @Transactional(readOnly = true) + public Genre findGenre(Long id) { + final Genre genre = em.find(Genre.class, id); + if (genre == null) { + throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", id)); + } + return genre; + } + + @Transactional(readOnly = true) + public List findAllGenres() { + return em.createQuery("select g from Genre g", Genre.class) + .getResultList(); + } + + @Transactional + public Genre updateGenre(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Genre name is null or empty"); + } + final Genre currentGenre = findGenre(id); + currentGenre.setName(name); + return em.merge(currentGenre); + } + + @Transactional + public Genre deleteGenre(Long id) { + final Genre currentGenre = findGenre(id); + em.remove(currentGenre); + return currentGenre; + } + + @Transactional + public void deleteAllGenres() { + em.createQuery("delete from Genre").executeUpdate(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..da7b0b1 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,11 @@ - +spring.main.banner-mode=off +#server.port=8080 +spring.datasource.url=jdbc:h2:file:./data +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.hibernate.ddl-auto=update +spring.h2.console.enabled=true +spring.h2.console.settings.trace=false +spring.h2.console.settings.web-allow-others=false diff --git a/src/test/java/com/example/demo/JpaAuthorsTest.java b/src/test/java/com/example/demo/JpaAuthorsTest.java new file mode 100644 index 0000000..2dbc153 --- /dev/null +++ b/src/test/java/com/example/demo/JpaAuthorsTest.java @@ -0,0 +1,92 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import com.example.demo.Books.Models.Author; +import com.example.demo.Books.Models.Book; +import com.example.demo.Books.Models.Genre; +import com.example.demo.Books.Service.AuthorService; +import com.example.demo.Books.Service.BookService; +import com.example.demo.Books.Service.GenreService; + +import jakarta.persistence.*; +import java.util.List; + +@SpringBootTest +public class JpaAuthorsTest { + private static final Logger log = LoggerFactory.getLogger(JpaBooksTest.class); + @Autowired + BookService bookService; + + @Autowired + AuthorService authorService; + + @Test + void testBookCreate() { + authorService.deleteAllAuthors(); + final Author author = authorService.addAuthor("Ivan", "Ivanov"); + log.info(author.toString()); + Assertions.assertNotNull(author.getId()); + } + + @Test + void testBookReadNotFound() { + authorService.deleteAllAuthors(); + Assertions.assertThrows(EntityNotFoundException.class, () -> authorService.findAuthor(-1L)); + } + + @Test + void findBook() { + authorService.deleteAllAuthors(); + final Author author = authorService.addAuthor("Ivan", "Ivanov"); + log.info(author.toString()); + final Author findAuthor = authorService.findAuthor(author.getId()); + log.info(findAuthor.toString()); + Assertions.assertEquals(author.toString(), findAuthor.toString()); + } + + @Test + void testReadAll() { + authorService.deleteAllAuthors(); + final Author author = authorService.addAuthor("Ivan", "Ivanov"); + log.info(author.toString()); + final List authors = authorService.findAllAuthors(); + Assertions.assertEquals(authors.size(), 1); + } + + @Test + void updateAuthor() { + authorService.deleteAllAuthors(); + final Author author = authorService.addAuthor("Ivan", "Ivanov"); + authorService.updateAuthor(author.getId(), "Alexei", null); + final Author findAuthor = authorService.findAuthor(author.getId()); + log.info(findAuthor.toString()); + Assertions.assertEquals(findAuthor.getName() + " " + findAuthor.getSurname(), "Alexei Ivanov"); + } + + @Test + void deleteAuthor() { + authorService.deleteAllAuthors(); + final Author author = authorService.addAuthor("Ivan", "Ivanov"); + log.info(author.toString()); + authorService.deleteAuthor(author.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> authorService.findAuthor(author.getId())); + } + + @Test + void addBook() { + authorService.deleteAllAuthors(); + bookService.findAllBooks(); + final Author author = authorService.addAuthor("Ivan", "Ivanov"); + final Book book = bookService.addBook("book"); + + bookService.addAuthor(book.getId(), author.getId()); + + final Author findAuthor = authorService.findAuthor(author.getId()); + Assertions.assertEquals(findAuthor.getBooks().get(0).getId(), book.getId()); + } +} diff --git a/src/test/java/com/example/demo/JpaBooksTest.java b/src/test/java/com/example/demo/JpaBooksTest.java new file mode 100644 index 0000000..a35c33e --- /dev/null +++ b/src/test/java/com/example/demo/JpaBooksTest.java @@ -0,0 +1,139 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import com.example.demo.Books.Models.Author; +import com.example.demo.Books.Models.Book; +import com.example.demo.Books.Models.Genre; +import com.example.demo.Books.Service.AuthorService; +import com.example.demo.Books.Service.BookService; +import com.example.demo.Books.Service.GenreService; + +import jakarta.persistence.*; +import java.util.List; + +@SpringBootTest +public class JpaBooksTest { + private static final Logger log = LoggerFactory.getLogger(JpaBooksTest.class); + @Autowired + BookService bookService; + + @Autowired + GenreService genreService; + + @Autowired + AuthorService authorService; + + @Test + void testBookCreate() { + bookService.deleteAllBooks(); + final Book book = bookService.addBook("book"); + log.info(book.toString()); + Assertions.assertNotNull(book.getId()); + } + + @Test + void testBookReadNotFound() { + bookService.deleteAllBooks(); + Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(-1L)); + } + + @Test + void findBook() { + bookService.deleteAllBooks(); + final Book book = bookService.addBook("book"); + log.info(book.toString()); + final Book findBook = bookService.findBook(book.getId()); + log.info(findBook.toString()); + Assertions.assertEquals(book.toString(), findBook.toString()); + } + + @Test + void updateBook() { + bookService.deleteAllBooks(); + final Book book = bookService.addBook("book"); + bookService.updateBook(book.getId(), "book 2"); + final Book findBook = bookService.findBook(book.getId()); + log.info(findBook.toString()); + Assertions.assertEquals(findBook.getName(), "book 2"); + } + + @Test + void testReadAll() { + bookService.deleteAllBooks(); + final Book book = bookService.addBook("book"); + log.info(book.toString()); + final List books = bookService.findAllBooks(); + Assertions.assertEquals(books.size(), 1); + } + + @Test + void deleteBook() { + bookService.deleteAllBooks(); + final Book book = bookService.addBook("book"); + log.info(book.toString()); + bookService.deleteBook(book.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(book.getId())); + } + + @Test + void findBooksByGenres() { + bookService.deleteAllBooks(); + genreService.deleteAllGenres(); + final Book book = bookService.addBook("book"); + bookService.addBook("book 2"); + final Genre genre = genreService.addGenre("Comedy"); + + bookService.addGenre(book.getId(), genre.getId()); + + final List books = bookService.findBookByGenre(book.getId(), genre.getId()); + Assertions.assertEquals(books.size(), 1); + } + + @Test + void findBooksByActor() { + bookService.deleteAllBooks(); + genreService.deleteAllGenres(); + final Book book = bookService.addBook("book"); + final Book book2 = bookService.addBook("book 2"); + bookService.addBook("book 3"); + final Author author = authorService.addAuthor("Ivan", "Ivanov"); + final Author author2 = authorService.addAuthor("Alexei", "Ivanov"); + + bookService.addAuthor(book.getId(), author.getId()); + bookService.addAuthor(book2.getId(), author2.getId()); + + final List books = bookService.findBookByAuthor(book.getId(), "Alexei"); + Assertions.assertEquals(books.size(), 2); + } + + @Test + void addGenre() { + bookService.deleteAllBooks(); + genreService.deleteAllGenres(); + final Book book = bookService.addBook("book"); + final Genre genre = genreService.addGenre("Comedy"); + + bookService.addGenre(book.getId(), genre.getId()); + + final Book findBook = bookService.findBook(book.getId()); + Assertions.assertEquals(findBook.getGenres().get(0).toString(), genre.toString()); + } + + @Test + void addAuthor() { + bookService.deleteAllBooks(); + genreService.deleteAllGenres(); + final Book book = bookService.addBook("book"); + final Author author = authorService.addAuthor("Ivan", "Ivanov"); + + bookService.addAuthor(book.getId(), author.getId()); + + final Book findBook = bookService.findBook(book.getId()); + Assertions.assertEquals(findBook.getAuthors().get(0).getId(), author.getId()); + } +} diff --git a/src/test/java/com/example/demo/JpaGenresTest.java b/src/test/java/com/example/demo/JpaGenresTest.java new file mode 100644 index 0000000..cdd78ac --- /dev/null +++ b/src/test/java/com/example/demo/JpaGenresTest.java @@ -0,0 +1,76 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import com.example.demo.Books.Models.Genre; +import com.example.demo.Books.Service.BookService; +import com.example.demo.Books.Service.GenreService; + +import jakarta.persistence.*; +import java.util.List; + +@SpringBootTest +public class JpaGenresTest { + private static final Logger log = LoggerFactory.getLogger(JpaGenresTest.class); + @Autowired + BookService bookService; + + @Autowired + GenreService genreService; + + @Test + void testGenreCreate() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + log.info(genre.toString()); + Assertions.assertNotNull(genre.getId()); + } + + @Test + void testGenreReadNotFound() { + genreService.deleteAllGenres(); + Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(-1L)); + } + + @Test + void findGenre() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + log.info(genre.toString()); + final Genre findGenre = genreService.findGenre(genre.getId()); + log.info(findGenre.toString()); + Assertions.assertEquals(genre.toString(), findGenre.toString()); + } + + @Test + void testReadAll() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + log.info(genre.toString()); + final List genres = genreService.findAllGenres(); + Assertions.assertEquals(genres.size(), 1); + } + + @Test + void updateGenre() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + genreService.updateGenre(genre.getId(), "Roman"); + final Genre findGenre = genreService.findGenre(genre.getId()); + log.info(findGenre.toString()); + Assertions.assertEquals(findGenre.getName(), "Roman"); + } + + @Test + void deleteGenre() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + log.info(genre.toString()); + genreService.deleteGenre(genre.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(genre.getId())); + } +}