Skip to content

Commit

Permalink
Merge pull request #2023
Browse files Browse the repository at this point in the history
Closes gh-2023
  • Loading branch information
rstoyanchev committed Nov 11, 2019
2 parents 5ee990e + 347f16c commit 23ec6d2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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 @@ -103,6 +103,19 @@ public void assertNode(byte[] content, @Nullable String encoding, final Matcher<
MatcherAssert.assertThat("XPath " + this.expression, node, matcher);
}

/**
* Parse the content, evaluate the XPath expression as a {@link NodeList},
* and assert it with the given {@code Matcher<NodeList>}.
* @since 5.2.2
*/
public void assertNodeList(byte[] content, @Nullable String encoding, final Matcher<? super NodeList> matcher)
throws Exception {

Document document = parseXmlByteArray(content, encoding);
NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class);
MatcherAssert.assertThat("XPath " + this.getXpathExpression(), nodeList, matcher);
}

/**
* Apply the XPath expression and assert the resulting content exists.
* @throws Exception if content parsing or expression evaluation fails
Expand Down
Expand Up @@ -22,6 +22,7 @@

import org.hamcrest.Matcher;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.springframework.lang.Nullable;
import org.springframework.mock.web.MockHttpServletResponse;
Expand Down Expand Up @@ -69,6 +70,18 @@ public ResultMatcher node(Matcher<? super Node> matcher) {
};
}

/**
* Evaluate the XPath and assert the {@link NodeList} content found with the
* given Hamcrest {@link Matcher}.
* @since 5.2.2
*/
public ResultMatcher nodeList(final Matcher<? super NodeList> matcher) {
return result -> {
MockHttpServletResponse response = result.getResponse();
this.xpathHelper.assertNodeList(response.getContentAsByteArray(), getDefinedEncoding(response), matcher);
};
}

/**
* Get the response encoding if explicitly defined in the response, {code null} otherwise.
*/
Expand Down
Expand Up @@ -43,18 +43,29 @@ public void node() throws Exception {
}

@Test
public void nodeNoMatch() throws Exception {
public void nodeNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar", null).node(Matchers.nullValue()).match(getStubMvcResult()));
}

@Test
public void nodeList() throws Exception {
new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.notNullValue()).match(getStubMvcResult());
}

@Test
public void nodeListNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.nullValue()).match(getStubMvcResult()));
}

@Test
public void exists() throws Exception {
new XpathResultMatchers("/foo/bar", null).exists().match(getStubMvcResult());
}

@Test
public void existsNoMatch() throws Exception {
public void existsNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/Bar", null).exists().match(getStubMvcResult()));
}
Expand All @@ -65,7 +76,7 @@ public void doesNotExist() throws Exception {
}

@Test
public void doesNotExistNoMatch() throws Exception {
public void doesNotExistNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar", null).doesNotExist().match(getStubMvcResult()));
}
Expand All @@ -76,7 +87,7 @@ public void nodeCount() throws Exception {
}

@Test
public void nodeCountNoMatch() throws Exception {
public void nodeCountNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar", null).nodeCount(1).match(getStubMvcResult()));
}
Expand All @@ -87,7 +98,7 @@ public void string() throws Exception {
}

@Test
public void stringNoMatch() throws Exception {
public void stringNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar[1]", null).string("112").match(getStubMvcResult()));
}
Expand All @@ -98,7 +109,7 @@ public void number() throws Exception {
}

@Test
public void numberNoMatch() throws Exception {
public void numberNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar[1]", null).number(111.1).match(getStubMvcResult()));
}
Expand All @@ -109,7 +120,7 @@ public void booleanValue() throws Exception {
}

@Test
public void booleanValueNoMatch() throws Exception {
public void booleanValueNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar[2]", null).booleanValue(false).match(getStubMvcResult()));
}
Expand Down

0 comments on commit 23ec6d2

Please sign in to comment.