Skip to content
Open
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,18 @@ 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 Entity<List<E>, EntityList<E>> {
*/
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 @@ -213,6 +213,35 @@ void entityList() {
.containsExactly(han, leia);

assertThat(getActualRequestDocument()).contains(document);

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 entityListWithOneElement() {

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
Expand Down