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.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          // Should not include statements from the named graph
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         // While the named graph 'graph' is not included, the relationship
105         // to that @id is included.
106         assertTrue(g.contains(test, pred4, graph));
107     }
108 }