BlackDuckIoWriter implements GraphWriter { public void writeGraph(StreamSupplier out, List<TraversalStrategy<?>> strategies, Graph graph) throws IOException { RxJavaBdioDocument document = new RxJavaBdioDocument(frame.context()); GraphTraversalSource g = graph.traversal().withStrategies(strategies.toArray(new TraversalStrategy<?>[strategies.size()])); BlackDuckIoWriterSpi spi = BlackDuckIoSpi.getForGraph(graph).writer(g, options, frame); try { Flowable.fromPublisher(spi.retrieveCompactedNodes()) .buffer(batchSize) .blockingSubscribe(document.write(spi.retrieveMetadata(), out)); } catch (UncheckedIOException e) { throw e.getCause(); } } private  BlackDuckIoWriter(Builder builder); void writeGraph(StreamSupplier out, List<TraversalStrategy<?>> strategies, Graph graph); @Override void writeGraph(OutputStream outputStream, Graph graph); @Override void writeVertex(OutputStream outputStream, Vertex v, Direction direction); @Override void writeVertex(OutputStream outputStream, Vertex v); @Override void writeEdge(OutputStream outputStream, Edge e); @Override void writeVertexProperty(OutputStream outputStream, @SuppressWarnings("rawtypes") VertexProperty vp); @Override void writeProperty(OutputStream outputStream, @SuppressWarnings("rawtypes") Property p); @Override void writeObject(OutputStream outputStream, Object object); static Builder build();  }
@Test public void writeMetadata() throws Exception { String metadataId = BdioObject.randomId(); ZonedDateTime creationDateTime = ZonedDateTime.now(ZoneId.systemDefault()); graph.addVertex( T.label, TT.Metadata, TT.id, metadataId, Bdio.DataProperty.creationDateTime.name(), creationDateTime.toString()); commit(); HeapOutputStream outputStream = new HeapOutputStream(); graph.io(testBdio(TT.Metadata, TT.id)).writeGraph(new BdioFile(outputStream)); List<String> entries = BdioTest.zipEntries(outputStream.getInputStream()); assertThat(entries).hasSize(1); assertThatJson(entries.get(0)).at("/@id").isEqualTo(metadataId); assertThatJson(entries.get(0)).at(Bdio.DataProperty.creationDateTime, 0, "@value").isEqualTo(creationDateTime.toString()); assertThatJson(entries.get(0)).arrayAt("/@graph").hasSize(0); }
@Test public void writeMissingMetadata() throws Exception { graph.addVertex( T.label, Bdio.Class.Project.name(), TT.id, BdioObject.randomId()); commit(); HeapOutputStream outputStream = new HeapOutputStream(); graph.io(testBdio(TT.Metadata, TT.id)).writeGraph(new BdioFile(outputStream)); List<String> entries = BdioTest.zipEntries(outputStream.getInputStream()); assertThat(entries).hasSize(2); assertThatJson(entries.get(0)).arrayAt("/@graph").hasSize(0); assertThatJson(entries.get(1)).arrayAt("/@graph").hasSize(1); assertThatJson(entries.get(1)).arrayAt("/@graph/0/@type").containsExactly(Bdio.Class.Project.toString()); }
@Test public void writeRelationship() throws Exception { String metadataId = BdioObject.randomId(); ZonedDateTime creationDateTime = ZonedDateTime.now(ZoneId.systemDefault()); graph.addVertex( T.label, TT.Metadata, TT.id, metadataId, Bdio.DataProperty.creationDateTime.name(), creationDateTime.toString()); String fileId = BdioObject.randomId(); Vertex file = graph.addVertex( T.label, Bdio.Class.File.name(), TT.id, fileId); String projectId = BdioObject.randomId(); graph.addVertex( T.label, Bdio.Class.Project.name(), TT.id, projectId) .addEdge(Bdio.ObjectProperty.base.name(), file); commit(); HeapOutputStream outputStream = new HeapOutputStream(); graph.io(testBdio(TT.Metadata, TT.id)).writeGraph(new BdioFile(outputStream)); List<String> entries = BdioTest.zipEntries(outputStream.getInputStream()); assertThat(entries).hasSize(2); assertThatJson(entries.get(0)).at("/@id").isEqualTo(metadataId); assertThatJson(entries.get(0)).at(Bdio.DataProperty.creationDateTime, 0, "@value").isEqualTo(creationDateTime.toString()); assertThatJson(entries.get(0)).arrayAt("/@graph").hasSize(0); assertThatJson(entries.get(1)).at("/@id").isEqualTo(metadataId); assertThatJson(entries.get(1)).arrayAt("/@graph").hasSize(2); int projectIndex = BdioTest.nodeIdentifiers(entries.get(1)).indexOf(projectId); assertThat(projectIndex).isAtLeast(0); assertThatJson(entries.get(1)).at("@graph", projectIndex, Bdio.ObjectProperty.base, 0, "@id").isEqualTo(fileId); }
@Test public void writeCustomProperty() throws Exception { graph.addVertex( T.label, Bdio.Class.Project.name(), TT.id, BdioObject.randomId(), "foobar", "testing"); commit(); HeapOutputStream nonCustomBuffer = new HeapOutputStream(); graph.io(testBdio(TT.Metadata, TT.id)).writeGraph(new BdioFile(nonCustomBuffer)); assertThatJson(BdioTest.zipEntries(nonCustomBuffer.getInputStream()).get(1)) .at("/@graph/0").doesNotContainName("foobar"); List<Object> expandContext = ImmutableList.of(Bdio.Context.DEFAULT.toString(), ImmutableMap.of("foobar", "http: HeapOutputStream customBuffer = new HeapOutputStream(); graph.io(testBdio(TT.Metadata, TT.id).context("", expandContext)).writeGraph(new BdioFile(customBuffer)); List<String> entries = BdioTest.zipEntries(customBuffer.getInputStream()); assertThatJson(entries.get(1)).at("/@graph/0").containsName("http: assertThatJson(entries.get(1)).at("/@graph/0").doesNotContainName("foobar"); }
@Test public void writeEmbeddedObject() throws Exception { Vertex component1 = graph.addVertex( T.label, Bdio.Class.Component.name(), TT.id, BdioObject.randomId()); Vertex component2 = graph.addVertex( T.label, Bdio.Class.Component.name(), TT.id, BdioObject.randomId()); Vertex dependency = graph.addVertex( T.label, Bdio.Class.Dependency.name()); component2.addEdge(Bdio.ObjectProperty.dependency.name(), dependency); dependency.addEdge(Bdio.ObjectProperty.dependsOn.name(), component1); commit(); HeapOutputStream outputStream = new HeapOutputStream(); graph.io(testBdio(TT.Metadata, TT.id)).writeGraph(new BdioFile(outputStream)); List<String> entries = BdioTest.zipEntries(outputStream.getInputStream()); assertThat(entries).hasSize(2); assertThatJson(entries.get(1)).arrayAt("/@graph").hasSize(2); assertThatJson(entries.get(1)).at("@graph", 1, Bdio.ObjectProperty.dependency, 0, Bdio.ObjectProperty.dependsOn, 0, "@id") .isEqualTo(component1.value(TT.id)); }