Skip to content

Commit

Permalink
feat: add the switch of "onlyCompareExistListElements" to only compar…
Browse files Browse the repository at this point in the history
…e the matched list elements (#58)
  • Loading branch information
coryhh authored May 21, 2024
1 parent 7678bc3 commit 38e3d30
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 28 deletions.
2 changes: 1 addition & 1 deletion arex-compare-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>arex-compare-parent</artifactId>
<groupId>com.arextest</groupId>
<version>0.2.13</version>
<version>0.2.14</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,6 +42,24 @@ public void appendFilterRules(List<Predicate<LogEntity>> 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<LogEntity> logEntities) {

Stream<LogEntity> stream = logEntities.stream();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LogEntity> {

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -352,4 +362,8 @@ public Boolean getUuidIgnore() {
public Boolean getIpIgnore() {
return ipIgnore;
}

public Boolean getOnlyCompareExistListElements() {
return onlyCompareExistListElements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -162,4 +172,8 @@ public Boolean getIpIgnore() {
return ipIgnore;
}

public Boolean getOnlyCompareExistListElements() {
return onlyCompareExistListElements;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class RulesConfig {

private boolean ipIgnore;

private boolean onlyCompareExistListElements;

//region: inner processed class
private List<List<ExpressionNodeEntity>> expressionExclusions;

Expand Down Expand Up @@ -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<List<ExpressionNodeEntity>> getExpressionExclusions() {
return expressionExclusions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<String>, List<TransformMethod>> decompressAndTransformConvert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}


}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.arextest</groupId>
<artifactId>arex-compare-parent</artifactId>
<packaging>pom</packaging>
<version>0.2.13</version>
<version>0.2.14</version>
<modules>
<module>arex-compare-extension</module>
<module>arex-compare-core</module>
Expand Down

0 comments on commit 38e3d30

Please sign in to comment.