Skip to content

Commit

Permalink
Fix onlyOutdated ungrouped component filtering
Browse files Browse the repository at this point in the history
Fixes: #4510

Signed-off-by: Seb Dangerfield <[email protected]>
  • Loading branch information
sedan07 committed Jan 3, 2025
1 parent b70ecc8 commit 40734fd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public PaginatedResult getComponents(final Project project, final boolean includ
" && !("+
" SELECT FROM org.dependencytrack.model.RepositoryMetaComponent m " +
" WHERE m.name == this.name " +
" && m.namespace == this.group " +
" && (m.namespace == this.group || (m.namespace == null && this.group == null)) " +
" && m.latestVersion != this.version " +
" && this.purl.matches('pkg:' + m.repositoryType.toString().toLowerCase() + '/%') " +
" ).isEmpty()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,50 @@ private Project prepareProject() throws MalformedPackageURLException {
return project;
}

/**
* Generate a project with ungrouped dependencies
* @return A project with 10 dependencies: <ul>
* <li>7 outdated dependencies</li>
* <li>3 recent dependencies</li></ul>
* @throws MalformedPackageURLException
*/
private Project prepareProjectUngroupedComponents() throws MalformedPackageURLException {
final Project project = qm.createProject("Ungrouped Application", null, null, null, null, null, true, false);
final List<String> directDepencencies = new ArrayList<>();
// Generate 10 dependencies
for (int i = 0; i < 10; i++) {
Component component = new Component();
component.setProject(project);
component.setName("component-name-"+i);
component.setVersion(String.valueOf(i)+".0");
component.setPurl(new PackageURL(RepositoryType.PYPI.toString(), null, "component-name-"+i , String.valueOf(i)+".0", null, null));

Check warning on line 140 in src/test/java/org/dependencytrack/resources/v1/ComponentResourceTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/test/java/org/dependencytrack/resources/v1/ComponentResourceTest.java#L140

No need to call String.valueOf to append to a string.
component = qm.createComponent(component, false);
// direct depencencies
if (i < 4) {
// 4 direct depencencies, 6 transitive depencencies
directDepencencies.add("{\"uuid\":\"" + component.getUuid() + "\"}");
}
// Recent & Outdated
if ((i < 7)) {
final var metaComponent = new RepositoryMetaComponent();
metaComponent.setRepositoryType(RepositoryType.PYPI);
metaComponent.setName("component-name-"+i);
metaComponent.setLatestVersion(String.valueOf(i+1)+".0");
metaComponent.setLastCheck(new Date());
qm.persist(metaComponent);
} else {
final var metaComponent = new RepositoryMetaComponent();
metaComponent.setRepositoryType(RepositoryType.PYPI);
metaComponent.setName("component-name-"+i);
metaComponent.setLatestVersion(String.valueOf(i)+".0");
metaComponent.setLastCheck(new Date());
qm.persist(metaComponent);
}
}
project.setDirectDependencies("[" + String.join(",", directDepencencies.toArray(new String[0])) + "]");
return project;
}

@Test
public void getOutdatedComponentsTest() throws MalformedPackageURLException {
final Project project = prepareProject();
Expand All @@ -138,6 +182,23 @@ public void getOutdatedComponentsTest() throws MalformedPackageURLException {
assertThat(json).hasSize(100); // Default page size is 100
}

@Test
public void getUngroupedOutdatedComponentsTest() throws MalformedPackageURLException {
final Project project = prepareProjectUngroupedComponents();

final Response response = jersey.target(V1_COMPONENT + "/project/" + project.getUuid())
.queryParam("onlyOutdated", true)
.queryParam("onlyDirect", false)
.request()
.header(X_API_KEY, apiKey)
.get(Response.class);
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
assertThat(response.getHeaderString(TOTAL_COUNT_HEADER)).isEqualTo("7"); // 7 outdated dependencies, direct and transitive

final JsonArray json = parseJsonArray(response);
assertThat(json).hasSize(7);
}

@Test
public void getOutdatedDirectComponentsTest() throws MalformedPackageURLException {
final Project project = prepareProject();
Expand All @@ -155,6 +216,23 @@ public void getOutdatedDirectComponentsTest() throws MalformedPackageURLExceptio
assertThat(json).hasSize(75);
}

@Test
public void getUngroupedOutdatedDirectComponentsTest() throws MalformedPackageURLException {
final Project project = prepareProjectUngroupedComponents();

final Response response = jersey.target(V1_COMPONENT + "/project/" + project.getUuid())
.queryParam("onlyOutdated", true)
.queryParam("onlyDirect", true)
.request()
.header(X_API_KEY, apiKey)
.get(Response.class);
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
assertThat(response.getHeaderString(TOTAL_COUNT_HEADER)).isEqualTo("3"); // 3 outdated dependencies

final JsonArray json = parseJsonArray(response);
assertThat(json).hasSize(3);
}

@Test
public void getAllComponentsTest() throws MalformedPackageURLException {
final Project project = prepareProject();
Expand Down

0 comments on commit 40734fd

Please sign in to comment.