lab-3 some mistakes

This commit is contained in:
Zakharov_Rostislav 2024-04-15 15:46:40 +04:00
parent 8ef96d2494
commit bc1395933a
10 changed files with 71 additions and 147 deletions

View File

@ -43,23 +43,23 @@ public class LibraryApplication implements CommandLineRunner {
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
if (args.length > 0 && args[0].equals("--populate")) { if (args.length > 0 && args[0].equals("--populate")) {
log.info("Create default types values"); log.info("Create default types values");
final var type1 = typeService.create(new TypeEntity(null, "type1")); final var type1 = typeService.create(new TypeEntity("type1"));
final var type2 = typeService.create(new TypeEntity(null, "type2")); final var type2 = typeService.create(new TypeEntity("type2"));
log.info("Create default authors values"); log.info("Create default authors values");
final var author1 = authorService.create(new AuthorEntity(null, "author1")); final var author1 = authorService.create(new AuthorEntity("author1"));
final var author2 = authorService.create(new AuthorEntity(null, "author2")); final var author2 = authorService.create(new AuthorEntity("author2"));
log.info("Create default books values"); log.info("Create default books values");
final var book1 = bookService.create(new BookEntity(null, "book1", type1, author1)); final var book1 = bookService.create(new BookEntity("book1", type1, author1));
final var book2 = bookService.create(new BookEntity(null, "book2", type1, author2)); final var book2 = bookService.create(new BookEntity("book2", type1, author2));
final var book3 = bookService.create(new BookEntity(null, "book3", type2, author1)); final var book3 = bookService.create(new BookEntity("book3", type2, author1));
final var book4 = bookService.create(new BookEntity(null, "book4", type2, author2)); final var book4 = bookService.create(new BookEntity("book4", type2, author2));
log.info("Create default users values"); log.info("Create default users values");
final var user1 = userService.create(new UserEntity(null, "user1", "123", null, null)); final var user1 = userService.create(new UserEntity("user1", "123"));
final var user2 = userService.create(new UserEntity(null, "user2", "123", null, null )); final var user2 = userService.create(new UserEntity("user2", "123"));
final var admin1 = userService.create(new UserEntity(null, "admin1", "123", null, null)); final var admin1 = userService.create(new UserEntity("admin1", "123"));
userService.giveAdminRole(admin1.getId()); userService.giveAdminRole(admin1.getId());
} }
} }

View File

@ -1,7 +1,6 @@
package com.ip.library.authors.service; package com.ip.library.authors.service;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -1,7 +1,5 @@
package com.ip.library.users.api; package com.ip.library.users.api;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;

View File

@ -1,8 +1,6 @@
package com.ip.library.users.service; package com.ip.library.users.service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -25,9 +25,9 @@ class AuthorsTests {
@BeforeEach @BeforeEach
void createData() { void createData() {
removeData(); removeData();
authorService.create(new AuthorEntity(null, "author1")); authorService.create(new AuthorEntity("author1"));
authorService.create(new AuthorEntity(null, "author2")); authorService.create(new AuthorEntity("author2"));
entity = authorService.create(new AuthorEntity(null, "author3")); entity = authorService.create(new AuthorEntity("author3"));
} }
@Test @Test
@ -44,14 +44,11 @@ class AuthorsTests {
@Test @Test
void updateTest() { void updateTest() {
final Long testId; final Long oldId = entity.getId();
if (entity.getId() != 1L)
testId = 1L;
else testId = 2L;
final String testName = entity.getName() + "TEST"; 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.assertEquals(3, authorService.getAll().size());
Assertions.assertNotEquals(testId, entity.getId()); Assertions.assertEquals(oldId, entity.getId());
Assertions.assertEquals(testName, entity.getName()); Assertions.assertEquals(testName, entity.getName());
} }
@ -59,7 +56,7 @@ class AuthorsTests {
void deleteTest() { void deleteTest() {
authorService.delete(entity.getId()); authorService.delete(entity.getId());
Assertions.assertEquals(2, authorService.getAll().size()); 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.assertEquals(3, authorService.getAll().size());
Assertions.assertNotEquals(entity.getId(), newEntity.getId()); Assertions.assertNotEquals(entity.getId(), newEntity.getId());
} }

View File

@ -37,13 +37,13 @@ class BooksTests {
@BeforeEach @BeforeEach
void createData() { void createData() {
removeData(); removeData();
type = typeService.create(new TypeEntity(null, "type1")); type = typeService.create(new TypeEntity("type1"));
var type2 = typeService.create(new TypeEntity(null, "type2")); var type2 = typeService.create(new TypeEntity("type2"));
author = authorService.create(new AuthorEntity(null, "author1")); author = authorService.create(new AuthorEntity("author1"));
var author2 = authorService.create(new AuthorEntity(null, "author2")); var author2 = authorService.create(new AuthorEntity("author2"));
bookService.create(new BookEntity(null, "book1", type, author2)); bookService.create(new BookEntity("book1", type, author2));
bookService.create(new BookEntity(null, "book2", type2, author)); bookService.create(new BookEntity("book2", type2, author));
book = bookService.create(new BookEntity(null, "book3", type, author)); book = bookService.create(new BookEntity("book3", type, author));
} }
@Test @Test
@ -62,16 +62,14 @@ class BooksTests {
@Test @Test
void updateTest() { void updateTest() {
final Long testId;
if (book.getId() != 1L)
testId = 1L;
else testId = 2L;
final String testName = book.getName() + "TEST"; final String testName = book.getName() + "TEST";
final TypeEntity testType = typeService.create(new TypeEntity(null, book.getType().getName() + "TEST")); final TypeEntity testType = typeService.create(
final AuthorEntity testAuthor = authorService.create(new AuthorEntity(null, book.getAuthor().getName() + "TEST")); new TypeEntity(book.getType().getName() + "TEST"));
book = bookService.update(book.getId(), new BookEntity(testId, testName, testType, testAuthor)); 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.assertEquals(3, bookService.getAll().size());
Assertions.assertNotEquals(testId, book.getId());
Assertions.assertEquals(testName, book.getName()); Assertions.assertEquals(testName, book.getName());
Assertions.assertEquals(testType, book.getType()); Assertions.assertEquals(testType, book.getType());
Assertions.assertEquals(testAuthor, book.getAuthor()); Assertions.assertEquals(testAuthor, book.getAuthor());
@ -81,7 +79,8 @@ class BooksTests {
void deleteTest() { void deleteTest() {
bookService.delete(book.getId()); bookService.delete(book.getId());
Assertions.assertEquals(2, bookService.getAll().size()); 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.assertEquals(3, bookService.getAll().size());
Assertions.assertNotEquals(book.getId(), newEntity.getId()); Assertions.assertNotEquals(book.getId(), newEntity.getId());
} }

View File

@ -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());
}
}

View File

@ -25,9 +25,9 @@ class TypesTests {
@BeforeEach @BeforeEach
void createData() { void createData() {
removeData(); removeData();
typeService.create(new TypeEntity(null, "type1")); typeService.create(new TypeEntity("type1"));
typeService.create(new TypeEntity(null, "type2")); typeService.create(new TypeEntity("type2"));
entity = typeService.create(new TypeEntity(null, "type3")); entity = typeService.create(new TypeEntity("type3"));
} }
@Test @Test
@ -39,19 +39,17 @@ class TypesTests {
@Test @Test
void getTest() { void getTest() {
Assertions.assertEquals(entity, typeService.get(entity.getId())); Assertions.assertEquals(entity, typeService.get(entity.getId()));
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L)); Assertions.assertThrows(
NotFoundException.class, () -> typeService.get(0L));
} }
@Test @Test
void updateTest() { void updateTest() {
final Long testId; final Long oldId = entity.getId();
if (entity.getId() != 1L)
testId = 1L;
else testId = 2L;
final String testName = entity.getName() + "TEST"; 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.assertEquals(3, typeService.getAll().size());
Assertions.assertNotEquals(testId, entity.getId()); Assertions.assertEquals(oldId, entity.getId());
Assertions.assertEquals(testName, entity.getName()); Assertions.assertEquals(testName, entity.getName());
} }
@ -59,7 +57,8 @@ class TypesTests {
void deleteTest() { void deleteTest() {
typeService.delete(entity.getId()); typeService.delete(entity.getId());
Assertions.assertEquals(2, typeService.getAll().size()); 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.assertEquals(3, typeService.getAll().size());
Assertions.assertNotEquals(entity.getId(), newEntity.getId()); Assertions.assertNotEquals(entity.getId(), newEntity.getId());
} }

View File

@ -1,5 +0,0 @@
package com.ip.library;
public class UserEntity {
}

View File

@ -1,8 +1,5 @@
package com.ip.library; package com.ip.library;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -31,7 +28,8 @@ class UsersTests {
@Autowired @Autowired
private UserService userService; private UserService userService;
private UserEntity user; private UserEntity user;
private BookEntity book; private BookEntity book1;
private BookEntity book2;
@AfterEach @AfterEach
void removeData() { void removeData() {
@ -44,14 +42,13 @@ class UsersTests {
@BeforeEach @BeforeEach
void createData() { void createData() {
removeData(); removeData();
var type1 = typeService.create(new TypeEntity(null, "type1")); var type = typeService.create(new TypeEntity("type1"));
var author1 = authorService.create(new AuthorEntity(null, "author1")); var author = authorService.create(new AuthorEntity("author1"));
book = bookService.create(new BookEntity(null, "book1", type1, author1)); book1 = bookService.create(new BookEntity("book1", type, author));
List<BookEntity> books = new ArrayList<BookEntity>(); book2 = bookService.create(new BookEntity("book2", type, author));
books.add(book); userService.create(new UserEntity("user1", "123"));
userService.create(new UserEntity(null, "user1", "123", "user", null)); userService.create(new UserEntity("user2", "456"));
userService.create(new UserEntity(null, "user2", "456", "user", null)); user = userService.create(new UserEntity("user3", "aqw2sed45"));
user = userService.create(new UserEntity(null, "user3", "aqw2sed45", "admin", books));
} }
@Test @Test
@ -72,22 +69,14 @@ class UsersTests {
@Test @Test
void updateTest() { void updateTest() {
final Long testId; final Long oldId = user.getId();
if (user.getId() != 1L)
testId = 1L;
else testId = 2L;
final String testName = user.getLogin() + "TEST"; final String testName = user.getLogin() + "TEST";
final String testPassword = user.getPassword() + "TEST"; final String testPassword = user.getPassword() + "TEST";
final String testRole = "admin"; user = userService.update(oldId, new UserEntity(testName, testPassword));
List<BookEntity> testBooks = new ArrayList<BookEntity>();
testBooks.add(book);
user = userService.update(user.getId(), new UserEntity(testId, testName,
testPassword, testRole, testBooks));
Assertions.assertEquals(3, userService.getAll().size()); Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertNotEquals(testId, user.getId()); Assertions.assertEquals(oldId, user.getId());
Assertions.assertEquals(testName, user.getLogin()); Assertions.assertEquals(testName, user.getLogin());
Assertions.assertNotEquals(testPassword, user.getPassword()); Assertions.assertNotEquals(testPassword, user.getPassword());
Assertions.assertNotEquals(testRole, user.getRole());
Assertions.assertEquals(0, user.getBooks().size()); Assertions.assertEquals(0, user.getBooks().size());
} }
@ -95,8 +84,8 @@ class UsersTests {
void deleteTest() { void deleteTest() {
userService.delete(user.getId()); userService.delete(user.getId());
Assertions.assertEquals(2, userService.getAll().size()); Assertions.assertEquals(2, userService.getAll().size());
final UserEntity newEntity = userService.create(new UserEntity(null, final UserEntity newEntity = userService.create(
user.getLogin(), user.getPassword(), null, null)); new UserEntity(user.getLogin(), user.getPassword()));
Assertions.assertEquals(3, userService.getAll().size()); Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertNotEquals(user.getId(), newEntity.getId()); Assertions.assertNotEquals(user.getId(), newEntity.getId());
} }
@ -116,4 +105,18 @@ class UsersTests {
userService.giveUserRole(user.getId()); userService.giveUserRole(user.getId());
Assertions.assertEquals("user", user.getRole()); 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());
}
} }