From bc1395933a2bbdcb7f08480043799ad93a3ef35d Mon Sep 17 00:00:00 2001
From: Zakharov_Rostislav <r.z.8102.80@gmail.com>
Date: Mon, 15 Apr 2024 15:46:40 +0400
Subject: [PATCH] lab-3 some mistakes

---
 .../com/ip/library/LibraryApplication.java    | 22 +++----
 .../authors/service/AuthorService.java        |  1 -
 .../com/ip/library/users/api/UserDto.java     |  2 -
 .../ip/library/users/service/UserService.java |  2 -
 .../java/com/ip/library/AuthorsTests.java     | 17 ++---
 .../test/java/com/ip/library/BooksTests.java  | 31 +++++----
 .../java/com/ip/library/FavoritiesTests.java  | 64 -------------------
 .../test/java/com/ip/library/TypesTests.java  | 21 +++---
 .../test/java/com/ip/library/UserEntity.java  |  5 --
 .../test/java/com/ip/library/UsersTests.java  | 53 +++++++--------
 10 files changed, 71 insertions(+), 147 deletions(-)
 delete mode 100644 SpringApp/library/src/test/java/com/ip/library/FavoritiesTests.java
 delete mode 100644 SpringApp/library/src/test/java/com/ip/library/UserEntity.java

diff --git a/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java b/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java
index 323416a..fe1bcc5 100644
--- a/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java
+++ b/SpringApp/library/src/main/java/com/ip/library/LibraryApplication.java
@@ -43,23 +43,23 @@ public class LibraryApplication implements CommandLineRunner {
 	public void run(String... args) throws Exception {
 		if (args.length > 0 && args[0].equals("--populate")) {
 			log.info("Create default types values");
-			final var type1 = typeService.create(new TypeEntity(null, "type1"));
-			final var type2 = typeService.create(new TypeEntity(null, "type2"));
+			final var type1 = typeService.create(new TypeEntity("type1"));
+			final var type2 = typeService.create(new TypeEntity("type2"));
 			
 			log.info("Create default authors values");
-			final var author1 = authorService.create(new AuthorEntity(null, "author1"));
-			final var author2 = authorService.create(new AuthorEntity(null, "author2"));
+			final var author1 = authorService.create(new AuthorEntity("author1"));
+			final var author2 = authorService.create(new AuthorEntity("author2"));
 			
 			log.info("Create default books values");
-			final var book1 = bookService.create(new BookEntity(null, "book1", type1, author1));
-			final var book2 = bookService.create(new BookEntity(null, "book2", type1, author2));
-			final var book3 = bookService.create(new BookEntity(null, "book3", type2, author1));
-			final var book4 = bookService.create(new BookEntity(null, "book4", type2, author2));
+			final var book1 = bookService.create(new BookEntity("book1", type1, author1));
+			final var book2 = bookService.create(new BookEntity("book2", type1, author2));
+			final var book3 = bookService.create(new BookEntity("book3", type2, author1));
+			final var book4 = bookService.create(new BookEntity("book4", type2, author2));
 			
 			log.info("Create default users values");
-			final var user1 = userService.create(new UserEntity(null, "user1", "123", null, null));
-			final var user2 = userService.create(new UserEntity(null, "user2", "123", null, null ));
-			final var admin1 = userService.create(new UserEntity(null, "admin1", "123", null, null));
+			final var user1 = userService.create(new UserEntity("user1", "123"));
+			final var user2 = userService.create(new UserEntity("user2", "123"));
+			final var admin1 = userService.create(new UserEntity("admin1", "123"));
 			userService.giveAdminRole(admin1.getId());
 		}
 	}
diff --git a/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java b/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java
index 9ebe2cb..27eee8f 100644
--- a/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java
+++ b/SpringApp/library/src/main/java/com/ip/library/authors/service/AuthorService.java
@@ -1,7 +1,6 @@
 package com.ip.library.authors.service;
 
 import java.util.List;
-import java.util.Optional;
 import java.util.stream.StreamSupport;
 
 import org.springframework.stereotype.Service;
diff --git a/SpringApp/library/src/main/java/com/ip/library/users/api/UserDto.java b/SpringApp/library/src/main/java/com/ip/library/users/api/UserDto.java
index a2854df..747ba0e 100644
--- a/SpringApp/library/src/main/java/com/ip/library/users/api/UserDto.java
+++ b/SpringApp/library/src/main/java/com/ip/library/users/api/UserDto.java
@@ -1,7 +1,5 @@
 package com.ip.library.users.api;
 
-import java.util.List;
-
 import com.fasterxml.jackson.annotation.JsonProperty;
 
 import jakarta.validation.constraints.NotBlank;
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 190503c..9280612 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
@@ -1,8 +1,6 @@
 package com.ip.library.users.service;
 
-import java.util.ArrayList;
 import java.util.List;
-import java.util.Optional;
 import java.util.stream.StreamSupport;
 
 import org.springframework.stereotype.Service;
diff --git a/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java b/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java
index 8015c9c..014b62d 100644
--- a/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java
+++ b/SpringApp/library/src/test/java/com/ip/library/AuthorsTests.java
@@ -25,9 +25,9 @@ class AuthorsTests {
 	@BeforeEach
     void createData() {
         removeData();
-		authorService.create(new AuthorEntity(null, "author1"));
-		authorService.create(new AuthorEntity(null, "author2"));
-        entity = authorService.create(new AuthorEntity(null, "author3"));
+		authorService.create(new AuthorEntity("author1"));
+		authorService.create(new AuthorEntity("author2"));
+        entity = authorService.create(new AuthorEntity("author3"));
     }
 
 	@Test
@@ -44,14 +44,11 @@ class AuthorsTests {
 
 	@Test
 	void updateTest() {
-		final Long testId;
-		if (entity.getId() != 1L)
-			testId = 1L;
-		else testId = 2L;
+		final Long oldId = entity.getId();
 		final String testName = entity.getName() + "TEST";
-		entity = authorService.update(entity.getId(), new AuthorEntity(testId, testName));
+		entity = authorService.update(oldId, new AuthorEntity(testName));
 		Assertions.assertEquals(3, authorService.getAll().size());
-		Assertions.assertNotEquals(testId, entity.getId());
+		Assertions.assertEquals(oldId, entity.getId());
 		Assertions.assertEquals(testName, entity.getName());
 	}
 
@@ -59,7 +56,7 @@ class AuthorsTests {
 	void deleteTest() {
 		authorService.delete(entity.getId());
 		Assertions.assertEquals(2, authorService.getAll().size());
-		final AuthorEntity newEntity = authorService.create(new AuthorEntity(null, entity.getName()));
+		final AuthorEntity newEntity = authorService.create(new AuthorEntity(entity.getName()));
 		Assertions.assertEquals(3, authorService.getAll().size());
 		Assertions.assertNotEquals(entity.getId(), newEntity.getId());
 	}
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 3041484..f3addf7 100644
--- a/SpringApp/library/src/test/java/com/ip/library/BooksTests.java
+++ b/SpringApp/library/src/test/java/com/ip/library/BooksTests.java
@@ -37,13 +37,13 @@ class BooksTests {
 	@BeforeEach
     void createData() {
         removeData();
-		type = typeService.create(new TypeEntity(null, "type1"));
-		var type2 = typeService.create(new TypeEntity(null, "type2"));
-		author = authorService.create(new AuthorEntity(null, "author1"));
-		var author2 = authorService.create(new AuthorEntity(null, "author2"));
-		bookService.create(new BookEntity(null, "book1", type, author2));
-		bookService.create(new BookEntity(null, "book2", type2, author));
-        book = bookService.create(new BookEntity(null, "book3", type, author));
+		type = typeService.create(new TypeEntity("type1"));
+		var type2 = typeService.create(new TypeEntity("type2"));
+		author = authorService.create(new AuthorEntity("author1"));
+		var author2 = authorService.create(new AuthorEntity("author2"));
+		bookService.create(new BookEntity("book1", type, author2));
+		bookService.create(new BookEntity("book2", type2, author));
+        book = bookService.create(new BookEntity("book3", type, author));
     }
 
 	@Test
@@ -62,16 +62,14 @@ class BooksTests {
 
 	@Test
 	void updateTest() {
-		final Long testId;
-		if (book.getId() != 1L)
-			testId = 1L;
-		else testId = 2L;
 		final String testName = book.getName() + "TEST";
-		final TypeEntity testType = typeService.create(new TypeEntity(null, book.getType().getName() + "TEST"));
-		final AuthorEntity testAuthor = authorService.create(new AuthorEntity(null, book.getAuthor().getName() + "TEST"));
-		book = bookService.update(book.getId(), new BookEntity(testId, testName, testType, testAuthor));
+		final TypeEntity testType = typeService.create(
+			new TypeEntity(book.getType().getName() + "TEST"));
+		final AuthorEntity testAuthor = authorService.create(
+			new AuthorEntity(book.getAuthor().getName() + "TEST"));
+		book = bookService.update(book.getId(), new BookEntity(
+			testName, testType, testAuthor));
 		Assertions.assertEquals(3, bookService.getAll().size());
-		Assertions.assertNotEquals(testId, book.getId());
 		Assertions.assertEquals(testName, book.getName());
 		Assertions.assertEquals(testType, book.getType());
 		Assertions.assertEquals(testAuthor, book.getAuthor());
@@ -81,7 +79,8 @@ class BooksTests {
 	void deleteTest() {
 		bookService.delete(book.getId());
 		Assertions.assertEquals(2, bookService.getAll().size());
-		final BookEntity newEntity = bookService.create(new BookEntity(null, book.getName(), book.getType(), book.getAuthor()));
+		final BookEntity newEntity = bookService.create(
+			new BookEntity(book.getName(), book.getType(), book.getAuthor()));
 		Assertions.assertEquals(3, bookService.getAll().size());
 		Assertions.assertNotEquals(book.getId(), newEntity.getId());
 	}
diff --git a/SpringApp/library/src/test/java/com/ip/library/FavoritiesTests.java b/SpringApp/library/src/test/java/com/ip/library/FavoritiesTests.java
deleted file mode 100644
index b061e45..0000000
--- a/SpringApp/library/src/test/java/com/ip/library/FavoritiesTests.java
+++ /dev/null
@@ -1,64 +0,0 @@
-package com.ip.library;
-
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-
-import com.ip.library.authors.model.AuthorEntity;
-import com.ip.library.authors.service.AuthorService;
-import com.ip.library.books.model.BookEntity;
-import com.ip.library.books.service.BookService;
-import com.ip.library.types.model.TypeEntity;
-import com.ip.library.users.model.UserEntity;
-import com.ip.library.types.service.TypeService;
-import com.ip.library.users.service.UserService;
-
-@SpringBootTest
-class FavoritiesTests {
-	@Autowired
-	private BookService bookService;
-	@Autowired
-	private TypeService typeService;
-	@Autowired
-	private AuthorService authorService;
-	@Autowired
-	private UserService userService;
-	private UserEntity user;
-	private BookEntity book1;
-	private BookEntity book2;
-
-	@AfterEach
-    void removeData() {
-        authorService.getAll().forEach(item -> authorService.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
-    void createData() {
-        removeData();
-		var type = typeService.create(new TypeEntity(null, "type1"));
-		var author = authorService.create(new AuthorEntity(null, "author1"));
-		user = userService.create(new UserEntity(null, "user", "password", null, null));
-		book1 = bookService.create(new BookEntity(null, "book1", type, author));
-		book2 = bookService.create(new BookEntity(null, "book2", type, author));
-    }
-
-	@Test
-	void addAndRemoveBooksTest() {
-		Assertions.assertEquals(0, user.getBooks().size());
-		userService.addBook(user.getId(), book1.getId());
-		Assertions.assertEquals(1, user.getBooks().size());
-		userService.addBook(user.getId(), book2.getId());
-		Assertions.assertEquals(2, user.getBooks().size());
-		Assertions.assertEquals(2, user.getBooks().size());
-		userService.removeBook(user.getId(), book1.getId());
-		Assertions.assertEquals(1, user.getBooks().size());
-		userService.removeBook(user.getId(), book2.getId());
-		Assertions.assertEquals(0, user.getBooks().size());
-	}
-}
diff --git a/SpringApp/library/src/test/java/com/ip/library/TypesTests.java b/SpringApp/library/src/test/java/com/ip/library/TypesTests.java
index da03ba3..9c0d5bb 100644
--- a/SpringApp/library/src/test/java/com/ip/library/TypesTests.java
+++ b/SpringApp/library/src/test/java/com/ip/library/TypesTests.java
@@ -25,9 +25,9 @@ class TypesTests {
 	@BeforeEach
     void createData() {
         removeData();
-		typeService.create(new TypeEntity(null, "type1"));
-		typeService.create(new TypeEntity(null, "type2"));
-        entity = typeService.create(new TypeEntity(null, "type3"));
+		typeService.create(new TypeEntity("type1"));
+		typeService.create(new TypeEntity("type2"));
+        entity = typeService.create(new TypeEntity("type3"));
     }
 
 	@Test
@@ -39,19 +39,17 @@ class TypesTests {
 	@Test
 	void getTest() {
 		Assertions.assertEquals(entity, typeService.get(entity.getId()));
-		Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
+		Assertions.assertThrows(
+			NotFoundException.class, () -> typeService.get(0L));
 	}
 
 	@Test
 	void updateTest() {
-		final Long testId;
-		if (entity.getId() != 1L)
-			testId = 1L;
-		else testId = 2L;
+		final Long oldId = entity.getId();
 		final String testName = entity.getName() + "TEST";
-		entity = typeService.update(entity.getId(), new TypeEntity(testId, testName));
+		entity = typeService.update(oldId, new TypeEntity(testName));
 		Assertions.assertEquals(3, typeService.getAll().size());
-		Assertions.assertNotEquals(testId, entity.getId());
+		Assertions.assertEquals(oldId, entity.getId());
 		Assertions.assertEquals(testName, entity.getName());
 	}
 
@@ -59,7 +57,8 @@ class TypesTests {
 	void deleteTest() {
 		typeService.delete(entity.getId());
 		Assertions.assertEquals(2, typeService.getAll().size());
-		final TypeEntity newEntity = typeService.create(new TypeEntity(null, entity.getName()));
+		final TypeEntity newEntity = typeService.create(
+			new TypeEntity(entity.getName()));
 		Assertions.assertEquals(3, typeService.getAll().size());
 		Assertions.assertNotEquals(entity.getId(), newEntity.getId());
 	}
diff --git a/SpringApp/library/src/test/java/com/ip/library/UserEntity.java b/SpringApp/library/src/test/java/com/ip/library/UserEntity.java
deleted file mode 100644
index 7e3437b..0000000
--- a/SpringApp/library/src/test/java/com/ip/library/UserEntity.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package com.ip.library;
-
-public class UserEntity {
-
-}
diff --git a/SpringApp/library/src/test/java/com/ip/library/UsersTests.java b/SpringApp/library/src/test/java/com/ip/library/UsersTests.java
index 9659d43..a648407 100644
--- a/SpringApp/library/src/test/java/com/ip/library/UsersTests.java
+++ b/SpringApp/library/src/test/java/com/ip/library/UsersTests.java
@@ -1,8 +1,5 @@
 package com.ip.library;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
@@ -31,7 +28,8 @@ class UsersTests {
 	@Autowired
 	private UserService userService;
 	private UserEntity user;
-	private BookEntity book;
+	private BookEntity book1;
+	private BookEntity book2;
 
 	@AfterEach
     void removeData() {
@@ -44,14 +42,13 @@ class UsersTests {
 	@BeforeEach
     void createData() {
         removeData();
-		var type1 = typeService.create(new TypeEntity(null, "type1"));
-		var author1 = authorService.create(new AuthorEntity(null, "author1"));
-		book = bookService.create(new BookEntity(null, "book1", type1, author1));
-		List<BookEntity> books = new ArrayList<BookEntity>();
-		books.add(book);
-		userService.create(new UserEntity(null, "user1", "123", "user", null));
-		userService.create(new UserEntity(null, "user2", "456", "user", null));
-        user = userService.create(new UserEntity(null, "user3", "aqw2sed45", "admin", books));
+		var type = typeService.create(new TypeEntity("type1"));
+		var author = authorService.create(new AuthorEntity("author1"));
+		book1 = bookService.create(new BookEntity("book1", type, author));
+		book2 = bookService.create(new BookEntity("book2", type, author));
+		userService.create(new UserEntity("user1", "123"));
+		userService.create(new UserEntity("user2", "456"));
+        user = userService.create(new UserEntity("user3", "aqw2sed45"));
     }
 
 	@Test
@@ -72,22 +69,14 @@ class UsersTests {
 
 	@Test
 	void updateTest() {
-		final Long testId;
-		if (user.getId() != 1L)
-			testId = 1L;
-		else testId = 2L;
+		final Long oldId = user.getId();
 		final String testName = user.getLogin() + "TEST";
 		final String testPassword = user.getPassword() + "TEST";
-		final String testRole = "admin";
-		List<BookEntity> testBooks = new ArrayList<BookEntity>();
-		testBooks.add(book);
-		user = userService.update(user.getId(), new UserEntity(testId, testName, 
-			testPassword, testRole, testBooks));
+		user = userService.update(oldId, new UserEntity(testName, testPassword));
 		Assertions.assertEquals(3, userService.getAll().size());
-		Assertions.assertNotEquals(testId, user.getId());
+		Assertions.assertEquals(oldId, user.getId());
 		Assertions.assertEquals(testName, user.getLogin());
 		Assertions.assertNotEquals(testPassword, user.getPassword());
-		Assertions.assertNotEquals(testRole, user.getRole());
 		Assertions.assertEquals(0, user.getBooks().size());
 	}
 
@@ -95,8 +84,8 @@ class UsersTests {
 	void deleteTest() {
 		userService.delete(user.getId());
 		Assertions.assertEquals(2, userService.getAll().size());
-		final UserEntity newEntity = userService.create(new UserEntity(null, 
-			user.getLogin(), user.getPassword(), null, null));
+		final UserEntity newEntity = userService.create(
+			new UserEntity(user.getLogin(), user.getPassword()));
 		Assertions.assertEquals(3, userService.getAll().size());
 		Assertions.assertNotEquals(user.getId(), newEntity.getId());
 	}
@@ -116,4 +105,18 @@ class UsersTests {
 		userService.giveUserRole(user.getId());
 		Assertions.assertEquals("user", user.getRole());
 	}
+
+	@Test
+	void BooksTest() {
+		Assertions.assertEquals(0, user.getBooks().size());
+		userService.addBook(user.getId(), book1.getId());
+		Assertions.assertEquals(1, user.getBooks().size());
+		userService.addBook(user.getId(), book2.getId());
+		Assertions.assertEquals(2, user.getBooks().size());
+		Assertions.assertEquals(2, user.getBooks().size());
+		userService.removeBook(user.getId(), book1.getId());
+		Assertions.assertEquals(1, user.getBooks().size());
+		userService.removeBook(user.getId(), book2.getId());
+		Assertions.assertEquals(0, user.getBooks().size());
+	}
 }