From 38e3d303d8f42cb2fecc806435c21c9d6e38079d Mon Sep 17 00:00:00 2001
From: Rui Chen <44330697+coryhh@users.noreply.github.com>
Date: Tue, 21 May 2024 17:34:55 +0800
Subject: [PATCH] feat: add the switch of "onlyCompareExistListElements" to
only compare the matched list elements (#58)
---
arex-compare-core/pom.xml | 2 +-
.../arextest/diff/handler/log/LogProcess.java | 22 +++++++++
.../OnlyExistListElementFilter.java | 15 ++++++
.../log/register/LogRegisterCondition.java | 11 +++--
.../arextest/diff/model/CompareOptions.java | 14 ++++++
.../arextest/diff/model/GlobalOptions.java | 14 ++++++
.../com/arextest/diff/model/RulesConfig.java | 10 ++++
.../diff/utils/DataBaseCompareUtil.java | 14 +-----
.../diff/utils/NormalCompareUtil.java | 10 +---
.../diff/utils/OptionsToRulesConvert.java | 6 +++
.../com/arextest/diff/sdk/CompareSDKTest.java | 48 +++++++++++++++++++
pom.xml | 2 +-
12 files changed, 140 insertions(+), 28 deletions(-)
create mode 100644 arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/OnlyExistListElementFilter.java
diff --git a/arex-compare-core/pom.xml b/arex-compare-core/pom.xml
index 5ba1b2c..9477835 100644
--- a/arex-compare-core/pom.xml
+++ b/arex-compare-core/pom.xml
@@ -5,7 +5,7 @@
arex-compare-parent
com.arextest
- 0.2.13
+ 0.2.14
4.0.0
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/handler/log/LogProcess.java b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/LogProcess.java
index 549ed4b..ce78c1b 100644
--- a/arex-compare-core/src/main/java/com/arextest/diff/handler/log/LogProcess.java
+++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/LogProcess.java
@@ -1,7 +1,11 @@
package com.arextest.diff.handler.log;
import com.arextest.diff.factory.PluginServiceFactory;
+import com.arextest.diff.handler.log.filterrules.IPFilter;
+import com.arextest.diff.handler.log.filterrules.OnlyCompareSameColumnsFilter;
+import com.arextest.diff.handler.log.filterrules.OnlyExistListElementFilter;
import com.arextest.diff.handler.log.filterrules.UnmatchedTypeFilter;
+import com.arextest.diff.handler.log.filterrules.UuidFilter;
import com.arextest.diff.model.RulesConfig;
import com.arextest.diff.model.log.LogEntity;
import com.arextest.diff.plugin.LogEntityFilter;
@@ -38,6 +42,24 @@ public void appendFilterRules(List> rules) {
this.filterRules.addAll(rules);
}
+ public void appendOtherFilterRules(RulesConfig rulesConfig) {
+ if (this.filterRules == null) {
+ this.filterRules = new ArrayList<>();
+ }
+ if (rulesConfig.isUuidIgnore()) {
+ this.filterRules.add(new UuidFilter());
+ }
+ if (rulesConfig.isIpIgnore()) {
+ this.filterRules.add(new IPFilter());
+ }
+ if (rulesConfig.isOnlyCompareCoincidentColumn()) {
+ this.filterRules.add(new OnlyCompareSameColumnsFilter());
+ }
+ if (rulesConfig.isOnlyCompareExistListElements()) {
+ this.filterRules.add(new OnlyExistListElementFilter());
+ }
+ }
+
public boolean process(List logEntities) {
Stream stream = logEntities.stream();
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/OnlyExistListElementFilter.java b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/OnlyExistListElementFilter.java
new file mode 100644
index 0000000..8372ed1
--- /dev/null
+++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/OnlyExistListElementFilter.java
@@ -0,0 +1,15 @@
+package com.arextest.diff.handler.log.filterrules;
+
+import com.arextest.diff.model.enumeration.ErrorType;
+import com.arextest.diff.model.log.LogEntity;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+public class OnlyExistListElementFilter implements Predicate {
+
+ @Override
+ public boolean test(LogEntity logEntity) {
+ return !Objects.equals(logEntity.getLogTag().getErrorType(), ErrorType.LIST_LEFT_MISSING)
+ && !Objects.equals(logEntity.getLogTag().getErrorType(), ErrorType.LIST_RIGHT_MISSING);
+ }
+}
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/handler/log/register/LogRegisterCondition.java b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/register/LogRegisterCondition.java
index 932ae62..57a884c 100644
--- a/arex-compare-core/src/main/java/com/arextest/diff/handler/log/register/LogRegisterCondition.java
+++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/register/LogRegisterCondition.java
@@ -19,7 +19,7 @@ public static boolean rejectRegister(Object obj1, Object obj2, LogMarker logMark
|| accordingNullEqualsNotExist(obj1, obj2, logMarker, compareContext);
}
- public static boolean accordingNullEqualsEmpty(Object obj1, Object obj2, LogMarker logMarker,
+ private static boolean accordingNullEqualsEmpty(Object obj1, Object obj2, LogMarker logMarker,
CompareContext compareContext) {
if (!compareContext.notDistinguishNullAndEmpty) {
@@ -38,7 +38,7 @@ public static boolean accordingNullEqualsEmpty(Object obj1, Object obj2, LogMark
}
}
- public static boolean accordingNullEqualsNotExist(Object obj1, Object obj2, LogMarker logMarker,
+ private static boolean accordingNullEqualsNotExist(Object obj1, Object obj2, LogMarker logMarker,
CompareContext compareContext) {
if (!compareContext.nullEqualsNotExist) {
@@ -48,7 +48,8 @@ public static boolean accordingNullEqualsNotExist(Object obj1, Object obj2, LogM
switch (logMarker) {
case RIGHT_OBJECT_MISSING:
case LEFT_OBJECT_MISSING:
- return judgeNullAndNotExist(obj1) && judgeNullAndNotExist(obj2);
+ case NULL_CHECK:
+ return judgeNullAndNotExistAndEmptyString(obj1) && judgeNullAndNotExistAndEmptyString(obj2);
default:
return false;
}
@@ -73,9 +74,11 @@ private static boolean jsonEmptyJudge(Object o) {
}
}
- private static boolean judgeNullAndNotExist(Object o) {
+ private static boolean judgeNullAndNotExistAndEmptyString(Object o) {
if (o == null || o instanceof NullNode) {
return true;
+ } else if (o instanceof TextNode) {
+ return Objects.equals(((TextNode) o).asText(), "");
}
return false;
}
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/model/CompareOptions.java b/arex-compare-core/src/main/java/com/arextest/diff/model/CompareOptions.java
index 3d3e296..df36d9a 100644
--- a/arex-compare-core/src/main/java/com/arextest/diff/model/CompareOptions.java
+++ b/arex-compare-core/src/main/java/com/arextest/diff/model/CompareOptions.java
@@ -105,6 +105,11 @@ public class CompareOptions {
*/
private Boolean ipIgnore;
+ /**
+ * This option is true, only compare the list elements which are existed in the baseMsg and testMsg
+ */
+ private Boolean onlyCompareExistListElements;
+
public CompareOptions() {
}
@@ -289,6 +294,11 @@ public CompareOptions putIpIgnore(Boolean ipIgnore) {
return this;
}
+ public CompareOptions putOnlyCompareExistListElements(Boolean onlyCompareExistListElements) {
+ this.onlyCompareExistListElements = onlyCompareExistListElements;
+ return this;
+ }
+
public String getCategoryType() {
return categoryType;
}
@@ -352,4 +362,8 @@ public Boolean getUuidIgnore() {
public Boolean getIpIgnore() {
return ipIgnore;
}
+
+ public Boolean getOnlyCompareExistListElements() {
+ return onlyCompareExistListElements;
+ }
}
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/model/GlobalOptions.java b/arex-compare-core/src/main/java/com/arextest/diff/model/GlobalOptions.java
index 8609575..0379df4 100644
--- a/arex-compare-core/src/main/java/com/arextest/diff/model/GlobalOptions.java
+++ b/arex-compare-core/src/main/java/com/arextest/diff/model/GlobalOptions.java
@@ -63,6 +63,11 @@ public class GlobalOptions {
*/
private Boolean ipIgnore;
+ /**
+ * This option is true, only compare the list elements which are existed in the baseMsg and testMsg
+ */
+ private Boolean onlyCompareExistListElements;
+
public GlobalOptions() {
this.nameToLower = false;
this.nullEqualsEmpty = false;
@@ -122,6 +127,11 @@ public GlobalOptions putIpIgnore(Boolean ipIgnore) {
return this;
}
+ public GlobalOptions putOnlyCompareExistListElements(Boolean onlyCompareExistListElements) {
+ this.onlyCompareExistListElements = onlyCompareExistListElements;
+ return this;
+ }
+
public String getPluginJarUrl() {
return pluginJarUrl;
}
@@ -162,4 +172,8 @@ public Boolean getIpIgnore() {
return ipIgnore;
}
+ public Boolean getOnlyCompareExistListElements() {
+ return onlyCompareExistListElements;
+ }
+
}
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/model/RulesConfig.java b/arex-compare-core/src/main/java/com/arextest/diff/model/RulesConfig.java
index f67a4cb..f3075eb 100644
--- a/arex-compare-core/src/main/java/com/arextest/diff/model/RulesConfig.java
+++ b/arex-compare-core/src/main/java/com/arextest/diff/model/RulesConfig.java
@@ -55,6 +55,8 @@ public class RulesConfig {
private boolean ipIgnore;
+ private boolean onlyCompareExistListElements;
+
//region: inner processed class
private List> expressionExclusions;
@@ -217,6 +219,14 @@ public void setIpIgnore(boolean ipIgnore) {
this.ipIgnore = ipIgnore;
}
+ public boolean isOnlyCompareExistListElements() {
+ return onlyCompareExistListElements;
+ }
+
+ public void setOnlyCompareExistListElements(boolean onlyCompareExistListElements) {
+ this.onlyCompareExistListElements = onlyCompareExistListElements;
+ }
+
public List> getExpressionExclusions() {
return expressionExclusions;
}
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/utils/DataBaseCompareUtil.java b/arex-compare-core/src/main/java/com/arextest/diff/utils/DataBaseCompareUtil.java
index 80b1132..4d2e1f4 100644
--- a/arex-compare-core/src/main/java/com/arextest/diff/utils/DataBaseCompareUtil.java
+++ b/arex-compare-core/src/main/java/com/arextest/diff/utils/DataBaseCompareUtil.java
@@ -6,10 +6,7 @@
import com.arextest.diff.handler.keycompute.KeyCompute;
import com.arextest.diff.handler.log.LogProcess;
import com.arextest.diff.handler.log.filterrules.ArexPrefixFilter;
-import com.arextest.diff.handler.log.filterrules.IPFilter;
-import com.arextest.diff.handler.log.filterrules.OnlyCompareSameColumnsFilter;
import com.arextest.diff.handler.log.filterrules.TimePrecisionFilter;
-import com.arextest.diff.handler.log.filterrules.UuidFilter;
import com.arextest.diff.handler.metric.TimeConsumerWatch;
import com.arextest.diff.handler.metric.TimeMetricLabel;
import com.arextest.diff.handler.parse.JSONParse;
@@ -28,7 +25,6 @@
import com.arextest.diff.model.parse.MsgObjCombination;
import com.arextest.diff.model.parse.MsgStructure;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@@ -146,15 +142,7 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) {
logProcess.appendFilterRules(
Arrays.asList(new TimePrecisionFilter(rulesConfig.getIgnoredTimePrecision()),
new ArexPrefixFilter()));
- if (rulesConfig.isUuidIgnore()) {
- logProcess.appendFilterRules(new UuidFilter());
- }
- if (rulesConfig.isOnlyCompareCoincidentColumn()) {
- logProcess.appendFilterRules(new OnlyCompareSameColumnsFilter());
- }
- if (rulesConfig.isIpIgnore()) {
- logProcess.appendFilterRules(Collections.singletonList(new IPFilter()));
- }
+ logProcess.appendOtherFilterRules(rulesConfig);
logs = compareHandler.doHandler(rulesConfig, keyComputeResponse, msgStructureFuture,
msgWhiteObj.getBaseObj(), msgWhiteObj.getTestObj(), logProcess);
timeConsumerWatch.end(TimeMetricLabel.COMPARE_HANDLER);
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/utils/NormalCompareUtil.java b/arex-compare-core/src/main/java/com/arextest/diff/utils/NormalCompareUtil.java
index 654f4f7..e9f0d5f 100644
--- a/arex-compare-core/src/main/java/com/arextest/diff/utils/NormalCompareUtil.java
+++ b/arex-compare-core/src/main/java/com/arextest/diff/utils/NormalCompareUtil.java
@@ -6,9 +6,7 @@
import com.arextest.diff.handler.keycompute.KeyCompute;
import com.arextest.diff.handler.log.LogProcess;
import com.arextest.diff.handler.log.filterrules.ArexPrefixFilter;
-import com.arextest.diff.handler.log.filterrules.IPFilter;
import com.arextest.diff.handler.log.filterrules.TimePrecisionFilter;
-import com.arextest.diff.handler.log.filterrules.UuidFilter;
import com.arextest.diff.handler.metric.TimeConsumerWatch;
import com.arextest.diff.handler.metric.TimeMetricLabel;
import com.arextest.diff.handler.parse.JSONParse;
@@ -25,7 +23,6 @@
import com.arextest.diff.model.parse.MsgObjCombination;
import com.arextest.diff.model.parse.MsgStructure;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@@ -137,12 +134,7 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) {
logProcess.appendFilterRules(
Arrays.asList(new TimePrecisionFilter(rulesConfig.getIgnoredTimePrecision()),
new ArexPrefixFilter()));
- if (rulesConfig.isUuidIgnore()) {
- logProcess.appendFilterRules(Collections.singletonList(new UuidFilter()));
- }
- if (rulesConfig.isIpIgnore()) {
- logProcess.appendFilterRules(Collections.singletonList(new IPFilter()));
- }
+ logProcess.appendOtherFilterRules(rulesConfig);
logs = compareHandler.doHandler(rulesConfig, keyComputeResponse, msgStructureFuture,
msgWhiteObj.getBaseObj(), msgWhiteObj.getTestObj(), logProcess);
timeConsumerWatch.end(TimeMetricLabel.COMPARE_HANDLER);
diff --git a/arex-compare-core/src/main/java/com/arextest/diff/utils/OptionsToRulesConvert.java b/arex-compare-core/src/main/java/com/arextest/diff/utils/OptionsToRulesConvert.java
index c7ba08d..1406c90 100644
--- a/arex-compare-core/src/main/java/com/arextest/diff/utils/OptionsToRulesConvert.java
+++ b/arex-compare-core/src/main/java/com/arextest/diff/utils/OptionsToRulesConvert.java
@@ -79,6 +79,9 @@ private static void globalOptionsToRules(GlobalOptions globalOptions, RulesConfi
if (globalOptions.getIpIgnore() != null) {
rulesConfig.setIpIgnore(globalOptions.getIpIgnore());
}
+ if (globalOptions.getOnlyCompareExistListElements() != null) {
+ rulesConfig.setOnlyCompareExistListElements(globalOptions.getOnlyCompareExistListElements());
+ }
}
private static void optionsToRules(CompareOptions compareOptions, RulesConfig rulesConfig) {
@@ -125,6 +128,9 @@ private static void optionsToRules(CompareOptions compareOptions, RulesConfig ru
if (compareOptions.getIpIgnore() != null) {
rulesConfig.setIpIgnore(compareOptions.getIpIgnore());
}
+ if (compareOptions.getOnlyCompareExistListElements() != null) {
+ rulesConfig.setOnlyCompareExistListElements(compareOptions.getOnlyCompareExistListElements());
+ }
}
private static Map, List> decompressAndTransformConvert(
diff --git a/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java b/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java
index 4c7eab9..0c4a5e4 100644
--- a/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java
+++ b/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java
@@ -809,5 +809,53 @@ public void testParmeter() {
Assertions.assertEquals(1, result.getLogs().size());
}
+ @Test
+ public void testOnlyCompareExistListElements() {
+ CompareSDK sdk = new CompareSDK();
+// sdk.getGlobalOptions().putOnlyCompareExistListElements(true);
+
+ String str1 = "{\n"
+ + " \"resultList\": [\n"
+ + " {\n"
+ + " \"student\": \"stuA\",\n"
+ + " \"age\": 18\n"
+ + " },\n"
+ + " {\n"
+ + " \"student\": \"stuB\",\n"
+ + " \"age\": 19,\n"
+ + " \"sex\": 0\n"
+ + " }\n"
+ + " ]\n"
+ + "}";
+
+ String str2 = "{\n"
+ + " \"resultList\": [\n"
+ + " {\n"
+ + " \"student\": \"stuA\",\n"
+ + " \"age\": 18\n"
+ + " }\n"
+ + " ]\n"
+ + "}";
+
+ CompareOptions compareOptions = CompareOptions.options().putOnlyCompareExistListElements(true);
+ CompareResult result = sdk.compare(str1, str2, compareOptions);
+ Assertions.assertEquals(0, result.getLogs().size());
+ }
+
+ @Test
+ public void testNullEqualsNotExist() {
+ CompareSDK sdk = new CompareSDK();
+ sdk.getGlobalOptions().putNullEqualsNotExist(true);
+
+ CompareResult result1 = sdk.compare("{\"name\":null}", "{}");
+ Assertions.assertEquals(0, result1.getLogs().size());
+
+ CompareResult result2 = sdk.compare("{\"name\":null}", "{\"name\":\"\"}");
+ Assertions.assertEquals(0, result2.getLogs().size());
+
+ CompareResult result3 = sdk.compare("{}", "{\"name\":\"\"}");
+ Assertions.assertEquals(0, result3.getLogs().size());
+ }
+
}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 53875f1..f2f1b7b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
com.arextest
arex-compare-parent
pom
- 0.2.13
+ 0.2.14
arex-compare-extension
arex-compare-core