Skip to content

Commit

Permalink
Crankshaft[BU0000019TRHGY] Client - Java @ 2015-06-30 16:52:10 +0000
Browse files Browse the repository at this point in the history
7d0cda5 Matt Thomson <mattjohnthomson@gmail.com>
Merge pull request #9 from gocardless/wrapped-lists

Make executeWrapped available for list requests

----------------

2b171a1 Matt Thomson <mattthomson@gocardless.com>
Make executeWrapped available for list requests

----------------
  • Loading branch information
Crankshaft Robot committed Jun 30, 2015
1 parent f77a9ea commit a070f63
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/com/gocardless/http/ListRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public S execute() {
return executor.execute(this, getHttpClient());
}

/**
* Executes this request.
*
* Returns a {@link com.gocardless.http.ApiResponse} that wraps the
* response entity.
*
* @throws com.gocardless.GoCardlessException
*/
public ApiResponse<S> executeWrapped() {
return executor.executeWrapped(this, getHttpClient());
}

@Override
protected ListResponse<T> parseResponse(Reader stream, ResponseParser responseParser) {
return responseParser.parsePage(stream, getEnvelope(), getTypeToken());
Expand Down Expand Up @@ -80,6 +92,8 @@ protected void setLimit(Integer limit) {

public interface ListRequestExecutor<S, T> {
S execute(ListRequest<S, T> request, HttpClient client);

ApiResponse<S> executeWrapped(ListRequest<S, T> request, HttpClient client);
}

public static <T> ListRequestExecutor<ListResponse<T>, T> pagingExecutor() {
Expand All @@ -89,6 +103,12 @@ public ListResponse<T> execute(ListRequest<ListResponse<T>, T> request,
HttpClient client) {
return client.execute(request);
}

@Override
public ApiResponse<ListResponse<T>> executeWrapped(
ListRequest<ListResponse<T>, T> request, HttpClient client) {
return client.executeWrapped(request);
}
};
}

Expand All @@ -98,6 +118,13 @@ public static <T> ListRequestExecutor<Iterable<T>, T> iteratingExecutor() {
public Iterable<T> execute(ListRequest<Iterable<T>, T> request, HttpClient client) {
return new PaginatingIterable<>(request, client);
}

@Override
public ApiResponse<Iterable<T>> executeWrapped(ListRequest<Iterable<T>, T> request,
HttpClient client) {
throw new IllegalStateException(
"executeWrapped not available when iterating through list responses");
}
};
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/gocardless/http/ListRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.assertj.core.api.Assertions.assertThat;

public class ListRequestTest {
@Rule
public MockHttp http = new MockHttp();
@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void shouldPerformListRequest() throws Exception {
Expand All @@ -31,6 +34,18 @@ public void shouldPerformListRequest() throws Exception {
http.assertRequestMade("GET", "/dummy?id=123");
}

@Test
public void shouldPerformWrappedListRequest() throws Exception {
http.enqueueResponse(200, "fixtures/page.json", ImmutableMap.of("foo", "bar"));
DummyListRequest<ListResponse<DummyItem>> request =
DummyListRequest.pageRequest(http.client());
ApiResponse<ListResponse<DummyItem>> result = request.executeWrapped();
assertThat(result.getStatusCode()).isEqualTo(200);
assertThat(result.getHeaders().get("foo")).containsExactly("bar");
assertThat(result.getResource().getItems()).hasSize(2);
http.assertRequestMade("GET", "/dummy?id=123");
}

@Test
public void shouldBeAbleToIterateThroughList() throws Exception {
http.enqueueResponse(200, "fixtures/first-page.json");
Expand All @@ -50,6 +65,14 @@ public void shouldBeAbleToIterateThroughList() throws Exception {
http.assertRequestMade("GET", "/dummy?after=ID123&id=123");
}

@Test
public void shouldNotAllowExecuteWrappedWhenIterating() {
DummyListRequest<Iterable<DummyItem>> request =
DummyListRequest.iterableRequest(http.client());
exception.expect(IllegalStateException.class);
request.executeWrapped();
}

static class DummyListRequest<S> extends ListRequest<S, DummyItem> {
private DummyListRequest(HttpClient httpClient, ListRequestExecutor<S, DummyItem> executor) {
super(httpClient, executor);
Expand Down

0 comments on commit a070f63

Please sign in to comment.