2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-23 04:28:01 +00:00

Add a test class

This commit is contained in:
vemacs 2015-02-04 08:02:30 -07:00
parent b684064c92
commit a80c3b51e1
5 changed files with 36 additions and 2 deletions

View File

@ -163,5 +163,11 @@
<version>2.2.0</version> <version>2.2.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -11,6 +11,7 @@ import com.google.common.collect.*;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.util.NameFetcher;
import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator; import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator;
import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.OkHttpClient;
import lombok.Getter; import lombok.Getter;
@ -432,6 +433,7 @@ public final class RedisBungee extends Plugin {
public Void call() throws Exception { public Void call() throws Exception {
service = Executors.newFixedThreadPool(16); service = Executors.newFixedThreadPool(16);
httpClient = new OkHttpClient(); httpClient = new OkHttpClient();
NameFetcher.setHttpClient(httpClient);
return null; return null;
} }
}); });

View File

@ -8,9 +8,11 @@ package com.imaginarycode.minecraft.redisbungee.util;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.imaginarycode.minecraft.redisbungee.RedisBungee; import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request; import com.squareup.okhttp.Request;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -20,10 +22,13 @@ import java.util.UUID;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NameFetcher { public class NameFetcher {
@Setter
private static OkHttpClient httpClient;
public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException { public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException {
String url = "https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names"; String url = "https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names";
Request request = new Request.Builder().url(url).get().build(); 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<List<Name>>() {}.getType(); Type listType = new TypeToken<List<Name>>() {}.getType();
List<Name> names = RedisBungee.getGson().fromJson(response, listType); List<Name> names = RedisBungee.getGson().fromJson(response, listType);

View File

@ -16,7 +16,7 @@ import java.util.*;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
/* Credits to evilmidget38 for this class. I modified it to use Gson. */ /* Credits to evilmidget38 for this class. I modified it to use Gson. */
class UUIDFetcher implements Callable<Map<String, UUID>> { public class UUIDFetcher implements Callable<Map<String, UUID>> {
private static final double PROFILES_PER_REQUEST = 100; private static final double PROFILES_PER_REQUEST = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private static final MediaType JSON = MediaType.parse("application/json"); private static final MediaType JSON = MediaType.parse("application/json");

View File

@ -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<String> names = NameFetcher.nameHistoryFromUuid(UUIDFetcher.getUUID(uuid));
String currentName = names.get(names.size() - 1);
System.out.println("Current name for UUID " + uuid + " is " + currentName);
}
}