@Override protected String parseResponse(HttpURLConnection connection) throws Exception { try { return super.parseResponse(connection); } catch (AuthorizationException | UnprocessableEntityException e) { if (e instanceof AuthorizationException) { String errorMessage = new ErrorWithResponse(403, e.getMessage()).getMessage(); throw new AuthorizationException(errorMessage); } else { throw new ErrorWithResponse(422, e.getMessage()); } } } }
@Test public void fromGraphQLJson_doesNotBlowUpParsingBadJson() { String badJson = stringFromFixture("random_json.json"); ErrorWithResponse errorWithResponse = ErrorWithResponse.fromGraphQLJson(badJson); assertEquals("Parsing error response failed", errorWithResponse.getMessage()); }
@Test public void constructor_handlesTopLevelErrors() { String topLevelError = stringFromFixture("errors/auth_fingerprint_error.json"); ErrorWithResponse errorWithResponse = new ErrorWithResponse(422, topLevelError); assertEquals("Authorization fingerprint is invalid", errorWithResponse.getMessage()); assertEquals(1, errorWithResponse.getFieldErrors().size()); }
@Test public void fromGraphQLJson_parsesGraphQLCoercionErrorsCorrectly() { String response = stringFromFixture("errors/graphql/coercion_error.json"); ErrorWithResponse errorWithResponse = ErrorWithResponse.fromGraphQLJson(response); assertEquals("Variable 'input' has coerced Null value for NonNull type 'String!'", errorWithResponse.getMessage()); assertEquals(422, errorWithResponse.getStatusCode()); }
@Test public void constructor_doesNotBlowUpParsingBadJson() { String badJson = stringFromFixture("random_json.json"); ErrorWithResponse errorWithResponse = new ErrorWithResponse(422, badJson); assertEquals("Parsing error response failed", errorWithResponse.getMessage()); }
@Test public void parcelsCorrectly() throws JSONException { ErrorWithResponse error = ErrorWithResponse.fromJson(stringFromFixture("errors/credit_card_error_response.json")); Parcel parcel = Parcel.obtain(); error.writeToParcel(parcel, 0); parcel.setDataPosition(0); ErrorWithResponse parceled = ErrorWithResponse.CREATOR.createFromParcel(parcel); assertEquals(error.getStatusCode(), parceled.getStatusCode()); assertEquals(error.getMessage(), parceled.getMessage()); assertEquals(error.getErrorResponse(), parceled.getErrorResponse()); assertEquals(error.getFieldErrors().size(), parceled.getFieldErrors().size()); } }
@Test public void getErrors_returnsErrorString() { ThreeDSecureAuthenticationResponse authResponse = ThreeDSecureAuthenticationResponse.fromJson( stringFromFixture("three_d_secure/authentication_response_with_error.json")); ErrorWithResponse errors = new ErrorWithResponse(0, authResponse.getErrors()); assertNull(authResponse.getCardNonce()); assertFalse(authResponse.isSuccess()); assertEquals("Failed to authenticate, please try a different form of payment", errors.getMessage()); }
@Test public void parseResponseFailsWithUserErrors() throws Exception { String baseUrl = "http://example.com/graphql"; HttpClient httpClient = new BraintreeGraphQLHttpClient(baseUrl, TOKENIZATION_KEY); httpClient = HttpClientTestUtils.stubResponse(httpClient, 200, FixturesHelper.stringFromFixture("errors/graphql/credit_card_error.json")); HttpURLConnection connection = httpClient.init(baseUrl); try { httpClient.parseResponse(connection); fail("No exception was thrown"); } catch (ErrorWithResponse e) { assertEquals("Input is invalid.", e.getMessage()); assertNotNull(e.errorFor("creditCard")); } }
@Test public void constructor_parsesErrorsCorrectly() { String response = stringFromFixture("errors/credit_card_error_response.json"); ErrorWithResponse errorWithResponse = new ErrorWithResponse(422, response); assertEquals("Credit card is invalid", errorWithResponse.getMessage()); assertEquals(422, errorWithResponse.getStatusCode()); assertNull(errorWithResponse.errorFor("creditCard").errorFor("postalCode")); assertEquals("Credit card must include number, payment_method_nonce, or venmo_sdk_payment_method_code", errorWithResponse.errorFor("creditCard").errorFor("base").getMessage()); assertEquals("Credit card number is required", errorWithResponse.errorFor("creditCard").errorFor("number").getMessage()); assertEquals("Expiration year is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationYear").getMessage()); }
@Test public void fromGraphQLJson_parsesErrorsCorrectly() { String response = stringFromFixture("errors/graphql/credit_card_error.json"); ErrorWithResponse errorWithResponse = ErrorWithResponse.fromGraphQLJson(response); assertEquals("Input is invalid.", errorWithResponse.getMessage()); assertEquals(422, errorWithResponse.getStatusCode()); assertEquals("Expiration month is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationMonth").getMessage()); assertEquals("Expiration year is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationYear").getMessage()); assertEquals("CVV verification failed", errorWithResponse.errorFor("creditCard").errorFor("cvv").getMessage()); }
@Test public void fromJson_parsesErrorsCorrectly() throws JSONException { String response = stringFromFixture("errors/credit_card_error_response.json"); ErrorWithResponse errorWithResponse = ErrorWithResponse.fromJson(response); assertEquals("Credit card is invalid", errorWithResponse.getMessage()); assertNull(errorWithResponse.errorFor("creditCard").errorFor("postalCode")); assertEquals("Credit card must include number, payment_method_nonce, or venmo_sdk_payment_method_code", errorWithResponse.errorFor("creditCard").errorFor("base").getMessage()); assertEquals("Credit card number is required", errorWithResponse.errorFor("creditCard").errorFor("number").getMessage()); assertEquals("Expiration year is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationYear").getMessage()); }