Fix correlation failure for-MigrateTest.Export by sorting json arrays
MODIFY
test/common/src/main/java/io/apiman/test/common/json/JsonCompare.java
test/common/src/main/java/io/apiman/test/common/json/JsonCompare.java
package io.apiman.test.common.json;
import java.io.InputStream;
-import java.util.LinkedHashMap;
-import java.util.Map;
+import java.util.*;
import org.json.JSONArray;
import org.json.JSONException;
+import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
@@ -65,6 +65,12 @@ public class JsonCompare {
return;
}
+ if (key.equals("PolicyDefinitions")){
+ // With this we will achieve that the order of the actual an expected json is always the same
+ expected = sortJsonArrayAlphabetically(expected, "policyImpl");
+ actual = sortJsonArrayAlphabetically(actual,"policyImpl");
+ }
+
super.recursivelyCompareJSONArray(key, expected, actual, result);
}
@@ -115,6 +121,35 @@ public class JsonCompare {
}
}
+ /**
+ * This will sort a json array in alphabetical order.
+ * @param jsonArray
+ * @return sorted jsonArray
+ * @throws JSONException
+ */
+ private JSONArray sortJsonArrayAlphabetically(JSONArray jsonArray, String key) throws JSONException {
+
+ List<JSONObject> jsonValues = new ArrayList<>();
+ for (int i = 0; i < jsonArray.length(); i++) {
+ jsonValues.add(jsonArray.getJSONObject(i));
+ }
+
+ Collections.sort(jsonValues, (o1, o2) -> {
+ String valA;
+ String valB;
+ try {
+ valA = (String) o1.get(key);
+ valB = (String) o2.get(key);
+ }
+ catch (JSONException e) {
+ throw new RuntimeException(e);
+ }
+ return valA.compareTo(valB);
+ });
+
+ return new JSONArray(jsonValues);
+ }
+
/**
* Asserts that the JSON document matches what we expected.

Fix correlation failure for-MigrateTest.Export by sorting json arrays (#745)