Refine valueIsEmpty check

This commit is contained in:
rstoyanchev
2022-02-07 17:01:47 +00:00
parent 0b449d89e1
commit 2095e1795f
3 changed files with 29 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -336,8 +336,7 @@ class DefaultGraphQlTester implements GraphQlTester {
@Override
public PathSpec pathDoesNotExist() {
this.responseContainer
.doAssert(() -> this.pathHelper.doesNotHaveJsonPath(this.responseContainer.jsonContent()));
this.responseContainer.doAssert(() -> this.pathHelper.doesNotHaveJsonPath(this.responseContainer.jsonContent()));
return this;
}
@@ -355,21 +354,13 @@ class DefaultGraphQlTester implements GraphQlTester {
@Override
public PathSpec valueIsEmpty() {
this.responseContainer.doAssert(() -> {
try {
this.pathHelper.assertValueIsEmpty(this.responseContainer.jsonContent());
}
catch (AssertionError ex) {
// ignore
}
});
this.responseContainer.doAssert(() -> this.pathHelper.assertValueIsEmpty(this.responseContainer.jsonContent()));
return this;
}
@Override
public PathSpec valueIsNotEmpty() {
this.responseContainer
.doAssert(() -> this.pathHelper.assertValueIsNotEmpty(this.responseContainer.jsonContent()));
this.responseContainer.doAssert(() -> this.pathHelper.assertValueIsNotEmpty(this.responseContainer.jsonContent()));
return this;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -249,15 +249,15 @@ public interface GraphQlTester {
PathSpec valueDoesNotExist();
/**
* Assert the value at the given path does not exist or is empty as defined in
* {@link org.springframework.util.ObjectUtils#isEmpty(Object)}.
* 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
* @see org.springframework.util.ObjectUtils#isEmpty(Object)
*/
PathSpec valueIsEmpty();
/**
* Assert the value at the given path is not {@link #valueIsEmpty()}.
* Assert the value at the given path is not {@link #valueIsEmpty() empty}.
* @return spec to assert the converted entity with
*/
PathSpec valueIsNotEmpty();

View File

@@ -66,16 +66,34 @@ public class GraphQlTesterTests {
@Test
void pathAndValueExistsAndEmptyChecks() throws Exception {
void pathAndValueExist() throws Exception {
String query = "{me {name, friends}}";
setResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}");
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute();
spec.path("me.name").pathExists().valueExists().valueIsNotEmpty();
spec.path("me.name").pathExists().valueExists();
spec.path("me.friends").pathExists().valueExists();
spec.path("hero").pathDoesNotExist().valueDoesNotExist();
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
}
@Test
void valueIsEmpty() throws Exception {
String query = "{me {name, friends}}";
setResponse("{\"me\": {\"name\":null, \"friends\":[]}}");
GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute();
spec.path("me.name").valueIsEmpty();
spec.path("me.friends").valueIsEmpty();
spec.path("hero").pathDoesNotExist().valueDoesNotExist().valueIsEmpty();
assertThatThrownBy(() -> spec.path("hero").valueIsEmpty())
.as("Path does not even exist")
.hasMessageContaining("No value at JSON path \"$['data']['hero']");
assertThat(this.inputCaptor.getValue().getQuery()).contains(query);
}