use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testInterpolatePathWithoutParams.
@Test
public void testInterpolatePathWithoutParams() {
final MockClient client = new MockClient("apiKey");
final String path = "/accounts";
final String interpolatedPath = client.interpolatePath(path);
assertEquals("/accounts", interpolatedPath);
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testInvalidApiKey.
@Test
public void testInvalidApiKey() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(404, "Not Found", getErrorJson("invalid_api_key")));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
// This test is important because it ensures that application/json response errors are based on the json
// body's error type and not the status code based error
assertThrows(InvalidApiKeyException.class, () -> {
client.getResource("code-aaron");
});
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testNonJsonError500.
@Test
public void testNonJsonError500() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
Headers headers = new Headers.Builder().build();
MediaType contentType = MediaType.get("text/html; charset=UTF-8");
when(mCall.execute()).thenReturn(MockClient.buildResponse(500, "Internal Server Error", "<html>badness</html>", headers, contentType));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(InternalServerException.class, () -> {
client.getResource("code-aaron");
});
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class PagerTest method testCount.
@Test
public void testCount() throws IOException {
final Call mCall = mock(Call.class);
Headers headers = new Headers.Builder().set("Recurly-Total-Records", "1337").build();
Answer answer = (i) -> {
Request request = i.getArgument(0);
assertEquals("HEAD", request.method());
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(200, "OK", getResourceFirstItemJson(), headers));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
Pager<MyResource> pager = client.listResources(null);
int count = pager.getCount();
assertEquals(1337, count);
}
use of com.recurly.v3.fixtures.MockClient 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();
});
}
Aggregations