lab-2 tests are completed

This commit is contained in:
Zakharov_Rostislav 2024-04-05 15:42:07 +04:00
parent e5fb124f32
commit b0e27141cb
6 changed files with 241 additions and 175 deletions

View File

@ -1,10 +1,9 @@
package com.ip.library;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -13,49 +12,55 @@ import com.ip.library.authors.model.AuthorEntity;
import com.ip.library.authors.service.AuthorService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class AuthorsTests {
@Autowired
private AuthorService authorService;
private AuthorEntity entity;
@AfterEach
void removeData() {
authorService.getAll().forEach(item -> authorService.delete(item.getId()));
}
@BeforeEach
void createData() {
removeData();
authorService.create(new AuthorEntity(null, "author1"));
authorService.create(new AuthorEntity(null, "author2"));
entity = authorService.create(new AuthorEntity(null, "author3"));
}
@Test
void createTest() {
Assertions.assertEquals(3, authorService.getAll().size());
Assertions.assertEquals("author3", entity.getName());
}
@Test
void getTest() {
Assertions.assertEquals(entity, authorService.get(entity.getId()));
Assertions.assertThrows(NotFoundException.class, () -> authorService.get(0L));
}
@Test
@Order(1)
void createTest() {
authorService.create(new AuthorEntity(null, "author1"));
authorService.create(new AuthorEntity(null, "author2"));
final AuthorEntity last = authorService.create(new AuthorEntity(null, "author3"));
Assertions.assertEquals(3, authorService.getAll().size());
Assertions.assertEquals(last, authorService.get(3L));
}
@Test
@Order(2)
void updateTest() {
final AuthorEntity entity = authorService.get(3L);
final String oldName = entity.getName();
final String test = oldName + "TEST";
final AuthorEntity newEntity = authorService.update(3L, new AuthorEntity(1L, test));
final Long testId;
if (entity.getId() != 1L)
testId = 1L;
else testId = 2L;
final String testName = entity.getName() + "TEST";
entity = authorService.update(entity.getId(), new AuthorEntity(testId, testName));
Assertions.assertEquals(3, authorService.getAll().size());
Assertions.assertEquals(newEntity, authorService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
Assertions.assertNotEquals(testId, entity.getId());
Assertions.assertEquals(testName, entity.getName());
}
@Test
@Order(3)
void deleteTest() {
authorService.delete(3L);
authorService.delete(entity.getId());
Assertions.assertEquals(2, authorService.getAll().size());
final AuthorEntity last = authorService.get(2L);
Assertions.assertEquals(2L, last.getId());
final AuthorEntity newEntity = authorService.create(new AuthorEntity(null, "Игровая приставка"));
final AuthorEntity newEntity = authorService.create(new AuthorEntity(null, entity.getName()));
Assertions.assertEquals(3, authorService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
Assertions.assertNotEquals(entity.getId(), newEntity.getId());
}
}

View File

@ -1,10 +1,9 @@
package com.ip.library;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -17,7 +16,6 @@ import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class BooksTests {
@Autowired
private BookService bookService;
@ -25,59 +23,66 @@ class BooksTests {
private TypeService typeService;
@Autowired
private AuthorService authorService;
private BookEntity book;
private TypeEntity type;
private AuthorEntity author;
@AfterEach
void removeData() {
bookService.getAll().forEach(item -> bookService.delete(item.getId()));
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
authorService.getAll().forEach(item -> authorService.delete(item.getId()));
}
@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));
}
@Test
void createTest() {
Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals("book3", book.getName());
Assertions.assertEquals(type, book.getType());
Assertions.assertEquals(author, book.getAuthor());
}
@Test
void getTest() {
Assertions.assertEquals(book, bookService.get(book.getId()));
Assertions.assertThrows(NotFoundException.class, () -> bookService.get(0L));
}
@Test
@Order(1)
void createTest() {
var type1 = typeService.create(new TypeEntity(null, "type1"));
var type2 = typeService.create(new TypeEntity(null, "type2"));
var author1 = authorService.create(new AuthorEntity(null, "author1"));
var author2 = authorService.create(new AuthorEntity(null, "author2"));
bookService.create(new BookEntity(null, "book1", type1, author1));
bookService.create(new BookEntity(null, "book2", type2, author2));
final BookEntity last = bookService.create(new BookEntity(null, "book3", type2, author1));
Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals(last, bookService.get(3L));
}
@Test
@Order(2)
void updateTest() {
final BookEntity entity = bookService.get(3L);
final String oldName = entity.getName();
final String test = oldName + "TEST";
final var oldType = entity.getType();
var testType = typeService.create(new TypeEntity(null, oldType.getName() + "test"));
final var oldAuthor = entity.getAuthor();
var testAuthor = authorService.create(new AuthorEntity(null, oldAuthor.getName() + "test"));
final BookEntity newEntity = bookService.update(3L, new BookEntity(1L, test, testType, testAuthor));
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));
Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals(newEntity, bookService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
Assertions.assertEquals(testType, newEntity.getType());
Assertions.assertNotEquals(oldType, newEntity.getType());
Assertions.assertEquals(testAuthor, newEntity.getAuthor());
Assertions.assertNotEquals(oldAuthor, newEntity.getAuthor());
Assertions.assertNotEquals(testId, book.getId());
Assertions.assertEquals(testName, book.getName());
Assertions.assertEquals(testType, book.getType());
Assertions.assertEquals(testAuthor, book.getAuthor());
}
@Test
@Order(3)
void deleteTest() {
bookService.delete(3L);
bookService.delete(book.getId());
Assertions.assertEquals(2, bookService.getAll().size());
final BookEntity last = bookService.get(2L);
Assertions.assertEquals(2L, last.getId());
var type2 = typeService.create(new TypeEntity(null, "type2"));
var author1 = authorService.create(new AuthorEntity(null, "author1"));
final BookEntity newEntity = bookService.create(new BookEntity(null, "book3", type2, author1));
final BookEntity newEntity = bookService.create(new BookEntity(null, book.getName(), book.getType(), book.getAuthor()));
Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
Assertions.assertNotEquals(book.getId(), newEntity.getId());
}
}

View File

@ -0,0 +1,64 @@
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

@ -1,10 +1,9 @@
package com.ip.library;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -13,49 +12,55 @@ import com.ip.library.types.model.TypeEntity;
import com.ip.library.types.service.TypeService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class TypesTests {
@Autowired
private TypeService typeService;
private TypeEntity entity;
@AfterEach
void removeData() {
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
}
@BeforeEach
void createData() {
removeData();
typeService.create(new TypeEntity(null, "type1"));
typeService.create(new TypeEntity(null, "type2"));
entity = typeService.create(new TypeEntity(null, "type3"));
}
@Test
void createTest() {
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals("type3", entity.getName());
}
@Test
void getTest() {
Assertions.assertEquals(entity, typeService.get(entity.getId()));
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
}
@Test
@Order(1)
void createTest() {
typeService.create(new TypeEntity(null, "type1"));
typeService.create(new TypeEntity(null, "type2"));
final TypeEntity last = typeService.create(new TypeEntity(null, "type3"));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(last, typeService.get(3L));
}
@Test
@Order(2)
void updateTest() {
final TypeEntity entity = typeService.get(3L);
final String oldName = entity.getName();
final String test = oldName + "TEST";
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
final Long testId;
if (entity.getId() != 1L)
testId = 1L;
else testId = 2L;
final String testName = entity.getName() + "TEST";
entity = typeService.update(entity.getId(), new TypeEntity(testId, testName));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(newEntity, typeService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
Assertions.assertNotEquals(testId, entity.getId());
Assertions.assertEquals(testName, entity.getName());
}
@Test
@Order(3)
void deleteTest() {
typeService.delete(3L);
typeService.delete(entity.getId());
Assertions.assertEquals(2, typeService.getAll().size());
final TypeEntity last = typeService.get(2L);
Assertions.assertEquals(2L, last.getId());
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "type3"));
final TypeEntity newEntity = typeService.create(new TypeEntity(null, entity.getName()));
Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
Assertions.assertNotEquals(entity.getId(), newEntity.getId());
}
}

View File

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

View File

@ -3,11 +3,10 @@ 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.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -22,7 +21,6 @@ import com.ip.library.users.model.UserEntity;
import com.ip.library.users.service.UserService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class UsersTests {
@Autowired
private BookService bookService;
@ -32,106 +30,90 @@ class UsersTests {
private AuthorService authorService;
@Autowired
private UserService userService;
private UserEntity user;
private BookEntity book;
@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 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));
}
@Test
void createTest() {
Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertEquals("user3", user.getName());
Assertions.assertEquals("aqw2sed45", user.getPassword());
Assertions.assertEquals("user", user.getRole());
Assertions.assertEquals(0, user.getBooks().size());
}
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
Assertions.assertEquals(user, userService.get(user.getId()));
Assertions.assertThrows(NotFoundException.class,
() -> userService.get(0L));
}
@Test
@Order(1)
void createTest() {
userService.create(new UserEntity(null, "user1", "123", "user", null));
userService.create(new UserEntity(null, "user2", "456", "user", null));
var type1 = typeService.create(new TypeEntity(null, "type1"));
var author1 = authorService.create(new AuthorEntity(null, "author1"));
var book1 = bookService.create(new BookEntity(null, "book1", type1, author1));
List<BookEntity> books = new ArrayList<BookEntity>();
books.add(book1);
final UserEntity last = userService.create(new UserEntity(
null, "user3", "aqw2sed45", "admin", books));
Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertEquals(last, userService.get(3L));
Assertions.assertNotEquals("admin", last.getRole());
Assertions.assertEquals("user", last.getRole());
Assertions.assertEquals(0, last.getBooks().size());
}
@Test
@Order(2)
void updateTest() {
final UserEntity entity = userService.get(3L);
final String oldName = entity.getName();
final String testName = oldName + "TEST";
final String oldPassword = entity.getPassword();
final String testPassword = oldPassword + "TEST";
final String oldRole = entity.getRole();
final Long testId;
if (user.getId() != 1L)
testId = 1L;
else testId = 2L;
final String testName = user.getName() + "TEST";
final String testPassword = user.getPassword() + "TEST";
final String testRole = "admin";
final UserEntity newEntity = userService.update(3L, new UserEntity(
1L, testName, testPassword, testRole, null));
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(newEntity, userService.get(3L));
Assertions.assertEquals(testName, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
Assertions.assertNotEquals(testPassword, newEntity.getPassword());
Assertions.assertEquals(oldPassword, newEntity.getPassword());
Assertions.assertNotEquals(testRole, newEntity.getRole());
Assertions.assertEquals(oldRole, newEntity.getRole());
Assertions.assertNotEquals(testId, user.getId());
Assertions.assertEquals(testName, user.getName());
Assertions.assertNotEquals(testPassword, user.getPassword());
Assertions.assertNotEquals(testRole, user.getRole());
Assertions.assertEquals(0, user.getBooks().size());
}
@Test
@Order(3)
void deleteTest() {
userService.delete(3L);
userService.delete(user.getId());
Assertions.assertEquals(2, userService.getAll().size());
final UserEntity last = userService.get(2L);
Assertions.assertEquals(2L, last.getId());
final UserEntity newEntity = userService.create(new UserEntity(null,
"user4", "123edrf456", "user", null));
user.getName(), user.getPassword(), null, null));
Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
Assertions.assertNotEquals(user.getId(), newEntity.getId());
}
@Test
@Order(4)
void changePasswordTest() {
String oldPassword = "123edrf456";
String newPassword = "123edrtghyuji098";
var user = userService.create(new UserEntity(
null, "user5", oldPassword, "user", null));
String newPassword = user.getPassword() + "TEST";
userService.changePassword(user.getId(), newPassword);
Assertions.assertEquals(newPassword, user.getPassword());
Assertions.assertNotEquals(oldPassword, user.getPassword());
}
@Test
@Order(5)
void changeRoleTest() {
var user = userService.create(new UserEntity(
null, "user6", "Password", null, null));
Assertions.assertEquals("user", user.getRole());
userService.giveAdminRole(user.getId());
Assertions.assertEquals("admin", user.getRole());
userService.giveUserRole(user.getId());
Assertions.assertEquals("user", user.getRole());
}
@Test
@Order(6)
void booksTest() {
var user = userService.create(new UserEntity(
null, "user7", "Password", null, null));
Assertions.assertEquals(0, user.getBooks().size());
var type1 = typeService.create(new TypeEntity(null, "type1"));
var author1 = authorService.create(new AuthorEntity(null, "author1"));
var book1 = bookService.create(new BookEntity(null, "book1", type1, author1));
var book2 = bookService.create(new BookEntity(null, "book2", type1, author1));
userService.addBook(user.getId(), book1.getId());
userService.addBook(user.getId(), book2.getId());
Assertions.assertEquals(0, 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());
}
}