Skip to content

Commit

Permalink
Issue skyscreamer#109 unique key for an arrays of json objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergei Argutin committed Sep 7, 2023
1 parent 7414e90 commit de8416f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/skyscreamer/jsonassert/Customization.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@
public final class Customization {
private final Pattern path;
private final ValueMatcher<Object> comparator;
private String arrayUniqueKey;

public Customization(String path, ValueMatcher<Object> comparator) {
assert path != null;
assert comparator != null;
this.path = Pattern.compile(buildPattern(path));
this.comparator = comparator;
}
public Customization(String path, String arrayUniqueKey) {
assert path != null;
this.path = Pattern.compile(buildPattern(path));
this.comparator = null;
this.arrayUniqueKey=arrayUniqueKey;
}

public String getArrayUniqueKey() {
return arrayUniqueKey;
}

private String buildPattern(String path) {
return buildPatternLevel1(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject exp

protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
String uniqueKey = findUniqueKey(expected);
doCompareJSONArrayOfJsonObjects(key, expected, actual, result, uniqueKey);
}

protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual, JSONCompareResult result, String uniqueKey) throws JSONException {
doCompareJSONArrayOfJsonObjects(key, expected, actual, result, uniqueKey);
}

private void doCompareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual,
JSONCompareResult result, String uniqueKey) throws JSONException {
if (uniqueKey == null || !isUsableAsUniqueKey(uniqueKey, actual)) {
// An expensive last resort
recursivelyCompareJSONArray(key, expected, actual, result);
Expand All @@ -103,7 +112,6 @@ protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSO
}
}
}

protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
Map<Object, Integer> expectedCount = JSONCompareUtil.getCardinalityMap(jsonArrayToList(expected));
Map<Object, Integer> actualCount = JSONCompareUtil.getCardinalityMap(jsonArrayToList(actual));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.skyscreamer.jsonassert.comparator;

import org.json.JSONArray;
import org.json.JSONException;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONCompareMode;
Expand All @@ -35,7 +36,7 @@ public CustomComparator(JSONCompareMode mode, Customization... customizations)
@Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException {
Customization customization = getCustomization(prefix);
if (customization != null) {
if (customization != null && customization.getArrayUniqueKey() == null) {
try {
if (!customization.matches(prefix, actualValue, expectedValue, result)) {
result.fail(prefix, expectedValue, actualValue);
Expand All @@ -45,7 +46,12 @@ public void compareValues(String prefix, Object expectedValue, Object actualValu
result.fail(prefix, e);
}
} else {
super.compareValues(prefix, expectedValue, actualValue, result);
String arrayUniqueKey = customization == null ? null : customization.getArrayUniqueKey();
if (expectedValue instanceof JSONArray && arrayUniqueKey != null) {
compareJSONArrayOfJsonObjects(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result, arrayUniqueKey);
} else {
super.compareValues(prefix, expectedValue, actualValue, result);
}
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/test/java/org/skyscreamer/jsonassert/JSONArrayObjectsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.skyscreamer.jsonassert;


import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.skyscreamer.jsonassert.comparator.CustomComparator;

public class JSONArrayObjectsTest {

public static final UUID RANDOM_ID_1 = UUID.randomUUID();
public static final UUID RANDOM_ID_2 = UUID.randomUUID();
public static final String ARRAY_NAME = "arrayName";
public static final String ID = "id";
public static final String DATE = "date";

@Test
public void testJsonArrayWithIgnoredFieldAndProvidedIdField() throws JSONException {

JSONArray jsonArray1 = getJsonArray(RANDOM_ID_1, RANDOM_ID_2);
JSONArray jsonArray2 = getJsonArray(RANDOM_ID_2, RANDOM_ID_1);

JSONObject jsonObject1 = new JSONObject();
jsonObject1.put(ARRAY_NAME, jsonArray1);

JSONObject jsonObject2 = new JSONObject();
jsonObject2.put(ARRAY_NAME, jsonArray2);

CustomComparator customComparator = new CustomComparator(
JSONCompareMode.LENIENT,
new Customization("**." + DATE, getDummyMatcher()), //ignoring this field
new Customization(ARRAY_NAME, ID)
);

JSONAssert.assertEquals(jsonObject1, jsonObject2, customComparator);
}

private static ValueMatcher<Object> getDummyMatcher() {
return new ValueMatcher<Object>() {
@Override
public boolean equal(Object o1, Object o2) {
return true;
}
};
}

private static JSONArray getJsonArray(UUID randomId2, UUID randomId1) throws JSONException {
JSONArray jsonArray1 = new JSONArray();

JSONObject jsonObject1 = new JSONObject();
jsonObject1.put(ID, randomId2);
jsonObject1.put(DATE, UUID.randomUUID());

JSONObject jsonObject2 = new JSONObject();
jsonObject2.put(ID, randomId1);
jsonObject2.put(DATE, UUID.randomUUID());

jsonArray1.put(jsonObject1);
jsonArray1.put(jsonObject2);
return jsonArray1;
}
}

0 comments on commit de8416f

Please sign in to comment.