Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
Add unit tests for APILimitUtils.
Browse files Browse the repository at this point in the history
Add more information on Javadoc.
  • Loading branch information
arinto committed Apr 4, 2014
1 parent 7cf04aa commit 33c80ed
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jinstagram/InstagramResponse.java
Expand Up @@ -11,14 +11,14 @@ public interface InstagramResponse {
* Get the available API limit. It correspond to the value of
* X-Ratelimit-Limit key in HTTP response headers. For Instagram
* v1 API, this method should return 5000.
* @return Available API limit
* @return Available API limit. -1 if headers invalid.
*/
public int getAPILimitStatus();

/**
* Get the remaining API limit. It correspond to the value of
* X-Ratelimit-Remaining key in HTTP response headers.
* @return Remaining API limit
* @return Remaining API limit. -1 if headers invalid.
*/
public int getRemainingLimitStatus();
}
14 changes: 8 additions & 6 deletions src/main/java/org/jinstagram/http/APILimitUtils.java
Expand Up @@ -8,17 +8,18 @@
* @author Arinto Murdopo
*
*/
public class APILimitUtils {
public final class APILimitUtils {

private static final String LIMIT_HEADER_KEY = "X-Ratelimit-Limit";
private static final String REMAINING_HEADER_KEY = "X-Ratelimit-Remaining";
protected static final String LIMIT_HEADER_KEY = "X-Ratelimit-Limit";
protected static final String REMAINING_HEADER_KEY = "X-Ratelimit-Remaining";

/**
* Get the available API limit. It correspond to the value of
* X-Ratelimit-Limit key in HTTP response headers. For Instagram
* v1 API, this method should return 5000.
* @param headers HTTP headers from a Response object
* @return Available API limit
* @return Available API limit. -1 if response header is invalid or does not contains the API
* limit information
*/
public static int getAPILimitStatus(Map<String, String> headers){
return APILimitUtils.getIntegerValue(headers, LIMIT_HEADER_KEY);
Expand All @@ -28,7 +29,8 @@ public static int getAPILimitStatus(Map<String, String> headers){
* Get the remaining API limit. It correspond to the value of
* X-Ratelimit-Remaining key in HTTP response headers.
* @param headers HTTP headers from a Response object
* @return Remaining API limit
* @return Remaining API limit. -1 if response header is invalid or does not contains the remaining
* limit information
*/
public static int getRemainingLimitStatus(Map<String, String> headers){
return APILimitUtils.getIntegerValue(headers, REMAINING_HEADER_KEY);
Expand All @@ -41,7 +43,7 @@ private static int getIntegerValue(Map<String, String> header, String key){
try {
value = Integer.valueOf(intValueStr);
} catch (NumberFormatException e) {
System.out.printf("Invalid Integer value for key: %s, value %s%n", key, intValueStr);
System.err.printf("Invalid Integer value for key: %s, value %s%n", key, intValueStr);
}
return value;
}
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/org/jinstagram/http/APILimitUtilsTest.java
@@ -0,0 +1,59 @@
package org.jinstagram.http;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class APILimitUtilsTest {

@Test
public void remainingBetweenMinAndMax() {
int limitValue = 5000;
int remainingValue = 3999;

Map<String, String> headers = new HashMap<String, String>();
headers.put(APILimitUtils.LIMIT_HEADER_KEY, String.valueOf(limitValue));
headers.put(APILimitUtils.REMAINING_HEADER_KEY, String.valueOf(remainingValue));

assertTrue(limitValue == Integer.valueOf(APILimitUtils.getAPILimitStatus(headers)));
assertTrue(remainingValue == Integer.valueOf(APILimitUtils.getRemainingLimitStatus(headers)));
}

@Test
public void remainingBetweenMax() {
int limitValue = 5000;
int remainingValue = limitValue;

Map<String, String> headers = new HashMap<String, String>();
headers.put(APILimitUtils.LIMIT_HEADER_KEY, String.valueOf(limitValue));
headers.put(APILimitUtils.REMAINING_HEADER_KEY, String.valueOf(remainingValue));

assertTrue(limitValue == APILimitUtils.getAPILimitStatus(headers));
assertTrue(remainingValue == APILimitUtils.getRemainingLimitStatus(headers));
}

@Test
public void remainingZero() {
int limitValue = 5000;
int remainingValue = 0;

Map<String, String> headers = new HashMap<String, String>();
headers.put(APILimitUtils.LIMIT_HEADER_KEY, String.valueOf(limitValue));
headers.put(APILimitUtils.REMAINING_HEADER_KEY, String.valueOf(remainingValue));

assertTrue(limitValue == APILimitUtils.getAPILimitStatus(headers));
assertTrue(remainingValue == APILimitUtils.getRemainingLimitStatus(headers));
}

@Test
public void emptyHeader() {
int defaultErrorValue = -1;
Map<String, String> headers = new HashMap<String, String>();

assertTrue(defaultErrorValue == APILimitUtils.getAPILimitStatus(headers));
assertTrue(defaultErrorValue == APILimitUtils.getRemainingLimitStatus(headers));
}
}

0 comments on commit 33c80ed

Please sign in to comment.