diff --git a/README.md b/README.md index 0fdf098..6f3564f 100644 --- a/README.md +++ b/README.md @@ -559,6 +559,25 @@ TravelTimeSDK sdk = TravelTimeSDK .build(); ``` +## Development + +We use Maven as a build automation tool in this project. + +To run unit tests you can execute: + +```shell +mvn test +``` + +Please note that you will need to export some valid credentials as environment variables in your shell to make tests work: + +```shell +export APP_ID="..." +export API_KEY="..." +export PROTO_USERNAME="..." +export PROTO_PASSWORD="..." +``` + ## Support If you have problems, please write an issue or contact us by writing to support@traveltime.com diff --git a/src/main/java/com/traveltime/sdk/dto/responses/errors/IOError.java b/src/main/java/com/traveltime/sdk/dto/responses/errors/IOError.java index 22bf766..289ad61 100644 --- a/src/main/java/com/traveltime/sdk/dto/responses/errors/IOError.java +++ b/src/main/java/com/traveltime/sdk/dto/responses/errors/IOError.java @@ -5,6 +5,8 @@ import lombok.Getter; import lombok.NonNull; +import io.vavr.control.Option; + @Getter @AllArgsConstructor public class IOError implements TravelTimeError { @@ -14,6 +16,9 @@ public class IOError implements TravelTimeError { @NonNull String errorMessage; + @Override + public Option retrieveCause() { return Option.of(cause); } + @Override public String getMessage() { return errorMessage; diff --git a/src/main/java/com/traveltime/sdk/dto/responses/errors/JsonProcessingError.java b/src/main/java/com/traveltime/sdk/dto/responses/errors/JsonProcessingError.java index 3eea717..ffa26f7 100644 --- a/src/main/java/com/traveltime/sdk/dto/responses/errors/JsonProcessingError.java +++ b/src/main/java/com/traveltime/sdk/dto/responses/errors/JsonProcessingError.java @@ -1,5 +1,6 @@ package com.traveltime.sdk.dto.responses.errors; +import io.vavr.control.Option; import lombok.AllArgsConstructor; import lombok.NonNull; import lombok.ToString; @@ -10,6 +11,9 @@ public class JsonProcessingError implements TravelTimeError { @NonNull String errorMsg; + @Override + public Option retrieveCause() { return Option.none(); } + @Override public String getMessage() { return errorMsg; diff --git a/src/main/java/com/traveltime/sdk/dto/responses/errors/ProtoError.java b/src/main/java/com/traveltime/sdk/dto/responses/errors/ProtoError.java index 0070220..69d9ea1 100644 --- a/src/main/java/com/traveltime/sdk/dto/responses/errors/ProtoError.java +++ b/src/main/java/com/traveltime/sdk/dto/responses/errors/ProtoError.java @@ -1,5 +1,6 @@ package com.traveltime.sdk.dto.responses.errors; +import io.vavr.control.Option; import lombok.AllArgsConstructor; import lombok.NonNull; import lombok.ToString; @@ -10,6 +11,9 @@ public class ProtoError implements TravelTimeError { @NonNull String errorMsg; + @Override + public Option retrieveCause() { return Option.none(); } + @Override public String getMessage() { return errorMsg; diff --git a/src/main/java/com/traveltime/sdk/dto/responses/errors/RequestError.java b/src/main/java/com/traveltime/sdk/dto/responses/errors/RequestError.java index 154c328..7b99eea 100644 --- a/src/main/java/com/traveltime/sdk/dto/responses/errors/RequestError.java +++ b/src/main/java/com/traveltime/sdk/dto/responses/errors/RequestError.java @@ -1,5 +1,6 @@ package com.traveltime.sdk.dto.responses.errors; +import io.vavr.control.Option; import lombok.AllArgsConstructor; import lombok.NonNull; import lombok.ToString; @@ -10,6 +11,9 @@ public class RequestError implements TravelTimeError { @NonNull String errorMsg; + @Override + public Option retrieveCause() { return Option.none(); } + @Override public String getMessage() { return errorMsg; diff --git a/src/main/java/com/traveltime/sdk/dto/responses/errors/ResponseError.java b/src/main/java/com/traveltime/sdk/dto/responses/errors/ResponseError.java index 5ae5225..56aee71 100644 --- a/src/main/java/com/traveltime/sdk/dto/responses/errors/ResponseError.java +++ b/src/main/java/com/traveltime/sdk/dto/responses/errors/ResponseError.java @@ -1,6 +1,7 @@ package com.traveltime.sdk.dto.responses.errors; import com.fasterxml.jackson.annotation.JsonIgnore; +import io.vavr.control.Option; import lombok.*; import lombok.extern.jackson.Jacksonized; @@ -25,6 +26,10 @@ public class ResponseError implements TravelTimeError { @NonNull Map> additionalInfo; + @Override + @JsonIgnore + public Option retrieveCause() { return Option.none(); } + @Override @JsonIgnore public String getMessage() { diff --git a/src/main/java/com/traveltime/sdk/dto/responses/errors/TravelTimeError.java b/src/main/java/com/traveltime/sdk/dto/responses/errors/TravelTimeError.java index e247b3a..96f764a 100644 --- a/src/main/java/com/traveltime/sdk/dto/responses/errors/TravelTimeError.java +++ b/src/main/java/com/traveltime/sdk/dto/responses/errors/TravelTimeError.java @@ -1,5 +1,8 @@ package com.traveltime.sdk.dto.responses.errors; +import io.vavr.control.Option; + public interface TravelTimeError { + Option retrieveCause(); String getMessage(); } diff --git a/src/main/java/com/traveltime/sdk/dto/responses/errors/ValidationError.java b/src/main/java/com/traveltime/sdk/dto/responses/errors/ValidationError.java index 621edd2..9e5a675 100644 --- a/src/main/java/com/traveltime/sdk/dto/responses/errors/ValidationError.java +++ b/src/main/java/com/traveltime/sdk/dto/responses/errors/ValidationError.java @@ -1,5 +1,6 @@ package com.traveltime.sdk.dto.responses.errors; +import io.vavr.control.Option; import lombok.AllArgsConstructor; import lombok.NonNull; import lombok.ToString; @@ -10,6 +11,9 @@ public class ValidationError implements TravelTimeError { @NonNull String errorMsg; + @Override + public Option retrieveCause() { return Option.none(); } + @Override public String getMessage() { return errorMsg; diff --git a/src/main/java/com/traveltime/sdk/dto/responses/errors/XmlError.java b/src/main/java/com/traveltime/sdk/dto/responses/errors/XmlError.java index 87060ef..c576084 100644 --- a/src/main/java/com/traveltime/sdk/dto/responses/errors/XmlError.java +++ b/src/main/java/com/traveltime/sdk/dto/responses/errors/XmlError.java @@ -1,5 +1,6 @@ package com.traveltime.sdk.dto.responses.errors; +import io.vavr.control.Option; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; @@ -12,6 +13,9 @@ public class XmlError implements TravelTimeError { @NonNull Throwable cause; + @Override + public Option retrieveCause() { return Option.of(cause); } + @Override public String getMessage() { return cause.getMessage(); diff --git a/src/test/java/com/traveltime/sdk/ErrorTest.java b/src/test/java/com/traveltime/sdk/ErrorTest.java new file mode 100644 index 0000000..1c7a60e --- /dev/null +++ b/src/test/java/com/traveltime/sdk/ErrorTest.java @@ -0,0 +1,57 @@ +package com.traveltime.sdk; + +import com.traveltime.sdk.auth.TravelTimeCredentials; +import com.traveltime.sdk.dto.requests.MapInfoRequest; +import com.traveltime.sdk.dto.responses.MapInfoResponse; +import com.traveltime.sdk.dto.responses.errors.IOError; +import com.traveltime.sdk.dto.responses.errors.ResponseError; +import com.traveltime.sdk.dto.responses.errors.TravelTimeError; +import com.traveltime.sdk.dto.responses.errors.XmlError; +import io.vavr.control.Either; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class ErrorTest { + TravelTimeSDK sdk; + + @Before + public void init() { + TravelTimeCredentials credentials = new TravelTimeCredentials( + "wrong-api-id-does-not-exist-123456789", + "wrong-api-key-12345678" + ); + sdk = new TravelTimeSDK(credentials); + } + + @Test + public void shouldDetectErrorForWrongCredentials() { + MapInfoRequest request = new MapInfoRequest(); + Either response = sdk.send(request); + Assert.assertTrue(response.isLeft()); + Assert.assertTrue(response.getLeft() instanceof ResponseError); + ResponseError responseError = (ResponseError) response.getLeft(); + Assert.assertEquals(7, (int) responseError.getErrorCode()); + Assert.assertEquals(401, (int) responseError.getHttpStatus()); + Assert.assertTrue(responseError.retrieveCause().isEmpty()); + } + + @Test + public void shouldRetrieveCauseForIOError() { + String message = "Booom"; + Throwable cause = new IllegalStateException(message); + IOError ioError = new IOError(cause, "some error occurred"); + Assert.assertFalse(ioError.retrieveCause().isEmpty()); + Assert.assertSame(message, ioError.retrieveCause().get().getMessage()); + } + + @Test + public void shouldRetrieveCauseForXmlError() { + String message = "Booom"; + Throwable cause = new IllegalStateException(message); + XmlError xmlError = new XmlError(cause); + Assert.assertFalse(xmlError.retrieveCause().isEmpty()); + Assert.assertSame(message, xmlError.retrieveCause().get().getMessage()); + } +}