I used the script that was provided to me as an asnwer to my older queries and I used get data to add teh JDBC resultset. The script hat was provided allowed to match to JSON responses and I was trying to match a JSON response with a JDBC resulset. This is what I have -
//def String mongoResponse
def String mongoResponse = context.expand( '${GetBenefitDetail_by Ben Seq#Response}' )
def String response = context.expand( '${JDBC Request#ResponseAsXml}' )
//def String response = context.expand( '${JDBC Request#policy}' )
mapper = new ObjectMapper()
JsonNode jsonResponse = mapper.readTree(response);
JsonNode mongoDBResponse = mapper.readTree(mongoResponse);
Set<JsonPathValue> pathValues = listLeafValues(jsonResponse, '$');
Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider(mapper))
.build();
pathValues.each {
try {
JsonPath jsonPath = JsonPath.compile(it.path);
JsonNode result = jsonPath.read(mongoDBResponse, configuration);
if (result != it.value) {
log.info('Node with path ' + it.path + ' and value ' + it.value + ' cannot be found')
}
} catch (Exception e) {
log.info('Node with path ' + it.path + ' and value ' + it.value + ' cannot be found')
}
}
Set<JsonPathValue> listLeafValues(JsonNode jsonObject, String path) {
if (jsonObject.isObject()) {
return getValuesFrom((ObjectNode) jsonObject, path);
} else if (jsonObject.isArray()) {
return getValuesFrom((ArrayNode) jsonObject, path);
} else {
}
}
Set<JsonPathValue> getValuesFrom(ObjectNode jsonObject, String path) {
Set<JsonPathValue> values = new LinkedHashSet<>();
Iterator<Map.Entry<String, JsonNode>> fields = jsonObject.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> field = fields.next();
JsonNode value = field.getValue();
String nodePath = path + "." + field.getKey();
addValue(values, value, nodePath);
}
return values;
}
Set<JsonPathValue> getValuesFrom(ArrayNode array, String nodePath) {
Set<JsonPathValue> values = new LinkedHashSet<>();
int index = 0;
for (JsonNode value : array) {
addValue(values, value, nodePath + "[" + index + "]");
index++;
}
return values;
}
def addValue(Set<JsonPathValue> values, JsonNode value, String nodePath) {
if (value.isObject()) {
values.addAll(getValuesFrom((ObjectNode) value, nodePath));
} else if (value.isArray()) {
values.addAll(getValuesFrom((ArrayNode) value, nodePath));
} else if (value.isValueNode()) {
values.add(new JsonPathValue(nodePath, value));
}
}