Added isSubset for testing nodes

This commit is contained in:
Andreas Isler
2025-11-25 05:58:25 +01:00
parent 3806797041
commit f8ee09c97a
3 changed files with 55 additions and 2 deletions
@@ -175,4 +175,53 @@ public class FlowableExcelMapper {
}
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;
}
}
@@ -1,6 +1,7 @@
package com.example.parser;
import com.fasterxml.jackson.databind.JsonNode;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
@@ -40,7 +41,10 @@ class FlowableExcelMapperTest {
}
@ParameterizedTest
@MethodSource("excelMapperTestData")
public void excelMapperTest(JsonNode sheet) {
logger.info("node2={}{}", System.lineSeparator(), sheet.toPrettyString());
public void excelMapperTest(JsonNode row) {
JsonNode asIs = row.get("root");
JsonNode toBe = row.get("test");
logger.info("{}asIs= {}{}toBe= {}{}", System.lineSeparator(), asIs.toPrettyString(), System.lineSeparator(), toBe.toPrettyString(), System.lineSeparator());
Assertions.assertThat(flowableExcelMapper.isSubset(asIs, toBe)).isTrue();
}
}
Binary file not shown.