1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rdf.integrationtests;
19
20 import static org.junit.Assert.assertTrue;
21
22 import java.io.InputStream;
23 import java.net.URL;
24
25 import org.apache.commons.rdf.api.Graph;
26 import org.apache.commons.rdf.api.IRI;
27 import org.apache.commons.rdf.api.Literal;
28 import org.apache.commons.rdf.api.RDF;
29 import org.apache.commons.rdf.jena.JenaDataset;
30 import org.apache.commons.rdf.jena.JenaRDF;
31 import org.apache.commons.rdf.jsonldjava.JsonLdGraph;
32 import org.apache.commons.rdf.jsonldjava.JsonLdRDF;
33 import org.apache.commons.rdf.rdf4j.RDF4J;
34 import org.apache.commons.rdf.rdf4j.RDF4JGraph;
35 import org.apache.commons.rdf.simple.SimpleRDF;
36 import org.apache.jena.riot.RDFDataMgr;
37 import org.eclipse.rdf4j.model.Model;
38 import org.eclipse.rdf4j.rio.RDFFormat;
39 import org.eclipse.rdf4j.rio.Rio;
40 import org.junit.Before;
41 import org.junit.Test;
42
43 import com.github.jsonldjava.core.JsonLdOptions;
44 import com.github.jsonldjava.core.JsonLdProcessor;
45 import com.github.jsonldjava.core.RDFDataset;
46 import com.github.jsonldjava.utils.JsonUtils;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public class JSONLDParsingTest {
71
72 static RDF rdf = new SimpleRDF();
73 static IRI alice = rdf.createIRI("http://example.com/Alice");
74 static IRI name = rdf.createIRI("http://schema.org/name");
75 static IRI type = rdf.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
76 static IRI person = rdf.createIRI("http://schema.org/Person");
77 static Literal aliceWLand = rdf.createLiteral("Alice W. Land");
78
79 URL aliceCached = getClass().getResource("/alice-cached.jsonld");
80 URL aliceEmbedded = getClass().getResource("/alice-embedded.jsonld");
81
82
83
84
85
86 @Before
87 public void checkTestResources() throws Exception {
88 aliceCached.openStream().close();
89 aliceEmbedded.openStream().close();
90
91
92 getClass().getResourceAsStream("/jarcache.json").close();
93 getClass().getResourceAsStream("/contexts/example.jsonld").close();
94
95 }
96
97 private void checkGraph(final Graph g) {
98 assertTrue(g.contains(alice, name, aliceWLand));
99 assertTrue(g.contains(alice, type, person));
100 }
101
102 @Test
103 public void jenaParseEmbedded() throws Exception {
104 jenaParse(aliceEmbedded);
105 }
106
107 @Test
108 public void jenaParseCached() throws Exception {
109
110
111 jenaParse(aliceCached);
112 }
113
114 private void jenaParse(final URL url) throws Exception {
115 try (final JenaDataset dataset = new JenaRDF().createDataset()) {
116 RDFDataMgr.read(dataset.asJenaDatasetGraph(), url.toExternalForm());
117 checkGraph(dataset.getGraph());
118 }
119 }
120
121 @Test
122 public void rdf4jParseEmbedded() throws Exception {
123 rdf4jParse(aliceEmbedded);
124 }
125
126 @Test
127 public void rdf4jParseCached() throws Exception {
128
129
130 rdf4jParse(aliceCached);
131 }
132
133 private void rdf4jParse(final URL url) throws Exception {
134 Model model;
135 try (InputStream in = url.openStream()) {
136 model = Rio.parse(in, url.toExternalForm(), RDFFormat.JSONLD);
137 }
138 try (final RDF4JGraph graph = new RDF4J().asGraph(model)) {
139 checkGraph(graph);
140 }
141 }
142
143 @Test
144 public void jsonldParseEmbedded() throws Exception {
145 jsonldParse(aliceEmbedded);
146 }
147
148 @Test
149 public void jsonldParseCached() throws Exception {
150
151
152 jsonldParse(aliceCached);
153 }
154
155 private void jsonldParse(final URL url) throws Exception {
156 final Object aliceJson = JsonUtils.fromURL(url, JsonUtils.getDefaultHttpClient());
157 final JsonLdOptions options = new JsonLdOptions();
158 options.setBase(url.toExternalForm());
159 final RDFDataset ds = (RDFDataset) JsonLdProcessor.toRDF(aliceJson);
160 try (final JsonLdGraph graph = new JsonLdRDF().asGraph(ds)) {
161 checkGraph(graph);
162 }
163 }
164 }