View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * COMMONSRDF-57 etc: For upgrades, ensure JSONLD-Java parses well in all
50   * implementations even if they might have slightly incompatible versions of
51   * their dependencies.
52   * <p>
53   * The <code>*Embedded</code> tests parse <code>alice-embedded.jsonld</code>
54   * from the test classpath through Jena, RDF4J and JSONLD-Java and verifies it
55   * contains the expected triples using {@link #checkGraph(Graph)}. This ensures
56   * that the versions of JSONLD-Java and Jackson are compatible with Jena and
57   * RDF4J.
58   * <p>
59   * The <code>*Cached</code> tests parse <code>alice-cached.jsonld</code>, which
60   * references an external <code>@context</code> of http://example.com/context -
61   * but using the <a href=
62   * "https://github.com/jsonld-java/jsonld-java#loading-contexts-from-classpathjar">jarcache.json</a>
63   * mechanism of JSONLD-Java, this context will be loaded from
64   * <code>contexts/example.jsonld</code> on the test classpath instead. This
65   * ensures that the versions of HTTPClient is compatible with JSONLD-Java
66   * (however it does not check that it is compatible with Jena and
67   * RDF4J's external fetching of RDF documents).
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       * Pre-test that src/test/resources files are on the classpath
84       *
85       */
86      @Before
87      public void checkTestResources() throws Exception {
88          aliceCached.openStream().close();
89          aliceEmbedded.openStream().close();
90          // Used by JSONLD-Java to avoid external dependencies. See
91          // https://github.com/jsonld-java/jsonld-java#loading-contexts-from-classpathjar
92          getClass().getResourceAsStream("/jarcache.json").close();
93          getClass().getResourceAsStream("/contexts/example.jsonld").close();
94          // (We'll use these to ensure our HTTPClient dependency works)
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         // Check if HTTPClient cache is used from
110         // jarcache.json
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         // Check if HTTPClient cache is used from
129         // jarcache.json
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         // Check if HTTPClient cache is used from
151         // jarcache.json
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 }