Skip to content

Commit

Permalink
Make getChildren and getNodes return DependencyTree (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Dec 26, 2022
1 parent d99d011 commit f13162a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import org.jfrog.build.api.util.Log;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -112,7 +110,7 @@ public void setMetadata(boolean metadata) {

public void setPrefix(String prefix) {
packagePrefix = prefix.toLowerCase() + "://";
getChildren().forEach(node -> ((DependencyTree)node).setPrefix(prefix));
getChildren().forEach(node -> node.setPrefix(prefix));
}

/**
Expand All @@ -126,15 +124,20 @@ public int getIssueCount() {
/**
* @return Node's children
*/
@SuppressWarnings({"WeakerAccess", "unchecked"})
public Vector<TreeNode> getChildren() {
return children != null ? children : new Vector<>();
@SuppressWarnings({"rawtypes", "UnnecessaryLocalVariable", "unchecked"})
public Vector<DependencyTree> getChildren() {
if (this.children == null) {
return new Vector<>();
}
// Force upcasting from TreeNode to DependencyTree
Vector children = this.children;
return children;
}

@JsonProperty(value = "nodes")
@SuppressWarnings({"unchecked", "unused"})
public List<TreeNode> getNodes() {
return children;
@SuppressWarnings({"unused"})
public List<DependencyTree> getNodes() {
return getChildren();
}

/**
Expand All @@ -148,7 +151,7 @@ public List<TreeNode> getNodes() {
@SuppressWarnings({"WeakerAccess", "unused"})
public Set<Issue> processTreeIssues() {
setIssuesComponent();
getChildren().forEach(child -> issues.addAll(((DependencyTree)child).processTreeIssues()));
getChildren().forEach(child -> issues.addAll(child.processTreeIssues()));
setTopIssue();
sortChildren();
return issues;
Expand All @@ -162,10 +165,7 @@ private void setIssuesComponent() {
}

private void sortChildren() {
Vector children = getChildren();
Vector<DependencyTree> dependencyTrees = (Vector<DependencyTree>) children;

dependencyTrees.sort(Comparator
getChildren().sort(Comparator
.comparing(DependencyTree::getTopIssue, Comparator.comparing(Issue::getSeverity))
.thenComparing(DependencyTree::getIssueCount)
.thenComparing(DependencyTree::getChildCount)
Expand Down Expand Up @@ -193,7 +193,7 @@ private void setTopIssue() {
public Set<License> processTreeViolatedLicenses() {
setViolatedLicensesComponent();
violatedLicenses.addAll(licenses.stream().filter(License::isViolate).collect(Collectors.toSet()));
getChildren().forEach(child -> violatedLicenses.addAll(((DependencyTree)child).processTreeViolatedLicenses()));
getChildren().forEach(child -> violatedLicenses.addAll(child.processTreeViolatedLicenses()));
return violatedLicenses;
}

Expand Down Expand Up @@ -231,7 +231,7 @@ public DependencyTree find(String componentId) {
return this;
}
return getChildren().stream()
.map(child -> ((DependencyTree)child).find(componentId))
.map(child -> child.find(componentId))
.filter(Objects::nonNull)
.findAny()
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.jfrog.build.extractor.scan;

import org.apache.commons.compress.utils.Lists;
import org.apache.commons.compress.utils.Sets;
import org.jfrog.build.api.util.NullLog;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.collections.Lists;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.*;

import static org.testng.Assert.*;
import static org.testng.AssertJUnit.assertEquals;
Expand Down Expand Up @@ -39,6 +36,26 @@ public void init() {
four.add(five); // 4 -> 5
}

@Test
public void testGetChildren() {
// Check root children
List<DependencyTree> rootChildren = root.getChildren();
assertTrue(rootChildren.contains(one));
assertTrue(rootChildren.contains(two));

// Check 'one' children
assertEquals(new ArrayList<>(), one.getChildren());

// Check 'two' children
List<DependencyTree> twoChildren = two.getChildren();
assertTrue(twoChildren.contains(three));
assertTrue(twoChildren.contains(four));

// Check 'four' and 'five' children
assertEquals(Lists.newArrayList(five), four.getChildren());
assertEquals(new ArrayList<>(), five.getChildren());
}

@Test
public void testInit() {
// Sanity test - Check tree with no issues
Expand Down Expand Up @@ -247,4 +264,4 @@ private License createLicense(boolean violating) {
private String generateUID() {
return UUID.randomUUID().toString();
}
}
}

0 comments on commit f13162a

Please sign in to comment.