Skip to content

Commit

Permalink
[PW-8014] Removed Error Deserialization (#961)
Browse files Browse the repository at this point in the history
* removed error deserialization and added IT's

* remove unused imports

* fix the tests that broke with new exception

* create separate field for responseBody
  • Loading branch information
jillingk authored Feb 10, 2023
1 parent fefb9d4 commit 71e2a01
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 66 deletions.
14 changes: 13 additions & 1 deletion src/main/java/com/adyen/service/exception/ApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ApiException extends Exception {
private int statusCode;

private Map<String, List<String>> responseHeaders;
private String responseBody;

public ApiException(String message, int statusCode) {
super(message);
Expand Down Expand Up @@ -65,8 +66,19 @@ public void setStatusCode(int statusCode) {
public Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
}

public String getResponseBody() {
return responseBody;
}
public void setResponseBody(String responseBody) {
this.responseBody = responseBody;
}
@Override
public String toString() {
return "ApiException{" + "error=" + error + ", statusCode=" + statusCode + ", message=" + getMessage() + ", responseHeaders=" + getResponseHeaders() + "}";
return "ApiException{" + "error=" + error +
", statusCode=" + statusCode +
", message=" + getMessage() +
", responseHeaders=" + getResponseHeaders() +
", responseBody=" + getResponseBody() + "}";
}
}
17 changes: 1 addition & 16 deletions src/main/java/com/adyen/service/resource/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@
import com.adyen.constants.ApiConstants;
import com.adyen.httpclient.ClientInterface;
import com.adyen.httpclient.HTTPClientException;
import com.adyen.model.ApiError;
import com.adyen.model.RequestOptions;
import com.adyen.service.exception.ApiException;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -110,25 +107,13 @@ public String request(String json, RequestOptions requestOptions, ApiConstants.H
public String request(String json, RequestOptions requestOptions, ApiConstants.HttpMethod httpMethod, Map<String, String> pathParams, Map<String, String> queryString) throws ApiException, IOException {
ClientInterface clientInterface = service.getClient().getHttpClient();
Config config = service.getClient().getConfig();
String responseBody;
ApiException apiException;

try {
return clientInterface.request(resolve(pathParams), json, config, service.isApiKeyRequired(), requestOptions, httpMethod, queryString);
} catch (HTTPClientException e) {
responseBody = e.getResponseBody();
apiException = new ApiException(e.getMessage(), e.getCode(), e.getResponseHeaders());
apiException.setResponseBody(e.getResponseBody());
}

// Enhance ApiException with more info from JSON payload
try {
ApiError apiError = GSON.fromJson(responseBody, new TypeToken<ApiError>() {
}.getType());
apiException.setError(apiError);
} catch (JsonSyntaxException ignored) {
throw new ApiException("Invalid response or an invalid X-API-Key key was used", apiException.getStatusCode());
}

throw apiException;
}

Expand Down
20 changes: 8 additions & 12 deletions src/test/java/com/adyen/BinLookupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public void TestGet3dsAvailabilityInvalidMerchantMocked() throws Exception {
binLookup.get3dsAvailability(threeDSAvailabilityRequest);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("901", e.getError().getErrorCode());
assertEquals(403, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertEquals(403, e.getStatusCode());
}
}

Expand Down Expand Up @@ -136,9 +135,8 @@ public void TestGetCostEstimateInvalidMerchantMocked() throws Exception {
binLookup.getCostEstimate(costEstimateRequest);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("901", e.getError().getErrorCode());
assertEquals(500, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertEquals(500, e.getStatusCode());
}
}

Expand Down Expand Up @@ -171,9 +169,8 @@ public void TestGetCostEstimateInvalidCardNumberMocked() throws Exception {
binLookup.getCostEstimate(costEstimateRequest);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("000", e.getError().getErrorCode());
assertEquals(422, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertEquals(422, e.getStatusCode());
}
}

Expand Down Expand Up @@ -203,9 +200,8 @@ public void TestGetCostEstimateInvalidAmountMocked() throws Exception {
binLookup.getCostEstimate(costEstimateRequest);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("100", e.getError().getErrorCode());
assertEquals(422, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertEquals(422, e.getStatusCode());
}
}

Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/adyen/ErrorHandlingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.adyen;

import com.adyen.enums.Environment;
import com.adyen.model.checkout.PaymentLinkResponse;
import com.adyen.model.checkout.ServiceError;
import com.adyen.model.management.AllowedOriginsResponse;
import com.adyen.model.management.CreateAllowedOriginRequest;
import com.adyen.model.management.JSON;
import com.adyen.model.management.RestServiceError;
import com.adyen.service.Checkout;
import com.adyen.service.exception.ApiException;
import com.adyen.service.management.MyApiCredential;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;

public class ErrorHandlingTest extends BaseTest{
@Test
@Ignore("Integration test")
public void addAllowedOriginFail() throws IOException, ApiException {
Client client = new Client(System.getenv("API_KEY"), Environment.TEST);
MyApiCredential service = new MyApiCredential(client);

CreateAllowedOriginRequest createAllowedOriginRequest = new CreateAllowedOriginRequest();
createAllowedOriginRequest.setDomain("https://google.com");
try
{
service.addAllowedOrigin(createAllowedOriginRequest);
} catch(ApiException e) {
Assert.assertTrue(e.getResponseBody().contains("Invalid allowed origin information provided."));
}
}

@Test
@Ignore("Integration test")
public void CheckoutErrorTest() throws IOException, ApiException {
Client client = new Client(System.getenv("API_KEY"), Environment.TEST);
Checkout service = new Checkout(client);
try
{
service.getPaymentLinks("1234");
} catch(ApiException e) {
Assert.assertTrue(e.getResponseBody().contains("Invalid payment link ID"));
}
}
}
6 changes: 1 addition & 5 deletions src/test/java/com/adyen/MarketPayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -982,11 +982,7 @@ public void TestTransferFundsErrorInvalid() throws Exception {
fund.transferFunds(transferFundsRequest);
fail("Exception expected");
} catch (ApiException e) {
assertEquals("8515898092864171", e.getError().getPspReference());
List<ErrorFieldType> invalidFieldsList = e.getError().getInvalidFields();
assertEquals(2, invalidFieldsList.size());
assertEquals(new Integer(29), invalidFieldsList.get(0).getErrorCode());
assertEquals("Account code does not exist or invalid", invalidFieldsList.get(0).getErrorDescription());
assertTrue(e.getResponseBody().contains("Account code does not exist or invalid"));
}

}
Expand Down
31 changes: 14 additions & 17 deletions src/test/java/com/adyen/PaymentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,8 @@ public void TestAuthoriseError010Mocked() throws Exception {
payment.authorise(paymentRequest);
fail("Exception expected");
} catch (ApiException e) {
String errorCode = e.getError().getErrorCode();
assertEquals("010", errorCode);

int status = e.getError().getStatus();
assertEquals(403, status);
assertTrue(e.getResponseBody().contains("010"));
assertEquals(403, e.getStatusCode());
}
}

Expand Down Expand Up @@ -363,9 +360,9 @@ public void TestGetAuthenticationResultErrorOldAuthentication() throws IOExcepti
payment.getAuthenticationResult(authenticationResultRequest);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("15_024", e.getError().getErrorCode());
assertEquals(422, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertTrue(e.getResponseBody().contains("15_024"));
assertEquals(422, e.getStatusCode());
}
}

Expand All @@ -379,9 +376,9 @@ public void TestGetAuthenticationResultErrorNotFound() throws IOException {
payment.getAuthenticationResult(authenticationResultRequest);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("15_012", e.getError().getErrorCode());
assertEquals(422, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertTrue(e.getResponseBody().contains("15_012"));
assertEquals(422, e.getStatusCode());
}
}

Expand All @@ -396,9 +393,9 @@ public void TestGetAuthenticationResultErrorInvalidPsp() throws IOException {
payment.getAuthenticationResult(authenticationResultRequest);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("15_011", e.getError().getErrorCode());
assertEquals(422, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertTrue(e.getResponseBody().contains("15_011"));
assertEquals(422, e.getStatusCode());
}
}

Expand All @@ -413,9 +410,9 @@ public void TestGetAuthenticationResultErrorNotAllowed() throws IOException {
payment.getAuthenticationResult(authenticationResultRequest);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("010", e.getError().getErrorCode());
assertEquals(403, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertTrue(e.getResponseBody().contains("010"));
assertEquals(403, e.getStatusCode());
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/test/java/com/adyen/PayoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static com.adyen.constants.ApiConstants.AdditionalData.FRAUD_RESULT_TYPE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class PayoutTest extends BaseTest {
Expand Down Expand Up @@ -122,9 +123,9 @@ public void testPayoutErrorMerchant() throws Exception {
payout.payout(request);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("901", e.getError().getErrorCode());
assertEquals(403, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertTrue(e.getResponseBody().contains("901"));
assertEquals(403, e.getStatusCode());
}
}

Expand All @@ -139,9 +140,9 @@ public void testPayoutErrorReference() throws Exception {
payout.payout(request);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("130", e.getError().getErrorCode());
assertEquals(422, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertTrue(e.getResponseBody().contains("130"));
assertEquals(422, e.getStatusCode());
}
}
}
13 changes: 7 additions & 6 deletions src/test/java/com/adyen/PosPaymentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class PosPaymentTest extends BaseTest {
Expand All @@ -54,9 +55,9 @@ public void testConnectedTerminalsErrorInvalidMerchant() throws Exception {
posPayment.connectedTerminals(request);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("901", e.getError().getErrorCode());
assertEquals(403, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertTrue(e.getResponseBody().contains("901"));
assertEquals(403, e.getStatusCode());
}
}

Expand All @@ -71,9 +72,9 @@ public void testConnectedTerminalsErrorMissingMerchant() throws Exception {
posPayment.connectedTerminals(request);
fail("Exception expected");
} catch (ApiException e) {
assertNotNull(e.getError());
assertEquals("702", e.getError().getErrorCode());
assertEquals(500, e.getError().getStatus());
assertNotNull(e.getResponseBody());
assertTrue(e.getResponseBody().contains("702"));
assertEquals(500, e.getStatusCode());
}
}
}
5 changes: 3 additions & 2 deletions src/test/java/com/adyen/RecurringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class RecurringTest extends BaseTest {
Expand Down Expand Up @@ -115,7 +116,7 @@ public void testDisable803() throws IOException {
fail("Exception expected!");
} catch (ApiException e) {
assertNotEquals(200, e.getStatusCode());
assertEquals("803", e.getError().getErrorCode());
assertTrue(e.getResponseBody().contains("803"));
}
}

Expand Down Expand Up @@ -145,7 +146,7 @@ public void testScheduleAccountUpdater130() throws IOException {
fail("Exception expected!");
} catch (ApiException e) {
assertNotEquals(200, e.getStatusCode());
assertEquals("130", e.getError().getErrorCode());
assertTrue(e.getResponseBody().contains("130"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/adyen/service/ResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testRequestException() throws IOException {
fail("Expected exception");
} catch (ApiException e) {
assertEquals(403, e.getStatusCode());
assertEquals("010", e.getError().getErrorCode());
assertTrue(e.getResponseBody().contains("010"));
}
}
}

0 comments on commit 71e2a01

Please sign in to comment.