From a80c3b51e1281c6531eee4d65e410837bf7e9de1 Mon Sep 17 00:00:00 2001 From: vemacs Date: Wed, 4 Feb 2015 08:02:30 -0700 Subject: [PATCH] Add a test class --- pom.xml | 6 ++++++ .../minecraft/redisbungee/RedisBungee.java | 2 ++ .../redisbungee/util/NameFetcher.java | 7 ++++++- .../redisbungee/util/UUIDFetcher.java | 2 +- .../redisbungee/test/UUIDNameTest.java | 21 +++++++++++++++++++ 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java diff --git a/pom.xml b/pom.xml index 2111c94..b60fd3b 100644 --- a/pom.xml +++ b/pom.xml @@ -163,5 +163,11 @@ 2.2.0 compile + + junit + junit + 4.11 + test + \ No newline at end of file diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index 921d1dd..f830a76 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -11,6 +11,7 @@ import com.google.common.collect.*; import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; +import com.imaginarycode.minecraft.redisbungee.util.NameFetcher; import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator; import com.squareup.okhttp.OkHttpClient; import lombok.Getter; @@ -432,6 +433,7 @@ public final class RedisBungee extends Plugin { public Void call() throws Exception { service = Executors.newFixedThreadPool(16); httpClient = new OkHttpClient(); + NameFetcher.setHttpClient(httpClient); return null; } }); diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java index cceb737..03753eb 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java @@ -8,9 +8,11 @@ package com.imaginarycode.minecraft.redisbungee.util; import com.google.gson.reflect.TypeToken; import com.imaginarycode.minecraft.redisbungee.RedisBungee; +import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import lombok.Setter; import java.io.IOException; import java.lang.reflect.Type; @@ -20,10 +22,13 @@ import java.util.UUID; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class NameFetcher { + @Setter + private static OkHttpClient httpClient; + public static List nameHistoryFromUuid(UUID uuid) throws IOException { String url = "https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names"; Request request = new Request.Builder().url(url).get().build(); - String response = RedisBungee.getHttpClient().newCall(request).execute().body().string(); + String response = httpClient.newCall(request).execute().body().string(); Type listType = new TypeToken>() {}.getType(); List names = RedisBungee.getGson().fromJson(response, listType); diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java index 144b1ee..741e1db 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java @@ -16,7 +16,7 @@ import java.util.*; import java.util.concurrent.Callable; /* Credits to evilmidget38 for this class. I modified it to use Gson. */ -class UUIDFetcher implements Callable> { +public class UUIDFetcher implements Callable> { private static final double PROFILES_PER_REQUEST = 100; private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; private static final MediaType JSON = MediaType.parse("application/json"); diff --git a/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java b/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java new file mode 100644 index 0000000..d9e399f --- /dev/null +++ b/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java @@ -0,0 +1,21 @@ +package com.imaginarycode.minecraft.redisbungee.test; + +import com.imaginarycode.minecraft.redisbungee.util.NameFetcher; +import com.imaginarycode.minecraft.redisbungee.util.UUIDFetcher; +import com.squareup.okhttp.OkHttpClient; +import org.junit.Test; + +import java.io.IOException; +import java.util.List; + +public class UUIDNameTest { + @Test + public void testUuidToName() throws IOException { + OkHttpClient httpClient = new OkHttpClient(); + String uuid = "68ec43f7234b41b48764dfb38b9ffe8c"; + NameFetcher.setHttpClient(httpClient); + List names = NameFetcher.nameHistoryFromUuid(UUIDFetcher.getUUID(uuid)); + String currentName = names.get(names.size() - 1); + System.out.println("Current name for UUID " + uuid + " is " + currentName); + } +}