From 3d638918f46311efabeed91691308c5307cfc5a8 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 21 Mar 2022 10:04:16 +0000 Subject: [PATCH] Polishing Closes gh-327 --- .../test/tester/DefaultGraphQlTester.java | 24 ++-- .../graphql/test/tester/GraphQlTester.java | 127 +++++++++--------- .../test/tester/GraphQlTesterTests.java | 9 +- .../graphql/test/tester/MovieCharacter.java | 5 + .../graphql/client/MovieCharacter.java | 1 + 5 files changed, 86 insertions(+), 80 deletions(-) diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java index 7d9b543d..626a3547 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java @@ -562,9 +562,9 @@ final class DefaultGraphQlTester implements GraphQlTester { @Override @SuppressWarnings("unchecked") - public EntityList contains(E... elements) { + public EntityList contains(E... values) { doAssert(() -> { - List expected = Arrays.asList(elements); + List expected = Arrays.asList(values); AssertionErrors.assertTrue("List at path '" + getPath() + "' does not contain " + expected, getEntity().containsAll(expected)); }); @@ -573,9 +573,9 @@ final class DefaultGraphQlTester implements GraphQlTester { @Override @SuppressWarnings("unchecked") - public EntityList doesNotContain(E... elements) { + public EntityList doesNotContain(E... values) { doAssert(() -> { - List expected = Arrays.asList(elements); + List expected = Arrays.asList(values); AssertionErrors.assertTrue( "List at path '" + getPath() + "' should not have contained " + expected, !getEntity().containsAll(expected)); @@ -585,9 +585,9 @@ final class DefaultGraphQlTester implements GraphQlTester { @Override @SuppressWarnings("unchecked") - public EntityList containsExactly(E... elements) { + public EntityList containsExactly(E... values) { doAssert(() -> { - List expected = Arrays.asList(elements); + List expected = Arrays.asList(values); AssertionErrors.assertTrue( "List at path '" + getPath() + "' should have contained exactly " + expected, getEntity().equals(expected)); @@ -603,18 +603,18 @@ final class DefaultGraphQlTester implements GraphQlTester { } @Override - public EntityList hasSizeLessThan(int boundary) { + public EntityList hasSizeLessThan(int size) { doAssert(() -> AssertionErrors.assertTrue( - "List at path '" + getPath() + "' should have size less than " + boundary, - getEntity().size() < boundary)); + "List at path '" + getPath() + "' should have size less than " + size, + getEntity().size() < size)); return this; } @Override - public EntityList hasSizeGreaterThan(int boundary) { + public EntityList hasSizeGreaterThan(int size) { doAssert(() -> AssertionErrors.assertTrue( - "List at path '" + getPath() + "' should have size greater than " + boundary, - getEntity().size() > boundary)); + "List at path '" + getPath() + "' should have size greater than " + size, + getEntity().size() > size)); return this; } } diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java index 5fbed6af..92c2e510 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java @@ -208,46 +208,46 @@ public interface GraphQlTester { } /** - * Declare options available to assert data at a given path. + * Options available to assert the response values at the current path. */ interface Path extends Traversable { /** - * Assert the given path exists, even if the value is {@code null}. - * @return spec to assert the converted entity with + * Verify the given path exists, even if the value is {@code null}. + * @return the same {@code Path} spec for further assertions */ Path pathExists(); /** * Assert the given path does not {@link #pathExists() exist}. - * @return spec to assert the converted entity with + * @return the same {@code Path} spec for further assertions */ Path pathDoesNotExist(); /** * Assert a value exists at the given path where the value is any {@code non-null} * value, possibly an empty array or map. - * @return spec to assert the converted entity with + * @return the same {@code Path} spec for further assertions */ Path valueExists(); /** * Assert a value does not {@link #valueExists() exist} at the given path. - * @return spec to assert the converted entity with + * @return the same {@code Path} spec for further assertions */ Path valueDoesNotExist(); /** * Assert the value at the given path does exist but is empty as defined * in {@link org.springframework.util.ObjectUtils#isEmpty(Object)}. - * @return spec to assert the converted entity with + * @return the same {@code Path} spec for further assertions * @see org.springframework.util.ObjectUtils#isEmpty(Object) */ Path valueIsEmpty(); /** * Assert the value at the given path is not {@link #valueIsEmpty() empty}. - * @return spec to assert the converted entity with + * @return the same {@code Path} spec for further assertions */ Path valueIsNotEmpty(); @@ -255,7 +255,7 @@ public interface GraphQlTester { * Convert the data at the given path to the target type. * @param entityType the type to convert to * @param the target entity type - * @return spec to assert the converted entity with + * @return an {@code Entity} spec to verify the decoded value with */ Entity entity(Class entityType); @@ -263,7 +263,7 @@ public interface GraphQlTester { * Convert the data at the given path to the target type. * @param entityType the type to convert to * @param the target entity type - * @return spec to assert the converted entity with + * @return an {@code Entity} spec to verify the decoded value with */ Entity entity(ParameterizedTypeReference entityType); @@ -271,7 +271,7 @@ public interface GraphQlTester { * Convert the data at the given path to a List of the target type. * @param elementType the type of element to convert to * @param the target entity type - * @return spec to assert the converted List of entities with + * @return an {@code EntityList} spec to verify the decoded values with */ EntityList entityList(Class elementType); @@ -279,7 +279,7 @@ public interface GraphQlTester { * Convert the data at the given path to a List of the target type. * @param elementType the type to convert to * @param the target entity type - * @return spec to assert the converted List of entities with + * @return an {@code EntityList} spec to verify the decoded values with */ EntityList entityList(ParameterizedTypeReference elementType); @@ -291,7 +291,7 @@ public interface GraphQlTester { * JSONassert library on to be * on the classpath. * @param expectedJson the expected JSON - * @return spec to specify a different path + * @return {@code Traversable} spec to select a different path * @see org.springframework.test.util.JsonExpectationsHelper#assertJsonEqual(String, * String) */ @@ -303,7 +303,7 @@ public interface GraphQlTester { * of formatting, along with lenient checking, e.g. extensible and non-strict * array ordering. * @param expectedJson the expected JSON - * @return spec to specify a different path + * @return {@code Traversable} spec to select a different path * @see org.springframework.test.util.JsonExpectationsHelper#assertJsonEqual(String, * String, boolean) */ @@ -312,121 +312,114 @@ public interface GraphQlTester { } /** - * Declare options available to assert data converted to an entity. + * Contains a decoded entity and provides options to assert it * * @param the entity type - * @param the spec type, including subtypes + * @param the {@code Entity} spec type */ interface Entity> extends Traversable { /** - * Assert the converted entity equals the given Object. - * @param expected the expected Object - * @param the spec type - * @return the same spec for more assertions + * Verify the decoded entity is equal to the given value. + * @param expected the expected value + * @return the {@code Entity} spec for further assertions */ T isEqualTo(Object expected); /** - * Assert the converted entity does not equal the given Object. - * @param other the Object to check against - * @param the spec type - * @return the same spec for more assertions + * Verify the decoded entity is not equal to the given value. + * @param other the value to check against + * @return the {@code Entity} spec for further assertions */ T isNotEqualTo(Object other); /** - * Assert the converted entity is the same instance as the given Object. - * @param expected the expected Object - * @param the spec type - * @return the same spec for more assertions + * Verify the decoded entity is the same instance as the given value. + * @param expected the expected value + * @return the {@code Entity} spec for further assertions */ T isSameAs(Object expected); /** - * Assert the converted entity is not the same instance as the given Object. - * @param other the Object to check against - * @param the spec type - * @return the same spec for more assertions + * Verify the decoded entity is not the same instance as the given value. + * @param other the value to check against + * @return the {@code Entity} spec for further assertions */ T isNotSameAs(Object other); /** - * Assert the converted entity matches the given predicate. - * @param predicate the expected Object - * @param the spec type - * @return the same spec for more assertions + * Verify the decoded entity matches the given predicate. + * @param predicate the predicate to apply + * @return the {@code Entity} spec for further assertions */ T matches(Predicate predicate); /** - * Perform any assertions on the converted entity, e.g. via AssertJ. - * @param consumer the consumer to inspect the entity with - * @param the spec type - * @return the same spec for more assertions + * Verify the entity with the given {@link Consumer}. + * @param consumer the consumer to apply + * @return the {@code Entity} spec for further assertions */ T satisfies(Consumer consumer); /** - * Return the converted entity. - * @return the converter entity + * Return the decoded entity value(s). */ D get(); } /** - * Extension of {@link Entity} with options available to assert data converted to - * a List of entities. + * Contains a List of decoded entities and provides options to assert them. * * @param the type of elements in the list */ interface EntityList extends Entity, EntityList> { /** - * Assert the list contains the given elements. - * @param elements values that are expected - * @return the same spec for more assertions + * Verify the list contains the given values, in any order. + * @param values values that are expected + * @return the {@code EntityList} spec for further assertions */ @SuppressWarnings("unchecked") - EntityList contains(E... elements); + EntityList contains(E... values); /** - * Assert the list does not contain the given elements. - * @param elements values that are not expected - * @return the same spec for more assertions + * Verify the list does not contain the given values. + * @param values the values that are not expected + * @return the {@code EntityList} spec for further assertions */ @SuppressWarnings("unchecked") - EntityList doesNotContain(E... elements); + EntityList doesNotContain(E... values); /** - * Assert the list contains the given elements. - * @param elements values that are expected - * @return the same spec for more assertions + * Verify that the list contains exactly the given values and nothing + * else, in the same order. + * @param values the expected values + * @return the {@code EntityList} spec for further assertions */ @SuppressWarnings("unchecked") - EntityList containsExactly(E... elements); + EntityList containsExactly(E... values); /** - * Assert the list contains the specified number of elements. - * @param size the number of elements expected - * @return the same spec for more assertions + * Verify the number of values in the list. + * @param size the expected size + * @return the {@code EntityList} spec for further assertions */ EntityList hasSize(int size); /** - * Assert the list contains fewer elements than the specified number. - * @param boundary the number to compare the number of elements to - * @return the same spec for more assertions + * Verify the list has fewer than the number of values. + * @param size the number to compare the actual size to + * @return the {@code EntityList} spec for further assertions */ - EntityList hasSizeLessThan(int boundary); + EntityList hasSizeLessThan(int size); /** - * Assert the list contains more elements than the specified number. - * @param boundary the number to compare the number of elements to - * @return the same spec for more assertions + * Verify the list has more than the specified number of values. + * @param size the number to compare the actual size to + * @return the {@code EntityList} spec for further assertions */ - EntityList hasSizeGreaterThan(int boundary); + EntityList hasSizeGreaterThan(int size); } diff --git a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java index c9adb946..349feb9c 100644 --- a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java +++ b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java @@ -138,7 +138,10 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport { MovieCharacter leia = MovieCharacter.create("Leia Organa"); MovieCharacter jabba = MovieCharacter.create("Jabba the Hutt"); - List actual = response.path("me.friends").entityList(MovieCharacter.class) + GraphQlTester.EntityList entityList = + response.path("me.friends").entityList(MovieCharacter.class); + + List actual = entityList .contains(han) .containsExactly(han, leia) .doesNotContain(jabba) @@ -149,6 +152,10 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport { assertThat(actual).containsExactly(han, leia); + assertThatThrownBy(() -> entityList.containsExactly(leia, han)) + .as("Should be exactly the same order") + .hasMessageStartingWith("List at path 'me.friends' should have contained exactly"); + response.path("me.friends") .entityList(new ParameterizedTypeReference() {}) .containsExactly(han, leia); diff --git a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/MovieCharacter.java b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/MovieCharacter.java index ae044648..14c5edc4 100644 --- a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/MovieCharacter.java +++ b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/MovieCharacter.java @@ -59,4 +59,9 @@ public class MovieCharacter { return (this.name != null) ? this.name.hashCode() : super.hashCode(); } + @Override + public String toString() { + return "MovieCharacter[name='" + this.name + "']"; + } + } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/client/MovieCharacter.java b/spring-graphql/src/test/java/org/springframework/graphql/client/MovieCharacter.java index 28f55666..a72d567e 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/client/MovieCharacter.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/client/MovieCharacter.java @@ -63,4 +63,5 @@ public class MovieCharacter { public String toString() { return "MovieCharacter[name='" + this.name + "']"; } + }