Introduce empty & non-empty JsonPath matchers
This commit introduces new `isEmpty()` and `isNotEmpty()` methods in `JsonPathResultMatchers` and `JsonPathRequestMatchers` which delegate to the new `assertValueIsEmpty()` and `assertValueIsNotEmpty()` methods in `JsonPathExpectationsHelper`, respectively. Issue: SPR-13352
This commit is contained in:
@@ -32,13 +32,15 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
public class JsonPathExpectationsHelperTests {
|
||||
|
||||
private static final String CONTENT = "{" + //
|
||||
"'str': 'foo', " + //
|
||||
"'num': 5, " + //
|
||||
"'bool': true, " + //
|
||||
"'arr': [42], " + //
|
||||
"'emptyArray': [], " + //
|
||||
"'colorMap': {'red': 'rojo'}, " + //
|
||||
"'emptyMap': {} " + //
|
||||
"'str': 'foo', " + //
|
||||
"'num': 5, " + //
|
||||
"'bool': true, " + //
|
||||
"'arr': [42], " + //
|
||||
"'colorMap': {'red': 'rojo'}, " + //
|
||||
"'whitespace': ' ', " + //
|
||||
"'emptyString': '', " + //
|
||||
"'emptyArray': [], " + //
|
||||
"'emptyMap': {} " + //
|
||||
"}";
|
||||
|
||||
private static final String SIMPSONS = "{ 'familyMembers': [ " + //
|
||||
@@ -113,8 +115,106 @@ public class JsonPathExpectationsHelperTests {
|
||||
|
||||
@Test
|
||||
public void doesNotExistForIndefinatePathWithEmptyResults() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotExist(SIMPSONS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsEmptyForAnEmptyString() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.emptyString").assertValueIsEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsEmptyForAnEmptyArray() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.emptyArray").assertValueIsEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsEmptyForAnEmptyMap() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.emptyMap").assertValueIsEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsEmptyForIndefinatePathWithEmptyResults() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").assertValueIsEmpty(SIMPSONS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsEmptyForIndefinatePathWithResults() throws Exception {
|
||||
String expression = "$.familyMembers[?(@.name == 'Bart')]";
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage("Expected an empty value at JSON path \"" + expression
|
||||
+ "\" but found: [{\"name\":\"Bart\"}]");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsEmpty(SIMPSONS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsEmptyForWhitespace() throws Exception {
|
||||
String expression = "$.whitespace";
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage("Expected an empty value at JSON path \"" + expression + "\" but found: ' '");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForString() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.str").assertValueIsNotEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForNumber() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.num").assertValueIsNotEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForBoolean() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.bool").assertValueIsNotEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForArray() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.arr").assertValueIsNotEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForMap() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.colorMap").assertValueIsNotEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForIndefinatePathWithResults() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").assertValueIsNotEmpty(SIMPSONS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForIndefinatePathWithEmptyResults() throws Exception {
|
||||
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
|
||||
new JsonPathExpectationsHelper(expression).doesNotExist(SIMPSONS);
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage("Expected a non-empty value at JSON path \"" + expression + "\" but found: []");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(SIMPSONS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForAnEmptyString() throws Exception {
|
||||
String expression = "$.emptyString";
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage("Expected a non-empty value at JSON path \"" + expression + "\" but found: ''");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForAnEmptyArray() throws Exception {
|
||||
String expression = "$.emptyArray";
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage("Expected a non-empty value at JSON path \"" + expression + "\" but found: []");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsNotEmptyForAnEmptyMap() throws Exception {
|
||||
String expression = "$.emptyMap";
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage("Expected a non-empty value at JSON path \"" + expression + "\" but found: {}");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,10 +234,17 @@ public class JsonPathExpectationsHelperTests {
|
||||
new JsonPathExpectationsHelper("$.str").assertValueIsString(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsStringForAnEmptyString() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.emptyString").assertValueIsString(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueIsStringForNonString() throws Exception {
|
||||
String expression = "$.bool";
|
||||
exception.expect(AssertionError.class);
|
||||
new JsonPathExpectationsHelper("$.bool").assertValueIsString(CONTENT);
|
||||
exception.expectMessage("Expected a string at JSON path \"" + expression + "\" but found: true");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsString(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,8 +254,10 @@ public class JsonPathExpectationsHelperTests {
|
||||
|
||||
@Test
|
||||
public void assertValueIsNumberForNonNumber() throws Exception {
|
||||
String expression = "$.bool";
|
||||
exception.expect(AssertionError.class);
|
||||
new JsonPathExpectationsHelper("$.bool").assertValueIsNumber(CONTENT);
|
||||
exception.expectMessage("Expected a number at JSON path \"" + expression + "\" but found: true");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsNumber(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -158,8 +267,10 @@ public class JsonPathExpectationsHelperTests {
|
||||
|
||||
@Test
|
||||
public void assertValueIsBooleanForNonBoolean() throws Exception {
|
||||
String expression = "$.num";
|
||||
exception.expect(AssertionError.class);
|
||||
new JsonPathExpectationsHelper("$.num").assertValueIsBoolean(CONTENT);
|
||||
exception.expectMessage("Expected a boolean at JSON path \"" + expression + "\" but found: 5");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsBoolean(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -174,8 +285,10 @@ public class JsonPathExpectationsHelperTests {
|
||||
|
||||
@Test
|
||||
public void assertValueIsArrayForNonArray() throws Exception {
|
||||
String expression = "$.str";
|
||||
exception.expect(AssertionError.class);
|
||||
new JsonPathExpectationsHelper("$.str").assertValueIsArray(CONTENT);
|
||||
exception.expectMessage("Expected an array at JSON path \"" + expression + "\" but found: 'foo'");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsArray(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -190,8 +303,10 @@ public class JsonPathExpectationsHelperTests {
|
||||
|
||||
@Test
|
||||
public void assertValueIsMapForNonMap() throws Exception {
|
||||
String expression = "$.str";
|
||||
exception.expect(AssertionError.class);
|
||||
new JsonPathExpectationsHelper("$.str").assertValueIsMap(CONTENT);
|
||||
exception.expectMessage("Expected a map at JSON path \"" + expression + "\" but found: 'foo'");
|
||||
new JsonPathExpectationsHelper(expression).assertValueIsMap(CONTENT);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,13 +33,14 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
public class JsonPathRequestMatchersTests {
|
||||
|
||||
private static final String REQUEST_CONTENT = "{" + //
|
||||
"'str': 'foo', " + //
|
||||
"'num': 5, " + //
|
||||
"'bool': true, " + //
|
||||
"'arr': [42], " + //
|
||||
"'emptyArray': [], " + //
|
||||
"'colorMap': {'red': 'rojo'}, " + //
|
||||
"'emptyMap': {} " + //
|
||||
"'str': 'foo', " + //
|
||||
"'num': 5, " + //
|
||||
"'bool': true, " + //
|
||||
"'arr': [42], " + //
|
||||
"'colorMap': {'red': 'rojo'}, " + //
|
||||
"'emptyString': '', " + //
|
||||
"'emptyArray': [], " + //
|
||||
"'emptyMap': {} " + //
|
||||
"}";
|
||||
|
||||
private static final MockClientHttpRequest request = new MockClientHttpRequest();
|
||||
@@ -114,6 +115,61 @@ public class JsonPathRequestMatchersTests {
|
||||
new JsonPathRequestMatchers("$.emptyMap").doesNotExist().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmptyForAnEmptyString() throws Exception {
|
||||
new JsonPathRequestMatchers("$.emptyString").isEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmptyForAnEmptyArray() throws Exception {
|
||||
new JsonPathRequestMatchers("$.emptyArray").isEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmptyForAnEmptyMap() throws Exception {
|
||||
new JsonPathRequestMatchers("$.emptyMap").isEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForString() throws Exception {
|
||||
new JsonPathRequestMatchers("$.str").isNotEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForNumber() throws Exception {
|
||||
new JsonPathRequestMatchers("$.num").isNotEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForBoolean() throws Exception {
|
||||
new JsonPathRequestMatchers("$.bool").isNotEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForArray() throws Exception {
|
||||
new JsonPathRequestMatchers("$.arr").isNotEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForMap() throws Exception {
|
||||
new JsonPathRequestMatchers("$.colorMap").isNotEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void isNotEmptyForAnEmptyString() throws Exception {
|
||||
new JsonPathRequestMatchers("$.emptyString").isNotEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void isNotEmptyForAnEmptyArray() throws Exception {
|
||||
new JsonPathRequestMatchers("$.emptyArray").isNotEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void isNotEmptyForAnEmptyMap() throws Exception {
|
||||
new JsonPathRequestMatchers("$.emptyMap").isNotEmpty().match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isArray() throws Exception {
|
||||
new JsonPathRequestMatchers("$.arr").isArray().match(request);
|
||||
|
||||
@@ -33,13 +33,14 @@ import org.springframework.test.web.servlet.StubMvcResult;
|
||||
public class JsonPathResultMatchersTests {
|
||||
|
||||
private static final String RESPONSE_CONTENT = "{" + //
|
||||
"'str': 'foo', " + //
|
||||
"'num': 5, " + //
|
||||
"'bool': true, " + //
|
||||
"'arr': [42], " + //
|
||||
"'emptyArray': [], " + //
|
||||
"'colorMap': {'red': 'rojo'}, " + //
|
||||
"'emptyMap': {} " + //
|
||||
"'str': 'foo', " + //
|
||||
"'num': 5, " + //
|
||||
"'bool': true, " + //
|
||||
"'arr': [42], " + //
|
||||
"'colorMap': {'red': 'rojo'}, " + //
|
||||
"'emptyString': '', " + //
|
||||
"'emptyArray': [], " + //
|
||||
"'emptyMap': {} " + //
|
||||
"}";
|
||||
|
||||
private static final StubMvcResult stubMvcResult;
|
||||
@@ -117,6 +118,61 @@ public class JsonPathResultMatchersTests {
|
||||
new JsonPathResultMatchers("$.emptyMap").doesNotExist().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmptyForAnEmptyString() throws Exception {
|
||||
new JsonPathResultMatchers("$.emptyString").isEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmptyForAnEmptyArray() throws Exception {
|
||||
new JsonPathResultMatchers("$.emptyArray").isEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEmptyForAnEmptyMap() throws Exception {
|
||||
new JsonPathResultMatchers("$.emptyMap").isEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForString() throws Exception {
|
||||
new JsonPathResultMatchers("$.str").isNotEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForNumber() throws Exception {
|
||||
new JsonPathResultMatchers("$.num").isNotEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForBoolean() throws Exception {
|
||||
new JsonPathResultMatchers("$.bool").isNotEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForArray() throws Exception {
|
||||
new JsonPathResultMatchers("$.arr").isNotEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEmptyForMap() throws Exception {
|
||||
new JsonPathResultMatchers("$.colorMap").isNotEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void isNotEmptyForAnEmptyString() throws Exception {
|
||||
new JsonPathResultMatchers("$.emptyString").isNotEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void isNotEmptyForAnEmptyArray() throws Exception {
|
||||
new JsonPathResultMatchers("$.emptyArray").isNotEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void isNotEmptyForAnEmptyMap() throws Exception {
|
||||
new JsonPathResultMatchers("$.emptyMap").isNotEmpty().match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isArray() throws Exception {
|
||||
new JsonPathResultMatchers("$.arr").isArray().match(stubMvcResult);
|
||||
|
||||
Reference in New Issue
Block a user