use of com.recurly.v3.fixtures.MyResource in project recurly-client-java by recurly.
the class PagerTest method testForEach.
@Test
public void testForEach() 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);
AtomicInteger count = new AtomicInteger(0);
pager.forEach(resource -> {
if (count.get() < 3) {
assertEquals("Resource Page 1", resource.getMyString());
} else {
assertEquals("Resource Page 2", resource.getMyString());
}
count.incrementAndGet();
});
}
use of com.recurly.v3.fixtures.MyResource 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.MyResource 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.MyResource 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.MyResource 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