Skip to content

Commit

Permalink
Refine valueIsEmpty check
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Feb 8, 2022
1 parent 0b449d8 commit 2095e17
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -336,8 +336,7 @@ public PathSpec pathExists() {

@Override
public PathSpec pathDoesNotExist() {
this.responseContainer
.doAssert(() -> this.pathHelper.doesNotHaveJsonPath(this.responseContainer.jsonContent()));
this.responseContainer.doAssert(() -> this.pathHelper.doesNotHaveJsonPath(this.responseContainer.jsonContent()));
return this;
}

Expand All @@ -355,21 +354,13 @@ public PathSpec valueDoesNotExist() {

@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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -249,15 +249,15 @@ interface PathSpec extends TraverseSpec {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2095e17

Please sign in to comment.