From 4db2006afd40cbd3c5de99566582a16a78ea51fd Mon Sep 17 00:00:00 2001 From: Sergio Visinoni Date: Fri, 30 Jan 2026 01:46:14 +0100 Subject: [PATCH] test(API): add missing tests for BookMapperV2 (#2495) * fix(API): add missing fields to BookMapperV2 This fixes a regression introduced in the data model refactoring work for multi-format support. BookMapperV2 was missing mapping for file-related fields. * Added missing mappings for BookMapperV2 * Added a test for BookMapperV2 to prevent future regressions * fix(api): fix the code after rebase --- .../booklore/mapper/v2/BookMapperV2Test.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 booklore-api/src/test/java/com/adityachandel/booklore/mapper/v2/BookMapperV2Test.java diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/mapper/v2/BookMapperV2Test.java b/booklore-api/src/test/java/com/adityachandel/booklore/mapper/v2/BookMapperV2Test.java new file mode 100644 index 000000000..cd50eb332 --- /dev/null +++ b/booklore-api/src/test/java/com/adityachandel/booklore/mapper/v2/BookMapperV2Test.java @@ -0,0 +1,74 @@ +package com.adityachandel.booklore.mapper.v2; + +import com.adityachandel.booklore.model.dto.Book; +import com.adityachandel.booklore.model.entity.BookFileEntity; +import com.adityachandel.booklore.model.entity.BookEntity; +import com.adityachandel.booklore.model.entity.LibraryEntity; +import com.adityachandel.booklore.model.entity.LibraryPathEntity; +import com.adityachandel.booklore.model.enums.BookFileType; +import org.junit.jupiter.api.Test; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class BookMapperV2Test { + + private final BookMapperV2 mapper = Mappers.getMapper(BookMapperV2.class); + + @Test + void shouldMapFilepathFieldsCorrectly() { + LibraryEntity library = new LibraryEntity(); + library.setId(123L); + library.setName("Test Library"); + + LibraryPathEntity libraryPath = new LibraryPathEntity(); + libraryPath.setId(1L); + libraryPath.setPath("/tmp/test-library"); + + BookEntity entity = new BookEntity(); + entity.setId(1L); + + BookFileEntity primaryFile = new BookFileEntity(); + primaryFile.setBook(entity); + primaryFile.setFileName("test-book.epub"); + primaryFile.setFileSubPath("fiction/science-fiction"); + primaryFile.setBookFormat(true); + primaryFile.setBookType(BookFileType.EPUB); + primaryFile.setFileSizeKb(1024L); + + BookFileEntity supplementaryFile = new BookFileEntity(); + supplementaryFile.setBook(entity); + supplementaryFile.setFileName("test-book-cover.jpg"); + supplementaryFile.setFileSubPath("fiction/science-fiction"); + supplementaryFile.setBookFormat(false); + supplementaryFile.setBookType(BookFileType.EPUB); + supplementaryFile.setFileSizeKb(256L); + + entity.setBookFiles(List.of(primaryFile, supplementaryFile)); + entity.setLibrary(library); + entity.setLibraryPath(libraryPath); + + Book dto = mapper.toDTO(entity); + + assertThat(dto).isNotNull(); + assertThat(dto.getId()).isEqualTo(1L); + assertThat(dto.getLibraryId()).isEqualTo(123L); + assertThat(dto.getLibraryName()).isEqualTo("Test Library"); + assertThat(dto.getLibraryPath()).isNotNull(); + assertThat(dto.getLibraryPath().getId()).isEqualTo(1L); + + assertThat(dto.getPrimaryFile()).isNotNull(); + assertThat(dto.getPrimaryFile().getBookType()).isEqualTo(BookFileType.EPUB); + assertThat(dto.getPrimaryFile().getFileName()).isEqualTo("test-book.epub"); + assertThat(dto.getPrimaryFile().getFileSubPath()).isEqualTo("fiction/science-fiction"); + assertThat(dto.getPrimaryFile().getFileSizeKb()).isEqualTo(1024L); + assertThat(dto.getPrimaryFile().getFilePath()).isNotNull(); + assertThat(dto.getAlternativeFormats()).isEmpty(); + + assertThat(dto.getSupplementaryFiles()).hasSize(1); + assertThat(dto.getSupplementaryFiles().get(0).getFileName()).isEqualTo("test-book-cover.jpg"); + assertThat(dto.getSupplementaryFiles().get(0).isBook()).isFalse(); + } +}