lab2 added booktests

This commit is contained in:
Zakharov_Rostislav 2024-03-20 17:14:42 +04:00
parent 7f502d2548
commit fa99474b5e
2 changed files with 67 additions and 1 deletions

View File

@ -33,6 +33,10 @@ public class BookService {
return result;
}
public List<BookEntity> getAll() {
return repository.getAll();
}
public BookEntity get(Long id) {
return Optional.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));

View File

@ -1,13 +1,75 @@
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.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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.authors.model.AuthorEntity;
import com.ip.library.authors.service.AuthorService;
import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class LibraryApplicationTests {
@Autowired
private BookService bookService;
@Autowired
private TypeService typeService;
@Autowired
private AuthorService authorService;
@Test
void contextLoads() {
void getTest() {
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 BookEntity newEntity = bookService.update(3L, new BookEntity(1L, test, entity.getType(), entity.getAuthor()));
Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals(newEntity, bookService.get(3L));
Assertions.assertEquals(test, newEntity.getName());
Assertions.assertNotEquals(oldName, newEntity.getName());
}
@Test
@Order(3)
void deleteTest() {
bookService.delete(3L);
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));
Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals(4L, newEntity.getId());
}
}