From 5c24e481cf5a63a43b8043f6e7b4f5398953b80a Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Fri, 26 Apr 2024 16:08:11 +0400 Subject: [PATCH] lab-3 add tests for jpql methods and make some fixes --- .../books/repository/BookRepository.java | 19 ++--- .../ip/library/books/service/BookService.java | 18 ++++- .../users/repository/UserRepository.java | 3 +- .../ip/library/users/service/UserService.java | 2 +- .../test/java/com/ip/library/BooksTests.java | 77 +++++++++++-------- .../java/com/ip/library/FavoritesTests.java | 29 +++++-- 6 files changed, 94 insertions(+), 54 deletions(-) diff --git a/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java b/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java index a0262f2..2a49435 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/repository/BookRepository.java @@ -3,6 +3,7 @@ package com.ip.library.books.repository; import java.util.List; import java.util.Optional; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; @@ -17,31 +18,28 @@ public interface BookRepository extends List findByTypeId(long typeId); - List findByTypeId(long typeId, Pageable pageable); + Page findByTypeId(long typeId, Pageable pageable); @Query( "select ab.book " + "from AuthorsBooksEntity ab " + "where ab.author.id = ?1 " + - "order by ab.book.id" - ) + "order by ab.book.id") List findByAuthorId(Long authorId); @Query( "select ab.book " + "from AuthorsBooksEntity ab " + "where ab.author.id = ?1 " + - "order by ab.book.id" - ) - List findByAuthorId(Long authorId, Pageable pageable); + "order by ab.book.id") + Page findByAuthorId(Long authorId, Pageable pageable); @Query( "select ab.book " + "from AuthorsBooksEntity ab " + "where ab.author.id = ?1 and " + "ab.book.type.id = ?2 " + - "order by ab.book.id" - ) + "order by ab.book.id") List findByAuthorIdAndTypeId(Long authorId, Long typeId); @Query( @@ -49,9 +47,8 @@ public interface BookRepository extends "from AuthorsBooksEntity ab " + "where ab.author.id = ?1 and " + "ab.book.type.id = ?2 " + - "order by ab.book.id" - ) - List findByAuthorIdAndTypeId(Long authorId, Long typeId, Pageable pageable); + "order by ab.book.id") + Page findByAuthorIdAndTypeId(Long authorId, Long typeId, Pageable pageable); @Query( "select count(*) as number " + diff --git a/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java b/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java index 610bdfd..e893c19 100644 --- a/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java +++ b/SpringApp/library/src/main/java/com/ip/library/books/service/BookService.java @@ -3,6 +3,7 @@ package com.ip.library.books.service; import java.util.List; import java.util.stream.StreamSupport; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -37,11 +38,10 @@ public class BookService { } @Transactional(readOnly = true) - public List getAll(long typeId, long authorId, int page, int size) { + public Page getAll(long typeId, long authorId, int page, int size) { PageRequest pageRequest = PageRequest.of(page, size); if (typeId <= 0L && authorId <= 0L) - return StreamSupport.stream(repository.findAll(pageRequest).spliterator(), - false).toList(); + return repository.findAll(pageRequest); if (authorId <= 0L) return repository.findByTypeId(typeId, pageRequest); if (typeId <= 0L) @@ -49,6 +49,18 @@ public class BookService { return repository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest); } + @Transactional(readOnly = true) + public List getAll(long typeId, long authorId) { + if (typeId <= 0L && authorId <= 0L) + return StreamSupport.stream(repository.findAll().spliterator(), + false).toList(); + if (authorId <= 0L) + return repository.findByTypeId(typeId); + if (typeId <= 0L) + return repository.findByAuthorId(authorId); + return repository.findByAuthorIdAndTypeId(authorId, typeId); + } + @Transactional(readOnly = true) public BookEntity get(long id) { return repository.findById(id) diff --git a/SpringApp/library/src/main/java/com/ip/library/users/repository/UserRepository.java b/SpringApp/library/src/main/java/com/ip/library/users/repository/UserRepository.java index 087947f..8c78de8 100644 --- a/SpringApp/library/src/main/java/com/ip/library/users/repository/UserRepository.java +++ b/SpringApp/library/src/main/java/com/ip/library/users/repository/UserRepository.java @@ -3,6 +3,7 @@ package com.ip.library.users.repository; import java.util.List; import java.util.Optional; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; @@ -28,5 +29,5 @@ public interface UserRepository extends "from FavoriteEntity f " + "where f.user.id = ?1 " + "order by f.book.id") - public List getUserFavorities(Long userId, Pageable pageable); + public Page getUserFavorities(Long userId, Pageable pageable); } diff --git a/SpringApp/library/src/main/java/com/ip/library/users/service/UserService.java b/SpringApp/library/src/main/java/com/ip/library/users/service/UserService.java index e8a6aba..1809381 100644 --- a/SpringApp/library/src/main/java/com/ip/library/users/service/UserService.java +++ b/SpringApp/library/src/main/java/com/ip/library/users/service/UserService.java @@ -103,7 +103,7 @@ public class UserService { } @Transactional(readOnly = true) - public List getUserFavorities (long userId, int page, int size) { + public Page getUserFavorities (long userId, int page, int size) { return repository.getUserFavorities(userId, PageRequest.of(page, size)); } } diff --git a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java index a72ea75..98e79a2 100644 --- a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java @@ -1,7 +1,6 @@ package com.ip.library; import java.util.List; -import java.util.ArrayList; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.AfterEach; @@ -27,10 +26,11 @@ class BooksTests { private TypeService typeService; @Autowired private AuthorService authorService; - private BookEntity book; - private TypeEntity type; + private BookEntity book1; + private BookEntity book2; + private BookEntity book3; + private TypeEntity type1; private AuthorEntity author1; - private AuthorEntity author2; @AfterEach void removeData() { @@ -42,62 +42,63 @@ class BooksTests { @BeforeEach void createData() { removeData(); - type = typeService.create(new TypeEntity("type1")); + type1 = typeService.create(new TypeEntity("type1")); var type2 = typeService.create(new TypeEntity("type2")); author1 = authorService.create(new AuthorEntity("author1")); - author2 = authorService.create(new AuthorEntity("author2")); - List list1 = new ArrayList<>(); - list1.add(author1); - list1.add(author2); - bookService.create(new BookEntity("book1", type)); - bookService.create(new BookEntity("book2", type2)); - book = bookService.create(new BookEntity("book3", type)); + var author2 = authorService.create(new AuthorEntity("author2")); + book1 = bookService.create(new BookEntity("book1", type1)); + book2 = bookService.create(new BookEntity("book2", type1)); + book3 = bookService.create(new BookEntity("book3", type2)); + bookService.addAuthor(author1.getId(), book1.getId()); + bookService.addAuthor(author1.getId(), book3.getId()); + bookService.addAuthor(author2.getId(), book1.getId()); + bookService.addAuthor(author2.getId(), book2.getId()); } @Test void createTest() { Assertions.assertEquals(3, bookService.getAll().size()); - Assertions.assertEquals("book3", book.getName()); - Assertions.assertEquals(type, book.getType()); - Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book.getId())); + Assertions.assertEquals("book1", book1.getName()); + Assertions.assertEquals(type1, book1.getType()); + Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book1.getId())); } @Test void getTest() { - Assertions.assertEquals(book, bookService.get(book.getId())); + Assertions.assertEquals(book1, bookService.get(book1.getId())); Assertions.assertThrows(NotFoundException.class, () -> bookService.get(0L)); } @Test void updateTest() { - final String testName = book.getName() + "TEST"; + final String testName = book1.getName() + "TEST"; final TypeEntity testType = typeService.create( - new TypeEntity(book.getType().getName() + "TEST")); - book = bookService.update(book.getId(), new BookEntity(testName, testType)); + new TypeEntity(book1.getType().getName() + "TEST")); + book1 = bookService.update(book1.getId(), new BookEntity(testName, testType)); Assertions.assertEquals(3, bookService.getAll().size()); - Assertions.assertEquals(testName, book.getName()); - Assertions.assertEquals(testType, book.getType()); + Assertions.assertEquals(testName, book1.getName()); + Assertions.assertEquals(testType, book1.getType()); } @Test void deleteTest() { - bookService.delete(book.getId()); + bookService.delete(book1.getId()); Assertions.assertEquals(2, bookService.getAll().size()); final BookEntity newEntity = bookService.create( - new BookEntity(book.getName(), book.getType())); + new BookEntity(book1.getName(), book1.getType())); Assertions.assertEquals(3, bookService.getAll().size()); - Assertions.assertNotEquals(book.getId(), newEntity.getId()); + Assertions.assertNotEquals(book1.getId(), newEntity.getId()); } @Test void nullNameTest() { Assertions.assertThrows( DataIntegrityViolationException.class, - () -> bookService.create(new BookEntity(null, book.getType())) + () -> bookService.create(new BookEntity(null, book1.getType())) ); Assertions.assertThrows( DataIntegrityViolationException.class, - () -> bookService.update(book.getId(), new BookEntity(null, book.getType())) + () -> bookService.update(book1.getId(), new BookEntity(null, book1.getType())) ); } @@ -105,11 +106,11 @@ class BooksTests { void nullTypeTest() { Assertions.assertThrows( DataIntegrityViolationException.class, - () -> bookService.create(new BookEntity(book.getName() + "TEST", null)) + () -> bookService.create(new BookEntity(book1.getName() + "TEST", null)) ); Assertions.assertThrows( DataIntegrityViolationException.class, - () -> bookService.update(book.getId(), new BookEntity(book.getName() + "TEST", null)) + () -> bookService.update(book1.getId(), new BookEntity(book1.getName() + "TEST", null)) ); } @@ -117,11 +118,27 @@ class BooksTests { void uniqueNameTest() { Assertions.assertThrows( IllegalArgumentException.class, - () -> bookService.create(new BookEntity(book.getName(), book.getType())) + () -> bookService.create(new BookEntity(book1.getName(), book1.getType())) ); Assertions.assertThrows( IllegalArgumentException.class, - () -> bookService.update(book.getId(), new BookEntity(book.getName(), book.getType())) + () -> bookService.update(book1.getId(), new BookEntity(book1.getName(), book1.getType())) ); } + + @Test + void getByFilterTest() { + //filter by type + List list = bookService.getAll(type1.getId(), -1L); + Assertions.assertEquals(2, list.size()); + Assertions.assertTrue(list.contains(book1) && list.contains(book2)); + //filter by author + list = bookService.getAll(-1L, author1.getId()); + Assertions.assertEquals(2, list.size()); + Assertions.assertTrue(list.contains(book1) && list.contains(book3)); + //filter by type and author + list = bookService.getAll(type1.getId(), author1.getId()); + Assertions.assertEquals(1, list.size()); + Assertions.assertTrue(list.contains(book1)); + } } diff --git a/SpringApp/library/src/test/java/com/ip/library/FavoritesTests.java b/SpringApp/library/src/test/java/com/ip/library/FavoritesTests.java index 65140f1..f3427cf 100644 --- a/SpringApp/library/src/test/java/com/ip/library/FavoritesTests.java +++ b/SpringApp/library/src/test/java/com/ip/library/FavoritesTests.java @@ -1,5 +1,7 @@ package com.ip.library; +import java.util.List; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -22,15 +24,16 @@ class FavoritesTests { private UserService userService; @Autowired private TypeService typeService; - private UserEntity user; + private UserEntity user1; + private UserEntity user2; private BookEntity book1; private BookEntity book2; @AfterEach void removeData() { + userService.getAll().forEach(item -> userService.delete(item.getId())); bookService.getAll().forEach(item -> bookService.delete(item.getId())); typeService.getAll().forEach(item -> typeService.delete(item.getId())); - userService.getAll().forEach(item -> userService.delete(item.getId())); } @BeforeEach @@ -39,15 +42,25 @@ class FavoritesTests { TypeEntity type = typeService.create(new TypeEntity("type1")); book1 = bookService.create(new BookEntity("book1", type)); book2 = bookService.create(new BookEntity("book2", type)); - userService.create(new UserEntity("user1", "123")); + user1 = userService.create(new UserEntity("user3", "aqw2sed45")); + user2 = userService.create(new UserEntity("user1", "123")); userService.create(new UserEntity("user2", "456")); - user = userService.create(new UserEntity("user3", "aqw2sed45")); + userService.addFavorite(user1.getId(), book2.getId()); + userService.addFavorite(user2.getId(), book1.getId()); + userService.addFavorite(user2.getId(), book2.getId()); } @Test - void favoritesTest() { - Assertions.assertEquals(0, userService.getUserFavorities(user.getId()).size()); - Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book1.getId())); - Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book2.getId())); + void getBookSubscribersNumberTest() { + Assertions.assertEquals(1, bookService.getBookSubscribersNumber(book1.getId())); + Assertions.assertEquals(2, bookService.getBookSubscribersNumber(book2.getId())); + } + + @Test + void getUserFavoritiesTest() { + List list = userService.getUserFavorities(user2.getId()); + Assertions.assertEquals(2, list.size()); + Assertions.assertTrue(list.contains(book1)); + Assertions.assertTrue(list.contains(book2)); } }