Skip to content

Commit

Permalink
added test for ComponentQueryManager with postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
sahibamittal committed Nov 14, 2023
1 parent 48a23e6 commit a0f8c01
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public List<Component> getComponents(final Project project, final boolean includ
* @return a List of Dependency objects
*/
public List<Component> getComponents(final Project project, final boolean includeMetrics, final boolean onlyOutdated, final boolean onlyDirect) {
List<Component> componentsResult = new ArrayList<>();
String queryString = """
SELECT DISTINCT 'org.dependencytrack.model.Component' AS "DN_TYPE",
"A0"."AUTHOR",
Expand Down Expand Up @@ -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<ComponentProjection> 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();
Expand All @@ -282,8 +289,9 @@ AND NOT (NOT EXISTS (
}
}
}
componentsResult.add(component);
}
return null;
return componentsResult;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component> 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 {
Expand Down

0 comments on commit a0f8c01

Please sign in to comment.