Skip to content

Commit

Permalink
Fix failing tests due to new UNIQUE constraints
Browse files Browse the repository at this point in the history
Tests made bad assumptions about duplicate projects being allowed.

Also, remove `JdbiTestUtil` which was only necessary to run JDBI tests with H2. Since we dropped H2, this is no longer required.

Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Feb 29, 2024
1 parent a2dc4ab commit e8b8158
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 111 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,147 +18,154 @@

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<Project> getProjects(@DefineOrdering(allowedColumns = "nameAlias") Ordering ordering);
@SqlQuery("SELECT \"ID\" AS \"id\", \"NAME\" AS \"nameAlias\" FROM \"COMPONENT\" ${ordering!}")
List<Component> getComponents(@DefineOrdering(allowedColumns = "nameAlias") Ordering ordering);

@SqlQuery("SELECT \"ID\" AS \"id\", \"NAME\" AS \"nameAlias\" FROM \"PROJECT\" ${ordering!}")
List<Project> getProjectsWithOrderingAlsoById(@DefineOrdering(allowedColumns = {"id", "nameAlias"}, alsoBy = "id DESC") Ordering ordering);
@SqlQuery("SELECT \"ID\" AS \"id\", \"NAME\" AS \"nameAlias\" FROM \"COMPONENT\" ${ordering!}")
List<Component> getComponentsWithOrderingAlsoById(@DefineOrdering(allowedColumns = {"id", "nameAlias"}, alsoBy = "id DESC") Ordering ordering);

}

private Jdbi jdbi;
private final Map<String, Long> projectIdsByName = new HashMap<>();
private Project project;
private final Map<String, Long> 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<Project> 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<Component> 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"
);
}

@Test
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<Project> 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<Component> 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<Project> 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<Component> 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<Project> 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<Component> 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<String, Long>();
final var duplicateComponentIdsByName = new HashMap<String, Long>();
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<Project> 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<Component> 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");
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -28,7 +28,7 @@ public interface TestDao {

@Before
public void setUp() {
jdbi = createLocalVanillaJdbi(qm)
jdbi = jdbi(qm)
.installPlugin(new SqlObjectPlugin())
.setTemplateEngine(FreemarkerEngine.instance());

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

Expand Down Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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/[email protected]");
RepositoryMetaComponent r1 = new RepositoryMetaComponent();
Expand Down

0 comments on commit e8b8158

Please sign in to comment.