Skip to content

Commit

Permalink
Saving progress on null result checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
byarger-ebay committed Aug 16, 2023
1 parent 96c2b8f commit b84e58e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.PathNotFoundException;

public class JPListOfIntegerCheck implements JsonPathExecutor, Serializable {
public class JPListOfIntegerCheck implements JsonPathExecutor, NullCheck, Serializable {

/**
*
Expand Down
37 changes: 25 additions & 12 deletions NST/src/main/java/com/ebay/jsonpath/JPListOfStringCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.PathNotFoundException;

public class JPListOfStringCheck implements JsonPathExecutor, Serializable {
public class JPListOfStringCheck implements JsonPathExecutor, NullCheck, Serializable {

/**
*
Expand All @@ -30,6 +30,8 @@ public class JPListOfStringCheck implements JsonPathExecutor, Serializable {

private List<String> limitedToValues = null;

private boolean isNull = false;

/**
* Run baseline checks for a list of strings - list is not null and indexes
* are not null or empty strings.
Expand Down Expand Up @@ -203,6 +205,11 @@ public String getAllExpectedValue() {
return allExpectedValue;
}

@Override
public void checkIsNull(boolean mustBeNull) {
isNull = mustBeNull;
}

@Override
public void processJsonPath(String jsonPath, SoftAssert softAssert, DocumentContext documentContext) {

Expand All @@ -218,7 +225,9 @@ public void processJsonPath(String jsonPath, SoftAssert softAssert, DocumentCont
return;
}

softAssert.assertNotNull(values, AssertMessageBuilder.build(jsonPath, "because the path does not exist."));
if (!isNull) {
softAssert.assertNotNull(values, AssertMessageBuilder.build(jsonPath, "because the path does not exist"));
}

if (values == null) {
return;
Expand All @@ -234,47 +243,51 @@ public void processJsonPath(String jsonPath, SoftAssert softAssert, DocumentCont
continue;
}

softAssert.assertNotNull(value, AssertMessageBuilder.build(jsonPath, String.format("with null value on index %d of the list of strings.", i)));
if (value != null) {
softAssert.assertTrue(!value.isEmpty(), AssertMessageBuilder.build(jsonPath, String.format("with empty value on index %d of the list of strings.", i)));
if (isNull) {
softAssert.assertNull(value, AssertMessageBuilder.build(jsonPath, "because the path does exist on index %d of the list of strings"));
} else {
softAssert.assertNotNull(value, AssertMessageBuilder.build(jsonPath, String.format("with null value on index %d of the list of strings", i)));
if (value != null) {
softAssert.assertTrue(!value.isEmpty(), AssertMessageBuilder.build(jsonPath, String.format("with empty value on index %d of the list of strings", i)));
}
}
}

if (allExpectedValue != null) {
for (String value : values) {
softAssert.assertEquals(value, allExpectedValue, AssertMessageBuilder.build(jsonPath, String.format("because path value %s did not equal expected value %s.", value, allExpectedValue)));
softAssert.assertEquals(value, allExpectedValue, AssertMessageBuilder.build(jsonPath, String.format("because path value %s did not equal expected value %s", value, allExpectedValue)));
}
}

if (exactLength != null) {
softAssert.assertEquals(values.size(), exactLength.intValue(), AssertMessageBuilder.build(jsonPath, "because path did not return expected number of results."));
softAssert.assertEquals(values.size(), exactLength.intValue(), AssertMessageBuilder.build(jsonPath, "because path did not return expected number of results"));
}

if (minLength != null) {
softAssert
.assertTrue(
values.size() >= minLength.intValue(),
AssertMessageBuilder.build(jsonPath, String.format("because path did not contain the minimum number of expected results %d. Found %d.", minLength, values.size())));
AssertMessageBuilder.build(jsonPath, String.format("because path did not contain the minimum number of expected results %d. Found %d", minLength, values.size())));
}

if (maxLength != null) {
softAssert
.assertTrue(
values.size() <= maxLength.intValue(),
AssertMessageBuilder.build(jsonPath, String.format("because path exceeded the maximum number of expected results %d. Found %d.", maxLength, values.size())));
AssertMessageBuilder.build(jsonPath, String.format("because path exceeded the maximum number of expected results %d. Found %d", maxLength, values.size())));
}

if (expectedValues != null) {
softAssert.assertTrue(values.equals(expectedValues), AssertMessageBuilder.build(jsonPath, String.format("because path values %s does not equal expected values %s.", values, expectedValues)));
softAssert.assertTrue(values.equals(expectedValues), AssertMessageBuilder.build(jsonPath, String.format("because path values %s does not equal expected values %s", values, expectedValues)));
}

if (containsValues != null) {
softAssert
.assertTrue(values.containsAll(containsValues), AssertMessageBuilder.build(jsonPath, String.format("because path values %s does not contain all of the values %s.", values, containsValues)));
.assertTrue(values.containsAll(containsValues), AssertMessageBuilder.build(jsonPath, String.format("because path values %s does not contain all of the values %s", values, containsValues)));
}

if (limitedToValues != null) {
softAssert.assertTrue(limitedToValues.containsAll(values), AssertMessageBuilder.build(jsonPath, String.format("because path values %s is not limited to the values %s.", values, limitedToValues)));
softAssert.assertTrue(limitedToValues.containsAll(values), AssertMessageBuilder.build(jsonPath, String.format("because path values %s is not limited to the values %s", values, limitedToValues)));
}
}
}
15 changes: 13 additions & 2 deletions NST/src/main/java/com/ebay/jsonpath/JPStringCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.PathNotFoundException;

public class JPStringCheck implements JsonPathExecutor, Serializable {
public class JPStringCheck implements JsonPathExecutor, NullCheck, Serializable {

/**
*
Expand All @@ -29,6 +29,8 @@ public class JPStringCheck implements JsonPathExecutor, Serializable {

private Integer maximumNumberOfCharacters = null;

private boolean isNull = false;

/**
* Run the baseline checks for a String - not null and not empty.
*/
Expand Down Expand Up @@ -178,6 +180,11 @@ public String getMatchesPattern() {
return matchesPattern;
}

@Override
public void checkIsNull(boolean mustBeNull) {
isNull = mustBeNull;
}

@Override
public void processJsonPath(String jsonPath, SoftAssert softAssert, DocumentContext documentContext) {

Expand All @@ -193,7 +200,11 @@ public void processJsonPath(String jsonPath, SoftAssert softAssert, DocumentCont
return;
}

softAssert.assertNotNull(value, AssertMessageBuilder.build(jsonPath, "with null string."));
if (isNull) {
softAssert.assertNull(value, AssertMessageBuilder.build(jsonPath, "with non-null string"));
} else {
softAssert.assertNotNull(value, AssertMessageBuilder.build(jsonPath, "with null string."));
}

if (value == null) {
return;
Expand Down
10 changes: 10 additions & 0 deletions NST/src/main/java/com/ebay/jsonpath/NullCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ebay.jsonpath;

public interface NullCheck {

/**
* Check if the node selected is returned as null.
* @param mustBeNull True to check that the value is null, false to make sure it isn't null.
*/
void checkIsNull(boolean mustBeNull);
}
25 changes: 25 additions & 0 deletions NST/src/test/java/com/ebay/jsonpath/JPListOfStringCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ public void failNullValue() {
softAssert.assertAll();
}

@Test(groups = "unitTest")
public void passNull() {

SoftAssert softAssert = new SoftAssert();

DocumentContext jsonPathDocument = JsonPath.using(config).parse("{\"foo\":[{\"text\":null}]}");
JPListOfStringCheck check = new JPListOfStringCheck();
check.checkIsNull(true);
check.processJsonPath("$.foo[*].text", softAssert, jsonPathDocument);

softAssert.assertAll();
}

@Test(expectedExceptions = AssertionError.class, groups = "unitTest")
public void failNotNull() {

SoftAssert softAssert = new SoftAssert();

DocumentContext jsonPathDocument = JsonPath.using(config).parse("{\"foo\":[{\"text\":null}]}");
JPListOfStringCheck check = new JPListOfStringCheck();
check.processJsonPath("$.foo[*].text", softAssert, jsonPathDocument);

softAssert.assertAll();
}

@Test(expectedExceptions = AssertionError.class, groups = "unitTest")
public void failNullMissingNode() {

Expand Down
25 changes: 25 additions & 0 deletions NST/src/test/java/com/ebay/jsonpath/JPStringCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ public void failNotNull() {
softAssert.assertAll();
}

@Test(groups = "unitTest")
public void passNull() {

SoftAssert softAssert = new SoftAssert();

DocumentContext jsonPathDocument = JsonPath.using(config).parse("{\"foo\":null}");
JPStringCheck check = new JPStringCheck();
check.checkIsNull(true);
check.processJsonPath("$.foo", softAssert, jsonPathDocument);

softAssert.assertAll();
}

@Test(expectedExceptions = AssertionError.class, groups = "unitTest")
public void failNullExpected() {

SoftAssert softAssert = new SoftAssert();

DocumentContext jsonPathDocument = JsonPath.using(config).parse("{\"foo\":null}");
JPStringCheck check = new JPStringCheck();
check.processJsonPath("$.foo", softAssert, jsonPathDocument);

softAssert.assertAll();
}

@Test(expectedExceptions = AssertionError.class, groups = "unitTest")
public void failClassCastException() {

Expand Down

0 comments on commit b84e58e

Please sign in to comment.