From 0cf1263191e8473b8295219878a025b1d80f1a93 Mon Sep 17 00:00:00 2001 From: rchen9 Date: Thu, 18 Jan 2024 18:32:42 +0800 Subject: [PATCH 1/2] feat: add ip filter --- .../handler/log/filterrules/IPFilter.java | 60 +++++++++++++++++++ .../diff/utils/DataBaseCompareUtil.java | 3 + .../diff/utils/NormalCompareUtil.java | 2 + .../com/arextest/diff/sdk/CompareSDKTest.java | 15 +++++ 4 files changed, 80 insertions(+) create mode 100644 arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/IPFilter.java 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..aabdaca --- /dev/null +++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/log/filterrules/IPFilter.java @@ -0,0 +1,60 @@ +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; + } + + public 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(); + } + + + public 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; + } + + public static void main(String[] args) { + String ip = "0:0:0:0:0:0:0:1"; + System.out.println(isIPv6(ip)); + } + +} 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..fa071c5 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,7 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) { if (rulesConfig.isOnlyCompareCoincidentColumn()) { logProcess.appendFilterRules(new OnlyCompareSameColumnsFilter()); } + 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..56e2240 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,7 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) { if (rulesConfig.isUuidIgnore()) { logProcess.appendFilterRules(Collections.singletonList(new UuidFilter())); } + 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/test/java/com/arextest/diff/sdk/CompareSDKTest.java b/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java index 7bda45e..71aa7c4 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 @@ -7,6 +7,8 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; +import org.junit.Assert; +import org.junit.Test; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -511,4 +513,17 @@ public void testPrimitiveArrayNodeAutoSort() { CompareResult result = sdk.compare(str1, str2); Assertions.assertEquals(0, result.getCode()); } + + @Test + public void testIpFilter() { + + CompareSDK sdk = new CompareSDK(); + + 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); + Assert.assertEquals(0, result.getCode()); + } + } \ No newline at end of file From de4b82a764f7e79e11e8056acdb3556815ef6a5a Mon Sep 17 00:00:00 2001 From: rchen9 Date: Tue, 23 Jan 2024 20:36:37 +0800 Subject: [PATCH 2/2] feat: support ignoring "ipv4" and "ipv6" --- arex-compare-core/pom.xml | 2 +- .../diff/handler/log/filterrules/IPFilter.java | 9 ++------- .../com/arextest/diff/model/CompareOptions.java | 14 ++++++++++++++ .../com/arextest/diff/model/GlobalOptions.java | 14 ++++++++++++++ .../java/com/arextest/diff/model/RulesConfig.java | 10 ++++++++++ .../arextest/diff/utils/DataBaseCompareUtil.java | 4 +++- .../com/arextest/diff/utils/NormalCompareUtil.java | 4 +++- .../arextest/diff/utils/OptionsToRulesConvert.java | 6 ++++++ .../java/com/arextest/diff/sdk/CompareSDKTest.java | 5 ++--- pom.xml | 2 +- 10 files changed, 56 insertions(+), 14 deletions(-) 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 index aabdaca..4f7faeb 100644 --- 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 @@ -23,7 +23,7 @@ public static boolean isIp(String baseStr, String testStr) { return false; } - public static boolean isIPv4(String ipAddress) { + 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); @@ -31,7 +31,7 @@ public static boolean isIPv4(String ipAddress) { } - public static boolean isIPv6(String ipAddress) { + 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); @@ -52,9 +52,4 @@ public boolean test(LogEntity logEntity) { return true; } - public static void main(String[] args) { - String ip = "0:0:0:0:0:0:0:1"; - System.out.println(isIPv6(ip)); - } - } 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 fa071c5..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 @@ -152,7 +152,9 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) { if (rulesConfig.isOnlyCompareCoincidentColumn()) { logProcess.appendFilterRules(new OnlyCompareSameColumnsFilter()); } - logProcess.appendFilterRules(Collections.singletonList(new IPFilter())); + 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 56e2240..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 @@ -140,7 +140,9 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) { if (rulesConfig.isUuidIgnore()) { logProcess.appendFilterRules(Collections.singletonList(new UuidFilter())); } - logProcess.appendFilterRules(Collections.singletonList(new IPFilter())); + 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 71aa7c4..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 @@ -7,8 +7,6 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; -import org.junit.Assert; -import org.junit.Test; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -518,12 +516,13 @@ public void testPrimitiveArrayNodeAutoSort() { 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); - Assert.assertEquals(0, result.getCode()); + 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