Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

Commit 5cafe52

Browse files
refactoring
1 parent 37c0214 commit 5cafe52

File tree

7 files changed

+74
-49
lines changed

7 files changed

+74
-49
lines changed

src/main/java/com/lvack/championggwrapper/data/base/WinRateGameCountRole.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
@Data @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true)
1111
public class WinRateGameCountRole extends WinRateGameCount {
1212
@SerializedName("role")
13-
Role role;
13+
private Role role;
1414
}

src/main/java/com/lvack/championggwrapper/retrofit/proxies/RateLimitingRetrofitProxy.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import com.google.common.util.concurrent.RateLimiter;
44
import com.lvack.championggwrapper.annotations.Permits;
5-
import com.lvack.championggwrapper.data.error.ErrorResponse;
65
import com.lvack.championggwrapper.retrofit.APIResponse;
76
import com.lvack.championggwrapper.retrofit.Waitable;
87
import lombok.experimental.Delegate;
98

9+
import java.lang.reflect.InvocationHandler;
1010
import java.lang.reflect.InvocationTargetException;
1111
import java.lang.reflect.Method;
1212
import java.lang.reflect.Proxy;
1313

1414

15-
public class RateLimitingRetrofitProxy<T> {
15+
public class RateLimitingRetrofitProxy<T> implements InvocationHandler {
1616
private final T wrappedInstance;
1717
private final T instance;
1818
private final RateLimiter rateLimiter;
@@ -21,15 +21,14 @@ private RateLimitingRetrofitProxy(RateLimiter rateLimiter, Class<T> service, T i
2121
this.wrappedInstance = instanceToWrap;
2222
this.rateLimiter = rateLimiter;
2323
//noinspection unchecked
24-
this.instance = (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[]{service},
25-
this::invoke);
24+
this.instance = (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[]{service}, this);
2625
}
2726

2827
public static <T> T wrap(RateLimiter rateLimiter, Class<T> service, T instanceToWrap) {
2928
return new RateLimitingRetrofitProxy<>(rateLimiter, service, instanceToWrap).instance;
3029
}
3130

32-
private Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
31+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
3332
if (method.getDeclaringClass() == Object.class) {
3433
return method.invoke(this, args);
3534
}

src/test/java/ChampionGGTest.java renamed to src/test/java/test/ChampionGGTest.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import com.google.gson.*;
1+
package test;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonPrimitive;
27
import com.lvack.championggwrapper.ChampionGGAPIFactory;
38
import com.lvack.championggwrapper.annotations.*;
49
import com.lvack.championggwrapper.data.champion.HighLevelChampionData;
510
import com.lvack.championggwrapper.data.error.ErrorResponse;
611
import com.lvack.championggwrapper.data.staticdata.Role;
712
import com.lvack.championggwrapper.data.staticdata.RoleStatOrder;
813
import com.lvack.championggwrapper.data.staticdata.StatOrder;
9-
import com.lvack.championggwrapper.gson.GsonProvider;
1014
import com.lvack.championggwrapper.retrofit.APIResponse;
1115
import com.lvack.championggwrapper.retrofit.ChampionGGAPI;
1216
import okhttp3.mockwebserver.MockResponse;
1317
import okhttp3.mockwebserver.MockWebServer;
1418
import org.apache.commons.lang3.ClassUtils;
1519
import org.junit.Assert;
1620
import org.junit.jupiter.api.*;
21+
import util.Constants;
22+
import util.MockDispatcher;
1723

1824
import java.io.IOException;
1925
import java.io.PrintWriter;
2026
import java.io.StringWriter;
2127
import java.lang.annotation.Annotation;
22-
import java.lang.reflect.Field;
2328
import java.lang.reflect.Method;
2429
import java.lang.reflect.Parameter;
2530
import java.lang.reflect.ParameterizedType;
@@ -29,9 +34,6 @@
2934

3035

3136
@DisplayName("ChampionGGAPI tests") class ChampionGGTest {
32-
static final Gson GSON = GsonProvider.getGsonBuilder()
33-
.registerTypeAdapter(MockResponse.class, new MockResponseDeSerializer())
34-
.create();
3537

3638
private static ChampionGGAPI API;
3739
private static MockWebServer webServer;
@@ -44,7 +46,7 @@ static void initChampionGGAPI() {
4446
webServer.setDispatcher(dispatcher);
4547
ChampionGGAPIFactory.BASE_URL = webServer.url("/").toString();
4648

47-
ChampionGGAPIFactory championGGAPIFactory = new ChampionGGAPIFactory(MockDispatcher.API_KEY, -1);
49+
ChampionGGAPIFactory championGGAPIFactory = new ChampionGGAPIFactory(Constants.API_KEY, -1);
4850
API = championGGAPIFactory.buildChampionGGAPI();
4951
}
5052

@@ -122,12 +124,12 @@ static void deleteChampionGGAPI() throws IOException {
122124
Assert.assertNull("success response has an error exception", actualError);
123125

124126
String expectedResponseJson = new String(response.getBody().clone().readByteArray(), "UTF-8");
125-
Object expectedContent = GSON.fromJson(expectedResponseJson,
127+
Object expectedContent = Constants.GSON.fromJson(expectedResponseJson,
126128
((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0]);
127129
Assert.assertEquals("response content does not match", expectedContent, actualContent);
128130

129-
JsonElement expectedJsonElement = GSON.fromJson(expectedResponseJson, JsonElement.class);
130-
JsonElement actualJsonElement = GSON.fromJson(GSON.toJson(actualContent), JsonElement.class);
131+
JsonElement expectedJsonElement = Constants.GSON.fromJson(expectedResponseJson, JsonElement.class);
132+
JsonElement actualJsonElement = Constants.GSON.fromJson(Constants.GSON.toJson(actualContent), JsonElement.class);
131133
assertEqualJsonElements(expectedJsonElement, actualJsonElement);
132134
// TODO: find fields that may be null; assertNoNullFields(actualContent);
133135
}
@@ -148,7 +150,7 @@ static void deleteChampionGGAPI() throws IOException {
148150
Assert.assertNotNull("api error response does not have an error response", actualErrorResponse);
149151
Assert.assertNull("api error response has an error exception", actualError);
150152

151-
ErrorResponse expectedErrorResponse = GSON.fromJson(new String(response.getBody().clone().readByteArray(), "UTF-8"),
153+
ErrorResponse expectedErrorResponse = Constants.GSON.fromJson(new String(response.getBody().clone().readByteArray(), "UTF-8"),
152154
ErrorResponse.class);
153155
Assert.assertEquals("error response content does not match", expectedErrorResponse, actualErrorResponse);
154156
}
@@ -185,7 +187,7 @@ void testRateLimiter() {
185187

186188
long requestDelay = 1000;
187189
double maxRequestsPerSecond = 1000.0 / requestDelay;
188-
ChampionGGAPIFactory factory = new ChampionGGAPIFactory(MockDispatcher.API_KEY, maxRequestsPerSecond);
190+
ChampionGGAPIFactory factory = new ChampionGGAPIFactory(Constants.API_KEY, maxRequestsPerSecond);
189191
ChampionGGAPI api = factory.buildChampionGGAPI();
190192

191193
final int calls = 4;
@@ -343,7 +345,7 @@ private void assertEqualJsonElements(JsonElement expected, JsonElement actual, S
343345
@Test
344346
void testObjectMethodOnApi() {
345347
//noinspection ResultOfMethodCallIgnored
346-
new ChampionGGAPIFactory(MockDispatcher.API_KEY, 10)
348+
new ChampionGGAPIFactory(Constants.API_KEY, 10)
347349
.buildChampionGGAPI().getClass();
348350
}
349351

@@ -363,11 +365,11 @@ private String getJsonPrimitiveType(JsonPrimitive primitive) {
363365
return "unknown";
364366
}
365367

366-
private void assertNoNullFields(Object object) {
368+
/* private void assertNoNullFields(Object object) {
367369
assertNoNullFields(object, "");
368-
}
370+
} */
369371

370-
private void assertNoNullFields(Object object, String path) {
372+
/* private void assertNoNullFields(Object object, String path) {
371373
Assert.assertNotNull("field at " + path + " is null", object);
372374
if (ClassUtils.isPrimitiveOrWrapper(object.getClass())) return;
373375
if (object instanceof String) return;
@@ -396,7 +398,7 @@ private void assertNoNullFields(Object object, String path) {
396398
}
397399
}
398400
}
399-
}
401+
} */
400402

401403
private String getMethodName(Method method, Object[] args) {
402404
return method.getDeclaringClass().getSimpleName() + "." + method.getName() +

src/test/java/DeSerializerTest.java renamed to src/test/java/test/DeSerializerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package test;
2+
13
import com.google.gson.*;
24
import com.google.gson.annotations.SerializedName;
35
import com.lvack.championggwrapper.data.error.AdvancedErrorResponse;

src/test/java/util/Constants.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package util;
2+
3+
import com.google.gson.Gson;
4+
import com.lvack.championggwrapper.gson.GsonProvider;
5+
import okhttp3.mockwebserver.MockResponse;
6+
7+
/**
8+
* ConstantsClass for champion-gg-wrapper
9+
*
10+
* @author Leon Vack - TWENTY |20
11+
*/
12+
13+
public class Constants {
14+
public static final Gson GSON = GsonProvider.getGsonBuilder()
15+
.registerTypeAdapter(MockResponse.class, new MockResponseDeSerializer())
16+
.create();
17+
public static final String API_KEY = "mock-api-key";
18+
}

src/test/java/MockDispatcher.java renamed to src/test/java/util/MockDispatcher.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package util;
2+
13
import com.google.common.util.concurrent.Uninterruptibles;
24
import com.lvack.championggwrapper.data.staticdata.Role;
35
import com.lvack.championggwrapper.data.staticdata.RoleStatOrder;
@@ -20,7 +22,6 @@
2022

2123

2224
public class MockDispatcher extends Dispatcher {
23-
static final String API_KEY = "mock-api-key";
2425
@Getter private MockResponse lastResponse;
2526
@Setter private long delay = 0;
2627
@Getter private List<Long> callTimes = new ArrayList<>();
@@ -40,22 +41,22 @@ private MockResponse getResponse(RecordedRequest request) {
4041
HttpUrl url = HttpUrl.parse("http://localhost" + path);
4142

4243
String apiKey = url.queryParameter("api_key");
43-
if (!Objects.equals(API_KEY, apiKey)) {
44+
if (!Objects.equals(Constants.API_KEY, apiKey)) {
4445
return new MockResponse().setBody("\n" +
45-
"<html>\n" +
46-
"<head><title>403 Forbidden</title></head>\n" +
47-
"<body bgcolor=\"white\">\n" +
48-
"<center><h1>403 Forbidden</h1></center>\n" +
49-
"<hr><center>openresty/1.9.3.2</center>\n" +
50-
"</body>\n" +
51-
"</html>\n" +
52-
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
53-
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
54-
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
55-
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
56-
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
57-
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n")
58-
.setResponseCode(403);
46+
"<html>\n" +
47+
"<head><title>403 Forbidden</title></head>\n" +
48+
"<body bgcolor=\"white\">\n" +
49+
"<center><h1>403 Forbidden</h1></center>\n" +
50+
"<hr><center>openresty/1.9.3.2</center>\n" +
51+
"</body>\n" +
52+
"</html>\n" +
53+
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
54+
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
55+
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
56+
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
57+
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n" +
58+
"<!-- a padding to disable MSIE and Chrome friendly error page -->\n")
59+
.setResponseCode(403);
5960
}
6061

6162
path = getPath(request, url);
@@ -67,24 +68,25 @@ private MockResponse getResponse(RecordedRequest request) {
6768

6869
Uninterruptibles.sleepUninterruptibly(delay, TimeUnit.MILLISECONDS);
6970

70-
return ChampionGGTest.GSON.fromJson(new InputStreamReader(jsonStream), MockResponse.class);
71+
return Constants.GSON.fromJson(new InputStreamReader(jsonStream), MockResponse.class);
7172
}
7273

7374
private String getPath(RecordedRequest request, HttpUrl url) {
75+
HttpUrl targetUrl;
7476
if (isPaged(url)) {
7577
HttpUrl.Builder builder = url.newBuilder();
7678
if (url.queryParameter("page") == null) builder.addQueryParameter("page", "1");
7779
if (url.queryParameter("limit") == null) builder.addQueryParameter("limit", "10");
78-
url = builder.build();
79-
}
80+
targetUrl = builder.build();
81+
} else targetUrl = url;
8082

81-
HttpUrl finalUrl = url;
83+
HttpUrl finalUrl = targetUrl;
8284
return "[" + request.getMethod() + "]" +
83-
finalUrl.pathSegments().stream().collect(Collectors.joining("-")) +
84-
finalUrl.queryParameterNames().stream().sorted()
85-
.filter(n -> !"api_key".equals(n))
86-
.map(n -> n + "=" + finalUrl.queryParameter(n))
87-
.collect(Collectors.joining(";", "[", "]")) + ".json";
85+
finalUrl.pathSegments().stream().collect(Collectors.joining("-")) +
86+
finalUrl.queryParameterNames().stream().sorted()
87+
.filter(n -> !"api_key".equals(n))
88+
.map(n -> n + "=" + finalUrl.queryParameter(n))
89+
.collect(Collectors.joining(";", "[", "]")) + ".json";
8890
}
8991

9092
private boolean isPaged(HttpUrl url) {

src/test/java/MockResponseDeSerializer.java renamed to src/test/java/util/MockResponseDeSerializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package util;
2+
13
import com.google.gson.*;
24
import okhttp3.mockwebserver.MockResponse;
35

@@ -22,7 +24,7 @@ public MockResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializat
2224

2325
try {
2426
jsonObject.add("body",
25-
new JsonPrimitive(new String(src.getBody().clone().readByteArray(), "UTF-8")));
27+
new JsonPrimitive(new String(src.getBody().clone().readByteArray(), "UTF-8")));
2628
} catch (UnsupportedEncodingException e) {
2729
throw new JsonParseException("failed to encode body with UTF-8", e);
2830
}

0 commit comments

Comments
 (0)