From d55fea7851168a1efa1d00fbbc07899b27cdf542 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 29 Oct 2024 10:35:50 +0100 Subject: [PATCH] replace own implementation by com.jayway.jsonpath.JsonPath --- .../hs/office/scenarios/UseCase.java | 59 +------------------ 1 file changed, 3 insertions(+), 56 deletions(-) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/UseCase.java b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/UseCase.java index 12d75732..751a027e 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/UseCase.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/UseCase.java @@ -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> { @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> { 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 - } }