diff --git a/arex-compare-core/pom.xml b/arex-compare-core/pom.xml index 8e8cd7c..0e18c98 100644 --- a/arex-compare-core/pom.xml +++ b/arex-compare-core/pom.xml @@ -5,7 +5,7 @@ arex-compare-parent com.arextest - 0.2.5 + 0.2.6 4.0.0 diff --git a/arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/IPFilter.java b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/IPFilter.java new file mode 100644 index 0000000..4f7faeb --- /dev/null +++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/IPFilter.java @@ -0,0 +1,55 @@ +package com.arextest.diff.handler.log.filterrules; + +import com.arextest.diff.model.enumeration.UnmatchedType; +import com.arextest.diff.model.log.LogEntity; +import java.util.function.Predicate; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class IPFilter implements Predicate { + + public static boolean isIp(String baseStr, String testStr) { + + if (baseStr.length() >= 7 && baseStr.length() <= 15 + && testStr.length() >= 7 && testStr.length() <= 15) { + return isIPv4(baseStr) && isIPv4(testStr); + } + + if (baseStr.length() >= 15 && baseStr.length() <= 39 + && testStr.length() >= 15 && testStr.length() <= 39) { + return isIPv6(baseStr) && isIPv6(testStr); + } + + return false; + } + + private static boolean isIPv4(String ipAddress) { + String ipv4Regex = "^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$"; + Pattern pattern = Pattern.compile(ipv4Regex); + Matcher matcher = pattern.matcher(ipAddress); + return matcher.matches(); + } + + + private static boolean isIPv6(String ipAddress) { + String ipv6Regex = "^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"; + Pattern pattern = Pattern.compile(ipv6Regex); + Matcher matcher = pattern.matcher(ipAddress); + return matcher.matches(); + } + + + @Override + public boolean test(LogEntity logEntity) { + int unmatchedType = logEntity.getPathPair().getUnmatchedType(); + if (unmatchedType == UnmatchedType.UNMATCHED) { + Object baseValue = logEntity.getBaseValue(); + Object testValue = logEntity.getTestValue(); + if (baseValue != null && testValue != null) { + return !isIp((String) baseValue, (String) testValue); + } + } + return true; + } + +} 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 8d89871..bb89171 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 @@ -92,6 +92,11 @@ public class CompareOptions { */ private Boolean uuidIgnore; + /** + * This option is true, the ipV4 and ipV6 is ignored when comparing + */ + private Boolean ipIgnore; + public CompareOptions() { } @@ -250,6 +255,11 @@ public CompareOptions putUuidIgnore(Boolean uuidIgnore) { return this; } + public CompareOptions putIpIgnore(Boolean ipIgnore) { + this.ipIgnore = ipIgnore; + return this; + } + public String getCategoryType() { return categoryType; } @@ -305,4 +315,8 @@ public Boolean getNullEqualsNotExist() { public Boolean getUuidIgnore() { return uuidIgnore; } + + public Boolean getIpIgnore() { + return ipIgnore; + } } 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 257e705..01c938a 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 @@ -58,6 +58,11 @@ public class GlobalOptions { */ private Boolean uuidIgnore; + /** + * This option is true, the ipV4 and ipV6 is ignored when comparing + */ + private Boolean ipIgnore; + public GlobalOptions() { this.nameToLower = false; this.nullEqualsEmpty = false; @@ -112,6 +117,11 @@ public GlobalOptions putUuidIgnore(boolean uuidIgnore) { return this; } + public GlobalOptions putIpIgnore(boolean ipIgnore) { + this.ipIgnore = ipIgnore; + return this; + } + public String getPluginJarUrl() { return pluginJarUrl; } @@ -148,4 +158,8 @@ public Boolean getUuidIgnore() { return uuidIgnore; } + public Boolean getIpIgnore() { + return ipIgnore; + } + } 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 69b4a94..56eebca 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 @@ -52,6 +52,8 @@ public class RulesConfig { private boolean uuidIgnore; + private boolean ipIgnore; + //region: inner processed class private List> expressionExclusions; @@ -206,6 +208,14 @@ public void setUuidIgnore(boolean uuidIgnore) { this.uuidIgnore = uuidIgnore; } + public boolean isIpIgnore() { + return ipIgnore; + } + + public void setIpIgnore(boolean ipIgnore) { + this.ipIgnore = ipIgnore; + } + 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 32ee2cc..80b1132 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,6 +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; @@ -27,6 +28,7 @@ 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; @@ -150,6 +152,9 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) { if (rulesConfig.isOnlyCompareCoincidentColumn()) { logProcess.appendFilterRules(new OnlyCompareSameColumnsFilter()); } + if (rulesConfig.isIpIgnore()) { + logProcess.appendFilterRules(Collections.singletonList(new IPFilter())); + } 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 d66c9c7..654f4f7 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,6 +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; @@ -139,6 +140,9 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) { if (rulesConfig.isUuidIgnore()) { logProcess.appendFilterRules(Collections.singletonList(new UuidFilter())); } + if (rulesConfig.isIpIgnore()) { + logProcess.appendFilterRules(Collections.singletonList(new IPFilter())); + } 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 bdac2c4..336e704 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 @@ -68,6 +68,9 @@ private static void globalOptionsToRules(GlobalOptions globalOptions, RulesConfi if (globalOptions.getUuidIgnore() != null) { rulesConfig.setUuidIgnore(globalOptions.getUuidIgnore()); } + if (globalOptions.getIpIgnore() != null) { + rulesConfig.setIpIgnore(globalOptions.getIpIgnore()); + } } private static void optionsToRules(CompareOptions compareOptions, RulesConfig rulesConfig) { @@ -110,6 +113,9 @@ private static void optionsToRules(CompareOptions compareOptions, RulesConfig ru if (compareOptions.getUuidIgnore() != null) { rulesConfig.setUuidIgnore(compareOptions.getUuidIgnore()); } + if (compareOptions.getIpIgnore() != null) { + rulesConfig.setIpIgnore(compareOptions.getIpIgnore()); + } } private static Map, DecompressConfig> decompressConfigConvert( 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 7bda45e..f691c60 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 @@ -511,4 +511,18 @@ public void testPrimitiveArrayNodeAutoSort() { CompareResult result = sdk.compare(str1, str2); Assertions.assertEquals(0, result.getCode()); } + + @Test + public void testIpFilter() { + + CompareSDK sdk = new CompareSDK(); + sdk.getGlobalOptions().putIpIgnore(true); + + String str1 = "{\"ip\":\"0001:0:0:0:0:0:0:1\"}"; + String str2 = "{\"ip\":\"0:0:0:0:0:0:0:1\"}"; + + CompareResult result = sdk.compare(str1, str2); + Assertions.assertEquals(0, result.getCode()); + } + } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 6b0e82c..8a7f169 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.arextest arex-compare-parent pom - 0.2.5 + 0.2.6 arex-compare-extension arex-compare-core