feature/use-case-acceptance-tests #116

Merged
hsh-michaelhoennig merged 49 commits from feature/use-case-acceptance-tests into master 2024-10-30 11:40:46 +01:00
Showing only changes of commit d55fea7851 - Show all commits

View File

@ -1,8 +1,8 @@
package net.hostsharing.hsadminng.hs.office.scenarios;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import io.restassured.http.ContentType;
import lombok.Getter;
import lombok.SneakyThrows;
@ -258,9 +258,7 @@ public abstract class UseCase<T extends UseCase<?>> {
@SneakyThrows
public String getFromBody(final String path) {
// FIXME: use JsonPath: https://www.baeldung.com/guide-to-jayway-jsonpath
final var rootNode = objectMapper.readTree(response.body());
return getPropertyFromJson(rootNode, path);
return JsonPath.parse(response.body()).read(path);
}
}
@ -296,58 +294,7 @@ public abstract class UseCase<T extends UseCase<?>> {
throw new AssertionFailure("exactly one value required, but got '" + one + "' and '" + another + "'");
}
private final String title(String resultAlias) {
private String title(String resultAlias) {
return getClass().getSimpleName().replaceAll("([a-z])([A-Z]+)", "$1 $2") + " => " + resultAlias;
}
// FIXME: refactor to own class
/**
* Extracts a property from a JsonNode based on a dotted path.
* Supports array notation like "users[0].address.city" and root arrays like "[0].user.address.city".
*
* @param rootNode the root JsonNode
* @param propertyPath the property path in dot notation (e.g., "[0].user.address.city")
* @return the extracted property value as a String
*/
public static String getPropertyFromJson(final JsonNode rootNode, final String propertyPath) {
final var pathParts = propertyPath.split("\\.");
var currentNode = rootNode;
// Traverse the JSON structure based on the path parts
for (final var part : pathParts) {
// Check if the part contains array notation like "[0]"
if (part.contains("[")) {
String arrayName;
final var arrayIndex = Integer.parseInt(part.substring(part.indexOf("[") + 1, part.indexOf("]")));
if (part.startsWith("[")) {
// This is a root-level array access (e.g., "[0]")
arrayName = null;
} else {
// This is a nested array access (e.g., "users[0]")
arrayName = part.substring(0, part.indexOf("["));
}
// If there's an array name, traverse to it
if (arrayName != null) {
currentNode = currentNode.path(arrayName);
}
// Ensure the current node is an array, then access the element at the index
if (currentNode.isArray()) {
currentNode = currentNode.get(arrayIndex);
}
} else {
// Traverse as a normal field
currentNode = currentNode.path(part);
}
// If at any point, the node is missing, return null
if (currentNode.isMissingNode()) {
return null;
}
}
return currentNode.asText(); // Return the final value as a String
}
}