Skip to content

Commit

Permalink
Feat/support custom compare (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryhh authored Nov 18, 2024
1 parent 0b0dba0 commit 660a231
Show file tree
Hide file tree
Showing 23 changed files with 662 additions and 56 deletions.
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,42 @@
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.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);
Boolean result = scriptSandbox.invoke(context, baseValue, testValue, scriptMethod);
if (result) {
return;
}
LogRegister.register(obj1, obj2, LogMarker.VALUE_DIFF, compareContext);
}

}
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.arextest.diff.model;

import com.arextest.diff.model.enumeration.CategoryType;
import com.arextest.diff.model.script.ScriptCompareConfig;
import com.arextest.diff.utils.ListUti;
import com.arextest.diff.utils.StringUtil;
import java.util.ArrayList;
Expand All @@ -23,7 +24,6 @@ public class CompareOptions {
* The decompressService which is loaded from this pluginJarUrl is the level of each compare.
*/
private String pluginJarUrl;

/**
* the collection of the node path chosen to compare
*/
Expand Down Expand Up @@ -60,6 +60,9 @@ public class CompareOptions {
*/
private Map<List<String>, List<List<String>>> listSortConfig;


private List<ScriptCompareConfig> scriptCompareConfigList;

/**
* change the message and configuration to lowercase, for the inconsistency between the actual
* message and the contract case
Expand Down Expand Up @@ -255,6 +258,28 @@ public CompareOptions putListSortConfig(Map<List<String>, List<List<String>>> li
return this;
}

public CompareOptions putScriptCompareConfig(ScriptCompareConfig scriptCompareConfig) {
if (scriptCompareConfig == null || ListUti.isEmpty(scriptCompareConfig.getNodePath())) {
return this;
}
if (this.scriptCompareConfigList == null) {
this.scriptCompareConfigList = new ArrayList<>();
}
this.scriptCompareConfigList.add(scriptCompareConfig);
return this;
}

public CompareOptions putScriptCompareConfig(Collection<ScriptCompareConfig> scriptCompareConfigList) {
if (scriptCompareConfigList == null || scriptCompareConfigList.isEmpty()) {
return this;
}
if (this.scriptCompareConfigList == null) {
this.scriptCompareConfigList = new ArrayList<>();
}
this.scriptCompareConfigList.addAll(scriptCompareConfigList);
return this;
}

public CompareOptions putNameToLower(Boolean nameToLower) {
this.nameToLower = nameToLower;
return this;
Expand Down Expand Up @@ -332,6 +357,10 @@ public Map<List<String>, List<List<String>>> getListSortConfig() {
return listSortConfig;
}

public List<ScriptCompareConfig> getScriptCompareConfigList() {
return scriptCompareConfigList;
}

public Boolean getNameToLower() {
return nameToLower;
}
Expand Down
Loading

0 comments on commit 660a231

Please sign in to comment.