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

feat: support ignoring "ipv4" and "ipv6" #49

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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.5</version>
<version>0.2.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -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<LogEntity> {

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;
}

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

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -305,4 +315,8 @@ public Boolean getNullEqualsNotExist() {
public Boolean getUuidIgnore() {
return uuidIgnore;
}

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

public Boolean getIpIgnore() {
return ipIgnore;
}

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

private boolean uuidIgnore;

private boolean ipIgnore;

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

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

}
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.5</version>
<version>0.2.6</version>
<modules>
<module>arex-compare-extension</module>
<module>arex-compare-core</module>
Expand Down