This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
148 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
api-sdk/src/main/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
This comment has been minimized.
Sorry, something went wrong.
PavloMyrotiuk
Contributor
|
||
{ | ||
public ApiException newException(String contents, int httpCode) | ||
{ | ||
ApiResponse<EmptyResponse> apiResponse = JsonReader.getApiResponse(contents, new TypeToken<ApiResponseWrapper<EmptyResponse>>() | ||
{ | ||
} | ||
); | ||
ApiCode apiCode = apiResponse.getCode(); | ||
List<String> messages = apiResponse.getMessages(); | ||
return new ApiException(messages, apiCode, httpCode); | ||
} | ||
|
||
public ApiException newException(IOException e) { | ||
return new ApiException(e, ApiCode.NETWORK_ERROR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
api-sdk/src/test/java/com/smartling/api/sdk/exceptions/ApiExceptionBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Discussed with @dlyash , enum is bad idea. It will cause exception if server add new code, revert to String