Files
gameyfin/backend/src/test/java/de/grimsi/gameyfin/rest/LibraryControllerTest.java
T
Simon 757b7e63d2 Release 1.3.2 (#74)
* Fixes #71

* [GH-61] Fix manual mapping leading to duplicates in DB

* [GH-73] Fix Gameyfin only detecting PC games

* Improve game title matching (#77)

* Implement some filename suffix logic

Removes some common file suffixes from files downloaded from for example itch.io. Also removes trailing/leading whitespace/-/_/./()  and version numbers starting with a "v" like "v1.2.3".

* Add edge cases for game titles (#76)

* Fix SONAR code smells

Co-authored-by: tr7zw <tr7zw@live.de>
Co-authored-by: Pfuenzle <dark.leon64@gmail.com>

* Validate some combinations of filename with added suffixes (#79)

Also fixes a bug of not removing trailing empty [].

* Improve test coverage (#70)

* Implemented missing testcases for IgdbWrapper

Refactored getPlatformBySlug to return Optional<>

* Fixed SONAR findings

* Implemented integration tests for the DB

* Started implementing tests for controller

* Finished GamesControllerTest

* Added ImageControllerTest

* Implemented LibraryControllerTest

* Add LibraryManagementControllerTest

* Updated some dependencies

* Add DownloadServiceTest

* Introduced "gameyfin.data" property to specify a folder for both cache and DB.

De-facto removed "gameyfin.db" and "gameyfin.cache" properties

Refactored file-system code to be cleaner and easier to test

* Refactored filesystem code
Implemented FilesystemServiceTest

* Fix SONAR code smells

* Implemented GameServiceTest

* Implemented ImageServiceTest

* Fix website scroll position when clicking on game covers in the library view (#94)

Fixes #81

* Expansion panels are now not collapsing when last active filter is de-selected (#95)

Fixes #86

---------

Co-authored-by: tr7zw <tr7zw@live.de>
Co-authored-by: Pfuenzle <dark.leon64@gmail.com>
2023-02-05 01:25:11 +01:00

181 lines
8.8 KiB
Java

package de.grimsi.gameyfin.rest;
import de.grimsi.gameyfin.dto.ImageDownloadResultDto;
import de.grimsi.gameyfin.dto.LibraryScanRequestDto;
import de.grimsi.gameyfin.dto.LibraryScanResult;
import de.grimsi.gameyfin.dto.LibraryScanResultDto;
import de.grimsi.gameyfin.entities.Library;
import de.grimsi.gameyfin.service.ImageService;
import de.grimsi.gameyfin.service.LibraryService;
import org.jeasy.random.EasyRandom;
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 java.nio.file.Path;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class LibraryControllerTest {
@InjectMocks
private LibraryController target;
@Mock
private LibraryService libraryServiceMock;
@Mock
private ImageService imageServiceMock;
private final EasyRandom easyRandom = new EasyRandom();
@Test
void scanLibraries_All_NoImages() {
int libraryCount = 5;
LibraryScanRequestDto input = new LibraryScanRequestDto("", false);
List<Library> libraries = easyRandom.objects(Library.class, libraryCount).toList();
LibraryScanResult lsr = easyRandom.nextObject(LibraryScanResult.class);
when(libraryServiceMock.getLibraries()).thenReturn(libraries);
when(libraryServiceMock.scanGameLibrary(any(Library.class))).thenReturn(lsr);
LibraryScanResultDto result = target.scanLibraries(input);
verify(libraryServiceMock, times(1)).getLibraries();
verify(libraryServiceMock, never()).getLibrary(any(String.class));
verify(libraryServiceMock, times(libraryCount)).scanGameLibrary(any(Library.class));
verify(imageServiceMock, never()).downloadCompanyLogosFromIgdb();
verify(imageServiceMock, never()).downloadGameCoversFromIgdb();
verify(imageServiceMock, never()).downloadGameScreenshotsFromIgdb();
assertThat(result.getNewGames()).isEqualTo(lsr.getNewGames() * libraries.size());
assertThat(result.getDeletedGames()).isEqualTo(lsr.getDeletedGames() * libraries.size());
assertThat(result.getNewUnmappableFiles()).isEqualTo(lsr.getNewUnmappableFiles() * libraries.size());
assertThat(result.getTotalGames()).isEqualTo(lsr.getTotalGames() * libraries.size());
assertThat(result.getCoverDownloads()).isZero();
assertThat(result.getScreenshotDownloads()).isZero();
assertThat(result.getCompanyLogoDownloads()).isZero();
}
@Test
void scanLibraries_Single_NoImages() {
String libraryPath = "some/random/path";
LibraryScanRequestDto input = new LibraryScanRequestDto(libraryPath, false);
Library library = easyRandom.nextObject(Library.class);
LibraryScanResult lsr = easyRandom.nextObject(LibraryScanResult.class);
when(libraryServiceMock.getLibrary(libraryPath)).thenReturn(library);
when(libraryServiceMock.scanGameLibrary(any(Library.class))).thenReturn(lsr);
LibraryScanResultDto result = target.scanLibraries(input);
verify(libraryServiceMock, never()).getLibraries();
verify(libraryServiceMock, times(1)).getLibrary(libraryPath);
verify(libraryServiceMock, times(1)).scanGameLibrary(any(Library.class));
verify(imageServiceMock, never()).downloadCompanyLogosFromIgdb();
verify(imageServiceMock, never()).downloadGameCoversFromIgdb();
verify(imageServiceMock, never()).downloadGameScreenshotsFromIgdb();
assertThat(result.getNewGames()).isEqualTo(lsr.getNewGames());
assertThat(result.getDeletedGames()).isEqualTo(lsr.getDeletedGames());
assertThat(result.getNewUnmappableFiles()).isEqualTo(lsr.getNewUnmappableFiles());
assertThat(result.getTotalGames()).isEqualTo(lsr.getTotalGames());
assertThat(result.getCoverDownloads()).isZero();
assertThat(result.getScreenshotDownloads()).isZero();
assertThat(result.getCompanyLogoDownloads()).isZero();
}
@Test
void scanLibraries_All_DownloadImages() {
int libraryCount = 5;
LibraryScanRequestDto input = new LibraryScanRequestDto("", true);
List<Library> libraries = easyRandom.objects(Library.class, libraryCount).toList();
LibraryScanResult lsr = easyRandom.nextObject(LibraryScanResult.class);
when(libraryServiceMock.getLibraries()).thenReturn(libraries);
when(libraryServiceMock.scanGameLibrary(any(Library.class))).thenReturn(lsr);
when(imageServiceMock.downloadGameCoversFromIgdb()).thenReturn(1);
when(imageServiceMock.downloadGameScreenshotsFromIgdb()).thenReturn(1);
when(imageServiceMock.downloadCompanyLogosFromIgdb()).thenReturn(1);
LibraryScanResultDto result = target.scanLibraries(input);
verify(libraryServiceMock, times(1)).getLibraries();
verify(libraryServiceMock, never()).getLibrary(any(String.class));
verify(libraryServiceMock, times(libraryCount)).scanGameLibrary(any(Library.class));
verify(imageServiceMock, times(1)).downloadCompanyLogosFromIgdb();
verify(imageServiceMock, times(1)).downloadGameCoversFromIgdb();
verify(imageServiceMock, times(1)).downloadGameScreenshotsFromIgdb();
assertThat(result.getNewGames()).isEqualTo(lsr.getNewGames() * libraries.size());
assertThat(result.getDeletedGames()).isEqualTo(lsr.getDeletedGames() * libraries.size());
assertThat(result.getNewUnmappableFiles()).isEqualTo(lsr.getNewUnmappableFiles() * libraries.size());
assertThat(result.getTotalGames()).isEqualTo(lsr.getTotalGames() * libraries.size());
assertThat(result.getCoverDownloads()).isEqualTo(1);
assertThat(result.getScreenshotDownloads()).isEqualTo(1);
assertThat(result.getCompanyLogoDownloads()).isEqualTo(1);
}
@Test
void scanLibraries_Single_DownloadImages() {
String libraryPath = "some/random/path";
LibraryScanRequestDto input = new LibraryScanRequestDto(libraryPath, true);
Library library = easyRandom.nextObject(Library.class);
LibraryScanResult lsr = easyRandom.nextObject(LibraryScanResult.class);
when(libraryServiceMock.getLibrary(libraryPath)).thenReturn(library);
when(libraryServiceMock.scanGameLibrary(any(Library.class))).thenReturn(lsr);
when(imageServiceMock.downloadGameCoversFromIgdb()).thenReturn(1);
when(imageServiceMock.downloadGameScreenshotsFromIgdb()).thenReturn(1);
when(imageServiceMock.downloadCompanyLogosFromIgdb()).thenReturn(1);
LibraryScanResultDto result = target.scanLibraries(input);
verify(libraryServiceMock, never()).getLibraries();
verify(libraryServiceMock, times(1)).getLibrary(libraryPath);
verify(libraryServiceMock, times(1)).scanGameLibrary(any(Library.class));
verify(imageServiceMock, times(1)).downloadCompanyLogosFromIgdb();
verify(imageServiceMock, times(1)).downloadGameCoversFromIgdb();
verify(imageServiceMock, times(1)).downloadGameScreenshotsFromIgdb();
assertThat(result.getNewGames()).isEqualTo(lsr.getNewGames());
assertThat(result.getDeletedGames()).isEqualTo(lsr.getDeletedGames());
assertThat(result.getNewUnmappableFiles()).isEqualTo(lsr.getNewUnmappableFiles());
assertThat(result.getTotalGames()).isEqualTo(lsr.getTotalGames());
assertThat(result.getCoverDownloads()).isEqualTo(1);
assertThat(result.getScreenshotDownloads()).isEqualTo(1);
assertThat(result.getCompanyLogoDownloads()).isEqualTo(1);
}
@Test
void downloadImages() {
when(imageServiceMock.downloadGameCoversFromIgdb()).thenReturn(1);
when(imageServiceMock.downloadGameScreenshotsFromIgdb()).thenReturn(1);
when(imageServiceMock.downloadCompanyLogosFromIgdb()).thenReturn(1);
ImageDownloadResultDto result = target.downloadImages();
verify(imageServiceMock, times(1)).downloadCompanyLogosFromIgdb();
verify(imageServiceMock, times(1)).downloadGameCoversFromIgdb();
verify(imageServiceMock, times(1)).downloadGameScreenshotsFromIgdb();
assertThat(result.getScreenshotDownloads()).isOne();
assertThat(result.getCoverDownloads()).isOne();
assertThat(result.getCompanyLogoDownloads()).isOne();
}
@Test
void getAllFiles() {
List<Path> gameFiles = easyRandom.objects(String.class, 5).map(Path::of).toList();
when(libraryServiceMock.getGameFiles()).thenReturn(gameFiles);
List<String> result = target.getAllFiles();
verify(libraryServiceMock, times(1)).getGameFiles();
assertThat(result).hasSameElementsAs(gameFiles.stream().map(Path::toString).toList());
}
}