lab-3 add consyraints testing + some minor fixes

This commit is contained in:
Zakharov_Rostislav 2024-04-17 14:27:47 +04:00
parent c71d75685b
commit 63d63d3b39
7 changed files with 179 additions and 20 deletions

View File

@ -41,26 +41,45 @@ public class LibraryApplication implements CommandLineRunner {
@Override @Override
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("type1")); // final var type1 = typeService.create(new TypeEntity("type1"));
final var type2 = typeService.create(new TypeEntity("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("author1")); // final var author1 = authorService.create(new AuthorEntity("author1"));
final var author2 = authorService.create(new AuthorEntity("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("book1", type1, author1)); // final var book1 = bookService.create(new BookEntity("book1", type1, author1));
final var book2 = bookService.create(new BookEntity("book2", type1, author2)); // final var book2 = bookService.create(new BookEntity("book2", type1, author2));
final var book3 = bookService.create(new BookEntity("book3", type2, author1)); // final var book3 = bookService.create(new BookEntity("book3", type2, author1));
final var book4 = bookService.create(new BookEntity("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("user1", "123")); // final var user1 = userService.create(new UserEntity("user1", "123"));
final var user2 = userService.create(new UserEntity("user2", "123")); // final var user2 = userService.create(new UserEntity("user2", "123"));
final var admin1 = userService.create(new UserEntity("admin1", "123")); // final var admin1 = userService.create(new UserEntity("admin1", "123"));
userService.giveAdminRole(admin1.getId()); // 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());
} }
} }

View File

@ -50,8 +50,8 @@ public class BookController {
@GetMapping @GetMapping
public List<BookDto> getAll( public List<BookDto> getAll(
@RequestParam(name = "typeId", defaultValue = "0") Long typeId, @RequestParam(name = "typeId", defaultValue = "-1") Long typeId,
@RequestParam(name = "authorId", defaultValue = "0") Long authorId, @RequestParam(name = "authorId", defaultValue = "-1") Long authorId,
@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) { @RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
return itemService.getAll(typeId, authorId).stream().map(this::toDto).toList(); return itemService.getAll(typeId, authorId).stream().map(this::toDto).toList();

View File

@ -44,6 +44,10 @@ public class UserEntity extends BaseEntity {
this.password = password; this.password = password;
} }
public UserEntity(String login) {
this.login = login;
}
public String getLogin() { public String getLogin() {
return login; return login;
} }

View File

@ -6,6 +6,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import com.ip.library.core.error.NotFoundException; import com.ip.library.core.error.NotFoundException;
import com.ip.library.authors.model.AuthorEntity; import com.ip.library.authors.model.AuthorEntity;
@ -60,4 +61,28 @@ class AuthorsTests {
Assertions.assertEquals(3, authorService.getAll().size()); Assertions.assertEquals(3, authorService.getAll().size());
Assertions.assertNotEquals(entity.getId(), newEntity.getId()); 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()))
);
}
} }

View File

@ -6,6 +6,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import com.ip.library.core.error.NotFoundException; import com.ip.library.core.error.NotFoundException;
import com.ip.library.types.model.TypeEntity; import com.ip.library.types.model.TypeEntity;
@ -85,4 +86,52 @@ class BooksTests {
Assertions.assertEquals(3, bookService.getAll().size()); Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertNotEquals(book.getId(), newEntity.getId()); 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()))
);
}
} }

View File

@ -6,6 +6,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import com.ip.library.core.error.NotFoundException; import com.ip.library.core.error.NotFoundException;
import com.ip.library.types.model.TypeEntity; import com.ip.library.types.model.TypeEntity;
@ -62,4 +63,28 @@ class TypesTests {
Assertions.assertEquals(3, typeService.getAll().size()); Assertions.assertEquals(3, typeService.getAll().size());
Assertions.assertNotEquals(entity.getId(), newEntity.getId()); 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()))
);
}
} }

View File

@ -6,6 +6,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import com.ip.library.core.error.NotFoundException; import com.ip.library.core.error.NotFoundException;
import com.ip.library.users.model.UserEntity; import com.ip.library.users.model.UserEntity;
@ -84,4 +85,40 @@ class UsersTests {
user = userService.giveUserRole(user.getId()); user = userService.giveUserRole(user.getId());
Assertions.assertEquals("user", user.getRole()); 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()))
);
}
} }