test json-ld parse by URL/Path/InputStream
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/21b68eea Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/21b68eea Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/21b68eea Branch: refs/heads/jena Commit: 21b68eeab8d2b148dd05f0947d7c11e6a126c068 Parents: 0d6a0f8 Author: Stian Soiland-Reyes <[email protected]> Authored: Wed Apr 13 02:45:25 2016 +0100 Committer: Stian Soiland-Reyes <[email protected]> Committed: Wed Apr 13 02:52:11 2016 +0100 ---------------------------------------------------------------------- .../rdf/jsonldjava/JsonLdParserBuilderTest.java | 88 +++++++++++++++++++- 1 file changed, 84 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/21b68eea/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java ---------------------------------------------------------------------- diff --git a/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java b/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java index df8511c..d3eae2b 100644 --- a/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java +++ b/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdParserBuilderTest.java @@ -17,20 +17,100 @@ */ package org.apache.commons.rdf.jsonldjava; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.InputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.concurrent.TimeUnit; 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.RDFSyntax; +import org.apache.commons.rdf.simple.Types; import org.junit.Test; public class JsonLdParserBuilderTest { + + private static final String TEST_JSONLD = "/test.jsonld"; + static JsonLdRDFTermFactory factory = new JsonLdRDFTermFactory(); + IRI test = factory.createIRI("http://example.com/test"); + IRI Type = factory.createIRI("http://example.com/Type"); + IRI type = factory.createIRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); + IRI pred1 = factory.createIRI("http://example.com/pred1"); + IRI pred2 = factory.createIRI("http://example.com/pred2"); + IRI pred3 = factory.createIRI("http://example.com/pred3"); + IRI pred4 = factory.createIRI("http://example.com/pred4"); + IRI other = factory.createIRI("http://example.com/other"); + IRI graph = factory.createIRI("http://example.com/graph"); + + + @Test public void parseByUrl() throws Exception { - URL url = getClass().getResource("/test.jsonld"); - IRI iri = new JsonLdRDFTermFactory().createIRI(url.toString()); - Graph g = new JsonLdParserBuilder().contentType(RDFSyntax.JSONLD).source(iri).parse().get(10, TimeUnit.SECONDS); - System.out.println(g); + URL url = getClass().getResource(TEST_JSONLD); + assertNotNull("Test resource not found: " + TEST_JSONLD, url); + IRI iri = factory.createIRI(url.toString()); + Graph g = new JsonLdParserBuilder() + .contentType(RDFSyntax.JSONLD).source(iri).parse() + .get(10, TimeUnit.SECONDS); + checkGraph(g); + } + + @Test + public void parseByPath() throws Exception { + Path path = Files.createTempFile("test", ".jsonld"); + path.toFile().deleteOnExit(); + try (InputStream is = getClass().getResourceAsStream(TEST_JSONLD)) { + assertNotNull("Test resource not found: " + TEST_JSONLD, is); + Files.copy(is, path, StandardCopyOption.REPLACE_EXISTING); + } + Graph g = new JsonLdParserBuilder() + .contentType(RDFSyntax.JSONLD).source(path).parse() + .get(10, TimeUnit.SECONDS); + checkGraph(g); + } + + @Test + public void parseByStream() throws Exception { + Graph g; + try (InputStream is = getClass().getResourceAsStream(TEST_JSONLD)) { + assertNotNull("Test resource not found: " + TEST_JSONLD, is); + g = new JsonLdParserBuilder() + .base("http://example.com/base/") + .contentType(RDFSyntax.JSONLD).source(is).parse() + .get(10, TimeUnit.SECONDS); + } + checkGraph(g); + } + + + private void checkGraph(Graph g) { + assertTrue(g.contains(test, type, Type)); + // Should not include statements from the named graph + + assertEquals(1, g.getTriples(test, pred1, null).count()); + assertEquals(1, g.getTriples(test, pred2, null).count()); + + assertEquals("Hello", + ((Literal) g.getTriples(test, pred1, null) + .findFirst().get().getObject() ).getLexicalForm()); + assertTrue(g.contains(test, pred2, other)); + + assertEquals("1337", + ((Literal) g.getTriples(test, pred3, null) + .findFirst().get().getObject() ).getLexicalForm()); + assertEquals(Types.XSD_INTEGER, + ((Literal) g.getTriples(test, pred3, null) + .findFirst().get().getObject() ).getDatatype()); + + // While the named graph 'graph' is not included, the relationship + // to that @id is included. + assertTrue(g.contains(test, pred4, graph)); } }
