1313import com .lvack .championggwrapper .data .staticdata .StatOrder ;
1414import com .lvack .championggwrapper .retrofit .APIResponse ;
1515import com .lvack .championggwrapper .retrofit .ChampionGGAPI ;
16+ import com .lvack .championggwrapper .retrofit .proxies .RateLimitingRetrofitProxy ;
1617import okhttp3 .mockwebserver .MockResponse ;
1718import okhttp3 .mockwebserver .MockWebServer ;
1819import org .apache .commons .lang3 .ClassUtils ;
2627import java .io .StringWriter ;
2728import java .io .UnsupportedEncodingException ;
2829import 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 .*;
3231import java .time .Duration ;
3332import java .util .*;
3433import java .util .stream .Collectors ;
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 ();
0 commit comments