From c5b167d0c384d8df133420acd99d7c938500d3a0 Mon Sep 17 00:00:00 2001 From: Simon Grimme Date: Thu, 20 Oct 2022 12:17:54 +0300 Subject: [PATCH] Implement some more test cases for ProtobufUtil and FilenameUtil --- backend/pom.xml | 6 ++ .../de/grimsi/gameyfin/igdb/IgdbWrapper.java | 7 +- .../gameyfin/util/FilenameUtilTest.java | 93 +++++++++++++++++++ .../gameyfin/util/ProtobufUtilTest.java | 20 ++++ .../src/test/resources/application-test.yml | 4 +- 5 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 backend/src/test/java/de/grimsi/gameyfin/util/FilenameUtilTest.java create mode 100644 backend/src/test/java/de/grimsi/gameyfin/util/ProtobufUtilTest.java diff --git a/backend/pom.xml b/backend/pom.xml index 2b5633f..ee8d887 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -141,6 +141,12 @@ mockwebserver test + + com.google.jimfs + jimfs + 1.2 + test + diff --git a/backend/src/main/java/de/grimsi/gameyfin/igdb/IgdbWrapper.java b/backend/src/main/java/de/grimsi/gameyfin/igdb/IgdbWrapper.java index 9b30bc5..c65d45b 100644 --- a/backend/src/main/java/de/grimsi/gameyfin/igdb/IgdbWrapper.java +++ b/backend/src/main/java/de/grimsi/gameyfin/igdb/IgdbWrapper.java @@ -9,7 +9,6 @@ import io.github.resilience4j.reactor.bulkhead.operator.BulkheadOperator; import io.github.resilience4j.reactor.ratelimiter.operator.RateLimiterOperator; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; @@ -111,7 +110,7 @@ public class IgdbWrapper { Igdb.GameResult.class ); - if(gameResult == null) return Collections.emptyList(); + if (gameResult == null) return Collections.emptyList(); return gameResult.getGamesList().stream().map(gameMapper::toAutocompleteSuggestionDto).toList(); } @@ -129,10 +128,10 @@ public class IgdbWrapper { // Try to remove brackets (and their content) at the end of the search term and search again // Although this process is recursive, we will only end up with a maximum recursion depth of two - Pattern brackets = Pattern.compile ("[()<>{}\\[\\]]"); + Pattern brackets = Pattern.compile("[()<>{}\\[\\]]"); Matcher hasBrackets = brackets.matcher(searchTerm); - if(hasBrackets.find()) { + if (hasBrackets.find()) { String searchTermWithoutBrackets = searchTerm.split(brackets.pattern())[0].trim(); log.warn("Trying again with search term '{}'", searchTermWithoutBrackets); return searchForGameByTitle(searchTermWithoutBrackets); diff --git a/backend/src/test/java/de/grimsi/gameyfin/util/FilenameUtilTest.java b/backend/src/test/java/de/grimsi/gameyfin/util/FilenameUtilTest.java new file mode 100644 index 0000000..9ea385b --- /dev/null +++ b/backend/src/test/java/de/grimsi/gameyfin/util/FilenameUtilTest.java @@ -0,0 +1,93 @@ +package de.grimsi.gameyfin.util; + +import com.google.common.jimfs.Configuration; +import com.google.common.jimfs.Jimfs; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.IOException; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Named.named; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +class FilenameUtilTest { + + private static final FileSystem unixFS = Jimfs.newFileSystem(Configuration.unix()); + private static final FileSystem osxFS = Jimfs.newFileSystem(Configuration.osX()); + private static final FileSystem winFS = Jimfs.newFileSystem(Configuration.windows()); + private static final List gameFileExtensions = List.of("extension_1", "extension_2", "extension_3"); + + @AfterAll + static void close() throws IOException { + unixFS.close(); + osxFS.close(); + winFS.close(); + } + + @ParameterizedTest + @MethodSource("fileSystems") + void getFilenameWithoutExtension_File(FileSystem fileSystem) throws IOException { + String filename = "example_file"; + + Path p = fileSystem.getPath("%s.%s".formatted(filename, gameFileExtensions.get(0))); + Files.createFile(p); + + String result = FilenameUtil.getFilenameWithoutExtension(p); + + assertThat(result).isEqualTo(filename); + } + + @ParameterizedTest + @MethodSource("fileSystems") + void getFilenameWithoutExtension_Folder(FileSystem fileSystem) throws IOException { + String filename = "example_folder"; + + Path p = fileSystem.getPath("%s.%s".formatted(filename, gameFileExtensions.get(0))); + Files.createDirectory(p); + + String result = FilenameUtil.getFilenameWithoutExtension(p); + + assertThat(result).isEqualTo("%s.%s".formatted(filename, gameFileExtensions.get(0))); + } + + @Test + void getFilenameWithExtension_Unix() { + } + + @Test + void getFilenameWithExtension_OSX() { + } + + @Test + void getFilenameWithExtension_Windows() { + } + + @Test + void hasGameArchiveExtension_Unix() { + } + + @Test + void hasGameArchiveExtension_OSX() { + } + + @Test + void hasGameArchiveExtension_Windows() { + } + + private static Stream fileSystems() { + return Stream.of( + arguments(named("Unix", unixFS)), + arguments(named("OSX", osxFS)), + arguments(named("Windows", winFS)) + ); + } +} \ No newline at end of file diff --git a/backend/src/test/java/de/grimsi/gameyfin/util/ProtobufUtilTest.java b/backend/src/test/java/de/grimsi/gameyfin/util/ProtobufUtilTest.java new file mode 100644 index 0000000..240b53b --- /dev/null +++ b/backend/src/test/java/de/grimsi/gameyfin/util/ProtobufUtilTest.java @@ -0,0 +1,20 @@ +package de.grimsi.gameyfin.util; + +import com.google.protobuf.Timestamp; +import org.junit.jupiter.api.Test; + +import java.time.Instant; + +import static org.assertj.core.api.Assertions.assertThat; + +class ProtobufUtilTest { + + @Test + void toInstant() { + Timestamp t = Timestamp.newBuilder().setSeconds(1).build(); + + Instant i = ProtobufUtil.toInstant(t); + + assertThat(i.getEpochSecond()).isEqualTo(1); + } +} \ No newline at end of file diff --git a/backend/src/test/resources/application-test.yml b/backend/src/test/resources/application-test.yml index 8702211..c02f7cf 100644 --- a/backend/src/test/resources/application-test.yml +++ b/backend/src/test/resources/application-test.yml @@ -2,6 +2,4 @@ gameyfin: igdb: api: client-id: igdb_client_id - client-secret: igdb_client_secret - config: - preferred-platforms: 6 + client-secret: igdb_client_secret \ No newline at end of file