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: ouput the type of error node #59

Merged
merged 5 commits into from
Aug 14, 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.14</version>
<version>0.2.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ private static LogEntity produceLog(Object obj1, Object obj2, int unmatchedType,
LogEntity log =
new LogEntity(obj1, obj2,
getUnmatchedPair(unmatchedType, compareContext).buildListKeys(currentListKeys));
log.simplifyLogMsg();
log.getLogTag().setErrorType(errorType);
return log;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public class CompareOptions {
private Boolean ipIgnore;

/**
* This option is true, only compare the list elements which are existed in the baseMsg and testMsg
* This option is true, only compare the list elements which are existed in the baseMsg and
* testMsg
*/
private Boolean onlyCompareExistListElements;

Expand Down Expand Up @@ -366,4 +367,5 @@ public Boolean getIpIgnore() {
public Boolean getOnlyCompareExistListElements() {
return onlyCompareExistListElements;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public class GlobalOptions {
private Boolean ipIgnore;

/**
* This option is true, only compare the list elements which are existed in the baseMsg and testMsg
* This option is true, only compare the list elements which are existed in the baseMsg and
* testMsg
*/
private Boolean onlyCompareExistListElements;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.arextest.diff.model.log;

import com.arextest.diff.model.enumeration.UnmatchedType;
import com.arextest.diff.model.log.LogTag.NodeErrorType;
import com.arextest.diff.utils.ListUti;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -40,9 +41,10 @@ public LogEntity() {
}

public LogEntity(Object baseValue, Object testValue, UnmatchedPairEntity pathPair) {
this.baseValue = valueToString(baseValue);
this.testValue = valueToString(testValue);
this.baseValue = baseValue;
this.testValue = testValue;
this.pathPair = pathPair;
addNodeErrorType();
processLogInfo(this.baseValue, this.testValue, pathPair.getUnmatchedType());
}

Expand Down Expand Up @@ -111,30 +113,6 @@ public void setAddRefPkNodePathRight(String addRefPkNodePathRight) {
this.addRefPkNodePathRight = addRefPkNodePathRight;
}

// public String getPath() {
// return path;
// }
//
// public void setPath(String path) {
// this.path = path;
// }
//
// public String getLeftPath() {
// return leftPath;
// }
//
// public void setLeftPath(String leftPath) {
// this.leftPath = leftPath;
// }
//
// public String getRightPath() {
// return rightPath;
// }
//
// public void setRightPath(String rightPath) {
// this.rightPath = rightPath;
// }

public int getWarn() {
return warn;
}
Expand All @@ -151,29 +129,9 @@ public void setLogTag(LogTag logTag) {
this.logTag = logTag;
}

// private void processPath() {
// if (this.pathPair != null) {
// if (this.pathPair.getLeftUnmatchedPath().size() >= this.pathPair.getRightUnmatchedPath().size()) {
// this.path = ListUti.convertPathToStringForShow(this.pathPair.getLeftUnmatchedPath());
// } else {
// this.path = ListUti.convertPathToStringForShow(this.pathPair.getRightUnmatchedPath());
// }
// this.leftPath = ListUti.convertPathToStringForShow(this.pathPair.getLeftUnmatchedPath());
// this.rightPath = ListUti.convertPathToStringForShow(this.pathPair.getRightUnmatchedPath());
// }
// }

private String valueToString(Object value) {
if (value instanceof NullNode || value == null) {
return null;
}
if (value instanceof ObjectNode || value instanceof ArrayNode) {
return null;
}
if (value instanceof JsonNode) {
return ((JsonNode) value).asText();
}
return value.toString();
public void simplifyLogMsg() {
this.baseValue = valueToString(baseValue);
this.testValue = valueToString(testValue);
}

private void processLogInfo(Object baseValue, Object testValue, int unmatchedType) {
Expand Down Expand Up @@ -231,6 +189,67 @@ private void processLogInfo(Object baseValue, Object testValue, int unmatchedTyp
}
}

private String valueToString(Object value) {
if (value instanceof NullNode || value == null) {
return null;
}
if (value instanceof ObjectNode) {
return "[Object]";
}
if (value instanceof ArrayNode) {
return "[List]";
}
if (value instanceof JsonNode) {
return ((JsonNode) value).asText();
}
return value.toString();
}

private void addNodeErrorType() {
NodeErrorType nodeErrorType = new NodeErrorType();
nodeErrorType.setBaseNodeType(judgeNodeErrorType(baseValue));
nodeErrorType.setTestNodeType(judgeNodeErrorType(testValue));
logTag.setNodeErrorType(nodeErrorType);
}

private String judgeNodeErrorType(Object value) {
if (value == null) {
return "null";
}
String simpleName = value.getClass().getSimpleName();
return convertJsonNodeToString(simpleName);
}

private String convertJsonNodeToString(String simpleClassName) {
switch (simpleClassName) {
case "ObjectNode":
return "object";
case "ArrayNode":
return "array";
case "NullNode":
return "null";
case "TextNode":
return "string";
case "IntNode":
return "int";
case "LongNode":
return "long";
case "DoubleNode":
return "double";
case "FloatNode":
return "float";
case "BigIntegerNode":
return "bigInteger";
case "BigDecimalNode":
return "bigDecimal";
case "BooleanNode":
return "boolean";
default:
return simpleClassName;
}
}


@Override
public String toString() {
if (this.pathPair == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class LogTag implements Serializable {
*/
private int errorType = 0;

private NodeErrorType nodeErrorType;

public LogTag() {
}

Expand All @@ -19,4 +21,37 @@ public int getErrorType() {
public void setErrorType(int errorType) {
this.errorType = errorType;
}

public NodeErrorType getNodeErrorType() {
return nodeErrorType;
}

public void setNodeErrorType(NodeErrorType nodeErrorType) {
this.nodeErrorType = nodeErrorType;
}

public static class NodeErrorType {

private String baseNodeType;
private String testNodeType;

public NodeErrorType() {
}

public String getBaseNodeType() {
return baseNodeType;
}

public void setBaseNodeType(String baseNodeType) {
this.baseNodeType = baseNodeType;
}

public String getTestNodeType() {
return testNodeType;
}

public void setTestNodeType(String testNodeType) {
this.testNodeType = testNodeType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -857,5 +857,38 @@ public void testNullEqualsNotExist() {
Assertions.assertEquals(0, result3.getLogs().size());
}

@Test
public void testSimplifyLogEntityMsg() {
CompareSDK sdk = new CompareSDK();

String baseMsg = "{\n"
+ " \"studentName\": \"xiaoming\",\n"
+ " \"classInfo\": {\n"
+ " \"name\": \"classA\",\n"
+ " \"location\": \"floor_A\"\n"
+ " }\n"
+ "}";
String testMsg = "{\n"
+ " \"studentName\": \"xiaoming\"\n"
+ "}";

CompareResult compare = sdk.compare(baseMsg, testMsg);
Assertions.assertEquals("[Object]", compare.getLogs().get(0).getBaseValue());
}

@Test
public void testOutPutErrorValueType() {
CompareSDK sdk = new CompareSDK();

String baseMsg = "{\n"
+ " \"score\": \"18\""
+ "}";
String testMsg = "{\n"
+ " \"score\": 19\n"
+ "}";

CompareResult compare = sdk.compare(baseMsg, testMsg);
Assertions.assertEquals("18", compare.getLogs().get(0).getBaseValue());
}

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