use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class PagerTest method testForLoop.
@Test
public void testForLoop() throws IOException {
final Call mCall = mock(Call.class);
AtomicBoolean firstCalled = new AtomicBoolean(false);
Answer answer = (i) -> {
Request request = i.getArgument(0);
HttpUrl url = request.url();
if (firstCalled.get()) {
assertEquals("/next", url.url().getPath());
}
firstCalled.set(true);
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(200, "OK", getResourceFirstPageJson("/next"))).thenReturn(MockClient.buildResponse(200, "OK", getResourceSecondPageJson()));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
Pager<MyResource> pager = client.listResources(null);
int count = 0;
for (MyResource res : pager) {
if (count < 3) {
assertEquals("Resource Page 1", res.getMyString());
} else {
assertEquals("Resource Page 2", res.getMyString());
}
count++;
}
assertEquals(5, count);
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class PagerTest method testEachItem.
@Test
public void testEachItem() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(200, "OK", getResourceSecondPageJson()));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
Pager<MyResource> pager = client.listResources(null);
pager.eachItem(resource -> assertNotNull(resource.getMyString()));
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class PagerTest method testEmptyList.
@Test
public void testEmptyList() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(200, "OK", getEmptyListJson()));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
Pager<MyResource> pager = client.listResources(null);
assertEquals(0, pager.getData().size());
for (MyResource myResource : pager) {
// This should not throw NullPointerException
myResource.getMyString();
}
pager.forEach(myResource -> myResource.getMyString());
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testNotFoundError.
@Test
public void testNotFoundError() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(404, "Not Found", getErrorJson("not_found")));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(NotFoundException.class, () -> {
client.getResource("code-aaron");
});
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testMakeRequestWithResource.
@Test
public void testMakeRequestWithResource() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
Request request = i.getArgument(0);
HttpUrl url = request.url();
assertEquals("GET", request.method());
assertEquals("/resources/code-aaron", url.url().getPath());
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(200, "OK", getResponseJson()));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
final MyResource resource = client.getResource("code-aaron");
assertEquals(MyResource.class, resource.getClass());
}
Aggregations