import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
+import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.System2;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
+import org.sonar.db.audit.AuditPersister;
+import org.sonar.db.audit.model.ComponentNewValue;
import org.sonar.db.project.ProjectDto;
import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.emptyList;
import static java.util.Collections.singleton;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
public class PortfolioDao implements Dao {
private final System2 system2;
private final UuidFactory uuidFactory;
+ private final AuditPersister auditPersister;
- public PortfolioDao(System2 system2, UuidFactory uuidFactory) {
- // TODO: Audits missing
+ public PortfolioDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) {
this.system2 = system2;
this.uuidFactory = uuidFactory;
+ this.auditPersister = auditPersister;
}
public List<PortfolioDto> selectAllRoots(DbSession dbSession) {
return mapper(dbSession).selectAll();
}
+ public List<PortfolioDto> selectTree(DbSession dbSession, String portfolioUuid) {
+ return mapper(dbSession).selectTree(portfolioUuid);
+ }
+
public Optional<PortfolioDto> selectByKey(DbSession dbSession, String key) {
return Optional.ofNullable(mapper(dbSession).selectByKey(key));
}
return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid));
}
+ public List<PortfolioDto> selectByUuids(DbSession dbSession, Set<String> uuids) {
+ if (uuids.isEmpty()) {
+ return emptyList();
+ }
+ return mapper(dbSession).selectByUuids(uuids);
+ }
+
public void insert(DbSession dbSession, PortfolioDto portfolio) {
checkArgument(portfolio.isRoot() == (portfolio.getUuid().equals(portfolio.getRootUuid())));
mapper(dbSession).insert(portfolio);
+ auditPersister.addComponent(dbSession, toComponentNewValue(portfolio));
}
- public void delete(DbSession dbSession, String portfolioUuid) {
- mapper(dbSession).deletePortfoliosByUuids(singleton(portfolioUuid));
- mapper(dbSession).deleteReferencesByPortfolioOrReferenceUuids(singleton(portfolioUuid));
- mapper(dbSession).deleteProjectsByPortfolioUuids(singleton(portfolioUuid));
- }
-
- public void deleteByUuids(DbSession dbSession, Set<String> portfolioUuids) {
- if (portfolioUuids.isEmpty()) {
- return;
- }
- mapper(dbSession).deleteByUuids(portfolioUuids);
+ public void delete(DbSession dbSession, PortfolioDto portfolio) {
+ mapper(dbSession).deletePortfoliosByUuids(singleton(portfolio.getUuid()));
+ mapper(dbSession).deleteReferencesByPortfolioOrReferenceUuids(singleton(portfolio.getUuid()));
+ mapper(dbSession).deleteProjectsByPortfolioUuids(singleton(portfolio.getUuid()));
+ auditPersister.deleteComponent(dbSession, toComponentNewValue(portfolio));
}
- public List<PortfolioDto> selectTree(DbSession dbSession, String portfolioUuid) {
- return mapper(dbSession).selectTree(portfolioUuid);
+ public void deleteAllDescendantPortfolios(DbSession dbSession, String rootUuid) {
+ // not audited but it's part of DefineWs
+ mapper(dbSession).deleteAllDescendantPortfolios(rootUuid);
}
public void update(DbSession dbSession, PortfolioDto portfolio) {
checkArgument(portfolio.isRoot() == (portfolio.getUuid().equals(portfolio.getRootUuid())));
portfolio.setUpdatedAt(system2.now());
mapper(dbSession).update(portfolio);
+ auditPersister.updateComponent(dbSession, toComponentNewValue(portfolio));
+ }
+
+ private static ComponentNewValue toComponentNewValue(PortfolioDto portfolio) {
+ return new ComponentNewValue(portfolio.getUuid(), portfolio.getName(), portfolio.getKey(), portfolio.isPrivate(),
+ portfolio.getDescription(), qualifier(portfolio));
+ }
+
+ private static String qualifier(PortfolioDto portfolioDto) {
+ return portfolioDto.isRoot() ? Qualifiers.VIEW : Qualifiers.SUBVIEW;
}
public Map<String, String> selectKeysByUuids(DbSession dbSession, Collection<String> uuids) {
.collect(Collectors.toMap(PortfolioDto::getUuid, PortfolioDto::getKey));
}
- public void deleteAllDescendantPortfolios(DbSession dbSession, String rootUuid) {
- mapper(dbSession).deleteAllDescendantPortfolios(rootUuid);
- }
-
public void addReference(DbSession dbSession, String portfolioUuid, String referenceUuid) {
mapper(dbSession).insertReference(new PortfolioReferenceDto()
.setUuid(uuidFactory.create())
return mapper(dbSession).selectAllReferencesToApplications();
}
- public Set<String> getReferences(DbSession dbSession, String portfolioUuid) {
- return mapper(dbSession).selectReferences(portfolioUuid);
+ public Set<String> selectReferenceUuids(DbSession dbSession, String portfolioUuid) {
+ return mapper(dbSession).selectReferenceUuids(portfolioUuid);
}
- public List<PortfolioDto> selectReferencersByKey(DbSession dbSession, String referenceKey) {
- return mapper(dbSession).selectReferencersByKey(referenceKey);
+ public List<ReferenceDto> selectAllReferencesInHierarchy(DbSession dbSession, String uuid) {
+ return mapper(dbSession).selectAllReferencesInHierarchy(uuid);
+ }
+
+ public List<PortfolioDto> selectReferencers(DbSession dbSession, String referenceUuid) {
+ return mapper(dbSession).selectReferencers(referenceUuid);
}
public List<PortfolioDto> selectRootOfReferencers(DbSession dbSession, String referenceUuid) {
return mapper(dbSession).deleteReference(portfolioUuid, referenceUuid);
}
- public ReferenceDto selectReference(DbSession dbSession, String portfolioUuid, String referenceKey) {
+ public ReferenceDetailsDto selectReference(DbSession dbSession, String portfolioUuid, String referenceKey) {
return mapper(dbSession).selectReference(portfolioUuid, referenceKey);
}
- public List<ProjectDto> getProjects(DbSession dbSession, String portfolioUuid) {
+ public List<ProjectDto> selectProjects(DbSession dbSession, String portfolioUuid) {
return mapper(dbSession).selectProjects(portfolioUuid);
}
- public List<PortfolioProjectDto> getAllProjectsInHierarchy(DbSession dbSession, String rootUuid) {
+ public List<PortfolioProjectDto> selectAllProjectsInHierarchy(DbSession dbSession, String rootUuid) {
return mapper(dbSession).selectAllProjectsInHierarchy(rootUuid);
}
+ public List<PortfolioProjectDto> selectAllPortfolioProjects(DbSession dbSession) {
+ return mapper(dbSession).selectAllPortfolioProjects();
+ }
+
public void addProject(DbSession dbSession, String portfolioUuid, String projectUuid) {
mapper(dbSession).insertProject(new PortfolioProjectDto()
.setUuid(uuidFactory.create())
mapper(dbSession).deleteAllProjects();
}
- public List<PortfolioProjectDto> selectAllProjectsOfPortfolios(DbSession dbSession) {
- return mapper(dbSession).selectAllProjectsOfPortfolios();
- }
-
- public List<ReferenceDto> selectAllReferencesInHierarchy(DbSession dbSession, String uuid) {
- return mapper(dbSession).selectAllReferencesInHierarchy(uuid);
- }
-
- public List<PortfolioDto> selectByUuids(DbSession dbSession, Set<String> uuidsToRefresh) {
- return mapper(dbSession).selectByUuids(uuidsToRefresh);
- }
-
private static PortfolioMapper mapper(DbSession session) {
return session.getMapper(PortfolioMapper.class);
}
package org.sonar.db.portfolio;
import java.util.Set;
-import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
import org.sonar.core.util.UuidFactoryFast;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.audit.AuditPersister;
import org.sonar.db.project.ProjectDto;
+import static java.util.Collections.emptySet;
+import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
public class PortfolioDaoTest {
private final System2 system2 = new AlwaysIncreasingSystem2(1L, 1);
@Rule
public DbTester db = DbTester.create(system2);
-
- private final PortfolioDao portfolioDao = new PortfolioDao(system2, UuidFactoryFast.getInstance());
+ private final AuditPersister audit = mock(AuditPersister.class);
+ private final PortfolioDao portfolioDao = new PortfolioDao(system2, UuidFactoryFast.getInstance(), audit);
private final DbSession session = db.getSession();
- @Test
- public void selectByKey_returns_empty_if_no_match() {
- assertThat(portfolioDao.selectByKey(session, "nonexisting")).isEmpty();
- }
-
@Test
public void selectAllRoots() {
- createPortfolio("p1", null, "p1");
- createPortfolio("p11", "p1", "p1");
- createPortfolio("p111", "p11", "p1");
- createPortfolio("p2", null, "p2");
+ PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
+ PortfolioDto p11 = addPortfolio(p1, "p11");
+ PortfolioDto p111 = addPortfolio(p11, "p111");
+ PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
assertThat(portfolioDao.selectAllRoots(session)).extracting("uuid")
.containsExactlyInAnyOrder("p1", "p2");
@Test
public void selectAll() {
- createPortfolio("p1", null, "p1");
- createPortfolio("p11", "p1", "p1");
- createPortfolio("p111", "p11", "p1");
- createPortfolio("p2", null, "p2");
+ PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
+ PortfolioDto p11 = addPortfolio(p1, "p11");
+ PortfolioDto p111 = addPortfolio(p11, "p111");
+ PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
assertThat(portfolioDao.selectAll(session)).extracting("uuid")
.containsExactlyInAnyOrder("p1", "p2", "p11", "p111");
}
+ @Test
+ public void selectTree() {
+ PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
+ PortfolioDto p11 = addPortfolio(p1, "p11");
+ PortfolioDto p111 = addPortfolio(p11, "p111");
+ PortfolioDto p12 = addPortfolio(p1, "p12");
+ PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
+
+ assertThat(portfolioDao.selectTree(session, "p1")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
+ assertThat(portfolioDao.selectTree(session, "p11")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
+ assertThat(portfolioDao.selectTree(session, "p111")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
+ assertThat(portfolioDao.selectTree(session, "p2")).extracting("uuid").containsOnly("p2");
+ }
+
+ @Test
+ public void selectByKey_returns_empty_if_no_match() {
+ assertThat(portfolioDao.selectByKey(session, "nonexisting")).isEmpty();
+ }
+
@Test
public void selectByKey_returns_match() {
PortfolioDto portfolio = db.components().insertPrivatePortfolioDto();
.containsExactly("NAME_name", "KEY_name", "name", "DESCRIPTION_name", "name", null, "NONE", null);
}
+ @Test
+ public void selectByUuids_returns_empty_if_no_match() {
+ db.components().insertPrivatePortfolioDto("name1");
+ assertThat(portfolioDao.selectByUuids(session, Set.of("name2"))).isEmpty();
+ }
+
+ @Test
+ public void selectByUuids_returns_empty_if_uuids_is_empty() {
+ db.components().insertPrivatePortfolioDto("name1");
+ assertThat(portfolioDao.selectByUuids(session, emptySet())).isEmpty();
+ }
+
+ @Test
+ public void selectByUuids_returns_matches() {
+ db.components().insertPrivatePortfolioDto("name1");
+ db.components().insertPrivatePortfolioDto("name2");
+ db.components().insertPrivatePortfolioDto("name3");
+
+ assertThat(portfolioDao.selectByUuids(session, Set.of("name1", "name2")))
+ .extracting("name", "key", "uuid", "description", "rootUuid", "parentUuid", "selectionMode", "selectionExpression")
+ .containsOnly(
+ tuple("NAME_name1", "KEY_name1", "name1", "DESCRIPTION_name1", "name1", null, "NONE", null),
+ tuple("NAME_name2", "KEY_name2", "name2", "DESCRIPTION_name2", "name2", null, "NONE", null));
+ }
+
@Test
public void insert_fails_if_root_is_inconsistent_with_parent() {
PortfolioDto portfolio = new PortfolioDto()
}
@Test
- public void update_portfolio() {
- db.components().insertPrivatePortfolioDto("name");
- PortfolioDto dto = portfolioDao.selectByUuid(session, "name").get();
- dto.setSelectionMode("newMode");
- dto.setSelectionExpression("newExp");
- dto.setDescription("newDesc");
- dto.setName("newName");
- dto.setRootUuid("root");
- dto.setParentUuid("parent");
- portfolioDao.update(session, dto);
-
- assertThat(portfolioDao.selectByUuid(session, "name").get())
- .extracting("name", "key", "uuid", "description", "private", "rootUuid", "parentUuid", "selectionMode", "selectionExpression")
- .containsExactly("newName", "KEY_name", "name", "newDesc", true, "root", "parent", "newMode", "newExp");
- }
-
- @Test
- public void selectTree() {
- PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
- PortfolioDto p11 = addPortfolio(p1, "p11");
- PortfolioDto p111 = addPortfolio(p11, "p111");
- PortfolioDto p12 = addPortfolio(p1, "p12");
- PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
-
- assertThat(portfolioDao.selectTree(session, "p1")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
- assertThat(portfolioDao.selectTree(session, "p11")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
- assertThat(portfolioDao.selectTree(session, "p111")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
- assertThat(portfolioDao.selectTree(session, "p2")).extracting("uuid").containsOnly("p2");
- }
-
- @Test
- public void deleteByUuids() {
+ public void delete() {
PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
PortfolioDto p3 = db.components().insertPrivatePortfolioDto("p3");
portfolioDao.addReference(session, "p2", "app1");
portfolioDao.addReference(session, "p4", "p1");
- portfolioDao.deleteByUuids(session, Set.of("p1", "p3"));
+ portfolioDao.delete(session, p1);
+ portfolioDao.delete(session, p3);
+
assertThat(db.select(session, "select uuid from portfolios")).extracting(m -> m.values().iterator().next())
.containsOnly("p2", "p4");
assertThat(db.select(session, "select portfolio_uuid from portfolio_references")).extracting(m -> m.values().iterator().next())
.containsOnly("p2");
assertThat(db.select(session, "select portfolio_uuid from portfolio_projects")).extracting(m -> m.values().iterator().next())
.containsOnly("p2");
+
+ verify(audit, times(2)).deleteComponent(any(), any());
}
@Test
- public void selectReferencersByKey() {
- PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1");
- PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2");
+ public void deleteAllDescendantPortfolios() {
+ PortfolioDto root = db.components().insertPrivatePortfolioDto();
+ PortfolioDto child1 = addPortfolio(root);
+ PortfolioDto child11 = addPortfolio(child1);
+ PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
- ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app1"));
- portfolioDao.addReference(session, "portfolio1", "portfolio2");
- portfolioDao.addReference(session, "portfolio1", app1.getUuid());
- portfolioDao.addReference(session, "portfolio2", app1.getUuid());
+ portfolioDao.deleteAllDescendantPortfolios(session, root.getUuid());
+ portfolioDao.deleteAllDescendantPortfolios(session, root2.getUuid());
- assertThat(portfolioDao.selectReferencersByKey(session, portfolio2.getKey()))
- .extracting("uuid").containsOnly("portfolio1");
+ assertThat(db.countSql(session, "select count(*) from portfolios where parent_uuid is not null")).isZero();
+ }
- assertThat(portfolioDao.selectReferencersByKey(session, "app1"))
- .extracting("uuid").containsOnly("portfolio1", "portfolio2");
+ @Test
+ public void update_portfolio() {
+ db.components().insertPrivatePortfolioDto("name");
+ PortfolioDto dto = portfolioDao.selectByUuid(session, "name").get();
+ dto.setSelectionMode("newMode");
+ dto.setSelectionExpression("newExp");
+ dto.setDescription("newDesc");
+ dto.setName("newName");
+ dto.setRootUuid("root");
+ dto.setParentUuid("parent");
+ portfolioDao.update(session, dto);
+
+ assertThat(portfolioDao.selectByUuid(session, "name").get())
+ .extracting("name", "key", "uuid", "description", "private", "rootUuid", "parentUuid", "selectionMode", "selectionExpression")
+ .containsExactly("newName", "KEY_name", "name", "newDesc", true, "root", "parent", "newMode", "newExp");
+ verify(audit).updateComponent(any(), any());
+
+ }
+
+ @Test
+ public void selectKeysByUuids() {
+ PortfolioDto root = db.components().insertPrivatePortfolioDto();
+ PortfolioDto child1 = addPortfolio(root);
+
+ assertThat(portfolioDao.selectKeysByUuids(session, Set.of(root.getUuid(), child1.getUuid())))
+ .containsOnly(entry(root.getUuid(), root.getKey()), entry(child1.getUuid(), child1.getKey()));
}
@Test
}
@Test
- public void insert_and_select_references() {
- assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+ public void add_and_select_references() {
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
portfolioDao.addReference(session, "portfolio1", "app1");
portfolioDao.addReference(session, "portfolio1", "app2");
portfolioDao.addReference(session, "portfolio2", "app3");
db.commit();
- assertThat(portfolioDao.getReferences(session, "portfolio1")).containsExactlyInAnyOrder("app1", "app2");
- assertThat(portfolioDao.getReferences(session, "portfolio2")).containsExactlyInAnyOrder("app3");
- assertThat(portfolioDao.getReferences(session, "portfolio3")).isEmpty();
+ assertThat(portfolioDao.selectReferenceUuids(session, "portfolio1")).containsExactlyInAnyOrder("app1", "app2");
+ assertThat(portfolioDao.selectReferenceUuids(session, "portfolio2")).containsExactlyInAnyOrder("app3");
+ assertThat(portfolioDao.selectReferenceUuids(session, "portfolio3")).isEmpty();
assertThat(db.countRowsOfTable("portfolio_references")).isEqualTo(3);
assertThat(db.select(session, "select created_at from portfolio_references"))
.containsExactlyInAnyOrder(1L, 2L, 3L);
}
+ @Test
+ public void selectAllReferencesInHierarchy() {
+ PortfolioDto root1 = db.components().insertPrivatePortfolioDto("root1");
+ PortfolioDto root2 = db.components().insertPrivatePortfolioDto("root2");
+ PortfolioDto sub1 = addPortfolio(root1, "sub1");
+ PortfolioDto sub11 = addPortfolio(sub1, "sub11");
+ ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setUuid("app1"));
+
+ db.components().addPortfolioReference("root1", "app1");
+ db.components().addPortfolioReference("root2", "app1");
+ db.components().addPortfolioReference("sub11", "root2");
+
+ assertThat(portfolioDao.selectAllReferencesInHierarchy(session, root1.getUuid()))
+ .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid)
+ .containsOnly(
+ tuple(root1.getUuid(), app1.getUuid()),
+ tuple(sub11.getUuid(), root2.getUuid()));
+ }
+
+ @Test
+ public void selectReferencers() {
+ PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1");
+ PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2");
+
+ ProjectDto app1 = db.components().insertPrivateApplicationDto(c -> c.setUuid("app1"));
+ portfolioDao.addReference(session, "portfolio1", "portfolio2");
+ portfolioDao.addReference(session, "portfolio1", app1.getUuid());
+ portfolioDao.addReference(session, "portfolio2", app1.getUuid());
+
+ assertThat(portfolioDao.selectReferencers(session, portfolio2.getUuid()))
+ .extracting("uuid").containsOnly("portfolio1");
+
+ assertThat(portfolioDao.selectReferencers(session, "app1"))
+ .extracting("uuid").containsOnly("portfolio1", "portfolio2");
+ }
+
+ @Test
+ public void selectReferencers_for_non_existing_reference() {
+ assertThat(portfolioDao.selectReferencers(session, "unknown")).isEmpty();
+ }
+
+ @Test
+ public void selectRootOfReferencers_returns_root() {
+ PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("name1");
+ PortfolioDto sub = addPortfolio(portfolio1, "sub1");
+ PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("name2");
+ db.components().addPortfolioReference(sub, portfolio2.getUuid());
+
+ assertThat(portfolioDao.selectRootOfReferencers(session, portfolio2.getUuid()))
+ .extracting("uuid")
+ .containsOnly(portfolio1.getUuid());
+ }
+
+ @Test
+ public void selectRootOfReferencers_for_non_existing_reference() {
+ assertThat(portfolioDao.selectRootOfReferencers(session, "nonexisting")).isEmpty();
+ }
+
+ @Test
+ public void deleteReferencesTo() {
+ db.components().insertPrivatePortfolioDto("portfolio1");
+ db.components().insertPrivatePortfolioDto("portfolio2");
+ db.components().insertPrivatePortfolioDto("portfolio3");
+
+ db.components().addPortfolioReference("portfolio1", "portfolio3");
+ db.components().addPortfolioReference("portfolio2", "portfolio3");
+ portfolioDao.deleteReferencesTo(session, "portfolio3");
+ session.commit();
+ assertThat(db.countRowsOfTable("portfolio_references")).isZero();
+ }
+
+ @Test
+ public void deleteReferencesTo_with_non_existing_reference_doesnt_fail() {
+ portfolioDao.deleteReferencesTo(session, "portfolio3");
+ }
+
+ @Test
+ public void deleteAllReferences() {
+ PortfolioDto root = db.components().insertPrivatePortfolioDto();
+ PortfolioDto child1 = addPortfolio(root);
+ PortfolioDto child11 = addPortfolio(child1);
+ PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
+ PortfolioDto root3 = db.components().insertPrivatePortfolioDto();
+ PortfolioDto root4 = db.components().insertPrivatePortfolioDto();
+
+ db.components().addPortfolioReference(child1.getUuid(), root2.getUuid());
+ db.components().addPortfolioReference(root3.getUuid(), root4.getUuid());
+ assertThat(db.countRowsOfTable("portfolio_references")).isEqualTo(2);
+
+ portfolioDao.deleteAllReferences(session);
+ session.commit();
+ assertThat(db.countRowsOfTable("portfolio_references")).isZero();
+ }
+
+ @Test
+ public void deleteReference() {
+ db.components().insertPrivatePortfolioDto("portfolio1");
+ db.components().insertPrivatePortfolioDto("portfolio2");
+ db.components().insertPrivatePortfolioDto("portfolio3");
+
+ db.components().addPortfolioReference("portfolio1", "portfolio3");
+ db.components().addPortfolioReference("portfolio2", "portfolio3");
+ portfolioDao.deleteReference(session, "portfolio1", "portfolio3");
+ assertThat(portfolioDao.selectAllReferencesToPortfolios(session))
+ .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid)
+ .containsOnly(tuple("portfolio2", "portfolio3"));
+ }
+
@Test
public void insert_and_select_projects() {
db.components().insertPrivateProject("project1");
db.components().insertPrivateProject("project2");
- assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
portfolioDao.addProject(session, "portfolio1", "project1");
portfolioDao.addProject(session, "portfolio1", "project2");
portfolioDao.addProject(session, "portfolio2", "project2");
db.commit();
- assertThat(portfolioDao.getProjects(session, "portfolio1")).extracting(ProjectDto::getUuid).containsOnly("project1", "project2");
- assertThat(portfolioDao.getProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
- assertThat(portfolioDao.getProjects(session, "portfolio3")).isEmpty();
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).extracting(ProjectDto::getUuid).containsOnly("project1", "project2");
+ assertThat(portfolioDao.selectProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
+ assertThat(portfolioDao.selectProjects(session, "portfolio3")).isEmpty();
assertThat(db.countRowsOfTable("portfolio_projects")).isEqualTo(3);
assertThat(db.select(session, "select created_at from portfolio_projects"))
db.components().insertPrivateProject("project1");
db.components().insertPrivateProject("project2");
- assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
portfolioDao.addProject(session, "portfolio1", "project1");
portfolioDao.addProject(session, "portfolio1", "project2");
portfolioDao.addProject(session, "portfolio2", "project2");
- assertThat(portfolioDao.getProjects(session, "portfolio1")).isNotEmpty();
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).isNotEmpty();
portfolioDao.deleteProjects(session, "portfolio1");
- assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
- assertThat(portfolioDao.getProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
+ assertThat(portfolioDao.selectProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
}
@Test
db.components().insertPrivateProject("project1");
db.components().insertPrivateProject("project2");
- assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
portfolioDao.addProject(session, "portfolio1", "project1");
portfolioDao.addProject(session, "portfolio1", "project2");
portfolioDao.addProject(session, "portfolio2", "project2");
- assertThat(portfolioDao.getProjects(session, "portfolio1")).isNotEmpty();
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).isNotEmpty();
portfolioDao.deleteProject(session, "portfolio1", "project2");
- assertThat(portfolioDao.getProjects(session, "portfolio1")).extracting(ProjectDto::getUuid).containsOnly("project1");
- assertThat(portfolioDao.getProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
+ assertThat(portfolioDao.selectProjects(session, "portfolio1")).extracting(ProjectDto::getUuid).containsOnly("project1");
+ assertThat(portfolioDao.selectProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
}
@Test
- public void getAllProjectsInHierarchy() {
+ public void selectAllProjectsInHierarchy() {
db.components().insertPrivateProject("p1");
db.components().insertPrivateProject("p2");
db.components().insertPrivateProject("p3");
portfolioDao.addProject(session, root2.getUuid(), "p4");
session.commit();
- assertThat(portfolioDao.getAllProjectsInHierarchy(session, root.getUuid()))
+ assertThat(portfolioDao.selectAllProjectsInHierarchy(session, root.getUuid()))
.extracting(PortfolioProjectDto::getProjectUuid).containsExactly("p1", "p2", "p3");
- assertThat(portfolioDao.getAllProjectsInHierarchy(session, "nonexisting")).isEmpty();
- }
-
- @Test
- public void deleteAllDescendantPortfolios() {
- PortfolioDto root = db.components().insertPrivatePortfolioDto();
- PortfolioDto child1 = addPortfolio(root);
- PortfolioDto child11 = addPortfolio(child1);
- PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
-
- portfolioDao.deleteAllDescendantPortfolios(session, root.getUuid());
- portfolioDao.deleteAllDescendantPortfolios(session, root2.getUuid());
-
- assertThat(db.countSql(session, "select count(*) from portfolios where parent_uuid is not null")).isZero();
- }
-
- @Test
- public void deleteAllReferences() {
- PortfolioDto root = db.components().insertPrivatePortfolioDto();
- PortfolioDto child1 = addPortfolio(root);
- PortfolioDto child11 = addPortfolio(child1);
- PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
- PortfolioDto root3 = db.components().insertPrivatePortfolioDto();
- PortfolioDto root4 = db.components().insertPrivatePortfolioDto();
-
- portfolioDao.addReference(session, child1.getUuid(), root2.getUuid());
- portfolioDao.addReference(session, root3.getUuid(), root4.getUuid());
- session.commit();
- assertThat(db.countRowsOfTable("portfolio_references")).isEqualTo(2);
-
- portfolioDao.deleteAllReferences(session);
- session.commit();
- assertThat(db.countRowsOfTable("portfolio_references")).isZero();
+ assertThat(portfolioDao.selectAllProjectsInHierarchy(session, "nonexisting")).isEmpty();
}
@Test
assertThat(db.countRowsOfTable(session, "portfolio_projects")).isZero();
}
- private PortfolioDto createPortfolio(String uuid, @Nullable String parentUuid, String rootUuid) {
- PortfolioDto p = new PortfolioDto()
- .setName("name_" + uuid)
- .setKey("key_" + uuid)
- .setUuid(uuid)
- .setDescription("desc_" + uuid)
- .setRootUuid(rootUuid)
- .setParentUuid(parentUuid)
- .setPrivate(true)
- .setSelectionExpression("exp")
- .setSelectionMode("mode")
- .setCreatedAt(1000L)
- .setUpdatedAt(2000L);
-
- portfolioDao.insert(session, p);
- return p;
- }
-
private PortfolioDto addPortfolio(PortfolioDto parent) {
return addPortfolio(parent, UuidFactoryFast.getInstance().create());
}