Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,16 @@ public EntityList<E> hasSizeGreaterThan(int size) {
getEntity().size() > size));
return this;
}

@Override
public Entity<E, ?> singleElement() {
this.hasSize(1);
E element = this.get().get(0);
@SuppressWarnings("unchecked")
Class<E> elementClass = (Class<E>) element.getClass();
return new DefaultPath(DefaultPath.this.basePath, DefaultPath.this.path + "[0]", DefaultPath.this.delegate)
.entity(elementClass);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ interface EntityList<E extends @Nullable Object> extends Entity<List<E>, EntityL
*/
EntityList<E> hasSizeGreaterThan(int size);

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ void entityList() {

String document = "{me {name, friends}}";
getGraphQlService().setDataAsJson(document,
"{" +
" \"me\":{" +
" \"name\":\"Luke Skywalker\","
+ " \"friends\":[{\"name\":\"Han Solo\"}, {\"name\":\"Leia Organa\"}]" +
" }" +
"}");
"""
{
"me":{
"name":"Luke Skywalker",
"friends":[{"name":"Han Solo"}, {"name":"Leia Organa"}]
}
}
""");

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

Expand Down Expand Up @@ -215,6 +217,54 @@ void entityList() {
assertThat(getActualRequestDocument()).contains(document);
}

@Test
void singleElementShouldThrowWhenMultiple() {

String document = "{me {name, friends}}";
getGraphQlService().setDataAsJson(document,
"""
{
"me":{
"name":"Luke Skywalker",
"friends":[{"name":"Han Solo"}, {"name":"Leia Organa"}]
}
}
""");

GraphQlTester.Response response = graphQlTester().document(document).execute();
GraphQlTester.EntityList<MovieCharacter> entityList = response.path("me.friends").entityList(MovieCharacter.class);
assertThatThrownBy(entityList::singleElement)
.as("Should have exactly one element")
.hasMessage("Expecting list " +
"[MovieCharacter[name='Han Solo'], MovieCharacter[name='Leia Organa']] " +
"at path 'me.friends' to have size == 1\n" +
"Request: document='{me {name, friends}}'");
}

@Test
void entityListWithSingleElement() {

String document = "{me {name, friends}}";
getGraphQlService().setDataAsJson(document,
"""
{
"me":{
"name":"Luke Skywalker",
"friends":[{"name":"Han Solo"}]
}
}
""");

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

MovieCharacter han = MovieCharacter.create("Han Solo");

response.path("me.friends")
.entityList(MovieCharacter.class)
.singleElement()
.isEqualTo(han);
}

@Test
void nestedPath() {

Expand Down
Loading