Search in sources :

Example 6 with MockClient

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");
    });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Call(okhttp3.Call) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) Headers(okhttp3.Headers) MediaType(okhttp3.MediaType) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 7 with MockClient

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());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Call(okhttp3.Call) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) MyRequest(com.recurly.v3.fixtures.MyRequest) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) MockClient(com.recurly.v3.fixtures.MockClient) MyResource(com.recurly.v3.fixtures.MyResource) Test(org.junit.jupiter.api.Test)

Example 8 with MockClient

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");
    });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Call(okhttp3.Call) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 9 with MockClient

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();
    });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Call(okhttp3.Call) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 10 with MockClient

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());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Call(okhttp3.Call) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) TransactionException(com.recurly.v3.exception.TransactionException) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Aggregations

MockClient (com.recurly.v3.fixtures.MockClient)26 Test (org.junit.jupiter.api.Test)26 MyResource (com.recurly.v3.fixtures.MyResource)20 IOException (java.io.IOException)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 Request (okhttp3.Request)20 Answer (org.mockito.stubbing.Answer)20 HashMap (java.util.HashMap)18 ApiException (com.recurly.v3.ApiException)14 ExceptionFactory (com.recurly.v3.exception.ExceptionFactory)14 InternalServerException (com.recurly.v3.exception.InternalServerException)14 InvalidApiKeyException (com.recurly.v3.exception.InvalidApiKeyException)14 NotFoundException (com.recurly.v3.exception.NotFoundException)14 TransactionException (com.recurly.v3.exception.TransactionException)14 ValidationException (com.recurly.v3.exception.ValidationException)14 FixtureConstants (com.recurly.v3.fixtures.FixtureConstants)14 MockQueryParams (com.recurly.v3.fixtures.MockQueryParams)14 MyRequest (com.recurly.v3.fixtures.MyRequest)14 InputStream (java.io.InputStream)14 Field (java.lang.reflect.Field)14