BlackDuckIoReader implements GraphReader { @SuppressWarnings("CheckReturnValue") public void readGraph(InputStream inputStream, String base, Object expandContext, List<TraversalStrategy<?>> strategies, Graph graph) throws IOException { RxJavaBdioDocument doc = new RxJavaBdioDocument(frame.context().newBuilder().base(base).expandContext(expandContext).build()); GraphTraversalSource g = graph.traversal().withStrategies(strategies.toArray(new TraversalStrategy<?>[strategies.size()])); BlackDuckIoReaderSpi spi = BlackDuckIoSpi.getForGraph(graph).reader(g, options, frame, batchSize); try { Flowable<Object> entries = doc.read(inputStream); if (options.metadataLabel().isPresent()) { entries = entries.publish().autoConnect(2); doc.metadata(entries).singleOrError().subscribe(spi::persistMetadata, RxJavaPlugins::onError); } doc.jsonLd(entries).frame(frame.serialize()).compose(spi::persistFramedEntries).blockingSubscribe(); } catch (RuntimeException e) { Throwable failure = unwrap(e); throwIfInstanceOf(failure, IOException.class); throwIfUnchecked(failure); if (failure instanceof NodeDoesNotExistException) { throw new BlackDuckIoReadGraphException("Failed to load BDIO due to invalid references in the input", failure); } else if (failure instanceof SQLException) { throw new BlackDuckIoReadGraphException("Failed to load BDIO due to a database error", failure); } throw new IllegalStateException("Unexpected checked exception in readGraph", failure); } } private  BlackDuckIoReader(Builder builder); @SuppressWarnings("CheckReturnValue") void readGraph(InputStream inputStream, String base, Object expandContext, List<TraversalStrategy<?>> strategies, Graph graph); @Override void readGraph(InputStream inputStream, Graph graph); @Override Vertex readVertex(InputStream inputStream, Function<Attachable<Vertex>, Vertex> vertexAttachMethod); @Override Vertex readVertex(InputStream inputStream, Function<Attachable<Vertex>, Vertex> vertexAttachMethod,
            Function<Attachable<Edge>, Edge> edgeAttachMethod, Direction attachEdgesOfThisDirection); @Override Iterator<Vertex> readVertices(InputStream inputStream, Function<Attachable<Vertex>, Vertex> vertexAttachMethod,
            Function<Attachable<Edge>, Edge> edgeAttachMethod, Direction attachEdgesOfThisDirection); @Override Edge readEdge(InputStream inputStream, Function<Attachable<Edge>, Edge> edgeAttachMethod); @SuppressWarnings("rawtypes") @Override VertexProperty readVertexProperty(InputStream inputStream, Function<Attachable<VertexProperty>, VertexProperty> vertexPropertyAttachMethod); @SuppressWarnings("rawtypes") @Override Property readProperty(InputStream inputStream, Function<Attachable<Property>, Property> propertyAttachMethod); @Override C readObject(InputStream inputStream, Class<? extends C> clazz); static Builder build();  }
@Test public void readEmpty() throws Exception { InputStream inputStream = new ByteArrayInputStream(new byte[0]); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null); GraphTraversal<Vertex, Vertex> namedGraphs = graph.traversal().V().hasLabel(TT.Metadata); assertThat(namedGraphs.hasNext()).isTrue(); }
@Test public void readCustomProperty() throws Exception { List<Object> expandContext = ImmutableList.of(Bdio.Context.DEFAULT.toString(), ImmutableMap.of("foobar", "http: BdioMetadata metadata = BdioMetadata.createRandomUUID(); Project projectModel = new Project(BdioObject.randomId()); projectModel.put("http: InputStream inputStream = BdioTest.zipJsonBytes(metadata.asNamedGraph(Lists.newArrayList(projectModel))); graph.io(testBdio(TT.unknown).context(null, expandContext)).readGraph(inputStream, null, null); Optional<Object> unknown = graph.traversal().V().hasLabel(Bdio.Class.Project.name()).values("_unknown").tryNext(); assertThat(unknown).isEmpty(); Optional<Object> foobar = graph.traversal().V().hasLabel(Bdio.Class.Project.name()).values("foobar").tryNext(); assertThat(foobar).hasValue("testing"); }
@Test public void readLogicallyEmpty() throws Exception { InputStream inputStream = new ByteArrayInputStream("{}".getBytes(UTF_8)); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null); GraphTraversal<Vertex, Vertex> namedGraphs = graph.traversal().V().hasLabel(TT.Metadata); assertThat(namedGraphs.hasNext()).isTrue(); }
@Test public void readMetadata() throws Exception { ZonedDateTime creationDateTime = ZonedDateTime.now(ZoneId.systemDefault()); BdioMetadata metadata = BdioMetadata.createRandomUUID().creationDateTime(creationDateTime); InputStream inputStream = BdioTest.zipJsonBytes(metadata.asNamedGraph()); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null); GraphTraversal<Vertex, Vertex> namedGraphs = graph.traversal().V().hasLabel(TT.Metadata); assertThat(namedGraphs.hasNext()).isTrue(); Vertex namedGraph = namedGraphs.next(); assertThat(namedGraphs.hasNext()).isFalse(); VertexProperty<String> idProperty = namedGraph.property(TT.id); assertThat(idProperty.isPresent()).isTrue(); assertThat(idProperty.value()).isEqualTo(metadata.id()); VertexProperty<ZonedDateTime> creationProperty = namedGraph.property(Bdio.DataProperty.creationDateTime.name()); assertThat(creationProperty.isPresent()).isTrue(); assertThat(creationProperty.value()).isEqualTo(creationDateTime); }
@Test public void readMetadataPartition() throws Exception { BdioMetadata metadata = BdioMetadata.createRandomUUID(); InputStream inputStream = BdioTest.zipJsonBytes(metadata.asNamedGraph()); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null, testPartition("abc")); Vertex namedGraph = graph.traversal().V().hasLabel(TT.Metadata).next(); VertexProperty<String> partition = namedGraph.property(TT.partition); assertThat(partition.isPresent()).isTrue(); assertThat(partition.value()).isEqualTo("abc"); }
@Test public void readIdOnlyMetadata() throws Exception { BdioMetadata metadata = BdioMetadata.createRandomUUID(); InputStream inputStream = BdioTest.zipJsonBytes(metadata.asNamedGraph()); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null); Vertex namedGraph = graph.traversal().V().hasLabel(TT.Metadata).next(); assertThat(namedGraph.property(TT.id).orElse(null)).isEqualTo(metadata.id()); }
@Test public void readFileWithAndWithoutFingerprint() throws Exception { BdioMetadata metadata = BdioMetadata.createRandomUUID(); File fileModel0 = new File(BdioObject.randomId()); File fileModel1 = new File(BdioObject.randomId()); File fileModel2 = new File(fileModel1.id()); fileModel1.fingerprint(singleton(Digest.of("sha1", "2d05a5f70ffb6fbf6fcbf65bb6f4cd48a8b2592a"))); InputStream inputStream = BdioTest.zipJsonBytes( metadata.asNamedGraph(Lists.newArrayList(fileModel0, fileModel1)), metadata.asNamedGraph(Lists.newArrayList(fileModel2), JsonLdConsts.ID)); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null); }
@Test public void splitNode() throws Exception { BdioMetadata metadata = BdioMetadata.createRandomUUID(); File fileModel1 = new File(BdioObject.randomId()); File fileModel2 = new File(fileModel1.id()); fileModel1.byteCount(103L); fileModel2.contentType(ContentType.parse("text/plain")); InputStream inputStream = BdioTest.zipJsonBytes( metadata.asNamedGraph(Lists.newArrayList(fileModel1)), metadata.asNamedGraph(Lists.newArrayList(fileModel2), JsonLdConsts.ID)); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null); GraphTraversal<Vertex, Vertex> files = graph.traversal().V().hasLabel(Bdio.Class.File.name()); assertThat(files.hasNext()).isTrue(); Vertex file = files.next(); assertThat(files.hasNext()).isFalse(); assertThat(file.property(Bdio.DataProperty.byteCount.name()).isPresent()).isTrue(); assertThat(file.property(Bdio.DataProperty.contentType.name()).isPresent()).isTrue(); }
@Test public void complexSplitNode() throws Exception { BdioMetadata metadata = BdioMetadata.createRandomUUID(); File fileModel1_1 = new File(BdioObject.randomId()); File fileModel1_2 = new File(fileModel1_1.id()); File fileModel1_3 = new File(fileModel1_1.id()); File fileModel1_4 = new File(fileModel1_1.id()); File fileModel2_1 = new File(BdioObject.randomId()); File fileModel2_2 = new File(fileModel2_1.id()); fileModel1_1.byteCount(103L); fileModel1_2.contentType(ContentType.parse("text/plain")); fileModel1_3.fingerprint(Collections.singleton(Digest.of("test", "abc"))); fileModel1_4.path("file:/testing1"); fileModel2_1.path("file:/testing2"); fileModel2_2.byteCount(101L); InputStream inputStream = BdioTest.zipJsonBytes( metadata.asNamedGraph(Lists.newArrayList(fileModel1_1, fileModel2_1)), metadata.asNamedGraph(Lists.newArrayList(fileModel1_2, fileModel2_2, fileModel1_3), JsonLdConsts.ID), metadata.asNamedGraph(Lists.newArrayList(fileModel1_4), JsonLdConsts.ID)); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null); GraphTraversalSource g = graph.traversal(); assertThat(g.V().hasLabel(Bdio.Class.File.name()).count().next()).isEqualTo(2); Vertex file1 = g.V().hasLabel(Bdio.Class.File.name()).has(TT.id, fileModel1_1.id()).next(); assertThat(file1.property(Bdio.DataProperty.byteCount.name()).isPresent()).isTrue(); assertThat(file1.property(Bdio.DataProperty.contentType.name()).isPresent()).isTrue(); assertThat(file1.property(Bdio.DataProperty.fingerprint.name()).isPresent()).isTrue(); assertThat(file1.property(Bdio.DataProperty.path.name()).isPresent()).isTrue(); Vertex file2 = g.V().hasLabel(Bdio.Class.File.name()).has(TT.id, fileModel2_1.id()).next(); assertThat(file2.property(Bdio.DataProperty.byteCount.name()).isPresent()).isTrue(); assertThat(file2.property(Bdio.DataProperty.path.name()).isPresent()).isTrue(); }
@Test public void splitNodeEdges() throws Exception { BdioMetadata metadata = BdioMetadata.createRandomUUID(); Project projectModel1 = new Project(BdioObject.randomId()); Project projectModel2 = new Project(projectModel1.id()); File fileModel = new File(BdioObject.randomId()); projectModel2.base(fileModel); InputStream inputStream = BdioTest.zipJsonBytes( metadata.asNamedGraph(Lists.newArrayList(projectModel1)), metadata.asNamedGraph(Lists.newArrayList(projectModel2, fileModel), JsonLdConsts.ID)); graph.io(testBdio(TT.Metadata, TT.id)).readGraph(inputStream, null, null); GraphTraversalSource g = graph.traversal(); assertThat(g.V().hasLabel(Bdio.Class.Project.name()).count().next()).isEqualTo(1); assertThat(g.V().hasLabel(Bdio.Class.Project.name()).out(Bdio.ObjectProperty.base.name()).values(TT.id).tryNext()).hasValue(fileModel.id()); }
@Test public void readUnknownCustomProperty() throws Exception { BdioMetadata metadata = BdioMetadata.createRandomUUID(); Project projectModel = new Project(BdioObject.randomId()); projectModel.put("http: InputStream inputStream = BdioTest.zipJsonBytes(metadata.asNamedGraph(Lists.newArrayList(projectModel))); graph.io(testBdio(TT.Metadata, TT.id, TT.unknown)).readGraph(inputStream, null, null); Optional<Object> unknown = graph.traversal().V().hasLabel(Bdio.Class.Project.name()).values(TT.unknown).tryNext(); assertThat(unknown).isPresent(); assertThat(unknown.get()).isInstanceOf(JsonNode.class); assertThat((JsonNode) unknown.get()).containsPair("http: }