Moved isSubset to mapper test

This commit is contained in:
Andreas Isler
2025-11-25 11:58:10 +01:00
parent f8ee09c97a
commit ff8f71abf4
3 changed files with 53 additions and 55 deletions
@@ -75,9 +75,7 @@ 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);
if (cellValue == null || cellValue.isEmpty()) { if (cellValue != null && !cellValue.isEmpty()) {
jsonNode = addNode(jsonNode, paths.get(cellNum), null);
} else {
String type = types.get(cellNum); String type = types.get(cellNum);
Object content; Object content;
switch (type) { switch (type) {
@@ -175,53 +173,4 @@ public class FlowableExcelMapper {
} }
return -1; return -1;
} }
public boolean isSubset(JsonNode asIs, JsonNode toBe) {
// If toBe is null, it is always a subset
if (toBe == null) {
return true;
}
// If asIs is null, but toBe is not, toBe is not a subset
if (asIs == null) {
return false;
}
// If toBo is a value node, compare values
if (toBe.isValueNode()) {
return asIs.isValueNode() && asIs.asText().equals(toBe.asText());
}
// If toBe is an array node, check is all elements of toBe exist in asIs
if(toBe.isArray()){
if (!asIs.isArray() || toBe.size() > asIs.size()) {
return false;
}
for (JsonNode toBeElement : toBe) {
boolean found = false;
for (JsonNode asIsElement : asIs) {
if (isSubset(asIsElement, toBeElement)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
// If toBe is an object node, check if all fields in toBe exist in asIs
if (toBe.isObject()){
if (!asIs.isObject()){
return false;
}
for (Iterator<String> iterator = toBe.fieldNames(); iterator.hasNext(); ) {
String fieldName = iterator.next();
if (!asIs.has(fieldName) || !isSubset(asIs.get(fieldName), toBe.get(fieldName))){
return false;
}
}
return true;
}
// If none of the above matches, return false
return false;
}
} }
@@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -22,8 +23,55 @@ class FlowableExcelMapperTest {
@Autowired @Autowired
private FlowableExcelMapper flowableExcelMapper; private FlowableExcelMapper flowableExcelMapper;
private static final Logger logger = LoggerFactory.getLogger(FlowableExcelMapperTest.class);
public boolean isSubset(JsonNode root, JsonNode test) {
// If test is null, it is always a subset of root
if (test == null) {
return true;
}
// If root is null, but test is not, test is not a subset of root
if (root == null) {
return false;
}
// If test is a value node, compare values
if (test.isValueNode()) {
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.isArray()){
if (!root.isArray() || test.size() > root.size()) {
return false;
}
for (JsonNode rootElement : test) {
boolean found = false;
for (JsonNode testElement : root) {
if (isSubset(testElement, rootElement)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
// If test is an object node, check if all fields in test exist in root
if (test.isObject()){
if (!root.isObject()){
return false;
}
for (Iterator<String> iterator = test.fieldNames(); iterator.hasNext(); ) {
String fieldName = iterator.next();
if (!root.has(fieldName) || !isSubset(root.get(fieldName), test.get(fieldName))){
return false;
}
}
return true;
}
// If none of the above matches, return false
return false;
}
protected Stream<Arguments> getArgumentsFromExcel(String path) { protected Stream<Arguments> getArgumentsFromExcel(String path) {
ArrayList<Arguments> argumentList = new ArrayList<>(); ArrayList<Arguments> argumentList = new ArrayList<>();
@@ -44,7 +92,8 @@ class FlowableExcelMapperTest {
public void excelMapperTest(JsonNode row) { public void excelMapperTest(JsonNode row) {
JsonNode asIs = row.get("root"); JsonNode asIs = row.get("root");
JsonNode toBe = row.get("test"); JsonNode toBe = row.get("test");
logger.info("{}asIs= {}{}toBe= {}{}", System.lineSeparator(), asIs.toPrettyString(), System.lineSeparator(), toBe.toPrettyString(), System.lineSeparator()); //logger.info("{}root={}{}test={}", System.lineSeparator(), asIs, System.lineSeparator(), toBe);
Assertions.assertThat(flowableExcelMapper.isSubset(asIs, toBe)).isTrue(); Assertions.assertThat(isSubset(asIs, toBe))
.withFailMessage(System.lineSeparator() + "root=" + asIs + System.lineSeparator() + "test=" + toBe).isTrue();
} }
} }
Binary file not shown.