use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testNonJsonError0.
@Test
public void testNonJsonError0() 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(0, "Not A Real Status", "<html>badness</html>", headers, contentType));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(ApiException.class, () -> {
client.getResource("code-aaron");
});
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testMakeRequestWithBody.
@Test
public void testMakeRequestWithBody() throws IOException {
final Call mCall = mock(Call.class);
AtomicBoolean postCalled = new AtomicBoolean(false);
AtomicBoolean putCalled = new AtomicBoolean(false);
Answer answer = (i) -> {
Request request = i.getArgument(0);
HttpUrl url = request.url();
switch(request.method()) {
case "POST":
assertEquals("/resources", url.url().getPath());
postCalled.set(true);
break;
case "PUT":
assertEquals("/resources/someId", url.url().getPath());
putCalled.set(true);
break;
default:
// Any other request method is a failure
Assert.fail();
}
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(200, "OK", getResponseJson())).thenReturn(MockClient.buildResponse(200, "OK", getResponseJson()));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
final MyRequest newResource = new MyRequest();
newResource.setMyString("aaron");
final MyResource resource = client.createResource(newResource);
assertEquals(MyResource.class, resource.getClass());
assertEquals("aaron", resource.getMyString());
assertTrue(postCalled.get());
final MyResource anotherResource = client.updateResource("someId", newResource);
assertEquals(MyResource.class, anotherResource.getClass());
assertEquals("aaron", anotherResource.getMyString());
assertTrue(putCalled.get());
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testNetworkError.
@Test
public void testNetworkError() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenThrow(new IOException());
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(NetworkException.class, () -> {
client.getResource("code-aaron");
});
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testBadMethodError.
@Test
public void testBadMethodError() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenThrow(new IOException());
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(IllegalArgumentException.class, () -> {
client.badRequestMethod();
});
}
use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.
the class BaseClientTest method testTransactionError.
@Test
public void testTransactionError() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(422, "Unprocessable Entity", getErrorResponse("transaction")));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
TransactionException t = assertThrows(TransactionException.class, () -> {
client.removeResource("code-aaron");
});
assertEquals("mbca9aaao6xr", t.getError().getTransactionError().getTransactionId());
}
Aggregations