Avoid using JSONPath#getPath()

It does not return the original path and omits expressions.

Closes gh-377
This commit is contained in:
rstoyanchev
2022-05-12 15:42:38 +01:00
parent 8549060d19
commit b07e4fdd64
2 changed files with 9 additions and 7 deletions

View File

@@ -330,24 +330,25 @@ final class DefaultGraphQlTester implements GraphQlTester {
private final JsonPathExpectationsHelper pathHelper;
private DefaultPath(String path, ResponseDelegate delegate) {
Assert.notNull(path, "`path` is required");
Assert.notNull(delegate, "ResponseContainer is required");
String fullPath = initFullPath(path);
this.path = path;
this.delegate = delegate;
this.jsonPath = initJsonPath(path);
this.pathHelper = new JsonPathExpectationsHelper(this.jsonPath.getPath());
this.jsonPath = JsonPath.compile(fullPath);
this.pathHelper = new JsonPathExpectationsHelper(fullPath);
}
private static JsonPath initJsonPath(String path) {
private static String initFullPath(String path) {
if (!StringUtils.hasText(path)) {
path = "$.data";
}
else if (!path.startsWith("$") && !path.startsWith("data.")) {
path = "$.data." + path;
}
return JsonPath.compile(path);
return path;
}
@Override

View File

@@ -48,9 +48,10 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
GraphQlTester.Response response = graphQlTester().document(document).execute();
response.path("me.name").hasValue();
response.path("me.friends").hasValue();
response.path("me[?(@.name == 'Luke Skywalker')].name").hasValue(); // gh-377
assertThatThrownBy(() -> response.path("hero").hasValue())
.hasMessageContaining("No value at JSON path \"$['data']['hero']");
.hasMessageContaining("No value at JSON path \"$.data.hero\"");
assertThat(getActualRequestDocument()).contains(document);
}
@@ -152,7 +153,7 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
"{" +
" \"me\":{" +
" \"name\":\"Luke Skywalker\","
+ " \"friends\":[{\"name\":\"Han Solo\"}, {\"name\":\"Leia Organa\"}]" +
+ " \"friends\":[{\"name\":\"Han Solo\"}, {\"name\":\"Leia Organa\"}]" +
" }" +
"}");