Skip to content

Commit 173a2be

Browse files
committed
Add singleElement() to GraphQlTester.EntityList
singleElement() is a utility method to check for lists of size one, and return the Entity in the same statement. Signed-off-by: Simone Conte <conte@adobe.com>
1 parent 7617dc0 commit 173a2be

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,18 @@ public EntityList<E> hasSizeGreaterThan(int size) {
655655
getEntity().size() > size));
656656
return this;
657657
}
658+
659+
@Override
660+
public Entity<E, ?> singleElement() {
661+
this.hasSize(1);
662+
663+
E element = this.get().get(0);
664+
@SuppressWarnings("unchecked")
665+
Class<E> elementClass = (Class<E>) element.getClass();
666+
667+
return new DefaultPath(DefaultPath.this.basePath, DefaultPath.this.path + "[0]", DefaultPath.this.delegate)
668+
.entity(elementClass);
669+
}
658670
}
659671

660672

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,13 @@ interface EntityList<E> extends Entity<List<E>, EntityList<E>> {
470470
*/
471471
EntityList<E> hasSizeGreaterThan(int size);
472472

473+
/**
474+
* Verify the list has a single element and return an {@code Entity} spec for it.
475+
* <p>This is a convenience method that combines {@link #hasSize(int) hasSize(1)}
476+
* with navigating to the first element in the list.
477+
* @return an {@code Entity} spec for the single element that allows further assertions
478+
*/
479+
Entity<E, ?> singleElement();
473480
}
474481

475482
/**

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,35 @@ void entityList() {
213213
.containsExactly(han, leia);
214214

215215
assertThat(getActualRequestDocument()).contains(document);
216+
217+
assertThatThrownBy(() -> entityList.singleElement())
218+
.as("Should have exactly one element")
219+
.hasMessage("Expecting list " +
220+
"[MovieCharacter[name='Han Solo'], MovieCharacter[name='Leia Organa']] " +
221+
"at path 'me.friends' to have size == 1\n" +
222+
"Request: document='{me {name, friends}}'");
223+
}
224+
225+
@Test
226+
void entityListWithOneElement() {
227+
228+
String document = "{me {name, friends}}";
229+
getGraphQlService().setDataAsJson(document,
230+
"{" +
231+
" \"me\":{" +
232+
" \"name\":\"Luke Skywalker\","
233+
+ " \"friends\":[{\"name\":\"Han Solo\"}]" +
234+
" }" +
235+
"}");
236+
237+
GraphQlTester.Response response = graphQlTester().document(document).execute();
238+
239+
MovieCharacter han = MovieCharacter.create("Han Solo");
240+
241+
response.path("me.friends")
242+
.entityList(MovieCharacter.class)
243+
.singleElement()
244+
.isEqualTo(han);
216245
}
217246

218247
@Test

0 commit comments

Comments
 (0)