Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: Failed to decompress the comparison field in case insensitive mode #63

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Map<List<NodeEntity>, String> getJSONParseResult(Object obj, RulesConfig
stringAndCompressParse.setNameToLower(rulesConfig.isNameToLower());
stringAndCompressParse.setPluginJarUrl(rulesConfig.getPluginJarUrl());
stringAndCompressParse.setTransFormConfigMap(rulesConfig.getTransformConfigMap());
stringAndCompressParse.getJSONParse(obj, obj);
stringAndCompressParse.getJSONParse(obj, obj, null);
// Convert field names in JSONObject to lowercase
if (rulesConfig.isNameToLower()) {
NameConvertUtil.nameConvert(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void setPluginJarUrl(String pluginJarUrl) {
this.pluginJarUrl = pluginJarUrl;
}

public void getJSONParse(Object obj, Object preObj) {
public void getJSONParse(Object obj, Object preObj, String currentName) {
if (obj == null || obj instanceof NullNode) {
return;
}
Expand All @@ -62,15 +62,15 @@ public void getJSONParse(Object obj, Object preObj) {
for (String fieldName : names) {
currentNode.add(new NodeEntity(nameToLower ? fieldName.toLowerCase() : fieldName, 0));
Object objFieldValue = jsonObject.get(fieldName);
getJSONParse(objFieldValue, obj);
getJSONParse(objFieldValue, obj, fieldName);
ListUti.removeLast(currentNode);
}
} else if (obj instanceof ArrayNode) {
ArrayNode objArray = (ArrayNode) obj;
for (int i = 0; i < objArray.size(); i++) {
currentNode.add(new NodeEntity(null, i));
Object element = objArray.get(i);
getJSONParse(element, obj);
getJSONParse(element, obj, String.valueOf(i));
ListUti.removeLast(currentNode);
}

Expand All @@ -96,10 +96,9 @@ public void getJSONParse(Object obj, Object preObj) {
return;
}
if (Objects.equals(objectBooleanPair.getValue(), Boolean.TRUE)) {
getJSONParse(objectBooleanPair.getKey(), preObj);
getJSONParse(objectBooleanPair.getKey(), preObj, null);
}

String currentName = getCurrentName(currentNode);
if (preObj instanceof ObjectNode) {
((ObjectNode) preObj).set(currentName, objectBooleanPair.getKey());
original.put(new ArrayList<>(currentNode), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,21 @@ public static boolean stringListEqualsOnWildcard(List<String> listA, List<String
}

public static List<String> convertToStringList(List<NodeEntity> list) {
return convertToStringList(list,false);
}

public static List<String> convertToStringList(List<NodeEntity> list, boolean ignoreCase) {
if (list == null || list.size() == 0) {
return Collections.emptyList();
}
List<String> nodes = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getNodeName() != null) {
nodes.add(list.get(i).getNodeName());
if (ignoreCase) {
nodes.add(list.get(i).getNodeName().toLowerCase());
} else {
nodes.add(list.get(i).getNodeName());
}
}
}
return nodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.arextest.diff.model.script.ScriptCompareConfig;
import com.arextest.diff.model.script.ScriptCompareConfig.ScriptMethod;
import com.arextest.diff.model.script.ScriptContentInfo;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -1025,7 +1027,8 @@ public void testScriptCompare3() {

CompareOptions compareOptions = new CompareOptions();
compareOptions.putScriptCompareConfig(
new ScriptCompareConfig(Arrays.asList("score"), new ScriptMethod("func_67356dbc7ac2aa763be9f8af", ""))
new ScriptCompareConfig(Arrays.asList("score"),
new ScriptMethod("func_67356dbc7ac2aa763be9f8af", ""))
);

String baseMsg = "{\n"
Expand Down Expand Up @@ -1097,4 +1100,45 @@ public void testScriptValueMissing() {
Assertions.assertEquals(2, compare.getLogs().size());
}

@Test
public void testScriptCompare4() throws Exception {
CompareSDK compareSDK = new CompareSDK();
compareSDK.getGlobalOptions()
.putNameToLower(true)
.putNullEqualsEmpty(true)
.putPluginJarUrl("./lib/arex-compare-sdk-plugin-0.1.0-jar-with-dependencies.jar");

String baseMsg = "{\"UPPERCASE\":\"{\\\"customerCurrency\\\":\\\"CNY\\\"}\",\"otherNode\":\"otherNode1\"}";
String testMsg = "{\"UPPERCASE\":\"{\\\"customerCurrency\\\":\\\"USD\\\"}\",\"otherNode\":\"otherNode2\"}";

CompareOptions options = new CompareOptions();

CompareResult result = compareSDK.compare(baseMsg, testMsg, options);
Assertions.assertEquals(2, result.getLogs().size());
Assertions.assertEquals(2, result.getLogs().get(0).getPathPair().getLeftUnmatchedPath().size());
Assertions.assertEquals(2,
result.getLogs().get(0).getPathPair().getRightUnmatchedPath().size());
Assertions.assertEquals("CNY", result.getLogs().get(0).getBaseValue());
Assertions.assertEquals("USD", result.getLogs().get(0).getTestValue());
}


@Test
public void testScriptCompare5() throws Exception {
CompareSDK compareSDK = new CompareSDK();
compareSDK.getGlobalOptions()
.putNameToLower(true)
.putNullEqualsEmpty(true)
.putPluginJarUrl("./lib/arex-compare-sdk-plugin-0.1.0-jar-with-dependencies.jar");

String baseMsg = "{\"root\":{\"UPPERCASE\":\"{\\\"customerCurrency\\\":\\\"CNY\\\"}\",\"otherNode\":\"otherNode1\"}}";
String testMsg = "{\"root\":{\"UPPERCASE\":\"{\\\"customerCurrency\\\":\\\"USD\\\"}\",\"otherNode\":\"otherNode2\"}}";

CompareOptions options = new CompareOptions();

CompareResult result = compareSDK.compare(baseMsg, testMsg, options);
Assertions.assertEquals(2, result.getParseNodePaths().get("root.uppercase").size());
Assertions.assertEquals("{\"customerCurrency\":\"CNY\"}", result.getParseNodePaths().get("root.uppercase").get(0));
}

}
Loading