commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [43/45] commons-rdf git commit: COMMONSRDF-57: Verify JSON-LD parses with mega-classpath
Date Fri, 10 Feb 2017 16:55:45 GMT
COMMONSRDF-57: Verify JSON-LD parses with mega-classpath


Project: http://git-wip-us.apache.org/repos/asf/commons-rdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rdf/commit/b1a31157
Tree: http://git-wip-us.apache.org/repos/asf/commons-rdf/tree/b1a31157
Diff: http://git-wip-us.apache.org/repos/asf/commons-rdf/diff/b1a31157

Branch: refs/heads/COMMONSRDF-47
Commit: b1a31157ee683e695501d77ece7d012c8066b1c0
Parents: df6c1a2
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Thu Feb 9 00:21:15 2017 +0000
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Thu Feb 9 00:27:20 2017 +0000

----------------------------------------------------------------------
 integration-tests/pom.xml                       |   3 +-
 .../rdf/integrationtests/JSONLDParsingTest.java | 164 +++++++++++++++++++
 .../src/test/resources/alice-cached.jsonld      |   9 +
 .../src/test/resources/alice-embedded.jsonld    |  16 ++
 .../src/test/resources/contexts/example.jsonld  |  11 ++
 .../src/test/resources/jarcache.json            |  10 ++
 6 files changed, 212 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b1a31157/integration-tests/pom.xml
----------------------------------------------------------------------
diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml
index 04496ef..4eeb7ec 100644
--- a/integration-tests/pom.xml
+++ b/integration-tests/pom.xml
@@ -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>

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b1a31157/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/JSONLDParsingTest.java
----------------------------------------------------------------------
diff --git a/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/JSONLDParsingTest.java
b/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/JSONLDParsingTest.java
new file mode 100644
index 0000000..3bd406a
--- /dev/null
+++ b/integration-tests/src/test/java/org/apache/commons/rdf/integrationtests/JSONLDParsingTest.java
@@ -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);
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b1a31157/integration-tests/src/test/resources/alice-cached.jsonld
----------------------------------------------------------------------
diff --git a/integration-tests/src/test/resources/alice-cached.jsonld b/integration-tests/src/test/resources/alice-cached.jsonld
new file mode 100644
index 0000000..494d20e
--- /dev/null
+++ b/integration-tests/src/test/resources/alice-cached.jsonld
@@ -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"
+}
+

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b1a31157/integration-tests/src/test/resources/alice-embedded.jsonld
----------------------------------------------------------------------
diff --git a/integration-tests/src/test/resources/alice-embedded.jsonld b/integration-tests/src/test/resources/alice-embedded.jsonld
new file mode 100644
index 0000000..7c8ca85
--- /dev/null
+++ b/integration-tests/src/test/resources/alice-embedded.jsonld
@@ -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"
+}
+

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b1a31157/integration-tests/src/test/resources/contexts/example.jsonld
----------------------------------------------------------------------
diff --git a/integration-tests/src/test/resources/contexts/example.jsonld b/integration-tests/src/test/resources/contexts/example.jsonld
new file mode 100644
index 0000000..0e58a65
--- /dev/null
+++ b/integration-tests/src/test/resources/contexts/example.jsonld
@@ -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"
+  }
+}

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/b1a31157/integration-tests/src/test/resources/jarcache.json
----------------------------------------------------------------------
diff --git a/integration-tests/src/test/resources/jarcache.json b/integration-tests/src/test/resources/jarcache.json
new file mode 100644
index 0000000..05eb13c
--- /dev/null
+++ b/integration-tests/src/test/resources/jarcache.json
@@ -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"
+  }
+]
\ No newline at end of file


Mime
View raw message