Skip to content

Commit

Permalink
bug fixes and more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryn101 committed Dec 17, 2013
1 parent f8aba29 commit 577f15a
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 97 deletions.
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
apply plugin: 'java'
apply plugin: 'jacoco'

sourceCompatibility = 1.7
version = '1.1'
version = '1.1.1'

repositories {
mavenCentral()
Expand Down Expand Up @@ -29,4 +30,12 @@ test {
onOutput { descriptor, event ->
logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
}
}

jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}
3 changes: 0 additions & 3 deletions junit.properties.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
api.key=
summoner.name=
summoner.id=
region=
rate.per10minutes=
rate.per10seconds=
2 changes: 2 additions & 0 deletions src/main/java/lol4j/protocol/dto/stats/ChampionStatDto.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package lol4j.protocol.dto.stats;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Created by Aaryn101 on 12/12/13.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class ChampionStatDto {
@JsonProperty(value = "c")
private int count;
private int id;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import lol4j.protocol.dto.summoner.SummonerDto;
import lol4j.protocol.dto.summoner.SummonerNameListDto;
import lol4j.protocol.resource.SummonerResource;
import lol4j.service.impl.ApiRequestManager;
import lol4j.util.Region;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
Expand Down Expand Up @@ -52,7 +55,13 @@ public SummonerDto getSummoner(Region region, String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Invalid summoner name");
}
String path = region.getName() + SLASH + RESOURCE_URI + SLASH + BY_NAME + SLASH + name;
String path = null;
try {
path = region.getName() + SLASH + RESOURCE_URI + SLASH + BY_NAME + SLASH +
URLEncoder.encode(name, ApiRequestManager.ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding: " + ApiRequestManager.ENCODING);
}

return getApiRequestManager().get(getBaseUri(), path, null, SummonerDto.class);
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/lol4j/service/impl/ApiRequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Created by Aaryn101 on 12/10/13.
*/
public class ApiRequestManager {
public static final String ENCODING = "UTF-8";
private static final int TEN_SECONDS = 10000;
private static final int TEN_MINUTES = 600000;
private String apiKey;
Expand Down Expand Up @@ -95,7 +96,7 @@ private String doRequest(String baseUri, String path, Map<String, Object> queryP
webTarget = webTarget.queryParam(queryParam.getKey(), queryParam.getValue());
}
}
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON).acceptEncoding("UTF-8");
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON).acceptEncoding(ENCODING);
Response response = invocationBuilder.get();

if (response.getStatus() != 200) {
Expand All @@ -108,8 +109,10 @@ private String doRequest(String baseUri, String path, Map<String, Object> queryP
}
}

perSecondsBucket.put(new Token(TEN_SECONDS, false));
perMinutesBucket.put(new Token(TEN_MINUTES, false));
if (usingRateLimiter) {
perSecondsBucket.put(new Token(TEN_SECONDS, false));
perMinutesBucket.put(new Token(TEN_MINUTES, false));
}

return response.readEntity(String.class);
}
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/ApiRequestManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import lol4j.exception.TooManyRequestsException;
import lol4j.util.Region;
import org.junit.Assert;

/**
* Created by Aaryn101 on 12/14/13.
*/
public class ApiRequestManagerTest {
private static final Region REGION = Region.NA;

@org.junit.Test
public void rateLimiter() {
Lol4JTestVariables vars = Lol4JTestVariables.getInstance();

try {
for (int i = 0; i < vars.getNumPerTenSeconds(); i++) {
vars.getClient().getAllChampions(vars.getRegion(), false);
for (int i = 0; i < Lol4JTestClient.getNumPerTenSeconds(); i++) {
Lol4JTestClient.getClient().getAllChampions(REGION, false);
}
}
catch(TooManyRequestsException e) {
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/ChampionResourceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
* Created by Aaryn101 on 12/13/13.
*/
public class ChampionResourceImplTest {
private static final Region REGION = Region.NA;

@org.junit.Test
public void getAllChampions() {
ChampionListDto championList = Lol4JTestVariables.getInstance().getClient().getAllChampions(Region.NA, false);
ChampionListDto championList = Lol4JTestClient.getClient().getAllChampions(REGION, false);

Assert.assertNotNull(championList);
Assert.assertNotNull(championList.getChampions());
Expand All @@ -30,7 +32,7 @@ public void getAllChampionsWithUnsupportedRegion() {
boolean exceptionThrown = false;

try {
Lol4JTestVariables.getInstance().getClient().getAllChampions(Region.TR, false);
Lol4JTestClient.getClient().getAllChampions(Region.TR, false);
}
catch(InvalidRegionException e) {
exceptionThrown = true;
Expand All @@ -44,7 +46,7 @@ public void getAllChampionsWithNullRegion() {
boolean exceptionThrown = false;

try {
Lol4JTestVariables.getInstance().getClient().getAllChampions(null, false);
Lol4JTestClient.getClient().getAllChampions(null, false);
}
catch(InvalidRegionException e) {
exceptionThrown = true;
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/GameResourceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
* Created by Aaryn101 on 12/14/13.
*/
public class GameResourceImplTest {
private static final long SUMMONER_ID = 19163557;
private static final Region REGION = Region.NA;

@org.junit.Test
public void getRecentGames() {
Lol4JTestVariables vars = Lol4JTestVariables.getInstance();
RecentGamesDto recentGames = vars.getClient().getRecentGames(vars.getRegion(), vars.getSummonerId());
RecentGamesDto recentGames = Lol4JTestClient.getClient().getRecentGames(REGION, SUMMONER_ID);

Assert.assertNotNull(recentGames);
Assert.assertNotNull(recentGames.getGames());
Expand All @@ -36,7 +38,7 @@ public void getRecentGamesWithUnsupportedRegion() {
boolean exceptionThrown = false;

try {
Lol4JTestVariables.getInstance().getClient().getRecentGames(Region.TR, 0L);
Lol4JTestClient.getClient().getRecentGames(Region.TR, 0L);
}
catch(InvalidRegionException e) {
exceptionThrown = true;
Expand All @@ -50,7 +52,7 @@ public void getRecentGamesWithNullRegion() {
boolean exceptionThrown = false;

try {
Lol4JTestVariables.getInstance().getClient().getRecentGames(null, 0L);
Lol4JTestClient.getClient().getRecentGames(null, 0L);
}
catch(InvalidRegionException e) {
exceptionThrown = true;
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/LeagueResourceImplTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import lol4j.exception.InvalidRegionException;
import lol4j.protocol.dto.league.LeagueDto;
import lol4j.util.Region;
import org.junit.Assert;

import java.util.Map;
Expand All @@ -8,10 +9,12 @@
* Created by Aaryn101 on 12/14/13.
*/
public class LeagueResourceImplTest {
private static final long SUMMONER_ID = 19163557;
private static final Region REGION = Region.NA;

@org.junit.Test
public void getLeaguesData() {
Lol4JTestVariables vars = Lol4JTestVariables.getInstance();
Map<String, LeagueDto> leaguesData = vars.getClient().getLeaguesData(vars.getRegion(), vars.getSummonerId());
Map<String, LeagueDto> leaguesData = Lol4JTestClient.getClient().getLeaguesData(REGION, SUMMONER_ID);

Assert.assertNotNull(leaguesData);

Expand All @@ -32,7 +35,7 @@ public void getAllChampionsWithNullRegion() {
boolean exceptionThrown = false;

try {
Lol4JTestVariables.getInstance().getClient().getLeaguesData(null, 0L);
Lol4JTestClient.getClient().getLeaguesData(null, 0L);
}
catch(InvalidRegionException e) {
exceptionThrown = true;
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/Lol4JTestClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import lol4j.client.Lol4JClient;
import lol4j.client.impl.Lol4JClientImpl;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
* Created by Aaryn101 on 12/14/13.
*/
public class Lol4JTestClient {
private static Lol4JTestClient instance;

private String apiKey = null;
private int numPerTenSeconds;
private int numPerTenMinutes;
private Lol4JClientImpl client = null;

private Lol4JTestClient() {
Properties p = new Properties();
try {
p.load(new FileReader(new File("junit.properties")));
apiKey = p.getProperty("api.key");
numPerTenMinutes = Integer.parseInt(p.getProperty("rate.per10minutes"));
numPerTenSeconds = Integer.parseInt(p.getProperty("rate.per10seconds"));
client = new Lol4JClientImpl(apiKey);
client.setRateLimit(numPerTenSeconds, numPerTenMinutes);
} catch (IOException e) {
e.printStackTrace();
}
}

public static Lol4JTestClient getInstance() {
if (instance == null) {
instance = new Lol4JTestClient();
}
return instance;
}

public static Lol4JClient getClient() {
return getInstance().client;
}

public static int getNumPerTenSeconds() {
return getInstance().numPerTenSeconds;
}

public static int getNumPerTenMinutes() {
return getInstance().numPerTenMinutes;
}
}
75 changes: 0 additions & 75 deletions src/test/java/Lol4JTestVariables.java

This file was deleted.

Loading

0 comments on commit 577f15a

Please sign in to comment.