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

Commit 22b6fda

Browse files
even more refactoring
1 parent dee156b commit 22b6fda

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ public static <T> T wrap(RateLimiter rateLimiter, Class<T> service, T instanceTo
2929
}
3030

3131
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
32-
if (method.getDeclaringClass() == Object.class) {
33-
return method.invoke(this, args);
34-
}
35-
3632
if (APIResponse.class.isAssignableFrom(method.getReturnType())) {
3733
return new FutureAPIResponse(method, args, getPermits(method));
3834
}

src/test/java/test/ChampionGGTest.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.lvack.championggwrapper.data.staticdata.StatOrder;
1414
import com.lvack.championggwrapper.retrofit.APIResponse;
1515
import com.lvack.championggwrapper.retrofit.ChampionGGAPI;
16+
import com.lvack.championggwrapper.retrofit.proxies.RateLimitingRetrofitProxy;
1617
import okhttp3.mockwebserver.MockResponse;
1718
import okhttp3.mockwebserver.MockWebServer;
1819
import org.apache.commons.lang3.ClassUtils;
@@ -26,9 +27,7 @@
2627
import java.io.StringWriter;
2728
import java.io.UnsupportedEncodingException;
2829
import java.lang.annotation.Annotation;
29-
import java.lang.reflect.Method;
30-
import java.lang.reflect.Parameter;
31-
import java.lang.reflect.ParameterizedType;
30+
import java.lang.reflect.*;
3231
import java.time.Duration;
3332
import java.util.*;
3433
import java.util.stream.Collectors;
@@ -40,8 +39,7 @@
4039
private static MockWebServer webServer;
4140
private static MockDispatcher dispatcher;
4241

43-
@BeforeAll
44-
static void initChampionGGAPI() {
42+
@BeforeAll static void initChampionGGAPI() {
4543
webServer = new MockWebServer();
4644
dispatcher = new MockDispatcher();
4745
webServer.setDispatcher(dispatcher);
@@ -51,8 +49,7 @@ static void initChampionGGAPI() {
5149
API = championGGAPIFactory.buildChampionGGAPI();
5250
}
5351

54-
@AfterAll
55-
static void deleteChampionGGAPI() throws IOException {
52+
@AfterAll static void deleteChampionGGAPI() throws IOException {
5653
webServer.shutdown();
5754
}
5855

@@ -90,8 +87,7 @@ static void deleteChampionGGAPI() throws IOException {
9087
return tests;
9188
}
9289

93-
@Test
94-
void testInvalidApiKey() {
90+
@Test void testInvalidApiKey() {
9591
dispatcher.setDelay(0);
9692
ChampionGGAPI championGGAPI = new ChampionGGAPIFactory("invalid-key").buildChampionGGAPI();
9793
APIResponse<List<HighLevelChampionData>> response = championGGAPI.getHighLevelChampionData();
@@ -106,15 +102,24 @@ void testInvalidApiKey() {
106102
Assert.assertNotNull("invalid api key response does not have an error exception", response.getError());
107103
}
108104

109-
@Test
110-
void testRateLimiter() {
105+
@Test void testRateLimiter() {
111106
dispatcher.setDelay(0);
112107

113108
long requestDelay = 1000;
114109
double maxRequestsPerSecond = 1000.0 / requestDelay;
115110
ChampionGGAPIFactory factory = new ChampionGGAPIFactory(Constants.API_KEY, maxRequestsPerSecond);
116111
ChampionGGAPI api = factory.buildChampionGGAPI();
117112

113+
try {
114+
InvocationHandler invocationHandler = Proxy.getInvocationHandler(api);
115+
Assert.assertNotNull("invocation handler for api is null", invocationHandler);
116+
Assert.assertTrue("invocation handler for api is not a rate limiting proxy",
117+
RateLimitingRetrofitProxy.class.isAssignableFrom(invocationHandler.getClass()));
118+
Assert.assertNotEquals("two apis from different factories are equal", api, API);
119+
} catch (IllegalArgumentException e) {
120+
Assert.fail("failed to get invocation from api, stacktrace: " + getStackTrace(e));
121+
}
122+
118123
final int calls = 4;
119124

120125
List<APIResponse<List<HighLevelChampionData>>> responses = new ArrayList<>();
@@ -136,8 +141,7 @@ void testRateLimiter() {
136141

137142
}
138143

139-
@Test
140-
void testSlowResponse() {
144+
@Test void testSlowResponse() {
141145
dispatcher.setDelay(1000);
142146

143147
final int calls = 4;
@@ -354,8 +358,7 @@ private void assertEqualsJsonPrimitive(JsonPrimitive expectedPrimitive, JsonPrim
354358
actualPrimitive + ") at " + path + " are not equal; ", expectedPrimitive, actualPrimitive);
355359
}
356360

357-
@Test
358-
void testObjectMethodOnApi() {
361+
@Test void testObjectMethodOnApi() {
359362
//noinspection ResultOfMethodCallIgnored
360363
new ChampionGGAPIFactory(Constants.API_KEY, 10)
361364
.buildChampionGGAPI().getClass();

src/test/java/test/DeSerializerTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
class DeSerializerTest {
2222

23-
@Test
24-
void testErrorResponseDeserializer() {
23+
@Test void testErrorResponseDeserializer() {
2524
Gson gson = new GsonBuilder().registerTypeAdapter(ErrorResponse.class, new ErrorResponseDeserializer()).create();
2625

2726
NormalErrorResponse normalErrorResponse = new NormalErrorResponse();
@@ -63,8 +62,7 @@ void testErrorResponseDeserializer() {
6362

6463
}
6564

66-
@Test
67-
void testEnumDeSerializer() {
65+
@Test void testEnumDeSerializer() {
6866
Gson gson = new GsonBuilder()
6967
.registerTypeAdapter(TestEnum.class, new EnumDeSerializer<>(TestEnum.class)).create();
7068

@@ -91,8 +89,7 @@ void testEnumDeSerializer() {
9189
() -> gson.fromJson(new JsonArray(), TestEnum.class));
9290
}
9391

94-
@Test
95-
void testGsonProvider() {
92+
@Test void testGsonProvider() {
9693
Assert.assertNotNull("gson provider did not provide a gson builder", GsonProvider.getGsonBuilder());
9794
Assert.assertNotNull("gson provider did not provide a gson instance", GsonProvider.getGson());
9895
Assert.assertEquals("gson provider did not provide the same gson instance twice",

0 commit comments

Comments
 (0)