@Test public void testJsonLdDefaultSerializer() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service3.write(getTriples(), out, JSONLD); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); service3.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertAll("Check compact serialization", checkCompactSerialization(output, graph)); }
@Test public void testJsonLdExpandedSerializer() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, JSONLD, expanded); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertAll("Check expanded serialization", checkExpandedSerialization(output, graph)); }
@Test public void testJsonLdExpandedFlatSerializer() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, JSONLD, expanded, flattened); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertAll("Check expanded serialization", checkFlattenedSerialization(output, graph)); }
@Test public void testTurtleSerializer() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, TURTLE); final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); final org.apache.jena.graph.Graph graph = createDefaultGraph(); RDFDataMgr.read(graph, in, Lang.TURTLE); assertTrue(validateGraph(rdf.asGraph(graph)), "Failed round-trip for Turtle!"); }
private static boolean validateGraph(final Graph graph) { return getTriples().map(graph::contains).reduce(true, (acc, x) -> acc && x); }
private static Stream<Executable> checkExpandedSerialization(final String serialized, final Graph graph) { return of( () -> assertTrue(serialized.contains("\"http://purl.org/dc/terms/title\":[{\"@value\":\"A title\"}]"), "no expanded dc:title property!"), () -> assertFalse(serialized.contains("\"@context\":"), "unexpected @context!"), () -> assertFalse(serialized.contains("\"@graph\":"), "unexpected @graph!"), () -> assertTrue(validateGraph(graph), "Not all triples present in output graph!")); }
@Test public void testHtmlSerializer2() throws Exception { final IOService service4 = new JenaIOService(mockNamespaceService, mockHtmlSerializer); final ByteArrayOutputStream out = new ByteArrayOutputStream(); service4.write(getComplexTriples(), out, RDFA); verify(mockHtmlSerializer).write(any(), eq(out), eq(null)); }
@Test public void testNullHtmlSerializer() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, RDFA); final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); final org.apache.jena.graph.Graph graph = createDefaultGraph(); RDFDataMgr.read(graph, in, Lang.TURTLE); assertTrue(validateGraph(rdf.asGraph(graph)), "null HTML serialization didn't default to Turtle!"); }
@Test public void testWriteError() throws IOException { doThrow(new IOException()).when(mockOutputStream).write(any(byte[].class), anyInt(), anyInt()); assertThrows(RuntimeTrellisException.class, () -> service.write(getTriples(), mockOutputStream, TURTLE), "No write exception on bad input stream!"); }
private static Stream<Executable> checkCompactSerialization(final String serialized, final Graph graph) { return of( () -> assertTrue(serialized.contains("\"title\":\"A title\""), "missing/invalid dc:title value!"), () -> assertTrue(serialized.contains("\"@context\":"), "missing @context!"), () -> assertFalse(serialized.contains("\"@graph\":"), "unexpected @graph!"), () -> assertTrue(validateGraph(graph), "Not all triples present in output graph!")); }
@Test public void testHtmlSerializer() throws Exception { final IOService service4 = new JenaIOService(mockNamespaceService, mockHtmlSerializer); final ByteArrayOutputStream out = new ByteArrayOutputStream(); service4.write(getComplexTriples(), out, RDFA, rdf.createIRI("http://example.org/")); verify(mockHtmlSerializer).write(any(), eq(out), eq("http://example.org/")); }
@Test public void testNTriplesSerializer() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service3.write(getTriples(), out, NTRIPLES); final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); final org.apache.jena.graph.Graph graph = createDefaultGraph(); RDFDataMgr.read(graph, in, Lang.NTRIPLES); assertTrue(validateGraph(rdf.asGraph(graph)), "Failed round-trip for N-Triples!"); }
@Test public void testJsonLdCompactedSerializer() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, JSONLD, compacted); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertAll("Check compact serialization", checkCompactSerialization(output, graph)); }
@Test public void testJsonLdFlattenedSerializer() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, JSONLD, flattened); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertAll("Check flattened serialization", checkFlattenedSerialization(output, graph)); }
@Test public void testWriteInvalidSyntax() { when(mockSyntax.mediaType()).thenReturn("fake/mediatype"); final ByteArrayOutputStream out = new ByteArrayOutputStream(); assertThrows(RuntimeTrellisException.class, () -> service.write(getTriples(), out, mockSyntax), "No exception thrown with invalid write syntax!"); }
private static Stream<Executable> checkFlattenedSerialization(final String serialized, final Graph graph) { return of( () -> assertTrue(serialized.contains("\"title\":\"A title\""), "missing/invalid dc:title value!"), () -> assertTrue(serialized.contains("\"@context\":"), "missing @context!"), () -> assertTrue(serialized.contains("\"@graph\":"), "unexpected @graph!"), () -> assertTrue(validateGraph(graph), "Not all triples present in output graph!")); } }
@Test public void testBufferedSerializer() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, RDFXML); final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); final org.apache.jena.graph.Graph graph = createDefaultGraph(); RDFDataMgr.read(graph, in, Lang.RDFXML); assertTrue(validateGraph(rdf.asGraph(graph)), "Failed round-trip for RDFXML!"); }
@Test public void testJsonLdCustomUnrecognizedSerializer() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, JSONLD, rdf.createIRI("http://www.example.org/context.jsonld")); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertAll("Check compact serialization", checkCompactSerialization(output, graph)); }
@Test public void testJsonLdFlattenedSerializer3() throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); service.write(getTriples(), out, JSONLD, expanded_flattened); final String output = out.toString("UTF-8"); final Graph graph = rdf.createGraph(); service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add); assertAll("Check flattened serialization", checkFlattenedSerialization(output, graph)); }
@Test public void testUpdateInvalidSyntax() { final Graph graph = rdf.createGraph(); getTriples().forEach(graph::add); final String patch = "UpdateList <#> <http://example.org/vocab#preferredLanguages> 1..2 ( \"fr\" ) ."; assertThrows(RuntimeTrellisException.class, () -> service.update(graph, patch, LD_PATCH, null), "No exception thrown with invalid update syntax!"); }