Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion riskified-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.riskified</groupId>
<artifactId>riskified-sdk</artifactId>
<version>5.0.1-rc.3</version>
<version>5.0.1-rc.4</version>
<name>Riskified SDK</name>
<description>Riskified rest api SDK for java</description>
<url>https://www.riskified.com</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,28 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
*/
private static class AuthenticationResultTypeAdapter extends TypeAdapter<AuthenticationResult> {
private final TypeAdapter<AuthenticationResult> delegateAdapter;
private final Map<String, String> propertyMap = new HashMap<>();
private final Map<String, String> keyConversionMap = new HashMap<>();

AuthenticationResultTypeAdapter(Gson gson, TypeAdapterFactory skipPast) {
// Get delegate adapter to avoid infinite recursion
this.delegateAdapter = gson.getDelegateAdapter(skipPast,
TypeToken.get(AuthenticationResult.class));

this.populateMap();
this.populateKeyConversionMap();

}

private void populateMap() {
this.propertyMap.put("tranStatus", "trans_status");
this.propertyMap.put("tran_status", "trans_status");
private void populateKeyConversionMap() {
this.keyConversionMap.put("tranStatus", "transStatus");
this.keyConversionMap.put("tran_status", "transStatus");

this.propertyMap.put("tranStatusReason", "trans_status_reason");
this.propertyMap.put("tran_status_reason", "trans_status_reason");
this.keyConversionMap.put("tranStatusReason", "transStatusReason");
this.keyConversionMap.put("tran_status_reason", "transStatusReason");

this.propertyMap.put("threeDChallenge", "three_d_challenge");
this.keyConversionMap.put("three_d_challenge", "threeDChallenge");

this.propertyMap.put("tra_exemption", "TRA_exemption");
this.propertyMap.put("traExemption", "TRA_exemption");

this.propertyMap.put("liabilityShift", "liability_shift");

this.propertyMap.put("createdAt", "created_at");
this.keyConversionMap.put("tra_exemption", "TRA_exemption");
this.keyConversionMap.put("traExemption", "TRA_exemption");
}

@Override
Expand Down Expand Up @@ -105,8 +101,8 @@ public AuthenticationResult read(JsonReader in) throws IOException {
String key = entry.getKey();
JsonElement value = entry.getValue();

if (this.propertyMap.containsKey(key)) {
transformed.add(this.propertyMap.get(key), value);
if (this.keyConversionMap.containsKey(key)) {
transformed.add(this.keyConversionMap.get(key), value);
} else {
transformed.add(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ public class AuthenticationResultAdapterTest {
@Before
public void setUp() {
gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
}

@Test
public void testLegacyFormatAllOldFields() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"eci\":\"05\"," +
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"tran_status\":\"Y\"," +
"\"tran_status_reason\":\"01\",\"liability_shift\":true," +
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{\"eci\":\"05\"," +
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"tranStatus\":\"Y\"," +
"\"tranStatusReason\":\"01\",\"liabilityShift\":true," +
"\"three_d_challenge\":false,\"tra_exemption\":true}}";

CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);
Expand All @@ -52,10 +51,10 @@ public void testLegacyFormatAllOldFields() {
@Test
public void testCurrentFormatAllNewFields() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"eci\":\"05\"," +
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"trans_status\":\"Y\"," +
"\"trans_status_reason\":\"01\",\"liability_shift\":true," +
"\"three_d_challenge\":false,\"TRA_exemption\":true}}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{\"eci\":\"05\"," +
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"transStatus\":\"Y\"," +
"\"transStatusReason\":\"01\",\"liabilityShift\":true," +
"\"threeDChallenge\":false,\"TRA_exemption\":true}}";

CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);

Expand All @@ -75,9 +74,9 @@ public void testCurrentFormatAllNewFields() {
@Test
public void testMixedFormat() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"eci\":\"05\"," +
"\"tran_status\":\"N\",\"trans_status_reason\":\"02\"," +
"\"liability_shift\":false,\"three_d_challenge\":true,\"tra_exemption\":false}}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{\"eci\":\"05\"," +
"\"tranStatus\":\"N\",\"transStatusReason\":\"02\"," +
"\"liabilityShift\":false,\"three_d_challenge\":true,\"tra_exemption\":false}}";

CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);

Expand All @@ -96,7 +95,7 @@ public void testMixedFormat() {
@Test
public void testNullAuthenticationResult() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":null}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":null}";

CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);

Expand All @@ -118,82 +117,50 @@ public void testMissingAuthenticationResult() {
@Test
public void testEnumValues() {
String json1 = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"tran_status\":\"A\"}}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{\"tranStatus\":\"A\"}}";
CreditCardPaymentDetails details1 = gson.fromJson(json1, CreditCardPaymentDetails.class);
assertEquals("TransStatus should be A", TransStatus.A, details1.getAuthenticationResults().getTransStatus());

String json2 = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"trans_status\":\"U\"}}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{\"transStatus\":\"U\"}}";
CreditCardPaymentDetails details2 = gson.fromJson(json2, CreditCardPaymentDetails.class);
assertEquals("TransStatus should be U", TransStatus.U, details2.getAuthenticationResults().getTransStatus());

String json3 = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"tran_status_reason\":\"03\"}}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{\"tranStatusReason\":\"03\"}}";
CreditCardPaymentDetails details3 = gson.fromJson(json3, CreditCardPaymentDetails.class);
assertEquals("TransStatusReason should be Zero_Three", TransStatusReason.Zero_Three,
details3.getAuthenticationResults().getTransStatusReason());
}

@Test
public void testRoundTripSerialization() {
AuthenticationResult result = new AuthenticationResult("05");
result.setCavv("AAABCZIhcQAAAABZlyFxAAAAAAA=");
result.setTransStatus(TransStatus.Y);
result.setTransStatusReason(TransStatusReason.Zero_One);
result.setLiabilityShift(true);
result.set3DChallenge(false);
result.setTRAExemption(true);

CreditCardPaymentDetails details = new CreditCardPaymentDetails("123456", "Y", "M", "****1234", "Visa");
details.setAuthenticationResult(result);

String json = gson.toJson(details);

assertTrue("Serialized JSON should contain trans_status", json.contains("\"trans_status\""));
assertTrue("Serialized JSON should contain trans_status_reason", json.contains("\"trans_status_reason\""));
assertTrue("Serialized JSON should contain TRA_exemption", json.contains("\"TRA_exemption\""));
assertFalse("Serialized JSON should NOT contain tran_status", json.contains("\"tran_status\""));
assertFalse("Serialized JSON should NOT contain tran_status_reason", json.contains("\"tran_status_reason\""));
assertFalse("Serialized JSON should NOT contain tra_exemption", json.contains("\"tra_exemption\""));

CreditCardPaymentDetails deserialized = gson.fromJson(json, CreditCardPaymentDetails.class);

assertNotNull("Deserialized details should not be null", deserialized);
assertNotNull("Deserialized authentication result should not be null", deserialized.getAuthenticationResults());
AuthenticationResult deserializedResult = deserialized.getAuthenticationResults();
assertEquals("ECI should match after round-trip", "05", deserializedResult.getEci());
assertEquals("TransStatus should match after round-trip", TransStatus.Y, deserializedResult.getTransStatus());
assertEquals("TransStatusReason should match after round-trip", TransStatusReason.Zero_One, deserializedResult.getTransStatusReason());
}

@Test(expected = JsonParseException.class)
public void testInvalidTypeString() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":\"invalid\"}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":\"invalid\"}";

gson.fromJson(json, CreditCardPaymentDetails.class);
}

@Test(expected = JsonParseException.class)
public void testInvalidTypeArray() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":[{\"eci\":\"05\"}]}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":[{\"eci\":\"05\"}]}";

gson.fromJson(json, CreditCardPaymentDetails.class);
}

@Test(expected = JsonParseException.class)
public void testInvalidTypeNumber() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":12345}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":12345}";

gson.fromJson(json, CreditCardPaymentDetails.class);
}

@Test
public void testEmptyObject() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{}}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{}}";

CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);

Expand All @@ -205,9 +172,9 @@ public void testEmptyObject() {
@Test
public void testAllFieldsPresent() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"eci\":\"05\"," +
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"tran_status\":\"Y\"," +
"\"tran_status_reason\":\"01\",\"liability_shift\":true," +
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{\"eci\":\"05\"," +
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"tranStatus\":\"Y\"," +
"\"tranStatusReason\":\"01\",\"liabilityShift\":true," +
"\"three_d_challenge\":false,\"tra_exemption\":true}}";

CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);
Expand All @@ -229,8 +196,8 @@ public void testAllFieldsPresent() {
public void testBooleanFieldTypes() {
// Test with true values
String json1 = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{" +
"\"liability_shift\":true,\"three_d_challenge\":true,\"tra_exemption\":true}}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{" +
"\"liabilityShift\":true,\"three_d_challenge\":true,\"tra_exemption\":true}}";

CreditCardPaymentDetails details1 = gson.fromJson(json1, CreditCardPaymentDetails.class);
AuthenticationResult result1 = details1.getAuthenticationResults();
Expand All @@ -240,8 +207,8 @@ public void testBooleanFieldTypes() {

// Test with false values
String json2 = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{" +
"\"liability_shift\":false,\"three_d_challenge\":false,\"tra_exemption\":false}}";
"\"credit_card_company\":\"Visa\",\"authenticationResult\":{" +
"\"liabilityShift\":false,\"three_d_challenge\":false,\"tra_exemption\":false}}";

CreditCardPaymentDetails details2 = gson.fromJson(json2, CreditCardPaymentDetails.class);
AuthenticationResult result2 = details2.getAuthenticationResults();
Expand All @@ -254,8 +221,8 @@ public void testBooleanFieldTypes() {
public void testPaypalPaymentDetails() {
String json = "{\"payer_email\":\"test@example.com\",\"payer_status\":\"verified\"," +
"\"payer_address_status\":\"confirmed\",\"protection_eligibility\":\"eligible\"," +
"\"authentication_result\":{\"eci\":\"07\",\"tran_status\":\"Y\"," +
"\"tran_status_reason\":\"01\",\"liability_shift\":true," +
"\"authenticationResult\":{\"eci\":\"07\",\"tranStatus\":\"Y\"," +
"\"tranStatusReason\":\"01\",\"liabilityShift\":true," +
"\"three_d_challenge\":false,\"tra_exemption\":true}}";

PaypalPaymentDetails details = gson.fromJson(json, PaypalPaymentDetails.class);
Expand All @@ -271,27 +238,4 @@ public void testPaypalPaymentDetails() {
assertFalse("3D challenge should be false", result.get3DChallenge());
assertTrue("TRA exemption should be true", result.getTRAExemption());
}

@Test
public void testLegacyFormatCamelCase() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"eci\":\"05\"," +
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"tranStatus\":\"Y\"," +
"\"tranStatusReason\":\"01\",\"liability_shift\":true," +
"\"threeDChallenge\":false,\"traExemption\":true}}";

CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);

assertNotNull("Payment details should not be null", details);
assertNotNull("Authentication result should not be null", details.getAuthenticationResults());

AuthenticationResult result = details.getAuthenticationResults();
assertEquals("ECI should match", "05", result.getEci());
assertEquals("CAVV should match", "AAABCZIhcQAAAABZlyFxAAAAAAA=", result.getCavv());
assertEquals("TransStatus should be Y", TransStatus.Y, result.getTransStatus());
assertEquals("TransStatusReason should be Zero_One", TransStatusReason.Zero_One, result.getTransStatusReason());
assertTrue("Liability shift should be true", result.getLiabilityShift());
assertFalse("3D challenge should be false", result.get3DChallenge());
assertTrue("TRA exemption should be true", result.getTRAExemption());
}
}