Skip to content
Permalink
Browse files
COMMONSRDF-57: Verify JSON-LD parses with mega-classpath
  • Loading branch information
stain committed Feb 9, 2017
1 parent df6c1a2 commit b1a31157ee683e695501d77ece7d012c8066b1c0
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 1 deletion.
@@ -49,7 +49,8 @@
<version>${project.version}</version>
</dependency>
<!-- NOTE: This might be quite version-sensitive (including transitives)
as both rdf4j and jena depend on jsonld -->
See the JSONLDParsingTest.
-->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>commons-rdf-jsonld-java</artifactId>
@@ -0,0 +1,164 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* 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.apache.commons.rdf.integrationtests;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.rdf.api.Graph;
import org.apache.commons.rdf.api.IRI;
import org.apache.commons.rdf.api.Literal;
import org.apache.commons.rdf.api.RDF;
import org.apache.commons.rdf.jena.JenaDataset;
import org.apache.commons.rdf.jena.JenaRDF;
import org.apache.commons.rdf.jsonldjava.JsonLdGraph;
import org.apache.commons.rdf.jsonldjava.JsonLdRDF;
import org.apache.commons.rdf.rdf4j.RDF4J;
import org.apache.commons.rdf.rdf4j.RDF4JGraph;
import org.apache.commons.rdf.simple.SimpleRDF;
import org.apache.jena.riot.RDFDataMgr;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonParseException;
import com.github.jsonldjava.core.JsonLdError;
import com.github.jsonldjava.core.JsonLdOptions;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.core.RDFDataset;
import com.github.jsonldjava.utils.JsonUtils;

/**
* COMMONSRDF-57 etc: For upgrades, ensure JSONLD-Java parses well in all
* implementations even if they might have slightly incompatible versions of
* their dependencies.
* <p>
* The <code>*Embedded</code> tests parse <code>alice-embedded.jsonld</code>
* from the test classpath through Jena, RDF4J and JSONLD-Java and verifies it
* contains the expected triples using {@link #checkGraph(Graph)}. This ensures
* that the versions of JSONLD-Java and Jackson are compatible with Jena and
* RDF4J.
* <p>
* The <code>*Cached</code> tests parse <code>alice-cached.jsonld</code>, which
* references an external <code>@context</code> of http://example.com/context -
* but using the <a href=
* "https://github.com/jsonld-java/jsonld-java#loading-contexts-from-classpathjar">jarcache.json</a>
* mechanism of JSONLD-Java, this context will be loaded from
* <code>contexts/example.jsonld</code> on the test classpath instead. This
* ensures that the versions of HTTPClient is compatible with JSONLD-Java
* (however it does not check that it is compatible with Jena and
* RDF4J's external fetching of RDF documents).
*
*/
public class JSONLDParsingTest {

static RDF rdf = new SimpleRDF();
static IRI alice = rdf.createIRI("http://example.com/Alice");
static IRI name = rdf.createIRI("http://schema.org/name");
static IRI type = rdf.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
static IRI person = rdf.createIRI("http://schema.org/Person");
static Literal aliceWLand = rdf.createLiteral("Alice W. Land");

URL aliceCached = getClass().getResource("/alice-cached.jsonld");
URL aliceEmbedded = getClass().getResource("/alice-embedded.jsonld");

/**
* Pre-test that src/test/resources files are on the classpath
*
*/
@Before
public void checkTestResources() throws Exception {
aliceCached.openStream().close();
aliceEmbedded.openStream().close();
// Used by JSONLD-Java to avoid external dependencies. See
// https://github.com/jsonld-java/jsonld-java#loading-contexts-from-classpathjar
getClass().getResourceAsStream("/jarcache.json").close();
getClass().getResourceAsStream("/contexts/example.jsonld").close();
// (We'll use these to ensure our HTTPClient dependency works)
}

private void checkGraph(Graph g) {
assertTrue(g.contains(alice, name, aliceWLand));
assertTrue(g.contains(alice, type, person));
}

@Test
public void jenaParseEmbedded() throws Exception {
jenaParse(aliceEmbedded);
}

@Test
public void jenaParseCached() throws Exception {
// Check if HTTPClient cache is used from
// jarcache.json
jenaParse(aliceCached);
}

private void jenaParse(URL url) {
JenaDataset dataset = new JenaRDF().createDataset();
RDFDataMgr.read(dataset.asJenaDatasetGraph(), url.toExternalForm());
checkGraph(dataset.getGraph());
}

@Test
public void rdf4jParseEmbedded() throws Exception {
rdf4jParse(aliceEmbedded);
}

@Test
public void rdf4jParseCached() throws Exception {
// Check if HTTPClient cache is used from
// jarcache.json
rdf4jParse(aliceCached);
}

private void rdf4jParse(URL url) throws IOException {
Model model;
try (InputStream in = url.openStream()) {
model = Rio.parse(in, url.toExternalForm(), RDFFormat.JSONLD);
}
RDF4JGraph graph = new RDF4J().asGraph(model);
checkGraph(graph);
}

@Test
public void jsonldParseEmbedded() throws Exception {
jsonldParse(aliceEmbedded);
}

@Test
public void jsonldParseCached() throws Exception {
// Check if HTTPClient cache is used from
// jarcache.json
jsonldParse(aliceCached);
}

private void jsonldParse(URL url) throws JsonParseException, IOException, JsonLdError {
Object aliceJson = JsonUtils.fromURL(url, JsonUtils.getDefaultHttpClient());
JsonLdOptions options = new JsonLdOptions();
options.setBase(url.toExternalForm());
RDFDataset ds = (RDFDataset) JsonLdProcessor.toRDF(aliceJson);
JsonLdGraph graph = new JsonLdRDF().asGraph(ds);
checkGraph(graph);
}
}
@@ -0,0 +1,9 @@
{ "http://purl.org/dc/terms/license" :
"Licensed to the Apache Software Foundation (ASF) under one or more\n contributor license agreements. See the NOTICE file distributed with\n this work for additional information regarding copyright ownership.\n The ASF licenses this file to You under the Apache License, Version 2.0\n (the \"License\"); you may not use this file except in compliance with\n the License. You may obtain a copy of the License at\n \n http://www.apache.org/licenses/LICENSE-2.0\n \n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n",

"@context": "http://example.com/context",
"@id": "ex:Alice",
"@type": "Person",
"name": "Alice W. Land"
}

@@ -0,0 +1,16 @@
{ "http://purl.org/dc/terms/license" :
"Licensed to the Apache Software Foundation (ASF) under one or more\n contributor license agreements. See the NOTICE file distributed with\n this work for additional information regarding copyright ownership.\n The ASF licenses this file to You under the Apache License, Version 2.0\n (the \"License\"); you may not use this file except in compliance with\n the License. You may obtain a copy of the License at\n \n http://www.apache.org/licenses/LICENSE-2.0\n \n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n",

"@context": {
"xsd": "http://www.w3.org/2001/XMLSchema#",
"schema": "http://schema.org/",
"ex": "http://example.com/",
"name": "schema:name",
"Person": "schema:Person"
},

"@id": "ex:Alice",
"@type": "Person",
"name": "Alice W. Land"
}

@@ -0,0 +1,11 @@
{ "http://purl.org/dc/terms/license" :
"Licensed to the Apache Software Foundation (ASF) under one or more\n contributor license agreements. See the NOTICE file distributed with\n this work for additional information regarding copyright ownership.\n The ASF licenses this file to You under the Apache License, Version 2.0\n (the \"License\"); you may not use this file except in compliance with\n the License. You may obtain a copy of the License at\n \n http://www.apache.org/licenses/LICENSE-2.0\n \n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n",

"@context": {
"xsd": "http://www.w3.org/2001/XMLSchema#",
"schema": "http://schema.org/",
"ex": "http://example.com/",
"name": "schema:name",
"Person": "schema:Person"
}
}
@@ -0,0 +1,10 @@
[
{
"X-License":
"Licensed to the Apache Software Foundation (ASF) under one or more\n contributor license agreements. See the NOTICE file distributed with\n this work for additional information regarding copyright ownership.\n The ASF licenses this file to You under the Apache License, Version 2.0\n (the \"License\"); you may not use this file except in compliance with\n the License. You may obtain a copy of the License at\n \n http://www.apache.org/licenses/LICENSE-2.0\n \n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n",

"Content-Location": "http://example.com/context",
"X-Classpath": "contexts/example.jsonld",
"Content-Type": "application/ld+json"
}
]

0 comments on commit b1a3115

Please sign in to comment.