Skip to content

Commit 4d50ecd

Browse files
committed
Revise JSON path options in GraphQlTester
Closes gh-278
1 parent 3d63891 commit 4d50ecd

File tree

3 files changed

+51
-63
lines changed

3 files changed

+51
-63
lines changed

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434

3535
import org.springframework.core.ParameterizedTypeReference;
3636
import org.springframework.core.ResolvableType;
37-
import org.springframework.graphql.support.DefaultGraphQlRequest;
3837
import org.springframework.graphql.GraphQlRequest;
3938
import org.springframework.graphql.GraphQlResponse;
4039
import org.springframework.graphql.ResponseError;
4140
import org.springframework.graphql.client.GraphQlTransport;
41+
import org.springframework.graphql.support.DefaultGraphQlRequest;
4242
import org.springframework.graphql.support.DocumentSource;
4343
import org.springframework.lang.Nullable;
4444
import org.springframework.test.util.AssertionErrors;
@@ -377,38 +377,25 @@ public Path path(String path) {
377377
}
378378

379379
@Override
380-
public Path pathExists() {
381-
this.delegate.doAssert(() -> this.pathHelper.hasJsonPath(this.delegate.jsonContent()));
382-
return this;
383-
}
384-
385-
@Override
386-
public Path pathDoesNotExist() {
387-
this.delegate.doAssert(() -> this.pathHelper.doesNotHaveJsonPath(this.delegate.jsonContent()));
388-
return this;
389-
}
390-
391-
@Override
392-
public Path valueExists() {
380+
public Path hasValue() {
393381
this.delegate.doAssert(() -> this.pathHelper.exists(this.delegate.jsonContent()));
394382
return this;
395383
}
396384

397385
@Override
398-
public Path valueDoesNotExist() {
399-
this.delegate.doAssert(() -> this.pathHelper.doesNotExist(this.delegate.jsonContent()));
400-
return this;
401-
}
402-
403-
@Override
404-
public Path valueIsEmpty() {
405-
this.delegate.doAssert(() -> this.pathHelper.assertValueIsEmpty(this.delegate.jsonContent()));
386+
public Path valueIsNull() {
387+
Assert.isTrue(this.jsonPath.isDefinite(), "isNull applies only to JSONPath targeting a single value");
388+
this.delegate.doAssert(() -> {
389+
Object value = this.pathHelper.evaluateJsonPath(this.delegate.jsonContent());
390+
AssertionErrors.assertNull(
391+
"Expected null value at JSON path \"" + path + "\" but found " + value, value);
392+
});
406393
return this;
407394
}
408395

409396
@Override
410-
public Path valueIsNotEmpty() {
411-
this.delegate.doAssert(() -> this.pathHelper.assertValueIsNotEmpty(this.delegate.jsonContent()));
397+
public Path pathDoesNotExist() {
398+
this.delegate.doAssert(() -> this.pathHelper.doesNotHaveJsonPath(this.delegate.jsonContent()));
412399
return this;
413400
}
414401

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -213,43 +213,21 @@ interface Response extends Traversable {
213213
interface Path extends Traversable {
214214

215215
/**
216-
* Verify the given path exists, even if the value is {@code null}.
216+
* Verify there is a {@code non-null} value or a non-empty list at the current path.
217217
* @return the same {@code Path} spec for further assertions
218218
*/
219-
Path pathExists();
219+
Path hasValue();
220220

221221
/**
222-
* Assert the given path does not {@link #pathExists() exist}.
223-
* @return the same {@code Path} spec for further assertions
224-
*/
225-
Path pathDoesNotExist();
226-
227-
/**
228-
* Assert a value exists at the given path where the value is any {@code non-null}
229-
* value, possibly an empty array or map.
230-
* @return the same {@code Path} spec for further assertions
222+
* Verify there is a {@code null} value at the current path.
231223
*/
232-
Path valueExists();
224+
Path valueIsNull();
233225

234226
/**
235-
* Assert a value does not {@link #valueExists() exist} at the given path.
227+
* Verify the current path does not exist.
236228
* @return the same {@code Path} spec for further assertions
237229
*/
238-
Path valueDoesNotExist();
239-
240-
/**
241-
* Assert the value at the given path does exist but is empty as defined
242-
* in {@link org.springframework.util.ObjectUtils#isEmpty(Object)}.
243-
* @return the same {@code Path} spec for further assertions
244-
* @see org.springframework.util.ObjectUtils#isEmpty(Object)
245-
*/
246-
Path valueIsEmpty();
247-
248-
/**
249-
* Assert the value at the given path is not {@link #valueIsEmpty() empty}.
250-
* @return the same {@code Path} spec for further assertions
251-
*/
252-
Path valueIsNotEmpty();
230+
Path pathDoesNotExist();
253231

254232
/**
255233
* Convert the data at the given path to the target type.

spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,61 @@
4040
public class GraphQlTesterTests extends GraphQlTesterTestSupport {
4141

4242
@Test
43-
void pathAndValueExist() {
43+
void hasValue() {
4444

4545
String document = "{me {name, friends}}";
4646
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
4747

4848
GraphQlTester.Response response = graphQlTester().document(document).execute();
49+
response.path("me.name").hasValue();
50+
response.path("me.friends").hasValue();
4951

50-
response.path("me.name").pathExists().valueExists();
51-
response.path("me.friends").pathExists().valueExists();
52-
response.path("hero").pathDoesNotExist().valueDoesNotExist();
52+
assertThatThrownBy(() -> response.path("hero").hasValue())
53+
.hasMessageContaining("No value at JSON path \"$['data']['hero']");
5354

5455
assertThat(request().getDocument()).contains(document);
5556
}
5657

5758
@Test
58-
void valueIsEmpty() {
59+
void valueIsNull() {
5960

6061
String document = "{me {name, friends}}";
61-
setMockResponse("{\"me\": {\"name\":null, \"friends\":[]}}");
62+
setMockResponse("{\"me\": {\"name\":null, \"friends\":null}}");
6263

6364
GraphQlTester.Response response = graphQlTester().document(document).execute();
6465

65-
response.path("me.name").valueIsEmpty();
66-
response.path("me.friends").valueIsEmpty();
66+
response.path("me.name").valueIsNull();
67+
response.path("me.friends").valueIsNull();
6768

68-
assertThatThrownBy(() -> response.path("hero").valueIsEmpty())
69-
.as("Path does not even exist")
70-
.hasMessageContaining("No value at JSON path \"$['data']['hero']");
69+
assertThatThrownBy(() -> response.path("me").valueIsNull())
70+
.hasMessageContaining("Expected null value at JSON path");
7171

7272
assertThat(request().getDocument()).contains(document);
7373
}
7474

75+
@Test
76+
void valueIsEmptyList() {
77+
78+
String document = "{me {name, friends}}";
79+
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
80+
81+
GraphQlTester.Response response = graphQlTester().document(document).execute();
82+
response.path("me.friends").hasValue().entityList(MovieCharacter.class).hasSize(0);
83+
}
84+
85+
@Test
86+
void pathDoesNotExist() {
87+
String document = "{me {name, friends}}";
88+
setMockResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
89+
90+
GraphQlTester.Response response = graphQlTester().document(document).execute();
91+
92+
response.path("hero").pathDoesNotExist();
93+
94+
assertThatThrownBy(() -> response.path("me.name").pathDoesNotExist())
95+
.hasMessageContaining("Expected no value at JSON path");
96+
}
97+
7598
@Test
7699
void matchesJson() {
77100

0 commit comments

Comments
 (0)