add test for UUIDFetcher

This commit is contained in:
vemacs 2015-02-10 08:04:03 -07:00
parent a80c3b51e1
commit 06c3935c39
3 changed files with 38 additions and 5 deletions

View File

@ -12,6 +12,7 @@ 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.UUIDFetcher;
import com.imaginarycode.minecraft.redisbungee.util.UUIDTranslator;
import com.squareup.okhttp.OkHttpClient;
import lombok.Getter;
@ -434,6 +435,7 @@ public final class RedisBungee extends Plugin {
service = Executors.newFixedThreadPool(16);
httpClient = new OkHttpClient();
NameFetcher.setHttpClient(httpClient);
UUIDFetcher.setHttpClient(httpClient);
return null;
}
});

View File

@ -9,8 +9,10 @@ package com.imaginarycode.minecraft.redisbungee.util;
import com.google.common.collect.ImmutableList;
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import lombok.Setter;
import java.util.*;
import java.util.concurrent.Callable;
@ -23,6 +25,9 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
private final List<String> names;
private final boolean rateLimiting;
@Setter
private static OkHttpClient httpClient;
public UUIDFetcher(List<String> names, boolean rateLimiting) {
this.names = ImmutableList.copyOf(names);
this.rateLimiting = rateLimiting;
@ -42,7 +47,7 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
for (int i = 0; i < requests; i++) {
String body = RedisBungee.getGson().toJson(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
Request request = new Request.Builder().url(PROFILE_URL).post(RequestBody.create(JSON, body)).build();
String response = RedisBungee.getHttpClient().newCall(request).execute().body().string();
String response = httpClient.newCall(request).execute().body().string();
Profile[] array = RedisBungee.getGson().fromJson(response, Profile[].class);
for (Profile profile : array) {
UUID uuid = UUIDFetcher.getUUID(profile.id);

View File

@ -6,16 +6,42 @@ import com.squareup.okhttp.OkHttpClient;
import org.junit.Test;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class UUIDNameTest {
private String[] uuidsToTest = {"68ec43f7234b41b48764dfb38b9ffe8c", "652a2bc4e8cd405db7b698156ee2dc09"};
private String[] namesToTest = {"vemacs"};
@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);
for (String uuid : uuidsToTest) {
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);
}
}
@Test
public void testNameToUuid() throws IOException {
OkHttpClient httpClient = new OkHttpClient();
UUIDFetcher.setHttpClient(httpClient);
for (String name : namesToTest) {
Map<String, UUID> uuidMap1;
try {
uuidMap1 = new UUIDFetcher(Collections.singletonList(name)).call();
for (Map.Entry<String, UUID> entry : uuidMap1.entrySet()) {
if (entry.getKey().equalsIgnoreCase(name)) {
System.out.println("Current UUID for name " + name + " is " + entry.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}