Added __N_A for negative tests
This commit is contained in:
@@ -62,7 +62,7 @@ public class FlowableExcelMapper {
|
|||||||
// The 1st row contains the paths
|
// The 1st row contains the paths
|
||||||
paths.addAll(rows.get(rowNum));
|
paths.addAll(rows.get(rowNum));
|
||||||
} else if (rowNum == 1) {
|
} else if (rowNum == 1) {
|
||||||
// The 1st row contains the types of the variables
|
// The 2nd row contains the types of the variables
|
||||||
types.addAll(rows.get(rowNum));
|
types.addAll(rows.get(rowNum));
|
||||||
} else {
|
} else {
|
||||||
// All other rows contain the values of the variables
|
// All other rows contain the values of the variables
|
||||||
@@ -75,11 +75,22 @@ public class FlowableExcelMapper {
|
|||||||
protected JsonNode rowToJsonNode(ArrayList<String> types, ArrayList<String> paths, ArrayList<String> row, JsonNode jsonNode) {
|
protected JsonNode rowToJsonNode(ArrayList<String> types, ArrayList<String> paths, ArrayList<String> row, JsonNode jsonNode) {
|
||||||
for (int cellNum = 0; cellNum < row.size(); cellNum++) {
|
for (int cellNum = 0; cellNum < row.size(); cellNum++) {
|
||||||
String cellValue = row.get(cellNum);
|
String cellValue = row.get(cellNum);
|
||||||
|
// Empty cells are not added to node
|
||||||
if (cellValue != null && !cellValue.isEmpty()) {
|
if (cellValue != null && !cellValue.isEmpty()) {
|
||||||
String type = types.get(cellNum);
|
String type = types.get(cellNum);
|
||||||
Object content;
|
Object content;
|
||||||
|
switch (cellValue) {
|
||||||
|
case "__NULL": content = null; break;
|
||||||
|
case "__N_A": content = "__N_A"; break;
|
||||||
|
default:
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "String": content = cellValue; break;
|
case "String":
|
||||||
|
switch (cellValue) {
|
||||||
|
case "__EMPTY": content = ""; break;
|
||||||
|
case "__BLANK": content = " "; break;
|
||||||
|
default: content = cellValue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case "Boolean": content = Boolean.parseBoolean(cellValue); break;
|
case "Boolean": content = Boolean.parseBoolean(cellValue); break;
|
||||||
case "Integer": content = Integer.parseInt(cellValue); break;
|
case "Integer": content = Integer.parseInt(cellValue); break;
|
||||||
case "Double": content = Double.parseDouble(cellValue); break;
|
case "Double": content = Double.parseDouble(cellValue); break;
|
||||||
@@ -99,6 +110,7 @@ public class FlowableExcelMapper {
|
|||||||
default:
|
default:
|
||||||
throw new RuntimeException("Variable type not supported: " + type);
|
throw new RuntimeException("Variable type not supported: " + type);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
jsonNode = addNode(jsonNode, paths.get(cellNum), content);
|
jsonNode = addNode(jsonNode, paths.get(cellNum), content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,34 +23,39 @@ class FlowableExcelMapperTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private FlowableExcelMapper flowableExcelMapper;
|
private FlowableExcelMapper flowableExcelMapper;
|
||||||
|
|
||||||
|
protected static final Logger logger = LoggerFactory.getLogger(FlowableExcelMapperTest.class);
|
||||||
|
|
||||||
|
|
||||||
public boolean isSubset(JsonNode root, JsonNode test) {
|
public boolean isSubset(JsonNode root, JsonNode test) {
|
||||||
// If test is null, it is always a subset of root
|
// If test is null, it is always a subset of root
|
||||||
if (test == null) {
|
if (test == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// If root is null, but test is not, test is not a subset of root
|
// If test is a value node, compare values
|
||||||
|
if (test.isValueNode()) {
|
||||||
|
if (test.isTextual() && "__N_A".equals(test.asText())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (root == null) {
|
if (root == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// If test is a value node, compare values
|
|
||||||
if (test.isValueNode()) {
|
|
||||||
return root.isValueNode() && root.asText().equals(test.asText());
|
return root.isValueNode() && root.asText().equals(test.asText());
|
||||||
}
|
}
|
||||||
// If test is an array node, check if all elements of test exist in root
|
// If test is an array node, check if all elements of test exist in root
|
||||||
if(test.isArray()){
|
if(test.isArray()){
|
||||||
if (!root.isArray() || test.size() > root.size()) {
|
for (int i = 0; i < test.size(); i++) {
|
||||||
|
JsonNode testElement = test.get(i);
|
||||||
|
JsonNode rootElement = null;
|
||||||
|
if (root != null) {
|
||||||
|
rootElement = root.get(i);
|
||||||
|
}
|
||||||
|
if (testElement.isTextual() && "__N_A".equals(testElement.asText())) {
|
||||||
|
if (rootElement != null && !rootElement.isNull()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (JsonNode rootElement : test) {
|
continue;
|
||||||
boolean found = false;
|
|
||||||
for (JsonNode testElement : root) {
|
|
||||||
if (isSubset(testElement, rootElement)) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
if (!isSubset(rootElement, testElement)) {
|
||||||
if (!found) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,12 +63,20 @@ class FlowableExcelMapperTest {
|
|||||||
}
|
}
|
||||||
// If test is an object node, check if all fields in test exist in root
|
// If test is an object node, check if all fields in test exist in root
|
||||||
if (test.isObject()){
|
if (test.isObject()){
|
||||||
if (!root.isObject()){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (Iterator<String> iterator = test.fieldNames(); iterator.hasNext(); ) {
|
for (Iterator<String> iterator = test.fieldNames(); iterator.hasNext(); ) {
|
||||||
String fieldName = iterator.next();
|
String fieldName = iterator.next();
|
||||||
if (!root.has(fieldName) || !isSubset(root.get(fieldName), test.get(fieldName))){
|
JsonNode testValue = test.get(fieldName);
|
||||||
|
JsonNode rootValue = null;
|
||||||
|
if (root != null) {
|
||||||
|
rootValue = root.get(fieldName);
|
||||||
|
}
|
||||||
|
if (testValue.isTextual() && "__N_A".equals(testValue.asText())) {
|
||||||
|
if (rootValue != null && !rootValue.isNull()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!isSubset(rootValue, testValue)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,16 +97,29 @@ class FlowableExcelMapperTest {
|
|||||||
return argumentList.stream();
|
return argumentList.stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Stream<Arguments> excelMapperTestData() {
|
protected Stream<Arguments> excelMapperTestDataOk() {
|
||||||
return getArgumentsFromExcel("flowableExcelMapperTestData.xlsx");
|
return getArgumentsFromExcel("flowableExcelMapperTestData_ok.xlsx");
|
||||||
}
|
}
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource("excelMapperTestData")
|
@MethodSource("excelMapperTestDataOk")
|
||||||
public void excelMapperTest(JsonNode row) {
|
public void excelMapperTestOk(JsonNode row) {
|
||||||
JsonNode asIs = row.get("root");
|
JsonNode root = row.get("root");
|
||||||
JsonNode toBe = row.get("test");
|
JsonNode test = row.get("test");
|
||||||
//logger.info("{}root={}{}test={}", System.lineSeparator(), asIs, System.lineSeparator(), toBe);
|
logger.info("{}root={}{}test={}", System.lineSeparator(), root, System.lineSeparator(), test);
|
||||||
Assertions.assertThat(isSubset(asIs, toBe))
|
Assertions.assertThat(isSubset(root, test))
|
||||||
.withFailMessage(System.lineSeparator() + "root=" + asIs + System.lineSeparator() + "test=" + toBe).isTrue();
|
.withFailMessage(System.lineSeparator() + "root=" + root + System.lineSeparator() + "test=" + test).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Stream<Arguments> excelMapperTestDataFail() {
|
||||||
|
return getArgumentsFromExcel("flowableExcelMapperTestData_fail.xlsx");
|
||||||
|
}
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("excelMapperTestDataFail")
|
||||||
|
public void excelMapperTestFail(JsonNode row) {
|
||||||
|
JsonNode root = row.get("root");
|
||||||
|
JsonNode test = row.get("test");
|
||||||
|
logger.info("{}root={}{}test={}", System.lineSeparator(), root, System.lineSeparator(), test);
|
||||||
|
Assertions.assertThat(isSubset(root, test))
|
||||||
|
.withFailMessage(System.lineSeparator() + "root=" + root + System.lineSeparator() + "test=" + test).isFalse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user