/*
*
*/
package com.damnhandy.uri.template.examples;
import java.net.InetAddress;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import com.damnhandy.uri.template.UriTemplate;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Request;
import com.ning.http.client.RequestBuilder;
/**
* <p>
* Some simple tests to format {@link UriTemplate} for the various GitHub APIs. This test assumes
* that the following system properties are set:
* </p>
* <pre>
* -Dgithub.username=<your github username>
* -Dgithub.password=<your github password>
* </pre>
* <p>
* If the properties are set, the test will not fail the build.
* </p>
* <p>This test demonstrates the utility of a single URI template that can be used
* to express the majority of the functionality offered by the GitHub API. For this test,
* the following template expression is used:
* </p>
* <pre>
* /repos{/user,repo,function,id}{?page,per_page}
* </pre>
*
*
* @author <a href="ryan@damnhandy.com">Ryan J. McDonough</a>
* @version $Revision: 1.1 $
*/
public class TestGitHubApis extends AbstractExampleTest
{
public static final String BASE = "https://api.github.com";
public static final String PATH_EXPRESSION = "/repos{/user,repo,function,id}";
public static final String PAGINATION = "{?page,per_page}";
/**
* Checks assumptions that the username and password properties are set and that
* the host is reachable.
*
*/
@BeforeClass
public static void setUp() throws Exception
{
Assume.assumeTrue(InetAddress.getByName("api.github.com").isReachable(1000));
Assume.assumeNotNull(System.getProperty("github.username"),
System.getProperty("github.password"));
}
@Test
public void testUserRepos() throws Exception
{
RequestBuilder builder = new RequestBuilder("GET");
Request request = builder.setUrl(
UriTemplate.fromTemplate(BASE)
.append("/users/{user}/repos")
.append(PAGINATION)
.set("user", "damnhandy")
.set("repo", "repos")
.set("page", 1)
.set("per_page", 4)
.expand()).build();
Assert.assertEquals("https://api.github.com/users/damnhandy/repos?page=1&per_page=4", request.getUrl());
executeRequest(createClient(), request);
}
/**
* This test demonstrates that while the expression defined 4 variables but
* we only supply 3 values, the expanded URI will not render a dangling "/"
* and generate a proper URI for the GitHub API.
*
* @throws Exception
*/
@Test
public void testCommitsApi() throws Exception
{
RequestBuilder builder = new RequestBuilder("GET");
Request request = builder.setUrl(
UriTemplate.fromTemplate(BASE)
.append(PATH_EXPRESSION)
.set("user", "damnhandy")
.set("repo", "Handy-URI-Templates")
.set("function","commits")
.expand()).build();
Assert.assertEquals("https://api.github.com/repos/damnhandy/Handy-URI-Templates/commits", request.getUrl());
executeRequest(createClient(), request);
}
/**
* Looks at a specific commit
*
* @throws Exception
*/
@Test
public void testCommitsApiWithSpecificCommit() throws Exception
{
RequestBuilder builder = new RequestBuilder("GET");
Request request = builder.setUrl(
UriTemplate.fromTemplate(BASE)
.append(PATH_EXPRESSION)
.set("user", "damnhandy")
.set("repo", "Handy-URI-Templates")
.set("function","commits")
.set("id","7cdf7ff75f8ede138228ceff7f5a1c18a5835b94")
.expand()).build();
Assert.assertEquals("https://api.github.com/repos/damnhandy/Handy-URI-Templates/commits/7cdf7ff75f8ede138228ceff7f5a1c18a5835b94", request.getUrl());
executeRequest(createClient(), request);
}
protected AsyncHttpClient createClient() {
return this.createClient(System.getProperty("github.username"), System.getProperty("github.password"));
}
}