lab-2 tests

This commit is contained in:
Zakharov_Rostislav 2024-03-29 14:04:34 +04:00
parent 5b19352e87
commit 69bbf08672
6 changed files with 169 additions and 20 deletions

View File

@ -49,16 +49,17 @@ public class LibraryApplication implements CommandLineRunner {
final var author1 = authorService.create(new AuthorEntity(null, "author1"));
final var author2 = authorService.create(new AuthorEntity(null, "author2"));
log.info("Create default users values");
final var user1 = userService.create(new UserEntity(null, "user1", "123", "user"));
final var user2 = userService.create(new UserEntity(null, "user2", "123", "user"));
final var admin1 = userService.create(new UserEntity(null, "admin1", "123", "admin"));
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));
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));
userService.giveAdminRole(admin1.getId());
}
}
}

View File

@ -61,4 +61,19 @@ public class UserController {
public UserDto delete(@PathVariable(name = "id") Long id) {
return toDto(userService.delete(id));
}
@PutMapping("password/{id}")
public UserDto changePassword(@PathVariable(name = "id") Long id, @RequestBody String newPassword) {
return toDto(userService.changePassword(id, newPassword));
}
@GetMapping("{id}/books/{bookId}")
public boolean addBook(@PathVariable(name = "id") Long id, @PathVariable(name = "bookId") Long bookId) {
return userService.addBook(id, bookId);
}
@DeleteMapping("{id}/books/{bookId}")
public boolean removeBook(@PathVariable(name = "id") Long id, @PathVariable(name = "bookId") Long bookId) {
return userService.removeBook(id, bookId);
}
}

View File

@ -1,8 +1,11 @@
package com.ip.library.users.api;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class UserDto {
private Long id;
@ -12,6 +15,8 @@ public class UserDto {
private String password;
@NotBlank
private String role;
@NotNull
private List<Long> books;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId() {
@ -30,7 +35,8 @@ public class UserDto {
this.name = name;
}
public String getPassword() {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public String getPassword() {
return password;
}
@ -46,4 +52,12 @@ public class UserDto {
public void setRole(String role) {
this.role = role;
}
public List<Long> getBooks() {
return books;
}
public void setBooks(List<Long> books) {
this.books = books;
}
}

View File

@ -1,23 +1,27 @@
package com.ip.library.users.model;
import java.util.List;
import java.util.Objects;
import com.ip.library.books.model.BookEntity;
import com.ip.library.core.model.BaseEntity;
public class UserEntity extends BaseEntity {
private String name;
private String password;
private String role;
private List<BookEntity> books;
public UserEntity() {
super();
}
public UserEntity(Long id, String name, String password, String role) {
public UserEntity(Long id, String name, String password, String role, List<BookEntity> books) {
super(id);
this.name = name;
this.password = password;
this.role = role;
this.books = books;
}
public String getName() {
@ -44,6 +48,14 @@ public class UserEntity extends BaseEntity {
this.role = role;
}
public List<BookEntity> getBooks() {
return books;
}
public void setBooks(List<BookEntity> books) {
this.books = books;
}
@Override
public int hashCode() {
return Objects.hash(id, name, password, role);
@ -59,6 +71,7 @@ public class UserEntity extends BaseEntity {
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getName(), name)
&& Objects.equals(other.getPassword(), password)
&& Objects.equals(other.getRole(), role);
&& Objects.equals(other.getRole(), role)
&& Objects.equals(other.getBooks(), books);
}
}

View File

@ -1,10 +1,13 @@
package com.ip.library.users.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService;
import com.ip.library.core.error.NotFoundException;
import com.ip.library.users.model.UserEntity;
import com.ip.library.users.repository.UserRepository;
@ -12,9 +15,11 @@ import com.ip.library.users.repository.UserRepository;
@Service
public class UserService {
private final UserRepository repository;
private final BookService bookService;
public UserService(UserRepository repository) {
public UserService(UserRepository repository, BookService bookService) {
this.repository = repository;
this.bookService = bookService;
}
public List<UserEntity> getAll() {
@ -27,16 +32,16 @@ public class UserService {
}
public UserEntity create(UserEntity entity) {
if (entity.getRole().equals("admin")) {
if (!entity.getRole().equals("user")) {
entity.setRole("user");
}
entity.setBooks(new ArrayList<BookEntity>());
return repository.create(entity);
}
public UserEntity update(Long id, UserEntity entity) {
final UserEntity existsEntity = get(id);
existsEntity.setName(entity.getName());
existsEntity.setPassword(entity.getPassword());
return repository.update(existsEntity);
}
@ -44,4 +49,34 @@ public class UserService {
final UserEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
public UserEntity giveAdminRole(Long id) {
final UserEntity existsEntity = get(id);
existsEntity.setRole("admin");
return repository.update(existsEntity);
}
public UserEntity giveUserRole(Long id) {
final UserEntity existsEntity = get(id);
existsEntity.setRole("user");
return repository.update(existsEntity);
}
public UserEntity changePassword(Long id, String newPassword) {
final UserEntity existsEntity = get(id);
existsEntity.setPassword(newPassword);
return repository.update(existsEntity);
}
public boolean addBook(Long id, Long bookId) {
final UserEntity existsEntity = get(id);
final BookEntity book = bookService.get(bookId);
return existsEntity.getBooks().add(book);
}
public boolean removeBook(Long id, Long bookId) {
final UserEntity existsEntity = get(id);
final BookEntity book = bookService.get(bookId);
return existsEntity.getBooks().remove(book);
}
}

View File

@ -1,5 +1,8 @@
package com.ip.library;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
@ -8,13 +11,25 @@ import org.junit.jupiter.api.TestMethodOrder;
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.core.error.NotFoundException;
import com.ip.library.types.model.TypeEntity;
import com.ip.library.types.service.TypeService;
import com.ip.library.users.model.UserEntity;
import com.ip.library.users.service.UserService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class UserServiceTests {
@Autowired
private BookService bookService;
@Autowired
private TypeService typeService;
@Autowired
private AuthorService authorService;
@Autowired
private UserService userService;
@ -26,12 +41,20 @@ class UserServiceTests {
@Test
@Order(1)
void createTest() {
userService.create(new UserEntity(null, "user1", "123", "user"));
userService.create(new UserEntity(null, "user2", "456", "admin"));
final UserEntity last = userService.create(new UserEntity(null, "user3", "aqw2sed45", "user"));
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", userService.get(2L).getRole());
Assertions.assertNotEquals("admin", last.getRole());
Assertions.assertEquals("user", last.getRole());
Assertions.assertEquals(0, last.getBooks().size());
}
@Test
@ -42,13 +65,18 @@ class UserServiceTests {
final String testName = oldName + "TEST";
final String oldPassword = entity.getPassword();
final String testPassword = oldPassword + "TEST";
final UserEntity newEntity = userService.update(3L, new UserEntity(1L, testName, testPassword, entity.getRole()));
final String oldRole = entity.getRole();
final String testRole = "admin";
final UserEntity newEntity = userService.update(3L, new UserEntity(
1L, testName, testPassword, testRole, null));
Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertEquals(newEntity, userService.get(3L));
Assertions.assertEquals(testName, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
Assertions.assertEquals(testPassword, newEntity.getPassword());
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
Assertions.assertNotEquals(testPassword, newEntity.getPassword());
Assertions.assertEquals(oldPassword, newEntity.getPassword());
Assertions.assertNotEquals(testRole, newEntity.getRole());
Assertions.assertEquals(oldRole, newEntity.getRole());
}
@Test
@ -58,9 +86,52 @@ class UserServiceTests {
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, "user3", "123edrf456", "user"));
final UserEntity newEntity = userService.create(new UserEntity(null,
"user4", "123edrf456", "user", null));
Assertions.assertEquals(3, userService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
}
@Test
@Order(4)
void changePasswordTest() {
String oldPassword = "123edrf456";
String newPassword = "123edrtghyuji098";
var user = userService.create(new UserEntity(
null, "user5", oldPassword, "user", null));
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());
}
}