1
1
package apoc .load ;
2
2
3
3
import apoc .util .TestUtil ;
4
- import org .apache .commons .lang .exception .ExceptionUtils ;
5
4
import org .junit .After ;
6
5
import org .junit .Before ;
7
6
import org .junit .Test ;
8
7
import org .neo4j .graphdb .GraphDatabaseService ;
9
- import org .neo4j .graphdb .QueryExecutionException ;
10
8
import org .neo4j .helpers .collection .Iterators ;
11
9
import org .neo4j .test .TestGraphDatabaseFactory ;
12
- import org .xml .sax .SAXParseException ;
13
10
14
11
import java .util .Collections ;
15
12
import java .util .List ;
22
19
23
20
public class XmlTest {
24
21
25
- public static final String XML_AS_NESTED_MAP =
22
+ private static final String XML_AS_NESTED_MAP =
26
23
"{_type=parent, name=databases, " +
27
24
"_children=[" +
28
25
"{_type=child, name=Neo4j, _text=Neo4j is a graph database}, " +
29
26
"{_type=child, name=relational, _children=[" +
30
27
"{_type=grandchild, name=MySQL, _text=MySQL is a database & relational}, " +
31
28
"{_type=grandchild, name=Postgres, _text=Postgres is a relational database}]}]}" ;
32
- public static final String XML_AS_NESTED_SIMPLE_MAP =
29
+ private static final String XML_AS_NESTED_SIMPLE_MAP =
33
30
"{_type=parent, name=databases, " +
34
31
"_child=[" +
35
32
"{_type=child, name=Neo4j, _text=Neo4j is a graph database}, " +
36
33
"{_type=child, name=relational, _grandchild=[" +
37
34
"{_type=grandchild, name=MySQL, _text=MySQL is a database & relational}, " +
38
35
"{_type=grandchild, name=Postgres, _text=Postgres is a relational database}]}]}" ;
39
- public static final String XML_XPATH_AS_NESTED_MAP =
36
+ static final String XML_XPATH_AS_NESTED_MAP =
40
37
"[{_type=book, id=bk103, _children=[{_type=author, _text=Corets, Eva}, {_type=title, _text=Maeve Ascendant}, {_type=genre, _text=Fantasy}, {_type=price, _text=5.95}, {_type=publish_date, _text=2000-11-17}, {_type=description, _text=After the collapse of a nanotechnology " +
41
38
"society in England, the young survivors lay the " +
42
39
"foundation for a new society.}]}]" ;
43
40
44
- public static final String XML_AS_SINGLE_LINE =
41
+ private static final String XML_AS_SINGLE_LINE =
45
42
"{_type=table, _children=[{_type=tr, _children=[{_type=td, _children=[{_type=img, src=pix/logo-tl.gif}]}]}]}" ;
46
43
47
- public static final String XML_AS_SINGLE_LINE_SIMPLE =
44
+ private static final String XML_AS_SINGLE_LINE_SIMPLE =
48
45
"{_type=table, _table=[{_type=tr, _tr=[{_type=td, _td=[{_type=img, src=pix/logo-tl.gif}]}]}]}" ;
49
46
50
47
private GraphDatabaseService db ;
@@ -64,7 +61,7 @@ public void tearDown() {
64
61
}
65
62
66
63
@ Test
67
- public void testLoadXml () throws Exception {
64
+ public void testLoadXml () {
68
65
testCall (db , "CALL apoc.load.xml('file:databases.xml')" , // YIELD value RETURN value
69
66
(row ) -> {
70
67
Object value = row .get ("value" );
@@ -73,7 +70,7 @@ public void testLoadXml() throws Exception {
73
70
}
74
71
75
72
@ Test
76
- public void testLoadXmlSimple () throws Exception {
73
+ public void testLoadXmlSimple () {
77
74
testCall (db , "CALL apoc.load.xmlSimple('file:databases.xml')" , // YIELD value RETURN value
78
75
(row ) -> {
79
76
Object value = row .get ("value" );
@@ -247,10 +244,10 @@ public void testLoadXmlWithNextWordRels() {
247
244
row -> assertNotNull (row .get ("node" )));
248
245
testResult (db , "match (n) return labels(n)[0] as label, count(*) as count" , result -> {
249
246
final Map <String , Long > resultMap = result .stream ().collect (Collectors .toMap (o -> (String )o .get ("label" ), o -> (Long )o .get ("count" )));
250
- assertEquals (2l , (long )resultMap .get ("XmlProcessingInstruction" ));
251
- assertEquals (1l , (long )resultMap .get ("XmlDocument" ));
252
- assertEquals (3263l , (long )resultMap .get ("XmlWord" ));
253
- assertEquals (454l , (long )resultMap .get ("XmlTag" ));
247
+ assertEquals (2L , (long )resultMap .get ("XmlProcessingInstruction" ));
248
+ assertEquals (1L , (long )resultMap .get ("XmlDocument" ));
249
+ assertEquals (3263L , (long )resultMap .get ("XmlWord" ));
250
+ assertEquals (454L , (long )resultMap .get ("XmlTag" ));
254
251
});
255
252
256
253
// no node more than one NEXT/NEXT_SIBLING
@@ -265,7 +262,7 @@ public void testLoadXmlWithNextWordRels() {
265
262
testResult (db , "match p=(:XmlDocument)-[:NEXT_WORD*]->(e:XmlWord) where not (e)-[:NEXT_WORD]->() return length(p) as len" ,
266
263
result -> {
267
264
Map <String , Object > r = Iterators .single (result );
268
- assertEquals (3263l , r .get ("len" ));
265
+ assertEquals (3263L , r .get ("len" ));
269
266
});
270
267
}
271
268
@@ -276,10 +273,10 @@ public void testLoadXmlWithNextEntityRels() {
276
273
row -> assertNotNull (row .get ("node" )));
277
274
testResult (db , "match (n) return labels(n)[0] as label, count(*) as count" , result -> {
278
275
final Map <String , Long > resultMap = result .stream ().collect (Collectors .toMap (o -> (String )o .get ("label" ), o -> (Long )o .get ("count" )));
279
- assertEquals (2l , (long )resultMap .get ("XmlProcessingInstruction" ));
280
- assertEquals (1l , (long )resultMap .get ("XmlDocument" ));
281
- assertEquals (3263l , (long )resultMap .get ("XmlCharacters" ));
282
- assertEquals (454l , (long )resultMap .get ("XmlTag" ));
276
+ assertEquals (2L , (long )resultMap .get ("XmlProcessingInstruction" ));
277
+ assertEquals (1L , (long )resultMap .get ("XmlDocument" ));
278
+ assertEquals (3263L , (long )resultMap .get ("XmlCharacters" ));
279
+ assertEquals (454L , (long )resultMap .get ("XmlTag" ));
283
280
});
284
281
285
282
// no node more than one NEXT/NEXT_SIBLING
@@ -294,7 +291,7 @@ public void testLoadXmlWithNextEntityRels() {
294
291
testResult (db , "match p=(:XmlDocument)-[:NE*]->(e:XmlCharacters) where not (e)-[:NE]->() return length(p) as len" ,
295
292
result -> {
296
293
Map <String , Object > r = Iterators .single (result );
297
- assertEquals (3263l , r .get ("len" ));
294
+ assertEquals (3263L , r .get ("len" ));
298
295
});
299
296
}
300
297
@@ -378,21 +375,17 @@ public void testLoadXmlFromTgzByUrl() {
378
375
});
379
376
}
380
377
381
- @ Test (expected = QueryExecutionException .class )
382
- public void testLoadXmlPreventXXEVulnerabilityThrowsQueryExecutionException () {
383
- try {
384
- testResult (db , "CALL apoc.load.xml('file:src/test/resources/xml/xxe.xml', '/catalog/book[genre=\" Computer\" ]') yield value as result" , (r ) -> {});
385
- } catch (QueryExecutionException e ) {
386
- // We want test that the cause of the exception is SAXParseException with the correct cause message
387
- Throwable except = ExceptionUtils .getRootCause (e );
388
- assertTrue (except instanceof SAXParseException );
389
- assertEquals ("DOCTYPE is disallowed when the feature \" http://apache.org/xml/features/disallow-doctype-decl\" set to true." , except .getMessage ());
390
- throw e ;
391
- }
378
+ @ Test
379
+ public void testExternalDTDschouldNotBeLoaded () {
380
+ testCall (db , "CALL apoc.load.xml('file:src/test/resources/xml/missingExternalDTD.xml', '/', null, true)" ,
381
+ (row ) -> {
382
+ Object value = row .get ("value" );
383
+ assertEquals ("{_type=document, _document=[null, {_type=title, _text=dtd 404}]}" , value .toString ());
384
+ });
392
385
}
393
386
394
387
@ Test
395
- public void testLoadXmlSingleLineSimple () throws Exception {
388
+ public void testLoadXmlSingleLineSimple () {
396
389
testCall (db , "CALL apoc.load.xml('file:src/test/resources/xml/singleLine.xml', '/', null, true)" , // YIELD value RETURN value
397
390
(row ) -> {
398
391
Object value = row .get ("value" );
@@ -401,7 +394,7 @@ public void testLoadXmlSingleLineSimple() throws Exception {
401
394
}
402
395
403
396
@ Test
404
- public void testLoadXmlSingleLine () throws Exception {
397
+ public void testLoadXmlSingleLine () {
405
398
testCall (db , "CALL apoc.load.xml('file:src/test/resources/xml/singleLine.xml')" , // YIELD value RETURN value
406
399
(row ) -> {
407
400
Object value = row .get ("value" );
0 commit comments