From 69ec5d706ba3f66ad9856cb14f0d8bc4a69b0a77 Mon Sep 17 00:00:00 2001 From: Oleksii Burov Date: Tue, 29 Sep 2015 16:49:13 +0300 Subject: [PATCH] #14 Added builder for exception. --- .../com/smartling/api/sdk/dto/ApiCode.java | 2 +- .../api/sdk/exceptions/ApiException.java | 37 +++++++++++- .../sdk/exceptions/ApiExceptionBuilder.java | 29 +++++++++ .../com/smartling/api/sdk/util/HttpUtils.java | 9 +-- .../exceptions/ApiExceptionBuilderTest.java | 59 +++++++++++++++++++ .../smartling/api/sdk/util/HttpUtilsTest.java | 33 +++++++---- 6 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilder.java create mode 100644 api-sdk/src/test/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilderTest.java diff --git a/api-sdk/src/main/java/com/smartling/api/sdk/dto/ApiCode.java b/api-sdk/src/main/java/com/smartling/api/sdk/dto/ApiCode.java index 15c33a9..9ad7a32 100644 --- a/api-sdk/src/main/java/com/smartling/api/sdk/dto/ApiCode.java +++ b/api-sdk/src/main/java/com/smartling/api/sdk/dto/ApiCode.java @@ -2,5 +2,5 @@ public enum ApiCode { - SUCCESS, VALIDATION_ERROR, AUTHENTICATION_ERROR, AUTHORIZATION_ERROR, RESOURCE_LOCKED, MAX_OPERATIONS_LIMIT_EXCEEDED, GENERAL_ERROR, MAINTENANCE_MODE_ERROR + SUCCESS, VALIDATION_ERROR, AUTHENTICATION_ERROR, AUTHORIZATION_ERROR, RESOURCE_LOCKED, MAX_OPERATIONS_LIMIT_EXCEEDED, GENERAL_ERROR, MAINTENANCE_MODE_ERROR, NETWORK_ERROR } diff --git a/api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiException.java b/api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiException.java index 471c9fb..09adebc 100644 --- a/api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiException.java +++ b/api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiException.java @@ -16,6 +16,13 @@ package com.smartling.api.sdk.exceptions; // TODO(AShesterov): refactor API-SDK: rename ApiException to SmartlingApiException + +import com.smartling.api.sdk.dto.ApiCode; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.List; + /** * Thrown when an exception has occurred when using the {@link com.smartling.api.sdk.FileApiClientAdapter}. */ @@ -23,13 +30,37 @@ public class ApiException extends Exception { private static final long serialVersionUID = -397098626101615761L; - public ApiException(final String message) + private ApiCode apiCode; + private int httpCode; + private List messages = new ArrayList<>(); + + ApiException(List messages, final ApiCode apiCode, int httpCode) { - super(message); + super(StringUtils.join(messages, " ,")); + this.messages = messages; + this.apiCode = apiCode; + this.httpCode = httpCode; } - public ApiException(final Exception e) + ApiException(final Exception e, ApiCode apiCode) { super(e); + messages.add(e.getMessage()); + this.apiCode = apiCode; + } + + public ApiCode getApiCode() + { + return apiCode; + } + + public int getHttpCode() + { + return httpCode; + } + + public List getMessages() + { + return messages; } } diff --git a/api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilder.java b/api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilder.java new file mode 100644 index 0000000..b1ad63a --- /dev/null +++ b/api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilder.java @@ -0,0 +1,29 @@ +package com.smartling.api.sdk.exceptions; + +import com.google.gson.reflect.TypeToken; +import com.smartling.api.sdk.JsonReader; +import com.smartling.api.sdk.dto.ApiCode; +import com.smartling.api.sdk.dto.ApiResponse; +import com.smartling.api.sdk.dto.ApiResponseWrapper; +import com.smartling.api.sdk.dto.EmptyResponse; + +import java.io.IOException; +import java.util.List; + +public class ApiExceptionBuilder +{ + public ApiException newException(String contents, int httpCode) + { + ApiResponse apiResponse = JsonReader.getApiResponse(contents, new TypeToken>() + { + } + ); + ApiCode apiCode = apiResponse.getCode(); + List messages = apiResponse.getMessages(); + return new ApiException(messages, apiCode, httpCode); + } + + public ApiException newException(IOException e) { + return new ApiException(e, ApiCode.NETWORK_ERROR); + } +} diff --git a/api-sdk/src/main/java/com/smartling/api/sdk/util/HttpUtils.java b/api-sdk/src/main/java/com/smartling/api/sdk/util/HttpUtils.java index 3dd65de..c59a6bf 100644 --- a/api-sdk/src/main/java/com/smartling/api/sdk/util/HttpUtils.java +++ b/api-sdk/src/main/java/com/smartling/api/sdk/util/HttpUtils.java @@ -18,7 +18,7 @@ import com.smartling.api.sdk.ProxyConfiguration; import com.smartling.api.sdk.dto.file.StringResponse; import com.smartling.api.sdk.exceptions.ApiException; - +import com.smartling.api.sdk.exceptions.ApiExceptionBuilder; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringUtils; @@ -85,15 +85,16 @@ public StringResponse executeHttpCall(final HttpRequestBase httpRequest, final P final String charset = EntityUtils.getContentCharSet(response.getEntity()); final StringResponse stringResponse = inputStreamToString(response.getEntity().getContent(), charset); - if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode == HttpStatus.SC_OK) return stringResponse; - throw new ApiException(stringResponse.getContents()); + throw new ApiExceptionBuilder().newException(stringResponse.getContents(), statusCode); } catch (final IOException ioe) { logger.error(String.format(LOG_MESSAGE_ERROR_TEMPLATE, ioe.getMessage())); - throw new ApiException(ioe); + throw new ApiExceptionBuilder().newException(ioe); } finally { diff --git a/api-sdk/src/test/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilderTest.java b/api-sdk/src/test/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilderTest.java new file mode 100644 index 0000000..b102866 --- /dev/null +++ b/api-sdk/src/test/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilderTest.java @@ -0,0 +1,59 @@ +package com.smartling.api.sdk.exceptions; + +import com.smartling.api.sdk.dto.ApiCode; +import org.junit.Test; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class ApiExceptionBuilderTest +{ + + public static final String ERROR_RESPONSE = "{\"response\":{\"data\":null,\"code\":\"VALIDATION_ERROR\",\"messages\":[\"apiKey parameter is required\",\"apiVersion parameter is required\"]}}"; + + private final ApiExceptionBuilder testedInstance = new ApiExceptionBuilder(); + + @Test + public void shouldReturnExceptionUsingContentsAndHttpCode() + { + ApiException apiException = testedInstance.newException(ERROR_RESPONSE, 0); + + assertThat(apiException, instanceOf(ApiException.class)); + } + + @Test + public void shouldRetrieveCode() + { + ApiException apiException = testedInstance.newException(ERROR_RESPONSE, 0); + + assertThat(apiException.getApiCode(), is(equalTo(ApiCode.VALIDATION_ERROR))); + } + + @Test + public void shouldRetrieveMessages() + { + ApiException apiException = testedInstance.newException(ERROR_RESPONSE, 0); + + assertThat(apiException.getMessages(), hasItems(equalTo("apiKey parameter is required"), equalTo("apiVersion parameter is required"))); + } + + @Test + public void shouldPassHttpCode() + { + ApiException apiException = testedInstance.newException(ERROR_RESPONSE, 123); + + assertThat(apiException.getHttpCode(), is(123)); + } + + @Test + public void shouldSetNetworkErrorCodeInCaseIoException() { + ApiException apiException = testedInstance.newException(new IOException("Some exception")); + + assertThat(apiException.getApiCode(), is(ApiCode.NETWORK_ERROR)); + } +} \ No newline at end of file diff --git a/api-sdk/src/test/java/com/smartling/api/sdk/util/HttpUtilsTest.java b/api-sdk/src/test/java/com/smartling/api/sdk/util/HttpUtilsTest.java index e2b7103..385227b 100644 --- a/api-sdk/src/test/java/com/smartling/api/sdk/util/HttpUtilsTest.java +++ b/api-sdk/src/test/java/com/smartling/api/sdk/util/HttpUtilsTest.java @@ -15,14 +15,9 @@ */ package com.smartling.api.sdk.util; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; - +import com.smartling.api.sdk.ProxyConfiguration; +import com.smartling.api.sdk.dto.file.StringResponse; +import com.smartling.api.sdk.exceptions.ApiException; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.StatusLine; @@ -35,9 +30,19 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; -import com.smartling.api.sdk.ProxyConfiguration; -import com.smartling.api.sdk.dto.file.StringResponse; -import com.smartling.api.sdk.exceptions.ApiException; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class HttpUtilsTest { @@ -50,7 +55,9 @@ public class HttpUtilsTest private HttpEntity httpEntity; private StatusLine statusLine; - private static final String TEST_RESPONSE = "test response"; + private static final String TEST_RESPONSE = "{\"response\":{\"data\":null,\"code\":\"VALIDATION_ERROR\",\"messages\":[\"apiKey parameter is required\"]}}"; + private static final String TEST_MESSAGE = "apiKey parameter is required"; + private static final String HOST = "host"; private static final String PASSWORD = "password"; private static final String PORT = "5000"; @@ -107,7 +114,7 @@ public void testExecuteHttpCall404() throws ApiException, ClientProtocolExceptio } catch (ApiException e) { - assertEquals(TEST_RESPONSE, e.getMessage()); + assertEquals(TEST_MESSAGE, e.getMessage()); } }