Search in sources :

Example 1 with MockClient

use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.

the class BaseClientTest method testUnknownError.

@Test
public void testUnknownError() throws IOException {
    final Call mCall = mock(Call.class);
    Answer answer = (i) -> {
        return mCall;
    };
    final Response response = MockClient.buildResponse(999, "Unknown", getErrorJson("unknown"));
    when(mCall.execute()).thenReturn(response);
    OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
    final MockClient client = new MockClient("apiKey", mockOkHttpClient);
    // asserts that generic api exception is thrown for unknown error
    assertThrows(ApiException.class, () -> {
        client.getResource("code-aaron");
    });
    final RecurlyException exception = ExceptionFactory.getExceptionClass(response);
    assertTrue(exception.toString().contains("ApiException"));
}
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) Response(okhttp3.Response) Call(okhttp3.Call) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 2 with MockClient

use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.

the class BaseClientTest method testCantSetApiUrlWithoutRecurlyInsecure.

@Test
public void testCantSetApiUrlWithoutRecurlyInsecure() throws Exception {
    final HashMap<String, String> environmentVariables = new HashMap<String, String>();
    environmentVariables.put("RECURLY_INSECURE", "false");
    setEnv(environmentVariables);
    final MockClient client = new MockClient("apiKey");
    final String originalUrl = client.getApiUrl();
    final String newApiUrl = "https://my.base.url/";
    client._setApiUrl(newApiUrl);
    assertEquals(originalUrl, client.getApiUrl());
    environmentVariables.clear();
    environmentVariables.put("RECURLY_INSECURE", "true");
    setEnv(environmentVariables);
}
Also used : HashMap(java.util.HashMap) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 3 with MockClient

use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.

the class BaseClientTest method testInterpolatePathMatching.

@Test
public void testInterpolatePathMatching() {
    final MockClient client = new MockClient("apiKey");
    final String path = "/url_path/{url_path}";
    final HashMap<String, String> urlParams = new HashMap<String, String>();
    urlParams.put("url_path", "replacement");
    final String interpolatedPath = client.interpolatePath(path, urlParams);
    assertEquals("/url_path/replacement", interpolatedPath);
}
Also used : HashMap(java.util.HashMap) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 4 with MockClient

use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.

the class BaseClientTest method testNetworkErrorWithoutResource.

@Test
public void testNetworkErrorWithoutResource() 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.removeResource("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 5 with MockClient

use of com.recurly.v3.fixtures.MockClient in project recurly-client-java by recurly.

the class BaseClientTest method testSetApiUrl.

@Test
public void testSetApiUrl() {
    final MockClient client = new MockClient("apiKey");
    final String newApiUrl = "https://my.base.url/";
    client._setApiUrl(newApiUrl);
    assertEquals(newApiUrl, client.getApiUrl());
}
Also used : 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