diff --git a/src/test/java/org/dependencytrack/persistence/jdbi/JdbiTestUtil.java b/src/test/java/org/dependencytrack/persistence/jdbi/JdbiTestUtil.java deleted file mode 100644 index 1c80d9001..000000000 --- a/src/test/java/org/dependencytrack/persistence/jdbi/JdbiTestUtil.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.dependencytrack.persistence.jdbi; - -import org.dependencytrack.persistence.QueryManager; -import org.jdbi.v3.core.Jdbi; - -public final class JdbiTestUtil { - - private JdbiTestUtil() { - } - - /** - * Create a {@link Jdbi} instance from a {@link QueryManager}, without any of - * the plugins and extensions registered by {@link JdbiFactory}. - * - * @param qm The {@link QueryManager} to use - * @return A new {@link Jdbi} instance - */ - public static Jdbi createLocalVanillaJdbi(final QueryManager qm) { - return Jdbi.create(new JdoConnectionFactory(qm.getPersistenceManager())); - } - -} diff --git a/src/test/java/org/dependencytrack/persistence/jdbi/binding/DefineOrderingTest.java b/src/test/java/org/dependencytrack/persistence/jdbi/binding/DefineOrderingTest.java index 88f45f099..8a2ab920e 100644 --- a/src/test/java/org/dependencytrack/persistence/jdbi/binding/DefineOrderingTest.java +++ b/src/test/java/org/dependencytrack/persistence/jdbi/binding/DefineOrderingTest.java @@ -2,6 +2,7 @@ import alpine.persistence.OrderDirection; import org.dependencytrack.PersistenceCapableTest; +import org.dependencytrack.model.Component; import org.dependencytrack.model.Project; import org.dependencytrack.persistence.Ordering; import org.jdbi.v3.core.Jdbi; @@ -17,53 +18,59 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.dependencytrack.persistence.jdbi.JdbiTestUtil.createLocalVanillaJdbi; +import static org.dependencytrack.persistence.jdbi.JdbiFactory.jdbi; public class DefineOrderingTest extends PersistenceCapableTest { public interface TestDao { - @SqlQuery("SELECT \"ID\" AS \"id\", \"NAME\" AS \"nameAlias\" FROM \"PROJECT\" ${ordering!}") - List getProjects(@DefineOrdering(allowedColumns = "nameAlias") Ordering ordering); + @SqlQuery("SELECT \"ID\" AS \"id\", \"NAME\" AS \"nameAlias\" FROM \"COMPONENT\" ${ordering!}") + List getComponents(@DefineOrdering(allowedColumns = "nameAlias") Ordering ordering); - @SqlQuery("SELECT \"ID\" AS \"id\", \"NAME\" AS \"nameAlias\" FROM \"PROJECT\" ${ordering!}") - List getProjectsWithOrderingAlsoById(@DefineOrdering(allowedColumns = {"id", "nameAlias"}, alsoBy = "id DESC") Ordering ordering); + @SqlQuery("SELECT \"ID\" AS \"id\", \"NAME\" AS \"nameAlias\" FROM \"COMPONENT\" ${ordering!}") + List getComponentsWithOrderingAlsoById(@DefineOrdering(allowedColumns = {"id", "nameAlias"}, alsoBy = "id DESC") Ordering ordering); } private Jdbi jdbi; - private final Map projectIdsByName = new HashMap<>(); + private Project project; + private final Map componentIdsByName = new HashMap<>(); @Before public void setUp() { - jdbi = createLocalVanillaJdbi(qm) + jdbi = jdbi(qm) .installPlugin(new SqlObjectPlugin()) .setTemplateEngine(FreemarkerEngine.instance()) - .registerRowMapper(Project.class, (rs, ctx) -> { - final var project = new Project(); - project.setId(rs.getLong("id")); - project.setName(rs.getString("nameAlias")); - return project; + .registerRowMapper(Component.class, (rs, ctx) -> { + final var component = new Component(); + component.setId(rs.getLong("id")); + component.setName(rs.getString("nameAlias")); + return component; }); + project = new Project(); + project.setName("project"); + qm.persist(project); + for (int i = 0; i < 5; i++) { - final var project = new Project(); - project.setName("project-" + i); - qm.persist(project); + final var component = new Component(); + component.setProject(project); + component.setName("component-" + i); + qm.persist(component); - projectIdsByName.put(project.getName(), project.getId()); + componentIdsByName.put(component.getName(), component.getId()); } } @Test public void testWithNullOrdering() { - final List projects = jdbi.withExtension(TestDao.class, dao -> dao.getProjects(null)); - assertThat(projects).extracting(Project::getName).containsExactlyInAnyOrder( - "project-0", - "project-1", - "project-2", - "project-3", - "project-4" + final List components = jdbi.withExtension(TestDao.class, dao -> dao.getComponents(null)); + assertThat(components).extracting(Component::getName).containsExactlyInAnyOrder( + "component-0", + "component-1", + "component-2", + "component-3", + "component-4" ); } @@ -71,93 +78,94 @@ public void testWithNullOrdering() { public void testWithDisallowedColumn() { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> jdbi.useExtension(TestDao.class, - dao -> dao.getProjects(new Ordering("NAME", OrderDirection.ASCENDING)))); + dao -> dao.getComponents(new Ordering("NAME", OrderDirection.ASCENDING)))); } @Test public void testWithOrderDirectionAscending() { - final List projects = jdbi.withExtension(TestDao.class, - dao -> dao.getProjects(new Ordering("nameAlias", OrderDirection.ASCENDING))); - - assertThat(projects).extracting(Project::getName).containsExactly( - "project-0", - "project-1", - "project-2", - "project-3", - "project-4" + final List components = jdbi.withExtension(TestDao.class, + dao -> dao.getComponents(new Ordering("nameAlias", OrderDirection.ASCENDING))); + + assertThat(components).extracting(Component::getName).containsExactly( + "component-0", + "component-1", + "component-2", + "component-3", + "component-4" ); } @Test public void testWithOrderDirectionDescending() { - final List projects = jdbi.withExtension(TestDao.class, - dao -> dao.getProjects(new Ordering("nameAlias", OrderDirection.DESCENDING))); - - assertThat(projects).extracting(Project::getName).containsExactly( - "project-4", - "project-3", - "project-2", - "project-1", - "project-0" + final List components = jdbi.withExtension(TestDao.class, + dao -> dao.getComponents(new Ordering("nameAlias", OrderDirection.DESCENDING))); + + assertThat(components).extracting(Component::getName).containsExactly( + "component-4", + "component-3", + "component-2", + "component-1", + "component-0" ); } @Test public void testWithOrderDirectionUnspecified() { - final List projects = jdbi.withExtension(TestDao.class, - dao -> dao.getProjects(new Ordering("nameAlias", OrderDirection.UNSPECIFIED))); - - assertThat(projects).extracting(Project::getName).containsExactly( - "project-0", - "project-1", - "project-2", - "project-3", - "project-4" + final List components = jdbi.withExtension(TestDao.class, + dao -> dao.getComponents(new Ordering("nameAlias", OrderDirection.UNSPECIFIED))); + + assertThat(components).extracting(Component::getName).containsExactly( + "component-0", + "component-1", + "component-2", + "component-3", + "component-4" ); } @Test public void testWithOrderingAlsoBy() { - final var duplicateProjectIdsByName = new HashMap(); + final var duplicateComponentIdsByName = new HashMap(); for (int i = 0; i < 2; i++) { - final var project = new Project(); - project.setName("project-" + i); - qm.persist(project); + final var component = new Component(); + component.setProject(project); + component.setName("component-" + i); + qm.persist(component); - duplicateProjectIdsByName.put(project.getName(), project.getId()); + duplicateComponentIdsByName.put(component.getName(), component.getId()); } - assertThat(qm.getCount(Project.class)).isEqualTo(7); - - final List projects = jdbi.withExtension(TestDao.class, - dao -> dao.getProjectsWithOrderingAlsoById(new Ordering("nameAlias", OrderDirection.ASCENDING))); - assertThat(projects).satisfiesExactly( - project -> { - assertThat(project.getId()).isEqualTo(duplicateProjectIdsByName.get("project-0")); - assertThat(project.getName()).isEqualTo("project-0"); + assertThat(qm.getCount(Component.class)).isEqualTo(7); + + final List components = jdbi.withExtension(TestDao.class, + dao -> dao.getComponentsWithOrderingAlsoById(new Ordering("nameAlias", OrderDirection.ASCENDING))); + assertThat(components).satisfiesExactly( + component -> { + assertThat(component.getId()).isEqualTo(duplicateComponentIdsByName.get("component-0")); + assertThat(component.getName()).isEqualTo("component-0"); }, - project -> { - assertThat(project.getId()).isEqualTo(projectIdsByName.get("project-0")); - assertThat(project.getName()).isEqualTo("project-0"); + component -> { + assertThat(component.getId()).isEqualTo(componentIdsByName.get("component-0")); + assertThat(component.getName()).isEqualTo("component-0"); }, - project -> { - assertThat(project.getId()).isEqualTo(duplicateProjectIdsByName.get("project-1")); - assertThat(project.getName()).isEqualTo("project-1"); + component -> { + assertThat(component.getId()).isEqualTo(duplicateComponentIdsByName.get("component-1")); + assertThat(component.getName()).isEqualTo("component-1"); }, - project -> { - assertThat(project.getId()).isEqualTo(projectIdsByName.get("project-1")); - assertThat(project.getName()).isEqualTo("project-1"); + component -> { + assertThat(component.getId()).isEqualTo(componentIdsByName.get("component-1")); + assertThat(component.getName()).isEqualTo("component-1"); }, - project -> { - assertThat(project.getId()).isEqualTo(projectIdsByName.get("project-2")); - assertThat(project.getName()).isEqualTo("project-2"); + component -> { + assertThat(component.getId()).isEqualTo(componentIdsByName.get("component-2")); + assertThat(component.getName()).isEqualTo("component-2"); }, - project -> { - assertThat(project.getId()).isEqualTo(projectIdsByName.get("project-3")); - assertThat(project.getName()).isEqualTo("project-3"); + component -> { + assertThat(component.getId()).isEqualTo(componentIdsByName.get("component-3")); + assertThat(component.getName()).isEqualTo("component-3"); }, - project -> { - assertThat(project.getId()).isEqualTo(projectIdsByName.get("project-4")); - assertThat(project.getName()).isEqualTo("project-4"); + component -> { + assertThat(component.getId()).isEqualTo(componentIdsByName.get("component-4")); + assertThat(component.getName()).isEqualTo("component-4"); } ); } diff --git a/src/test/java/org/dependencytrack/persistence/jdbi/binding/DefinePaginationTest.java b/src/test/java/org/dependencytrack/persistence/jdbi/binding/DefinePaginationTest.java index 1b0df0cf0..0e13eb5e1 100644 --- a/src/test/java/org/dependencytrack/persistence/jdbi/binding/DefinePaginationTest.java +++ b/src/test/java/org/dependencytrack/persistence/jdbi/binding/DefinePaginationTest.java @@ -13,7 +13,7 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -import static org.dependencytrack.persistence.jdbi.JdbiTestUtil.createLocalVanillaJdbi; +import static org.dependencytrack.persistence.jdbi.JdbiFactory.jdbi; public class DefinePaginationTest extends PersistenceCapableTest { @@ -28,7 +28,7 @@ public interface TestDao { @Before public void setUp() { - jdbi = createLocalVanillaJdbi(qm) + jdbi = jdbi(qm) .installPlugin(new SqlObjectPlugin()) .setTemplateEngine(FreemarkerEngine.instance()); diff --git a/src/test/java/org/dependencytrack/persistence/jdbi/mapping/PaginatedResultRowReducerTest.java b/src/test/java/org/dependencytrack/persistence/jdbi/mapping/PaginatedResultRowReducerTest.java index 987c2f942..2e0ec60b1 100644 --- a/src/test/java/org/dependencytrack/persistence/jdbi/mapping/PaginatedResultRowReducerTest.java +++ b/src/test/java/org/dependencytrack/persistence/jdbi/mapping/PaginatedResultRowReducerTest.java @@ -15,7 +15,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.dependencytrack.persistence.jdbi.JdbiTestUtil.createLocalVanillaJdbi; +import static org.dependencytrack.persistence.jdbi.JdbiFactory.jdbi; public class PaginatedResultRowReducerTest extends PersistenceCapableTest { @@ -48,7 +48,7 @@ public interface TestDao { @Before public void setUp() { - jdbi = createLocalVanillaJdbi(qm) + jdbi = jdbi(qm) .installPlugin(new SqlObjectPlugin()) .setTemplateEngine(FreemarkerEngine.instance()) .registerRowMapper(String.class, (rs, ctx) -> rs.getString("NAME")); diff --git a/src/test/java/org/dependencytrack/resources/v1/FindingResourceTest.java b/src/test/java/org/dependencytrack/resources/v1/FindingResourceTest.java index 6a683a63f..bfee0c95b 100644 --- a/src/test/java/org/dependencytrack/resources/v1/FindingResourceTest.java +++ b/src/test/java/org/dependencytrack/resources/v1/FindingResourceTest.java @@ -72,7 +72,7 @@ public void setUp() throws Exception { @Test public void getFindingsByProjectTest() { Project p1 = qm.createProject("Acme Example", null, "1.0", null, null, null, true, false); - Project p2 = qm.createProject("Acme Example", null, "1.0", null, null, null, true, false); + Project p2 = qm.createProject("Acme Example", null, "2.0", null, null, null, true, false); Component c1 = createComponent(p1, "Component A", "1.0"); Component c2 = createComponent(p1, "Component B", "1.0"); Component c3 = createComponent(p1, "Component C", "1.0"); @@ -151,7 +151,7 @@ public void getFindingsByProjectInvalidTest() { @Test public void exportFindingsByProjectTest() { Project p1 = qm.createProject("Acme Example", null, "1.0", null, null, null, true, false); - Project p2 = qm.createProject("Acme Example", null, "1.0", null, null, null, true, false); + Project p2 = qm.createProject("Acme Example", null, "2.0", null, null, null, true, false); Component c1 = createComponent(p1, "Component A", "1.0"); Component c2 = createComponent(p1, "Component B", "1.0"); Component c3 = createComponent(p1, "Component C", "1.0"); @@ -238,7 +238,7 @@ public void exportFindingsByProjectInvalidTest() { @Test public void getFindingsByProjectWithComponentLatestVersionTest() { Project p1 = qm.createProject("Acme Example", null, "1.0", null, null, null, true, false); - Project p2 = qm.createProject("Acme Example", null, "1.0", null, null, null, true, false); + Project p2 = qm.createProject("Acme Example", null, "2.0", null, null, null, true, false); Component c1 = createComponent(p1, "Component A", "1.0"); c1.setPurl("pkg:/maven/org.acme/component-a@1.0.0"); RepositoryMetaComponent r1 = new RepositoryMetaComponent();