lab-3 add consyraints testing + some minor fixes
This commit is contained in:
parent
c71d75685b
commit
63d63d3b39
@ -41,26 +41,45 @@ public class LibraryApplication implements CommandLineRunner {
|
||||
|
||||
@Override
|
||||
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("type1"));
|
||||
final var type2 = typeService.create(new TypeEntity("type2"));
|
||||
// if (args.length > 0 && args[0].equals("--populate")) {
|
||||
// log.info("Create default types values");
|
||||
// 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("author1"));
|
||||
final var author2 = authorService.create(new AuthorEntity("author2"));
|
||||
// log.info("Create default authors values");
|
||||
// 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("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 books values");
|
||||
// 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("user1", "123"));
|
||||
final var user2 = userService.create(new UserEntity("user2", "123"));
|
||||
final var admin1 = userService.create(new UserEntity("admin1", "123"));
|
||||
userService.giveAdminRole(admin1.getId());
|
||||
}
|
||||
// log.info("Create default users values");
|
||||
// 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());
|
||||
// }
|
||||
log.info("Create default types values");
|
||||
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("author1"));
|
||||
final var author2 = authorService.create(new AuthorEntity("author2"));
|
||||
|
||||
log.info("Create default books values");
|
||||
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("user1", "123"));
|
||||
final var user2 = userService.create(new UserEntity("user2", "123"));
|
||||
final var admin1 = userService.create(new UserEntity("admin1", "123"));
|
||||
userService.giveAdminRole(admin1.getId());
|
||||
}
|
||||
}
|
||||
|
@ -50,8 +50,8 @@ public class BookController {
|
||||
|
||||
@GetMapping
|
||||
public List<BookDto> getAll(
|
||||
@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
|
||||
@RequestParam(name = "authorId", defaultValue = "0") Long authorId,
|
||||
@RequestParam(name = "typeId", defaultValue = "-1") Long typeId,
|
||||
@RequestParam(name = "authorId", defaultValue = "-1") Long authorId,
|
||||
@RequestParam(name = "page", defaultValue = "0") int page,
|
||||
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
|
||||
return itemService.getAll(typeId, authorId).stream().map(this::toDto).toList();
|
||||
|
@ -44,6 +44,10 @@ public class UserEntity extends BaseEntity {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public UserEntity(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ 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 org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.authors.model.AuthorEntity;
|
||||
@ -60,4 +61,28 @@ class AuthorsTests {
|
||||
Assertions.assertEquals(3, authorService.getAll().size());
|
||||
Assertions.assertNotEquals(entity.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullNameTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> authorService.create(new AuthorEntity(null))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> authorService.update(entity.getId(), new AuthorEntity(null))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uniqueNameTest() {
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> authorService.create(new AuthorEntity(entity.getName()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> authorService.update(entity.getId(), new AuthorEntity(entity.getName()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ 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;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
@ -85,4 +86,52 @@ class BooksTests {
|
||||
Assertions.assertEquals(3, bookService.getAll().size());
|
||||
Assertions.assertNotEquals(book.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullNameTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.create(new BookEntity(null, book.getType(), book.getAuthor()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.update(book.getId(), new BookEntity(null, book.getType(), book.getAuthor()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullTypeTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.create(new BookEntity(book.getName() + "TEST", null, book.getAuthor()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.update(book.getId(), new BookEntity(book.getName() + "TEST", null, book.getAuthor()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullAuthorTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.create(new BookEntity(book.getName() + "TEST", book.getType(), null))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> bookService.update(book.getId(), new BookEntity(book.getName() + "TEST", book.getType(), null))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uniqueNameTest() {
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> bookService.create(new BookEntity(book.getName(), book.getType(), book.getAuthor()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> bookService.update(book.getId(), new BookEntity(book.getName(), book.getType(), book.getAuthor()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ 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 org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.types.model.TypeEntity;
|
||||
@ -62,4 +63,28 @@ class TypesTests {
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertNotEquals(entity.getId(), newEntity.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullNameTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> typeService.create(new TypeEntity(null))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> typeService.update(entity.getId(), new TypeEntity(null))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uniqueNameTest() {
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> typeService.create(new TypeEntity(entity.getName()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> typeService.update(entity.getId(), new TypeEntity(entity.getName()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ 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 org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import com.ip.library.core.error.NotFoundException;
|
||||
import com.ip.library.users.model.UserEntity;
|
||||
@ -84,4 +85,40 @@ class UsersTests {
|
||||
user = userService.giveUserRole(user.getId());
|
||||
Assertions.assertEquals("user", user.getRole());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullLoginTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> userService.create(new UserEntity(null, user.getPassword()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> userService.update(user.getId(), new UserEntity(null))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullPasswordTest() {
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> userService.create(new UserEntity(user.getLogin() + "TEST", null))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
DataIntegrityViolationException.class,
|
||||
() -> userService.changePassword(user.getId(), null)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uniqueLoginTest() {
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> userService.create(new UserEntity(user.getLogin(), user.getPassword()))
|
||||
);
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> userService.update(user.getId(), new UserEntity(user.getLogin()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user