diff --git a/build.gradle b/build.gradle index fd3879db..3fee09c6 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,12 @@ jacocoTestReport { compileJava { sourceCompatibility '1.7' targetCompatibility '1.7' + + options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked" << "-Werror" +} + +compileTestJava { + options.compilerArgs << "-Xlint:deprecation" << "-Werror" } buildscript { @@ -45,11 +51,6 @@ buildscript { classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' classpath 'gradle.plugin.com.auth0.gradle:oss-library:0.9.0' } - - tasks.withType(JavaCompile) { - options.compilerArgs << "-Xlint:deprecation" - options.compilerArgs << "-Werror" - } } repositories { diff --git a/src/main/java/com/auth0/exception/APIException.java b/src/main/java/com/auth0/exception/APIException.java index 02fd8ab6..12b51b23 100644 --- a/src/main/java/com/auth0/exception/APIException.java +++ b/src/main/java/com/auth0/exception/APIException.java @@ -136,7 +136,8 @@ private static String obtainExceptionMessage(Map values) { Object description = values.get("description"); if (description instanceof String) { return (String) description; - } else { + } else if (description instanceof Map){ + @SuppressWarnings("unchecked") PasswordStrengthErrorParser policy = new PasswordStrengthErrorParser((Map) description); return policy.getDescription(); } diff --git a/src/test/java/com/auth0/exception/APIExceptionTest.java b/src/test/java/com/auth0/exception/APIExceptionTest.java index 441b3622..6cfb99e4 100644 --- a/src/test/java/com/auth0/exception/APIExceptionTest.java +++ b/src/test/java/com/auth0/exception/APIExceptionTest.java @@ -3,6 +3,7 @@ import org.junit.Before; import org.junit.Test; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -13,16 +14,85 @@ public class APIExceptionTest { private Map values; + private final static int ERROR_CODE = 42; + private final static String EXPECTED_ERROR_MESSAGE_PREFIX = "Request failed with status code " + ERROR_CODE + ": "; + @Before public void setUp() { values = new HashMap<>(); } + @Test + public void shouldGetMessageFromErrorDescription() { + values.put("error_description", "some error description"); + values.put("description", "some description"); + values.put("message", "some message"); + values.put("error", "some error"); + APIException apiException = new APIException(values, ERROR_CODE); + assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some error description")); + } + + @Test + public void shouldGetMessageFromDescriptionAsString() { + values.put("message", "some message"); + values.put("error", "some error"); + APIException apiException = new APIException(values, ERROR_CODE); + assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some message")); + } + + @Test + public void shouldGetMessageFromDescriptionAsMap() { + Map rules = new HashMap<>(); + rules.put("verified", false); + rules.put("code", "lengthAtLeast"); + rules.put("format", Collections.singletonList(8)); + rules.put("message", "some password length message"); + + Map passwordStrengthError = new HashMap<>(); + passwordStrengthError.put("rules", Collections.singletonList(rules)); + + values.put("description", passwordStrengthError); + values.put("message", "some message"); + values.put("error", "some error"); + + APIException apiException = new APIException(values, ERROR_CODE); + assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some password length message")); + } + + @Test + public void shouldGetDefaultMessageWhenDescriptionNotMapOrString() { + values.put("description", Collections.singletonList("some description")); + + APIException apiException = new APIException(values, ERROR_CODE); + assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "Unknown exception")); + } + + @Test + public void shouldGetMessageFromMessage() { + values.put("message", "some message"); + values.put("error", "some error"); + APIException apiException = new APIException(values, ERROR_CODE); + assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some message")); + } + + @Test + public void shouldGetMessageFromError() { + values.put("error", "some error"); + APIException apiException = new APIException(values, ERROR_CODE); + assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "some error")); + } + + @Test + public void shouldGetDefaultMessage() { + APIException apiException = new APIException(values, ERROR_CODE); + assertThat(apiException.getMessage(), is(EXPECTED_ERROR_MESSAGE_PREFIX + "Unknown exception")); + } + @Test public void shouldBeMfaRequiredWithMfaTokenError() { values.put("error", "mfa_required"); values.put("mfa_token", "some-mfa-token"); - APIException apiException = new APIException(values, 42); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isMultifactorRequired(), is(true)); assertThat(apiException.getValue("mfa_token"), is("some-mfa-token")); } @@ -31,7 +101,7 @@ public void shouldBeMfaRequiredWithMfaTokenError() { public void shouldBeInvalidCredentialsErrorWhenInvalidGrant() { values.put("error", "invalid_grant"); values.put("error_description", "Wrong email or password."); - APIException apiException = new APIException(values, 42); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isInvalidCredentials(), is(true)); } @@ -39,7 +109,7 @@ public void shouldBeInvalidCredentialsErrorWhenInvalidGrant() { public void shouldBeInvalidCredentialsErrorWhenInvalidUserPassword() { values.put("error", "invalid_user_password"); values.put("error_description", "Wrong email or password."); - APIException apiException = new APIException(values, 42); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isInvalidCredentials(), is(true)); } @@ -47,21 +117,21 @@ public void shouldBeInvalidCredentialsErrorWhenInvalidUserPassword() { public void shouldNotBeInvalidCredentialsErrorWhenWrongDescription() { values.put("error", "invalid_grant"); values.put("error_description", "some message"); - APIException apiException = new APIException(values, 42); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isInvalidCredentials(), is(false)); } @Test public void shouldBeAccessDeniedError() { values.put("error", "access_denied"); - APIException apiException = new APIException(values, 42); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isAccessDenied(), is(true)); } @Test public void shouldBeMfaEnrollementRequiredError() { values.put("error", "unsupported_challenge_type"); - APIException apiException = new APIException(values, 42); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isMultifactorEnrollRequired(), is(true)); } @@ -69,7 +139,7 @@ public void shouldBeMfaEnrollementRequiredError() { public void shouldBeMfaTokenInvalidErrorForMalformedToken() { values.put("error", "invalid_grant"); values.put("error_description", "Malformed mfa_token"); - APIException apiException = new APIException(values, 42); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isMultifactorTokenInvalid(), is(true)); } @@ -77,7 +147,7 @@ public void shouldBeMfaTokenInvalidErrorForMalformedToken() { public void shouldBeMfaTokenInvalidErrorForExpiredToken() { values.put("error", "expired_token"); values.put("error_description", "mfa_token is expired"); - APIException apiException = new APIException(values, 1); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isMultifactorTokenInvalid(), is(true)); } @@ -85,7 +155,7 @@ public void shouldBeMfaTokenInvalidErrorForExpiredToken() { public void shouldNotBeMfaTokenInvalidErrorForExpiredTokenWhenWrongDescription() { values.put("error", "expired_token"); values.put("error_description", "some message"); - APIException apiException = new APIException(values, 1); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isMultifactorTokenInvalid(), is(false)); } @@ -93,20 +163,20 @@ public void shouldNotBeMfaTokenInvalidErrorForExpiredTokenWhenWrongDescription() public void shouldNotBeMfaTokenInvalidErrorForMalformedTokenWhenWrongDescription() { values.put("error", "invalid_grant"); values.put("error_description", "some message"); - APIException apiException = new APIException(values, 1); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isMultifactorTokenInvalid(), is(false)); } @Test public void shouldBeMfaEnrollementRequired() { values.put("error", "unsupported_challenge_type"); - APIException apiException = new APIException(values, 401); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isMultifactorEnrollRequired(), is(true)); } @Test public void checkErrorTypesShouldHandleNullError() { - APIException apiException = new APIException(values, 42); + APIException apiException = new APIException(values, ERROR_CODE); assertThat(apiException.isAccessDenied(), is(false)); assertThat(apiException.isInvalidCredentials(), is(false)); assertThat(apiException.isMultifactorRequired(), is(false));