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 custom compare #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/maven-core-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '21'
distribution: 'temurin'
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
server-username: MAVEN_USERNAME # env variable for username in deploy
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/maven-extension-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '21'
distribution: 'temurin'
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
server-username: MAVEN_USERNAME # env variable for username in deploy
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/maven-parent-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '21'
distribution: 'temurin'
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
server-username: MAVEN_USERNAME # env variable for username in deploy
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ut.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java-version: [ 8, 11 ]
java-version: [21]
steps:
- uses: actions/checkout@v3
- name: "Test for JDK ${{ matrix.java-version }}"
Expand Down
6 changes: 5 additions & 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.17</version>
<version>0.2.18</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -36,6 +36,10 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.javadelight</groupId>
<artifactId>delight-nashorn-sandbox</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.arextest.diff.handler.log.register.LogRegister;
import com.arextest.diff.model.compare.CompareContext;
import com.arextest.diff.model.enumeration.ParentNodeType;
import com.arextest.diff.model.exception.FindErrorException;
import com.arextest.diff.model.log.NodeEntity;
import com.arextest.diff.utils.IgnoreUtil;
import com.arextest.diff.utils.ListUti;
Expand All @@ -12,9 +13,12 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;

public class GenericCompare {

private static final Logger LOGGER = Logger.getLogger(GenericCompare.class.getName());

public static void jsonCompare(Object obj1, Object obj2, CompareContext compareContext)
throws Exception {

Expand Down Expand Up @@ -42,6 +46,18 @@ public static void jsonCompare(Object obj1, Object obj2, CompareContext compareC
return;
}

// custom compare
try {
if (ScriptCompare.isScriptComparisonRequired(fuzzyPath, compareContext)) {
ScriptCompare.scriptCompare(obj1, obj2, fuzzyPath, compareContext);
return;
}
} catch (FindErrorException e) {
throw e;
} catch (Exception e) {
LOGGER.warning("Script compare error: " + e.getMessage());
}

// field missing
if (obj1 == null && obj2 == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.arextest.diff.compare;

import com.arextest.diff.handler.log.LogMarker;
import com.arextest.diff.handler.log.register.LogRegister;
import com.arextest.diff.model.compare.CompareContext;
import com.arextest.diff.model.enumeration.ParentNodeType;
import com.arextest.diff.model.exception.FindErrorException;
import com.arextest.diff.model.script.ScriptCompareConfig.ScriptMethod;
import com.arextest.diff.model.script.ScriptMethodContext;
import com.arextest.diff.model.script.ScriptSandbox;
import com.arextest.diff.utils.JacksonHelperUtil;
import java.util.List;
import javax.script.ScriptException;

public class ScriptCompare {

public static boolean isScriptComparisonRequired(List<String> fuzzyPath,
CompareContext compareContext) {
return compareContext.scriptCompareConfigMap != null
&& compareContext.scriptCompareConfigMap.containsKey(fuzzyPath);
}

// custom compare
public static void scriptCompare(Object obj1, Object obj2, List<String> fuzzyPath,
CompareContext compareContext)
throws ScriptException, NoSuchMethodException, FindErrorException {

ScriptMethodContext context = new ScriptMethodContext();
context.setBasePath(compareContext.currentNodeLeft);
context.setTestPath(compareContext.currentNodeRight);

Object baseValue = JacksonHelperUtil.objectMapper.convertValue(obj1, Object.class);
Object testValue = JacksonHelperUtil.objectMapper.convertValue(obj2, Object.class);
ScriptSandbox scriptSandbox = compareContext.scriptSandbox;
ScriptMethod scriptMethod = compareContext.scriptCompareConfigMap.get(fuzzyPath);
int result = scriptSandbox.invoke(context, baseValue, testValue, scriptMethod);
if (result == ScriptCompareType.Matched) {
return;
}
LogRegister.register(obj1, obj2,
ScriptCompareType.convert(result, compareContext.parentNodeType), compareContext);
}

public interface ScriptCompareType {

int Matched = 1;
int Unmatched = 2;
// the node of basic msg is missing
int LEFT_MISSING = 3;
// the node of test msg is missing
int RIGHT_MISSING = 4;

static LogMarker convert(int type, int parentType) {
switch (type) {
case Unmatched:
return LogMarker.VALUE_DIFF;
case LEFT_MISSING:
return parentType == ParentNodeType.OBJECT ? LogMarker.LEFT_OBJECT_MISSING
: LogMarker.LEFT_ARRAY_MISSING;
case RIGHT_MISSING:
return parentType == ParentNodeType.OBJECT ? LogMarker.RIGHT_OBJECT_MISSING
: LogMarker.RIGHT_ARRAY_MISSING;
default:
return LogMarker.UNKNOWN;
}

}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,35 @@ public class TaskThreadFactory {
private static final int CORE_SIZE = Runtime.getRuntime().availableProcessors();
private static final int MAX_POOL_SIZE = CORE_SIZE + 1;
private static final int QUENE_SIZE = 500;
public static ExecutorService jsonObjectThreadPool = new ThreadPoolExecutor(CORE_SIZE,

public static ExecutorService jsonObjectThreadPool = new ThreadPoolExecutor(
CORE_SIZE,
MAX_POOL_SIZE,
1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(QUENE_SIZE),
1,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(QUENE_SIZE),
new NamedThreadFactory("JsonObject"),
new CallerRunsPolicyWithReport("JsonObject"));
public static ExecutorService structureHandlerThreadPool = new ThreadPoolExecutor(CORE_SIZE,
new CallerRunsPolicyWithReport("JsonObject")
);

public static ExecutorService structureHandlerThreadPool = new ThreadPoolExecutor(
CORE_SIZE,
MAX_POOL_SIZE,
1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(QUENE_SIZE),
1,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(QUENE_SIZE),
new NamedThreadFactory("structureHandler"),
new CallerRunsPolicyWithReport("structureHandler"));
new CallerRunsPolicyWithReport("structureHandler")
);

public static ExecutorService jsEvalThreadPool = new ThreadPoolExecutor(
CORE_SIZE,
MAX_POOL_SIZE,
1,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(QUENE_SIZE),
new NamedThreadFactory("jsEval"),
new CallerRunsPolicyWithReport("jsEval")
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public List<LogEntity> doHandler(RulesConfig rulesConfig, KeyComputeResponse key
compareContext.logProcess = logProcess;
compareContext.quickCompare = rulesConfig.isQuickCompare();

compareContext.scriptSandbox = rulesConfig.getScriptSandbox();
compareContext.scriptCompareConfigMap = rulesConfig.getScriptCompareConfigMap();

if (msgStructureFuture != null) {
MutablePair<MsgStructure, MsgStructure> msgStructureMutablePair = msgStructureFuture.join();
compareContext.baseMsgStructure = msgStructureMutablePair.getLeft();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,70 @@
package com.arextest.diff.handler.log;

public enum LogMarker {
ZERO,

// This marker indicates that there is a null value at the recursive entry
NULL_CHECK,

// This marker indicates that there is different type
TYPE_DIFF,
UNKNOWN(0),

// This mark indicates that there is unmatched value at the comparison of value
VALUE_DIFF(1),

// This marker indicates that there is right missing at the comparison of object
RIGHT_OBJECT_MISSING,
RIGHT_OBJECT_MISSING(2),

// This marker indicates that there is left missing at the comparison of object
LEFT_OBJECT_MISSING,
LEFT_OBJECT_MISSING(3),

// This marker indicates that there is different count at the comparison of array
DIFF_ARRAY_COUNT,
// This mark indicates that there is left missing at the comparision of array
LEFT_ARRAY_MISSING(4),

// This marker indicates that there is right missing at the comparison of array
RIGHT_ARRAY_MISSING,
RIGHT_ARRAY_MISSING(5),

// This marker indicates that there is not unique key at the left search of listkey
REPEAT_LEFT_KEY,
// This marker indicates that there is a null value at the recursive entry
NULL_CHECK(6),

// This mark indicates that there is right missing at the comparison of listKey mode
RIGHT_ARRAY_MISSING_KEY,
// This marker indicates that there is different type
TYPE_DIFF(7),

// This mark indicates that there is left missing at the comparision of array
LEFT_ARRAY_MISSING,
// This marker indicates that there is different count at the comparison of array
DIFF_ARRAY_COUNT(8),

// This marker indicates that there is not unique key at the left search of listkey
REPEAT_LEFT_KEY(9),

// This marker indicates that there is not unique key at the right search of listkey
REPEAT_RIGHT_KEY,
REPEAT_RIGHT_KEY(10),

// This mark indicates that there is right missing at the comparison of listKey mode
RIGHT_ARRAY_MISSING_KEY(11),

// This mark indicates that there is left missing at the comparison of listKey mode
LEFT_ARRAY_MISSING_KEY,
LEFT_ARRAY_MISSING_KEY(12),

// This mark indicates that there is left reference not fund at the comparison of value
LEFT_REF_NOT_FOUND,
LEFT_REF_NOT_FOUND(13),

// This mark indicates that there is right reference not found at the comparison of value
RIGHT_REF_NOT_FOUND,
RIGHT_REF_NOT_FOUND(14);

private Integer code;

LogMarker(int code) {
this.code = code;
}

public Integer getCode() {
return code;
}

public static LogMarker from(int code) {
for (LogMarker type : LogMarker.values()) {
if (type.getCode() == code) {
return type;
}
}
return UNKNOWN;
}


// This mark indicates that there is unmatched value at the comparison of value
VALUE_DIFF,
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public static void register(Object obj1, Object obj2, LogMarker logMarker,
}
LogEntity log = null;
switch (logMarker) {
case UNKNOWN:
log = produceLog(obj1, obj2, UnmatchedType.UNMATCHED, ErrorType.NA,
compareContext.currentListKeysLeft, compareContext);
break;
case NULL_CHECK:
log = nullCheck(obj1, obj2, logMarker, compareContext);
break;
Expand Down
Loading
Loading