Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

package org.springframework.test.util;

import java.lang.Boolean;
import java.lang.Number;
import java.lang.String;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
Expand All @@ -28,6 +32,7 @@
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.MatcherAssert.*;
import static org.springframework.test.util.AssertionErrors.*;

Expand Down Expand Up @@ -138,7 +143,47 @@ public void assertValueIsArray(String responseContent) throws ParseException {
Object actualValue = evaluateJsonPath(responseContent);
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
assertTrue(reason, actualValue instanceof List);
assertThat(reason, actualValue, instanceOf(List.class));
}

/**
* Apply the JSON path and assert the resulting value is a boolean.
*/
public void assertValueIsBoolean(String responseContent) throws ParseException {
Object actualValue = evaluateJsonPath(responseContent);
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
assertThat(reason, actualValue, instanceOf(Boolean.class));
}

/**
* Apply the JSON path and assert the resulting value is a number.
*/
public void assertValueIsNumber(String responseContent) throws ParseException {
Object actualValue = evaluateJsonPath(responseContent);
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
assertThat(reason, actualValue, instanceOf(Number.class));
}

/**
* Apply the JSON path and assert the resulting value is a map.
*/
public void assertValueIsMap(String responseContent) throws ParseException {
Object actualValue = evaluateJsonPath(responseContent);
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
assertThat(reason, actualValue, instanceOf(Map.class));
}

/**
* Apply the JSON path and assert the resulting value is a string.
*/
public void assertValueIsString(String responseContent) throws ParseException {
Object actualValue = evaluateJsonPath(responseContent);
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
assertThat(reason, actualValue, instanceOf(String.class));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void match(MvcResult result) throws Exception {
}

/**
* Evluate the JSON path and assert the content found is an array.
* Evaluate the JSON path and assert the content found is an array.
*/
public ResultMatcher isArray() {
return new ResultMatcher() {
Expand All @@ -111,4 +111,56 @@ public void match(MvcResult result) throws Exception {
};
}

/**
* Evaluate the JSON path and assert the content found is a boolean.
*/
public ResultMatcher isBoolean() {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
String content = result.getResponse().getContentAsString();
jsonPathHelper.assertValueIsBoolean(content);
}
};
}

/**
* Evaluate the JSON path and assert the content found is a number.
*/
public ResultMatcher isNumber() {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
String content = result.getResponse().getContentAsString();
jsonPathHelper.assertValueIsNumber(content);
}
};
}

/**
* Evaluate the JSON path and assert the content found is a map.
*/
public ResultMatcher isMap() {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
String content = result.getResponse().getContentAsString();
jsonPathHelper.assertValueIsMap(content);
}
};
}

/**
* Evaluate the JSON path and assert the content found is a string.
*/
public ResultMatcher isString() {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
String content = result.getResponse().getContentAsString();
jsonPathHelper.assertValueIsString(content);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,47 @@ public void isArrayNoMatch() throws Exception {
new JsonPathResultMatchers("$.bar").isArray().match(getStubMvcResult());
}

@Test
public void isBooleanMatch() throws Exception {
new JsonPathResultMatchers("$.icanhaz").isBoolean().match(getStubMvcResult());
}

@Test(expected=AssertionError.class)
public void isBooleanNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").isBoolean().match(getStubMvcResult());
}

@Test
public void isNumberMatch() throws Exception {
new JsonPathResultMatchers("$.howmanies").isNumber().match(getStubMvcResult());
}

@Test(expected=AssertionError.class)
public void isNumberNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").isNumber().match(getStubMvcResult());
}

@Test
public void isMapMatch() throws Exception {
new JsonPathResultMatchers("$.cheeseburger").isMap().match(getStubMvcResult());
}

@Test(expected=AssertionError.class)
public void isMapNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").isMap().match(getStubMvcResult());
}

@Test
public void isStringMatch() throws Exception {
new JsonPathResultMatchers("$.foo").isString().match(getStubMvcResult());
}

@Test(expected=AssertionError.class)
public void isStringNoMatch() throws Exception {
new JsonPathResultMatchers("$.qux").isString().match(getStubMvcResult());
}

private static final String RESPONSE_CONTENT = "{\"foo\":\"bar\", \"qux\":[\"baz1\",\"baz2\"]}";
private static final String RESPONSE_CONTENT = "{\"foo\":\"bar\", \"qux\":[\"baz1\",\"baz2\"], \"icanhaz\":true, \"howmanies\": 5, \"cheeseburger\": {\"pickles\": true} }";

private StubMvcResult getStubMvcResult() throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
Expand Down