lab-3 some mistakes
This commit is contained in:
parent
8ef96d2494
commit
bc1395933a
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
package com.ip.library;
|
||||
|
||||
public class UserEntity {
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user