Chore(api): Migrate BookCreatorServiceTests to AssertJ and remove unnessesary tests (#229)
* chore(apiTests): migrate BookCreatorServiceTest assertions to AssertJ - Replaced JUnit assertions with AssertJ for richer failure messages - Tightened collection assertions to verify element values rather than just size - Removed redundant addAuthorsToBook_nullExistingAuthorsOnEntity_initializesSet test - Added coverage-gap comment noting untested methods and missing cases * chore(apiTests): reduce test complexity in BookCreatorServiceTests - Changed to using parameterized tests where tests were the same apart from inputs - Modified tests to be more specific about what exactly that test is checking
This commit is contained in:
@@ -10,21 +10,33 @@ import org.booklore.repository.*;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/* BookCreatorService has the following functions that are not tested and should be added in the future
|
||||
- createShellBook()
|
||||
- saveConnections()
|
||||
- setComicMetadataDto()
|
||||
- populateComicMetadataRelationships()
|
||||
- addCreatorsWithRole()
|
||||
- truncate()
|
||||
|
||||
Gaps in existing coverage:
|
||||
- addCategoriesToBook, addMoodsToBook, addTagsToBook: missing "not found in repo → save and add" cases (covered for authors only)*/
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class BookCreatorServiceTest {
|
||||
|
||||
@@ -51,19 +63,12 @@ class BookCreatorServiceTest {
|
||||
bookEntity = BookEntity.builder().metadata(metadata).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAuthorsToBook_nullAuthors_doesNothing() {
|
||||
bookCreatorService.addAuthorsToBook(null, bookEntity);
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
void addAuthorsToBook_givenNullOrEmptySet_doesNothing(Set<String> authors) {
|
||||
bookCreatorService.addAuthorsToBook(authors, bookEntity);
|
||||
|
||||
assertNull(bookEntity.getMetadata().getAuthors());
|
||||
verifyNoInteractions(authorRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAuthorsToBook_emptyAuthors_doesNothing() {
|
||||
bookCreatorService.addAuthorsToBook(Set.of(), bookEntity);
|
||||
|
||||
assertNull(bookEntity.getMetadata().getAuthors());
|
||||
assertThat(bookEntity.getMetadata().getAuthors()).isNull();
|
||||
verifyNoInteractions(authorRepository);
|
||||
}
|
||||
|
||||
@@ -74,36 +79,17 @@ class BookCreatorServiceTest {
|
||||
|
||||
bookCreatorService.addAuthorsToBook(Set.of("Test Author"), bookEntity);
|
||||
|
||||
assertNotNull(bookEntity.getMetadata().getAuthors());
|
||||
assertEquals(1, bookEntity.getMetadata().getAuthors().size());
|
||||
assertThat(bookEntity.getMetadata().getAuthors())
|
||||
.extracting(AuthorEntity::getName)
|
||||
.containsExactly("Test Author");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAuthorsToBook_nullExistingAuthorsOnEntity_initializesSet() {
|
||||
assertNull(bookEntity.getMetadata().getAuthors());
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
void addCategoriesToBook_givenNullOrEmptySet_doesNothing(Set<String> categories) {
|
||||
bookCreatorService.addCategoriesToBook(categories, bookEntity);
|
||||
|
||||
AuthorEntity author = AuthorEntity.builder().name("Author").build();
|
||||
when(authorRepository.findByName("Author")).thenReturn(Optional.of(author));
|
||||
|
||||
bookCreatorService.addAuthorsToBook(Set.of("Author"), bookEntity);
|
||||
|
||||
assertNotNull(bookEntity.getMetadata().getAuthors());
|
||||
assertTrue(bookEntity.getMetadata().getAuthors().contains(author));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addCategoriesToBook_nullCategories_doesNothing() {
|
||||
bookCreatorService.addCategoriesToBook(null, bookEntity);
|
||||
|
||||
assertNull(bookEntity.getMetadata().getCategories());
|
||||
verifyNoInteractions(categoryRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addCategoriesToBook_emptyCategories_doesNothing() {
|
||||
bookCreatorService.addCategoriesToBook(Set.of(), bookEntity);
|
||||
|
||||
assertNull(bookEntity.getMetadata().getCategories());
|
||||
assertThat(bookEntity.getMetadata().getCategories()).isNull();
|
||||
verifyNoInteractions(categoryRepository);
|
||||
}
|
||||
|
||||
@@ -114,23 +100,17 @@ class BookCreatorServiceTest {
|
||||
|
||||
bookCreatorService.addCategoriesToBook(Set.of("Fiction"), bookEntity);
|
||||
|
||||
assertNotNull(bookEntity.getMetadata().getCategories());
|
||||
assertEquals(1, bookEntity.getMetadata().getCategories().size());
|
||||
assertThat(bookEntity.getMetadata().getCategories())
|
||||
.extracting(CategoryEntity::getName)
|
||||
.containsExactly("Fiction");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addMoodsToBook_nullMoods_doesNothing() {
|
||||
bookCreatorService.addMoodsToBook(null, bookEntity);
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
void addMoodsToBook_givenNullOrEmptySet_doesNothing(Set<String> moods) {
|
||||
bookCreatorService.addMoodsToBook(moods, bookEntity);
|
||||
|
||||
assertNull(bookEntity.getMetadata().getMoods());
|
||||
verifyNoInteractions(moodRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addMoodsToBook_emptyMoods_doesNothing() {
|
||||
bookCreatorService.addMoodsToBook(Set.of(), bookEntity);
|
||||
|
||||
assertNull(bookEntity.getMetadata().getMoods());
|
||||
assertThat(bookEntity.getMetadata().getMoods()).isNull();
|
||||
verifyNoInteractions(moodRepository);
|
||||
}
|
||||
|
||||
@@ -141,23 +121,17 @@ class BookCreatorServiceTest {
|
||||
|
||||
bookCreatorService.addMoodsToBook(Set.of("Dark"), bookEntity);
|
||||
|
||||
assertNotNull(bookEntity.getMetadata().getMoods());
|
||||
assertEquals(1, bookEntity.getMetadata().getMoods().size());
|
||||
assertThat(bookEntity.getMetadata().getMoods())
|
||||
.extracting(MoodEntity::getName)
|
||||
.containsExactly("Dark");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addTagsToBook_nullTags_doesNothing() {
|
||||
bookCreatorService.addTagsToBook(null, bookEntity);
|
||||
@ParameterizedTest
|
||||
@NullAndEmptySource
|
||||
void addTagsToBook_givenNullOrEmptySet_doesNothing(Set<String> tags) {
|
||||
bookCreatorService.addTagsToBook(tags, bookEntity);
|
||||
|
||||
assertNull(bookEntity.getMetadata().getTags());
|
||||
verifyNoInteractions(tagRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addTagsToBook_emptyTags_doesNothing() {
|
||||
bookCreatorService.addTagsToBook(Set.of(), bookEntity);
|
||||
|
||||
assertNull(bookEntity.getMetadata().getTags());
|
||||
assertThat(bookEntity.getMetadata().getTags()).isNull();
|
||||
verifyNoInteractions(tagRepository);
|
||||
}
|
||||
|
||||
@@ -168,8 +142,9 @@ class BookCreatorServiceTest {
|
||||
|
||||
bookCreatorService.addTagsToBook(Set.of("favorite"), bookEntity);
|
||||
|
||||
assertNotNull(bookEntity.getMetadata().getTags());
|
||||
assertEquals(1, bookEntity.getMetadata().getTags().size());
|
||||
assertThat(bookEntity.getMetadata().getTags())
|
||||
.extracting(TagEntity::getName)
|
||||
.containsExactly("favorite");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -182,18 +157,25 @@ class BookCreatorServiceTest {
|
||||
|
||||
bookCreatorService.addAuthorsToBook(Set.of("New Author"), bookEntity);
|
||||
|
||||
assertEquals(2, bookEntity.getMetadata().getAuthors().size());
|
||||
assertThat(bookEntity.getMetadata().getAuthors())
|
||||
.isNotNull()
|
||||
.extracting(AuthorEntity::getName)
|
||||
.contains("Existing", "New Author");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAuthorsToBook_newAuthorNotInRepo_savesAndAdds() {
|
||||
AuthorEntity saved = AuthorEntity.builder().name("Brand New").build();
|
||||
when(authorRepository.findByName("Brand New")).thenReturn(Optional.empty());
|
||||
when(authorRepository.save(any())).thenReturn(saved);
|
||||
when(authorRepository.save(any(AuthorEntity.class))).thenReturn(saved);
|
||||
|
||||
bookCreatorService.addAuthorsToBook(Set.of("Brand New"), bookEntity);
|
||||
|
||||
verify(authorRepository).save(any());
|
||||
assertEquals(1, bookEntity.getMetadata().getAuthors().size());
|
||||
verify(authorRepository).save(argThat(author -> "Brand New".equals(author.getName())));
|
||||
|
||||
assertThat(bookEntity.getMetadata().getAuthors())
|
||||
.isNotNull()
|
||||
.extracting(AuthorEntity::getName)
|
||||
.containsExactly("Brand New");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user