/** * Access to request body matchers using a * <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression to * inspect a specific subset of the body and a Hamcrest match for asserting * the value found at the JSON path. * @param expression the JSON path expression * @param matcher a matcher for the value expected at the JSON path */ public static <T> RequestMatcher jsonPath(String expression, Matcher<T> matcher) { return new JsonPathRequestMatchers(expression).value(matcher); }
/** * Access to request body matchers using an XPath to inspect a specific * subset of the body. The XPath expression can be a parameterized string * using formatting specifiers as defined in * {@link String#format(String, Object...)}. * @param expression the XPath optionally parameterized with arguments * @param args arguments to parameterize the XPath expression with */ public static XpathRequestMatchers xpath(String expression, Object... args) throws XPathExpressionException { return new XpathRequestMatchers(expression, null, args); }
/** * Access to request body matchers using a * <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression to * inspect a specific subset of the body. The JSON path expression can be a * parameterized string using formatting specifiers as defined in * {@link String#format(String, Object...)}. * @param expression the JSON path optionally parameterized with arguments * @param args arguments to parameterize the JSON path expression with */ public static JsonPathRequestMatchers jsonPath(String expression, Object... args) { return new JsonPathRequestMatchers(expression, args); }
@Test public void value() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach")) .andExpect(jsonPath("$.performers[1].name").value("Yehudi Menuhin")) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void exists() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0]").exists()) .andExpect(jsonPath("$.composers[1]").exists()) .andExpect(jsonPath("$.composers[2]").exists()) .andExpect(jsonPath("$.composers[3]").exists()) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void doesNotExist() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist()) .andExpect(jsonPath("$.composers[?(@.name == 'Robert Schuuuuuuman')]").doesNotExist()) .andExpect(jsonPath("$.composers[4]").doesNotExist()) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void isBoolean() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0].someBoolean").isBoolean()) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void isNumber() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0].someDouble").isNumber()) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void isString() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0].name").isString()) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void isArray() throws Exception { this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers").isArray()) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void testDoesNotExist() throws Exception { String composer = "/ns:people/composers/composer[%s]"; String performer = "/ns:people/performers/performer[%s]"; this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/xml")) .andExpect(xpath(composer, NS, 0).doesNotExist()) .andExpect(xpath(composer, NS, 5).doesNotExist()) .andExpect(xpath(performer, NS, 0).doesNotExist()) .andExpect(xpath(performer, NS, 3).doesNotExist()) .andRespond(withSuccess()); executeAndVerify(); }
@Test public void testBoolean() throws Exception { String performerBooleanValue = "/ns:people/performers/performer[%s]/someBoolean"; this.mockServer.expect(requestTo("/composers")) .andExpect(content().contentType("application/xml")) .andExpect(xpath(performerBooleanValue, NS, 1).booleanValue(false)) .andExpect(xpath(performerBooleanValue, NS, 2).booleanValue(true)) .andRespond(withSuccess()); executeAndVerify(); }
/** * Assert the request content type as a String. */ public RequestMatcher contentType(String expectedContentType) { return contentType(MediaType.parseMediaType(expectedContentType)); }
/** * Access to request body matchers. */ public static ContentRequestMatchers content() { return new ContentRequestMatchers(); }
/** * Parse the expected and actual strings as JSON and assert the two * are "similar" - i.e. they contain the same attribute-value pairs * regardless of formatting with a lenient checking (extensible, and non-strict array * ordering). * <p>Use of this matcher requires the <a * href="http://jsonassert.skyscreamer.org/">JSONassert</a> library. * @param expectedJsonContent the expected JSON content * @since 5.0.5 */ public RequestMatcher json(final String expectedJsonContent) { return json(expectedJsonContent, false); }
@Override public final void match(ClientHttpRequest request) throws IOException, AssertionError { try { MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; matchInternal(mockRequest); } catch (Exception ex) { throw new AssertionError("Failed to parse expected or actual XML request content", ex); } }
@Override public final void match(ClientHttpRequest request) throws IOException, AssertionError { try { MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; matchInternal(mockRequest); } catch (ParseException ex) { throw new AssertionError("Failed to parse JSON request content", ex); } }
@Override public final void match(ClientHttpRequest request) throws IOException, AssertionError { try { MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; matchInternal(mockRequest); } catch (Exception ex) { throw new AssertionError("Failed to parse XML request content", ex); } }
/** * Assert the request content type is compatible with the given * content type as defined by {@link MediaType#isCompatibleWith(MediaType)}. */ public RequestMatcher contentTypeCompatibleWith(String contentType) { return contentTypeCompatibleWith(MediaType.parseMediaType(contentType)); }
/** * Access to response body matchers using an XPath to inspect a specific * subset of the body. The XPath expression can be a parameterized string * using formatting specifiers as defined in * {@link String#format(String, Object...)}. * @param expression the XPath optionally parameterized with arguments * @param namespaces namespaces referenced in the XPath expression * @param args arguments to parameterize the XPath expression with */ public static XpathRequestMatchers xpath(String expression, Map<String, String> namespaces, Object... args) throws XPathExpressionException { return new XpathRequestMatchers(expression, namespaces, args); }