forked from xmlunit/xmlunit
/
MultipleNodeAssert.java
279 lines (231 loc) · 9.44 KB
/
MultipleNodeAssert.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
This file is licensed to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.xmlunit.assertj;
import org.assertj.core.api.AbstractListAssert;
import org.assertj.core.api.FactoryBasedNavigableIterableAssert;
import org.assertj.core.api.ObjectAssert;
import org.assertj.core.description.Description;
import org.w3c.dom.Node;
import org.xmlunit.builder.Input;
import org.xmlunit.util.Convert;
import org.xmlunit.xpath.JAXPXPathEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.xpath.XPathFactory;
import static org.xmlunit.assertj.error.ShouldAnyNodeHaveXPath.shouldAnyNodeHaveXPath;
/**
* Assertion methods for {@link Iterable} of {@link Node}.
*
* <p><b>Simple Example</b></p>
*
* <pre>
* import static org.xmlunit.assertj.XmlAssert.assertThat;
*
* final String xml = "<a><b attr=\"abc\"></b></a>";
*
* assertThat(xml).nodesByXPath("//a/b").haveAttribute("attr").
* </pre>
*
* @since XMLUnit 2.6.1
*/
public class MultipleNodeAssert extends FactoryBasedNavigableIterableAssert<MultipleNodeAssert, Iterable<Node>, Node, SingleNodeAssert> {
interface SingleNodeAssertConsumer {
void accept(SingleNodeAssert t);
}
private static final AssertFactoryProvider ASSERT_FACTORY_PROVIDER = new AssertFactoryProvider();
private MultipleNodeAssert(Iterable<Node> nodes, JAXPXPathEngine engine) {
super(nodes, MultipleNodeAssert.class, ASSERT_FACTORY_PROVIDER.create(engine));
}
static MultipleNodeAssert create(Object xmlSource, Map<String, String> prefix2Uri, DocumentBuilderFactory dbf,
XPathFactory xpf, String xPath) {
AssertionsAdapter.assertThat(xPath).isNotBlank();
final JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
if (prefix2Uri != null) {
engine.setNamespaceContext(prefix2Uri);
}
Source s = Input.from(xmlSource).build();
Node root = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
Iterable<Node> nodes = engine.selectNodes(xPath, root);
return new MultipleNodeAssert(nodes, engine)
.describedAs("XPath \"%s\" evaluated to node set", xPath);
}
/**
* Equivalent for {@link #isNotEmpty()}.
*/
public MultipleNodeAssert exist() {
return isNotEmpty();
}
/**
* Equivalent for {@link #isEmpty()}.
*/
public void doNotExist() {
isEmpty();
}
/**
* Verifies that all the actual nodes have attribute with given name.
* <p>
* If the actual nodes iterable is empty, this assertion succeeds as there is no elements to check.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @throws AssertionError if one or more nodes don't have attribute with given name.
*/
public MultipleNodeAssert haveAttribute(final String attributeName) {
isNotNull();
allSatisfy(new SingleNodeAssertConsumer() {
@Override
public void accept(SingleNodeAssert singleNodeAssert) {
singleNodeAssert.hasAttribute(attributeName);
}
});
return this;
}
/**
* Verifies that all the actual nodes have attribute with given name and value.
* <p>
* If the actual nodes iterable is empty, this assertion succeeds as there is no elements to check.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @throws AssertionError if one or more nodes don't have attribute with given name and value.
*/
public MultipleNodeAssert haveAttribute(final String attributeName, final String attributeValue) {
isNotNull();
allSatisfy(new SingleNodeAssertConsumer() {
@Override
public void accept(SingleNodeAssert singleNodeAssert) {
singleNodeAssert.hasAttribute(attributeName, attributeValue);
}
});
return this;
}
/**
* Verifies that all the actual nodes don't have attribute with given name.
* <p>
* If the actual nodes iterable is empty, this assertion succeeds as there is no elements to check.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @throws AssertionError if any node has attribute with given name.
*/
public MultipleNodeAssert doNotHaveAttribute(final String attributeName) {
isNotNull();
allSatisfy(new SingleNodeAssertConsumer() {
@Override
public void accept(SingleNodeAssert singleNodeAssert) {
singleNodeAssert.doesNotHaveAttribute(attributeName);
}
});
return this;
}
/**
* Verifies that all the actual nodes don't have attribute with given name and value.
* <p>
* If the actual nodes iterable is empty, this assertion succeeds as there is no elements to check.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @throws AssertionError if any node has attribute with given name and value.
*/
public MultipleNodeAssert doNotHaveAttribute(final String attributeName, final String attributeValue) {
isNotNull();
allSatisfy(new SingleNodeAssertConsumer() {
@Override
public void accept(SingleNodeAssert singleNodeAssert) {
singleNodeAssert.doesNotHaveAttribute(attributeName, attributeValue);
}
});
return this;
}
/**
* Verifies that any of actual nodes has given {@code xPath}.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @throws AssertionError if all nodes don't have xpath.
* @since XMLUnit 2.6.4
*/
public MultipleNodeAssert containsAnyNodeHavingXPath(String xPath) {
isNotNull();
int index = 0;
for (Node node : actual) {
final SingleNodeAssert singleNodeAssert = toAssert(node, navigationDescription("check node at index " + index));
if (!singleNodeAssert.isNodeSetEmpty(xPath)) {
return this;
}
index++;
}
throwAssertionError(shouldAnyNodeHaveXPath(xPath));
return null; //fix compile issue
}
/**
* Verifies that all of actual nodes have given {@code xPath}.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @throws AssertionError if some node doesn't have xpath.
* @since XMLUnit 2.6.4
*/
public MultipleNodeAssert containsAllNodesHavingXPath(final String xPath) {
isNotNull();
allSatisfy(new SingleNodeAssertConsumer() {
@Override
public void accept(SingleNodeAssert singleNodeAssert) {
singleNodeAssert.hasXPath(xPath);
}
});
return this;
}
/**
* Extracting values of given node's attribute.
* If a node doesn't have the attribute then {@code null} value is return.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @since XMLUnit 2.6.4
*/
public AbstractListAssert<?, List<? extends String>, String, ObjectAssert<String>> extractingAttribute(String attribute) {
isNotNull();
List<String> values = new ArrayList<>();
for (Node node : actual) {
values.add(NodeUtils.attributeValue(node, attribute));
}
String extractedDescription = String.format("Extracted attribute: %s", attribute);
String description = Description.mostRelevantDescription(this.info.description(), extractedDescription);
return newListAssertInstance(values).as(description);
}
/**
* Extracting text content of given nodes.
* If a node doesn't have the text then an empty string is returned.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @since XMLUnit 2.8.0
*/
public AbstractListAssert<?, List<? extends String>, String, ObjectAssert<String>> extractingText() {
isNotNull();
List<String> values = new ArrayList<>();
for (Node node : actual) {
String textContent = node.getTextContent();
if (textContent != null) {
textContent = textContent.trim();
}
values.add(textContent);
}
String extractedDescription = "Extracted text content";
String description = Description.mostRelevantDescription(this.info.description(), extractedDescription);
return newListAssertInstance(values).as(description);
}
private void allSatisfy(SingleNodeAssertConsumer consumer) {
int index = 0;
for (Node node : actual) {
final SingleNodeAssert singleNodeAssert = toAssert(node, navigationDescription("check node at index " + index));
consumer.accept(singleNodeAssert);
index++;
}
}
}