1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rdf.jsonldjava;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.InputStream;
25 import java.net.URL;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.StandardCopyOption;
29 import java.util.concurrent.TimeUnit;
30
31 import org.apache.commons.rdf.api.Graph;
32 import org.apache.commons.rdf.api.IRI;
33 import org.apache.commons.rdf.api.Literal;
34 import org.apache.commons.rdf.api.RDFSyntax;
35 import org.apache.commons.rdf.jsonldjava.experimental.JsonLdParser;
36 import org.apache.commons.rdf.simple.Types;
37 import org.junit.Test;
38
39 public class JsonLdParserBuilderTest {
40 private static final String TEST_JSONLD = "/test.jsonld";
41
42 static JsonLdRDF factory = new JsonLdRDF();
43 IRI test = factory.createIRI("http://example.com/test");
44 IRI Type = factory.createIRI("http://example.com/Type");
45 IRI type = factory.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
46 IRI pred1 = factory.createIRI("http://example.com/pred1");
47 IRI pred2 = factory.createIRI("http://example.com/pred2");
48 IRI pred3 = factory.createIRI("http://example.com/pred3");
49 IRI pred4 = factory.createIRI("http://example.com/pred4");
50 IRI other = factory.createIRI("http://example.com/other");
51 IRI graph = factory.createIRI("http://example.com/graph");
52
53 @Test
54 public void parseByUrl() throws Exception {
55 final URL url = getClass().getResource(TEST_JSONLD);
56 assertNotNull("Test resource not found: " + TEST_JSONLD, url);
57 final IRI iri = factory.createIRI(url.toString());
58 try (final Graph g = factory.createGraph()) {
59 new JsonLdParser().contentType(RDFSyntax.JSONLD).source(iri).target(g).parse().get(10, TimeUnit.SECONDS);
60 checkGraph(g);
61 }
62 }
63
64 @Test
65 public void parseByPath() throws Exception {
66 final Path path = Files.createTempFile("test", ".jsonld");
67 path.toFile().deleteOnExit();
68 try (InputStream is = getClass().getResourceAsStream(TEST_JSONLD)) {
69 assertNotNull("Test resource not found: " + TEST_JSONLD, is);
70 Files.copy(is, path, StandardCopyOption.REPLACE_EXISTING);
71 }
72 try (final Graph g = factory.createGraph()) {
73 new JsonLdParser().contentType(RDFSyntax.JSONLD).source(path).target(g).parse().get(10, TimeUnit.SECONDS);
74 checkGraph(g);
75 }
76 }
77
78 @Test
79 public void parseByStream() throws Exception {
80 try (final Graph g = factory.createGraph()) {
81 try (InputStream is = getClass().getResourceAsStream(TEST_JSONLD)) {
82 assertNotNull("Test resource not found: " + TEST_JSONLD, is);
83 new JsonLdParser().base("http://example.com/base/").contentType(RDFSyntax.JSONLD).source(is).target(g)
84 .parse().get(10, TimeUnit.SECONDS);
85 }
86 checkGraph(g);
87 }
88 }
89
90 private void checkGraph(final Graph g) {
91 assertTrue(g.contains(test, type, Type));
92
93
94 assertEquals(1, g.stream(test, pred1, null).count());
95 assertEquals(1, g.stream(test, pred2, null).count());
96
97 assertEquals("Hello", ((Literal) g.stream(test, pred1, null).findFirst().get().getObject()).getLexicalForm());
98 assertTrue(g.contains(test, pred2, other));
99
100 assertEquals("1337", ((Literal) g.stream(test, pred3, null).findFirst().get().getObject()).getLexicalForm());
101 assertEquals(Types.XSD_INTEGER,
102 ((Literal) g.stream(test, pred3, null).findFirst().get().getObject()).getDatatype());
103
104
105
106 assertTrue(g.contains(test, pred4, graph));
107 }
108 }