From 83293804a51303167263574e74f0b6f1af21ad82 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 25 Oct 2019 15:12:01 +0300 Subject: [PATCH] Fixed UUID parsing from String for JSON requests Due to the way Optional#orElse works, the part inside orElse was run even when the UUID could be parsed from the given String, leading to unnecessary work as well as errors in the player UUID query (That uses names and doesn't look at uuids like the server query) Fixed by splitting the two execution branches Affects issues: - Fixed #1200 (Manually tested) --- .../plan/identification/Identifiers.java | 17 ++++++++++---- .../plan/identification/UUIDUtilityTest.java | 23 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/identification/UUIDUtilityTest.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java b/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java index 2e6c30f87..9377c7ec8 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/identification/Identifiers.java @@ -24,6 +24,7 @@ import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQuerie import javax.inject.Inject; import javax.inject.Singleton; +import java.util.Optional; import java.util.UUID; /** @@ -45,8 +46,11 @@ public class Identifiers { String serverIndentifier = target.getParameter("server") .orElseThrow(() -> new BadRequestException("'server' parameter was not defined.")); - return UUIDUtility.parseFromString(serverIndentifier) - .orElse(getServerUUIDFromName(serverIndentifier)); + Optional parsed = UUIDUtility.parseFromString(serverIndentifier); + if (parsed.isPresent()) { + return parsed.get(); + } + return getServerUUIDFromName(serverIndentifier); } private UUID getServerUUIDFromName(String serverName) throws BadRequestException { @@ -57,10 +61,13 @@ public class Identifiers { public UUID getPlayerUUID(RequestTarget target) throws BadRequestException { String playerIdentifier = target.getParameter("player") - .orElseThrow(() -> new BadRequestException("'player' parameter was not defined.")); + .orElseThrow(() -> new BadRequestException("'player' parameter was not defined.")).trim(); - return UUIDUtility.parseFromString(playerIdentifier) - .orElse(getPlayerUUIDFromName(playerIdentifier)); + Optional parsed = UUIDUtility.parseFromString(playerIdentifier); + if (parsed.isPresent()) { + return parsed.get(); + } + return getPlayerUUIDFromName(playerIdentifier); } private UUID getPlayerUUIDFromName(String playerName) throws BadRequestException { diff --git a/Plan/common/src/test/java/com/djrapitops/plan/identification/UUIDUtilityTest.java b/Plan/common/src/test/java/com/djrapitops/plan/identification/UUIDUtilityTest.java new file mode 100644 index 000000000..71766f24c --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/identification/UUIDUtilityTest.java @@ -0,0 +1,23 @@ +package com.djrapitops.plan.identification; + +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; +import utilities.OptionalAssert; + +import java.util.UUID; + +/** + * Tests for {@link UUIDUtility}. + * + * @author Rsl1122 + */ +@RunWith(JUnitPlatform.class) +class UUIDUtilityTest { + + @Test + void stringUUIDIsParsed() { + String test = "f3cc3e96-1bc9-35ad-994f-d894e9764b93"; + OptionalAssert.equals(UUID.fromString(test), UUIDUtility.parseFromString(test)); + } +} \ No newline at end of file