refactor(api): modernize codebase with Java 21 idioms and improve null safety (#2106)

* fix(controller): handle null filename in getBackgroundImage method

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* refactor(tests): replace hardcoded replace mode with variable in BookMetadataUpdaterCategoryTest

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2026-01-02 10:16:01 -07:00
committed by GitHub
parent 5a251d182a
commit b3ac58207a
32 changed files with 177 additions and 171 deletions
@@ -0,0 +1,37 @@
package com.adityachandel.booklore.controller;
import com.adityachandel.booklore.service.book.BookService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class BookMediaControllerTest {
@Mock
private BookService bookService;
@InjectMocks
private BookMediaController bookMediaController;
@Test
void getBackgroundImage_shouldReturnBadRequestWhenFilenameIsNull() {
Resource resource = mock(Resource.class);
when(bookService.getBackgroundImage()).thenReturn(resource);
when(resource.exists()).thenReturn(true);
when(resource.getFilename()).thenReturn(null);
ResponseEntity<Resource> response = bookMediaController.getBackgroundImage();
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
}
@@ -18,7 +18,7 @@ class BookLoreUserEntityTest {
assertThat(user.isDefaultPassword()).isFalse();
BookLoreUserEntity defaultUser = BookLoreUserEntity.builder().build();
BookLoreUserEntity defaultUser = BookLoreUserEntity.builder().isDefaultPassword(false).build();
assertThat(defaultUser.isDefaultPassword()).isFalse();
}
}
@@ -57,8 +57,8 @@ class BookMarkServiceTest {
@BeforeEach
void setUp() {
userDto = BookLoreUser.builder().id(userId).build();
userEntity = BookLoreUserEntity.builder().id(userId).build();
userDto = BookLoreUser.builder().id(userId).isDefaultPassword(false).build();
userEntity = BookLoreUserEntity.builder().id(userId).isDefaultPassword(false).build();
bookEntity = BookEntity.builder().id(bookId).build();
bookmarkEntity = BookMarkEntity.builder().id(bookmarkId).user(userEntity).book(bookEntity).cfi("cfi").title("title").version(1L).build();
bookmarkDto = BookMark.builder().id(bookmarkId).bookId(bookId).cfi("cfi").title("title").build();
@@ -74,7 +74,7 @@ class BookMarkServiceTest {
assertNotNull(result);
assertEquals(1, result.size());
assertEquals(bookmarkId, result.get(0).getId());
assertEquals(bookmarkId, result.getFirst().getId());
verify(bookMarkRepository).findByBookIdAndUserIdOrderByPriorityAscCreatedAtDesc(bookId, userId);
}
@@ -72,7 +72,7 @@ class KoboStatusSyncProtectionTest {
@BeforeEach
void setUp() {
testUser = BookLoreUser.builder().id(1L).username("testuser").build();
testUser = BookLoreUser.builder().id(1L).username("testuser").isDefaultPassword(false).build();
testUserEntity = new BookLoreUserEntity();
testUserEntity.setId(1L);
testBook = new BookEntity();
@@ -69,7 +69,7 @@ class BookServiceTest {
.id(1L)
.permissions(perms)
.assignedLibraries(List.of())
.build();
.isDefaultPassword(false).build();
}
@Test
@@ -106,7 +106,7 @@ class BookServiceTest {
List<Book> result = bookService.getBooksByIds(Set.of(2L), false);
assertEquals(1, result.size());
assertEquals(2L, result.get(0).getId());
assertEquals(2L, result.getFirst().getId());
}
}
@@ -236,7 +236,7 @@ class BookUpdateServiceTest {
verify(userBookProgressRepository).bulkUpdateReadStatus(eq(1L), eq(new ArrayList<>(existing)), eq(ReadStatus.READ), any(), any());
verify(userBookProgressRepository).saveAll(anyList());
assertEquals(3, result.size());
assertEquals(ReadStatus.READ, result.get(0).getReadStatus());
assertEquals(ReadStatus.READ, result.getFirst().getReadStatus());
}
@Test
@@ -348,7 +348,7 @@ class BookUpdateServiceTest {
verify(userBookProgressRepository).bulkUpdatePersonalRating(eq(1L), eq(new ArrayList<>(existing)), eq(5));
verify(userBookProgressRepository).saveAll(anyList());
assertEquals(3, result.size());
assertEquals(5, result.get(0).getPersonalRating());
assertEquals(5, result.getFirst().getPersonalRating());
}
@Test
@@ -365,7 +365,7 @@ class BookUpdateServiceTest {
List<PersonalRatingUpdateResponse> result = bookUpdateService.resetPersonalRating(bookIds);
verify(userBookProgressRepository).bulkUpdatePersonalRating(eq(1L), eq(new ArrayList<>(bookIds)), isNull());
assertEquals(2, result.size());
assertNull(result.get(0).getPersonalRating());
assertNull(result.getFirst().getPersonalRating());
}
@Test
@@ -19,6 +19,7 @@ import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
@@ -239,12 +240,12 @@ class CbxConversionServiceTest {
try (ZipFile zipFile = ZipFile.builder().setFile(epubFile).get()) {
List<ZipArchiveEntry> imageEntries = Collections.list(zipFile.getEntries()).stream()
.filter(entry -> entry.getName().startsWith("OEBPS/Images/page-"))
.sorted((e1, e2) -> e1.getName().compareTo(e2.getName()))
.sorted(Comparator.comparing(ZipArchiveEntry::getName))
.toList();
List<ZipArchiveEntry> htmlEntries = Collections.list(zipFile.getEntries()).stream()
.filter(entry -> entry.getName().startsWith("OEBPS/Text/page-"))
.sorted((e1, e2) -> e1.getName().compareTo(e2.getName()))
.sorted(Comparator.comparing(ZipArchiveEntry::getName))
.toList();
assertThat(imageEntries).hasSize(expectedPageCount);
@@ -54,11 +54,11 @@ class KoboAutoShelfServiceTest {
testUser1 = BookLoreUserEntity.builder()
.id(100L)
.build();
.isDefaultPassword(false).build();
testUser2 = BookLoreUserEntity.builder()
.id(200L)
.build();
.isDefaultPassword(false).build();
koboShelf1 = ShelfEntity.builder()
.id(10L)
@@ -61,8 +61,8 @@ class KoboLibrarySnapshotServiceTest {
@BeforeEach
void setUp() {
owner = BookLoreUserEntity.builder().id(1L).build();
otherUser = BookLoreUserEntity.builder().id(2L).build();
owner = BookLoreUserEntity.builder().id(1L).isDefaultPassword(false).build();
otherUser = BookLoreUserEntity.builder().id(2L).isDefaultPassword(false).build();
LibraryEntity ownersLibrary = LibraryEntity.builder()
.users(List.of(owner))
@@ -39,7 +39,7 @@ class KoboSettingsServiceTest {
@BeforeEach
void setUp() {
user = BookLoreUser.builder().id(1L).build();
user = BookLoreUser.builder().id(1L).isDefaultPassword(false).build();
settingsEntity = KoboUserSettingsEntity.builder()
.id(10L)
.userId(1L)
@@ -23,8 +23,6 @@ import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@@ -112,7 +110,7 @@ class LibraryProcessingServiceTest {
List<LibraryFile> processedFiles = captor.getValue();
assertThat(processedFiles).hasSize(1);
assertThat(processedFiles.get(0).getFileName()).isEqualTo("book2.epub");
assertThat(processedFiles.getFirst().getFileName()).isEqualTo("book2.epub");
}
@Test
@@ -227,8 +225,8 @@ class LibraryProcessingServiceTest {
List<LibraryFile> processedFiles = captor.getValue();
assertThat(processedFiles).hasSize(1);
assertThat(processedFiles.get(0).getFileName()).isEqualTo("book1.epub");
assertThat(processedFiles.get(0).getFileSubPath()).isEqualTo("fantasy");
assertThat(processedFiles.getFirst().getFileName()).isEqualTo("book1.epub");
assertThat(processedFiles.getFirst().getFileSubPath()).isEqualTo("fantasy");
}
@Test
@@ -179,7 +179,6 @@ class BookMetadataUpdaterTest {
MetadataUpdateContext context = MetadataUpdateContext.builder()
.bookEntity(bookEntity)
.metadataUpdateWrapper(wrapper)
.mergeMoods(false)
.replaceMode(MetadataReplaceMode.REPLACE_ALL)
.build();
@@ -215,7 +214,6 @@ class BookMetadataUpdaterTest {
MetadataUpdateContext context = MetadataUpdateContext.builder()
.bookEntity(bookEntity)
.metadataUpdateWrapper(wrapper)
.mergeMoods(false)
.replaceMode(MetadataReplaceMode.REPLACE_ALL)
.build();
@@ -81,9 +81,9 @@ class MetadataMatchServiceTest {
@Test
void calculateMatchScore_shouldScoreLockedNullSeriesNumber() {
weights = MetadataMatchWeights.builder()
.title(10)
.seriesNumber(5)
.build();
.title(10)
.seriesNumber(5)
.build();
// Total 15
@@ -147,7 +147,7 @@ class LubimyCzytacParserTest {
assertNotNull(results);
assertFalse(results.isEmpty(), "Should return results for real book");
BookMetadata firstResult = results.get(0);
BookMetadata firstResult = results.getFirst();
assertNotNull(firstResult.getTitle(), "Title should be present");
assertNotNull(firstResult.getLubimyczytacId(), "LubimyCzytac ID should be present");
assertTrue(firstResult.getAuthors() != null && !firstResult.getAuthors().isEmpty(),
@@ -120,9 +120,7 @@ class EpubMetadataWriterTest {
imageBytes
);
assertDoesNotThrow(() -> {
writer.replaceCoverImageFromUpload(bookEntity, coverFile);
});
assertDoesNotThrow(() -> writer.replaceCoverImageFromUpload(bookEntity, coverFile));
}
}
@@ -52,7 +52,7 @@ class CbxReaderServiceTest {
private Path cbzFile;
private Path cacheDir;
private BookEntity testBook;
private Long bookId = 113L;
private final Long bookId = 113L;
@BeforeEach
void setUp() throws IOException {
@@ -101,8 +101,8 @@ class CbxReaderServiceTest {
assertEquals(130, pages.size(),
"Page count should be 130 (actual comic pages), not 260 (including __MACOSX files)");
assertEquals(1, pages.get(0));
assertEquals(130, pages.get(pages.size() - 1));
assertEquals(1, pages.getFirst());
assertEquals(130, pages.getLast());
List<Path> cachedFiles = Files.list(cacheDir)
.filter(Files::isRegularFile)
@@ -153,7 +153,7 @@ class CbxReaderServiceTest {
.sorted()
.toList();
assertEquals("DW_4D_001.jpg", cachedFiles.get(0).getFileName().toString());
assertEquals("DW_4D_001.jpg", cachedFiles.getFirst().getFileName().toString());
assertEquals("DW_4D_130.jpg", cachedFiles.get(129).getFileName().toString());
}
@@ -59,7 +59,7 @@ class FileServiceTest {
.verticalCroppingEnabled(true)
.horizontalCroppingEnabled(true)
.aspectRatioThreshold(2.5)
.build();
.smartCroppingEnabled(true).build();
AppSettings appSettings = AppSettings.builder()
.coverCroppingSettings(coverCroppingSettings)
.build();
@@ -728,7 +728,7 @@ class FileServiceTest {
.verticalCroppingEnabled(false)
.horizontalCroppingEnabled(false)
.aspectRatioThreshold(2.5)
.build();
.smartCroppingEnabled(true).build();
AppSettings appSettings = AppSettings.builder()
.coverCroppingSettings(disabledSettings)
.build();
@@ -1151,7 +1151,7 @@ class FileServiceTest {
.verticalCroppingEnabled(true)
.horizontalCroppingEnabled(true)
.aspectRatioThreshold(2.5)
.build();
.smartCroppingEnabled(true).build();
AppSettings appSettings = AppSettings.builder()
.coverCroppingSettings(coverCroppingSettings)
.build();