diff --git a/src/main/java/org/dependencytrack/persistence/ComponentQueryManager.java b/src/main/java/org/dependencytrack/persistence/ComponentQueryManager.java index e825afad7..4e46fe075 100644 --- a/src/main/java/org/dependencytrack/persistence/ComponentQueryManager.java +++ b/src/main/java/org/dependencytrack/persistence/ComponentQueryManager.java @@ -158,6 +158,7 @@ public List getComponents(final Project project, final boolean includ * @return a List of Dependency objects */ public List getComponents(final Project project, final boolean includeMetrics, final boolean onlyOutdated, final boolean onlyDirect) { + List componentsResult = new ArrayList<>(); String queryString = """ SELECT DISTINCT 'org.dependencytrack.model.Component' AS "DN_TYPE", "A0"."AUTHOR", @@ -242,27 +243,33 @@ AND NOT (NOT EXISTS ( FROM "REPOSITORY_META_COMPONENT" "M" WHERE "M"."NAME" = "A0"."NAME" AND "M"."NAMESPACE" = "A0"."GROUP" AND "M"."LATEST_VERSION" <> "A0"."VERSION" - AND "A0"."PURL" LIKE (('pkg:' || LOWER("M"."REPOSITORY_TYPE")) || '/%') ESCAPE E'\\')) + AND "A0"."PURL" LIKE (('pkg:' || LOWER("M"."REPOSITORY_TYPE")) || '/%') ESCAPE E'\\\\')) """; } if (onlyDirect) { queryString += """ - AND "B0"."DIRECT_DEPENDENCIES" LIKE (('%"uuid":"' || "A0"."UUID") || '"%') ESCAPE E'\\' + AND "B0"."DIRECT_DEPENDENCIES" LIKE (('%' || "A0"."UUID") || '%') ESCAPE E'\\\\' """; } if (orderBy == null) { queryString += """ ORDER BY "NUCORDER0", - "NUCORDER1" DESC; + "NUCORDER1" DESC FETCH NEXT 100 ROWS ONLY; """; } final Query query = pm.newQuery(Query.SQL, queryString); List resultSet; try { + if (pagination != null && pagination.isPaginated()) { + final long begin = pagination.getOffset(); + final long end = begin + pagination.getLimit(); + query.setRange(begin, end); + } query.setParameters(project.getId()); resultSet = List.copyOf(query.executeResultList(ComponentProjection.class)); + System.out.println(resultSet.size()); } finally { query.closeAll(); @@ -282,8 +289,9 @@ AND NOT (NOT EXISTS ( } } } + componentsResult.add(component); } - return null; + return componentsResult; } /** diff --git a/src/test/java/org/dependencytrack/persistence/ComponentQueryManangerPostgresTest.java b/src/test/java/org/dependencytrack/persistence/ComponentQueryManangerPostgresTest.java index 6a93a330a..6408d94e6 100644 --- a/src/test/java/org/dependencytrack/persistence/ComponentQueryManangerPostgresTest.java +++ b/src/test/java/org/dependencytrack/persistence/ComponentQueryManangerPostgresTest.java @@ -13,14 +13,40 @@ import java.util.Date; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; + public class ComponentQueryManangerPostgresTest extends AbstractPostgresEnabledTest { @Test - public void testGetComponents() throws MalformedPackageURLException { + public void testGetAllComponents() throws MalformedPackageURLException { + + final Project project = prepareProject(); + var components = qm.getComponents(project, false, false, false); + assertThat(components.size()).isEqualTo(100); + } + + @Test + public void testGetOutdatedComponents() throws MalformedPackageURLException { final Project project = prepareProject(); - List components = qm.getComponents(project, false, true, true); + var components = qm.getComponents(project, false, true, false); + assertThat(components.size()).isEqualTo(100); + } + @Test + public void testGetDirectComponents() throws MalformedPackageURLException { + + final Project project = prepareProject(); + var components = qm.getComponents(project, false, false, true); + assertThat(components.size()).isEqualTo(100); + } + + @Test + public void testGetOutdatedDirectComponents() throws MalformedPackageURLException { + + final Project project = prepareProject(); + var components = qm.getComponents(project, false, true, true); + assertThat(components.size()).isEqualTo(75); } private Project prepareProject() throws MalformedPackageURLException {