Polishing

Closes gh-327
This commit is contained in:
rstoyanchev
2022-03-21 10:04:16 +00:00
parent 4e63970175
commit 3d638918f4
5 changed files with 86 additions and 80 deletions

View File

@@ -562,9 +562,9 @@ final class DefaultGraphQlTester implements GraphQlTester {
@Override
@SuppressWarnings("unchecked")
public EntityList<E> contains(E... elements) {
public EntityList<E> contains(E... values) {
doAssert(() -> {
List<E> expected = Arrays.asList(elements);
List<E> 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<E> doesNotContain(E... elements) {
public EntityList<E> doesNotContain(E... values) {
doAssert(() -> {
List<E> expected = Arrays.asList(elements);
List<E> 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<E> containsExactly(E... elements) {
public EntityList<E> containsExactly(E... values) {
doAssert(() -> {
List<E> expected = Arrays.asList(elements);
List<E> 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<E> hasSizeLessThan(int boundary) {
public EntityList<E> 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<E> hasSizeGreaterThan(int boundary) {
public EntityList<E> 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;
}
}

View File

@@ -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 <D> the target entity type
* @return spec to assert the converted entity with
* @return an {@code Entity} spec to verify the decoded value with
*/
<D> Entity<D, ?> entity(Class<D> 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 <D> the target entity type
* @return spec to assert the converted entity with
* @return an {@code Entity} spec to verify the decoded value with
*/
<D> Entity<D, ?> entity(ParameterizedTypeReference<D> 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 <D> 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
*/
<D> EntityList<D> entityList(Class<D> 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 <D> 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
*/
<D> EntityList<D> entityList(ParameterizedTypeReference<D> elementType);
@@ -291,7 +291,7 @@ public interface GraphQlTester {
* <a href="https://jsonassert.skyscreamer.org/">JSONassert</a> 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 <D> the entity type
* @param <S> the spec type, including subtypes
* @param <S> the {@code Entity} spec type
*/
interface Entity<D, S extends Entity<D, S>> extends Traversable {
/**
* Assert the converted entity equals the given Object.
* @param expected the expected Object
* @param <T> 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 extends S> T isEqualTo(Object expected);
/**
* Assert the converted entity does not equal the given Object.
* @param other the Object to check against
* @param <T> 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 extends S> T isNotEqualTo(Object other);
/**
* Assert the converted entity is the same instance as the given Object.
* @param expected the expected Object
* @param <T> 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 extends S> 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 <T> 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 extends S> T isNotSameAs(Object other);
/**
* Assert the converted entity matches the given predicate.
* @param predicate the expected Object
* @param <T> 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 extends S> T matches(Predicate<D> predicate);
/**
* Perform any assertions on the converted entity, e.g. via AssertJ.
* @param consumer the consumer to inspect the entity with
* @param <T> 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 extends S> T satisfies(Consumer<D> 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 <E> the type of elements in the list
*/
interface EntityList<E> extends Entity<List<E>, EntityList<E>> {
/**
* 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<E> contains(E... elements);
EntityList<E> 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<E> doesNotContain(E... elements);
EntityList<E> 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<E> containsExactly(E... elements);
EntityList<E> 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<E> 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<E> hasSizeLessThan(int boundary);
EntityList<E> 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<E> hasSizeGreaterThan(int boundary);
EntityList<E> hasSizeGreaterThan(int size);
}

View File

@@ -138,7 +138,10 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport {
MovieCharacter leia = MovieCharacter.create("Leia Organa");
MovieCharacter jabba = MovieCharacter.create("Jabba the Hutt");
List<MovieCharacter> actual = response.path("me.friends").entityList(MovieCharacter.class)
GraphQlTester.EntityList<MovieCharacter> entityList =
response.path("me.friends").entityList(MovieCharacter.class);
List<MovieCharacter> 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<MovieCharacter>() {})
.containsExactly(han, leia);

View File

@@ -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 + "']";
}
}

View File

@@ -63,4 +63,5 @@ public class MovieCharacter {
public String toString() {
return "MovieCharacter[name='" + this.name + "']";
}
}