Search in sources :

Example 6 with MyResource

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();
    });
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) Request(okhttp3.Request) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) okhttp3(okhttp3) Assertions(org.junit.jupiter.api.Assertions) MyResource(com.recurly.v3.fixtures.MyResource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Answer(org.mockito.stubbing.Answer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Request(okhttp3.Request) MockClient(com.recurly.v3.fixtures.MockClient) MyResource(com.recurly.v3.fixtures.MyResource) Test(org.junit.jupiter.api.Test)

Example 7 with MyResource

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);
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) Request(okhttp3.Request) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) okhttp3(okhttp3) Assertions(org.junit.jupiter.api.Assertions) MyResource(com.recurly.v3.fixtures.MyResource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Answer(org.mockito.stubbing.Answer) Request(okhttp3.Request) MockClient(com.recurly.v3.fixtures.MockClient) MyResource(com.recurly.v3.fixtures.MyResource) Test(org.junit.jupiter.api.Test)

Example 8 with MyResource

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()));
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) Request(okhttp3.Request) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) okhttp3(okhttp3) Assertions(org.junit.jupiter.api.Assertions) MyResource(com.recurly.v3.fixtures.MyResource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) MyResource(com.recurly.v3.fixtures.MyResource) Test(org.junit.jupiter.api.Test)

Example 9 with MyResource

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());
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) Request(okhttp3.Request) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) okhttp3(okhttp3) Assertions(org.junit.jupiter.api.Assertions) MyResource(com.recurly.v3.fixtures.MyResource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) MyResource(com.recurly.v3.fixtures.MyResource) Test(org.junit.jupiter.api.Test)

Example 10 with MyResource

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());
}
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) Request(okhttp3.Request) 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)

Aggregations

MyResource (com.recurly.v3.fixtures.MyResource)11 Test (org.junit.jupiter.api.Test)11 MockClient (com.recurly.v3.fixtures.MockClient)9 IOException (java.io.IOException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Request (okhttp3.Request)9 Answer (org.mockito.stubbing.Answer)9 NoSuchElementException (java.util.NoSuchElementException)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 okhttp3 (okhttp3)6 Assertions (org.junit.jupiter.api.Assertions)6 Mockito (org.mockito.Mockito)6 HashMap (java.util.HashMap)4 MediaType (okhttp3.MediaType)4 ApiException (com.recurly.v3.ApiException)3 ExceptionFactory (com.recurly.v3.exception.ExceptionFactory)3 InternalServerException (com.recurly.v3.exception.InternalServerException)3 InvalidApiKeyException (com.recurly.v3.exception.InvalidApiKeyException)3 NotFoundException (com.recurly.v3.exception.NotFoundException)3 TransactionException (com.recurly.v3.exception.TransactionException)3