Created
March 25, 2015 23:04
-
-
Save ryankennedy/33ab65b4f99362c3470b to your computer and use it in GitHub Desktop.
Using DropwizardClientRule to test the GitHub API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hypnoticocelot.github; | |
import io.dropwizard.jackson.Jackson; | |
import io.dropwizard.jersey.jackson.JacksonMessageBodyProvider; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import java.util.Optional; | |
import static org.junit.Assert.assertEquals; | |
public class GithubClientTest { | |
@Rule | |
public GithubServiceRule github = new GithubServiceRule(); | |
private GithubClient githubClient; | |
@Before | |
public void configureClient() { | |
final Client client = ClientBuilder.newBuilder() | |
.register(new JacksonMessageBodyProvider(Jackson.newObjectMapper(), null)) | |
.build(); | |
githubClient = new GithubClientBuilder(client) | |
.baseUri(github.getServiceUri()) | |
.build(); | |
} | |
@Test | |
public void testGetPublicRepositories() throws Exception { | |
final Repository[] repositories = githubClient.getPublicRepositories(Optional.empty()); | |
assertEquals(100, repositories.length); | |
assertEquals(364, repositories[repositories.length - 1].getId()); | |
} | |
@Test | |
public void testRateLimitedRequest() throws Exception { | |
github.activateRateLimitFailures(); | |
try { | |
githubClient.getPublicRepositories(Optional.empty()); | |
} catch (RateLimitedException e) { | |
assertEquals(0, e.getRateLimitDetails().getRemaining()); | |
} | |
} | |
@Test(expected = FailedRequestException.class) | |
public void testFailedGetPublicRepositories() throws Exception { | |
github.activateFailedRequests(); | |
githubClient.getPublicRepositories(Optional.empty()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hypnoticocelot.github; | |
import io.dropwizard.testing.junit.DropwizardClientRule; | |
import org.junit.rules.TestRule; | |
import org.junit.runner.Description; | |
import org.junit.runners.model.Statement; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import java.io.IOException; | |
import java.util.concurrent.TimeUnit; | |
import static io.dropwizard.testing.FixtureHelpers.fixture; | |
public class GithubServiceRule implements TestRule { | |
private DropwizardClientRule clientRule; | |
private boolean rateLimitFailuresEnabled; | |
private boolean failedRequestsEnabled; | |
public GithubServiceRule() { | |
// Let reset() be the judge of default state. | |
reset(); | |
clientRule = new DropwizardClientRule( | |
new GithubResource() | |
); | |
} | |
@Override | |
public Statement apply(Statement base, Description description) { | |
reset(); | |
return clientRule.apply(base, description); | |
} | |
public String getServiceUri() { | |
return clientRule.baseUri().toString(); | |
} | |
public void activateRateLimitFailures() { | |
rateLimitFailuresEnabled = true; | |
} | |
public void activateFailedRequests() { | |
failedRequestsEnabled = true; | |
} | |
private void reset() { | |
rateLimitFailuresEnabled = false; | |
failedRequestsEnabled = false; | |
} | |
@Path("/repositories") | |
@Produces(MediaType.APPLICATION_JSON) | |
public class GithubResource { | |
@GET | |
public Response getPublicRepos(@QueryParam("since") @DefaultValue("-1") long since) throws IOException { | |
if (rateLimitFailuresEnabled) { | |
throw new WebApplicationException( | |
Response.status(Response.Status.FORBIDDEN) | |
.header("X-RateLimit-Limit", 5000) | |
.header("X-RateLimit-Remaining", 0) | |
.header("X-RateLimit-Reset", secondsFromNow(60*60)) | |
.build() | |
); | |
} else if (failedRequestsEnabled) { | |
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); | |
} | |
return Response.ok(fixture("com/hypnoticocelot/github/repositories.json")) | |
.header("X-RateLimit-Limit", 5000) | |
.header("X-RateLimit-Remaining", 4999) | |
.header("X-RateLimit-Reset", secondsFromNow(60*60)) | |
.build(); | |
} | |
private long secondsFromNow(long seconds) { | |
return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + seconds; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment