BlackDuckIoNormalization { public static Builder build() { return new Builder(); } private  BlackDuckIoNormalization(Builder builder); void normalize(List<TraversalStrategy<?>> strategies, Graph graph); void normalize(Graph graph); static Builder build();  }
@Test public void addMissingFileParents() throws IOException { InputStream inputStream = new NamedGraphBuilder() .fileCollection(f -> {}) .file(f -> f.path("file: .relateToFirst(FileCollection.class, FileCollection::base) .file(f -> f.path("file: .file(f -> f.path("file: .file(f -> f.path("file: .build(); BlackDuckIo bdio = graph.io(testBdio()); bdio.readGraph(inputStream, null, null); spi(bdio, graph.traversal().withStrategies(testImplicitConstant())).addMissingFileParents(); GraphTraversalSource g = graph.traversal(); assertThat(g.V().hasLabel(Bdio.Class.File.name()).has(TT.implicit).count().next()) .named("implicit file count").isEqualTo(3); List<Object> leafPaths = g.V() .hasLabel(Bdio.Class.FileCollection.name()) .out(Bdio.ObjectProperty.base.name()) .repeat(in(Bdio.ObjectProperty.parent.name())) .until(not(in(Bdio.ObjectProperty.parent.name()))) .values(Bdio.DataProperty.path.name()) .toList(); assertThat(leafPaths).containsExactly("file: List<Object> rootPaths = g.V() .hasLabel(Bdio.Class.File.name()) .not(inE(Bdio.ObjectProperty.parent.name())) .repeat(out(Bdio.ObjectProperty.parent.name())) .until(not(out(Bdio.ObjectProperty.parent.name()))) .dedup() .values(Bdio.DataProperty.path.name()) .toList(); assertThat(rootPaths).containsExactly("file: }
@Test public void identifyRootProject() throws IOException { InputStream inputStream = new NamedGraphBuilder() .project(p -> {}) .build(); BlackDuckIo bdio = graph.io(testBdio(TT.Metadata, TT.root)); bdio.readGraph(inputStream, null, null); spi(bdio, graph.traversal().withStrategies(testImplicitConstant())).identifyRoot(); GraphTraversalSource g = graph.traversal(); List<Vertex> roots = g.V().hasLabel(TT.Metadata).out(TT.root).toList(); assertThat(roots).hasSize(1); assertThat(roots.get(0).label()).isEqualTo(Bdio.Class.Project.name()); }
@Test public void identifyRootProjectWithEdges() throws IOException { InputStream inputStream = new NamedGraphBuilder() .project(p -> p.name("project")) .project(p -> p.name("subproject")) .relateToFirst(Project.class, Project::subproject) .project(p -> p.name("subsubproject")) .relateToLast(Project.class, Project::subproject) .project(p -> p.name("oldproject")) .relateToFirst(Project.class, Project::previousVersion) .build(); BlackDuckIo bdio = graph.io(testBdio(TT.Metadata, TT.root)); bdio.readGraph(inputStream, null, null); spi(bdio, graph.traversal().withStrategies(testImplicitConstant())).identifyRoot(); GraphTraversalSource g = graph.traversal(); List<Vertex> roots = g.V().hasLabel(TT.Metadata).out(TT.root).toList(); assertThat(roots).hasSize(1); assertThat(roots.get(0).<String> value(Bdio.DataProperty.name.name())).isEqualTo("project"); }
@Test public void addMissingProjectDependencies() throws IOException { InputStream inputStream = new NamedGraphBuilder() .project(p -> {}) .component(c -> c.name("test1")) .component(c -> c.name("test2")) .build(); BlackDuckIo bdio = graph.io(testBdio(TT.Metadata, TT.root)); bdio.readGraph(inputStream, null, null); BlackDuckIoNormalizationSpi spi = spi(bdio, graph.traversal().withStrategies(testImplicitConstant())); spi.identifyRoot(); spi.addMissingProjectDependencies(); GraphTraversalSource g = graph.traversal(); List<String> directDependencyNames = g.V().hasLabel(TT.Metadata) .out(TT.root) .out(Bdio.ObjectProperty.dependency.name()) .out(Bdio.ObjectProperty.dependsOn.name()) .<String> values(Bdio.DataProperty.name.name()) .toList(); assertThat(directDependencyNames).containsExactly("test1", "test2"); }
@Test public void implyFileSystemType() throws IOException { InputStream inputStream = new NamedGraphBuilder() .fileCollection(f -> {}) .file(f -> f.path("file: .relateToFirst(FileCollection.class, FileCollection::base) .file(f -> f.path("file: .file(f -> f.path("file: .file(f -> f.path("file: .file(f -> f.path("file: .file(f -> f.path("zip:file:%2F%2F%2Ffoo%2Fbar%2Ftest.zip#test.txt").encoding("UTF-8")) .build(); BlackDuckIo bdio = graph.io(testBdio(TT.Metadata, TT.root)); bdio.readGraph(inputStream, null, null); BlackDuckIoNormalizationSpi spi = spi(bdio, graph.traversal().withStrategies(testImplicitConstant())); spi.addMissingFileParents(); spi.implyFileSystemTypes(); GraphTraversalSource g = graph.traversal(); List<Vertex> files = g.V().hasLabel(Bdio.Class.File.name()).toList(); for (Vertex file : files) { VertexProperty<String> fileSystemType = file.property(Bdio.DataProperty.fileSystemType.name()); assertThat(fileSystemType.isPresent()).isTrue(); String path = file.value(Bdio.DataProperty.path.name()); if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.DIRECTORY.toString()); } else if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.DIRECTORY.toString()); } else if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.SYMLINK.toString()); } else if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.REGULAR.toString()); } else if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.DIRECTORY_ARCHIVE.toString()); } else if (path.equals("zip:file:%2F%2F%2Ffoo%2Fbar%2Ftest.zip#test.txt")) { assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.REGULAR_TEXT.toString()); } } }
@Test public void implyFileSystemType_noOverwrite() throws IOException { InputStream inputStream = new NamedGraphBuilder() .fileCollection(f -> {}) .file(f -> f.path("file: .relateToFirst(FileCollection.class, FileCollection::base) .file(f -> f.path("file: .file(f -> f.path("file: .file(f -> f.path("file: .file(f -> f.path("file: .file(f -> f.path("zip:file:%2F%2F%2Ffoo%2Fbar%2Ftest.zip#test.txt").encoding("UTF-8").fileSystemType(Bdio.FileSystemType.SYMLINK.toString())) .build(); BlackDuckIo bdio = graph.io(testBdio(TT.Metadata, TT.root)); bdio.readGraph(inputStream, null, null); BlackDuckIoNormalizationSpi spi = spi(bdio, graph.traversal().withStrategies(testImplicitConstant())); spi.addMissingFileParents(); spi.implyFileSystemTypes(); GraphTraversalSource g = graph.traversal(); List<Vertex> files = g.V().hasLabel(Bdio.Class.File.name()).toList(); for (Vertex file : files) { VertexProperty<String> fileSystemType = file.property(Bdio.DataProperty.fileSystemType.name()); assertThat(fileSystemType.isPresent()).isTrue(); String path = file.value(Bdio.DataProperty.path.name()); if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.SYMLINK.toString()); } else if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.DIRECTORY.toString()); } else if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.SYMLINK.toString()); } else if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.SYMLINK.toString()); } else if (path.equals("file: assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.SYMLINK.toString()); } else if (path.equals("zip:file:%2F%2F%2Ffoo%2Fbar%2Ftest.zip#test.txt")) { assertThat(fileSystemType.value()).isEqualTo(Bdio.FileSystemType.SYMLINK.toString()); } } }