diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2023-06-16 14:48:59 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-06-29 20:05:14 +0000 |
commit | 114a4deec2e278438cb9db6666b9bc8dfc46ab2c (patch) | |
tree | 046a3509a9af9f2e3b4052200078f01cb18ce698 /server/sonar-db-dao/src/it | |
parent | 63fc7d901eaacd42e9a40099906c38ae2aa779d4 (diff) | |
download | sonarqube-114a4deec2e278438cb9db6666b9bc8dfc46ab2c.tar.gz sonarqube-114a4deec2e278438cb9db6666b9bc8dfc46ab2c.zip |
SONAR-19558 Refactor 'issues', 'issue_changes', Components and view indexes
Diffstat (limited to 'server/sonar-db-dao/src/it')
5 files changed, 61 insertions, 78 deletions
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentDaoIT.java index b9b9934f3cf..688190bac7b 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentDaoIT.java @@ -879,11 +879,11 @@ public class ComponentDaoIT { db.components().insertComponent(newProjectCopy(project1, subView)); ComponentDto viewWithoutProject = db.components().insertPrivatePortfolio(); - assertThat(underTest.selectProjectsFromView(dbSession, view.uuid(), view.uuid())).containsExactlyInAnyOrder(project1.uuid()); - assertThat(underTest.selectProjectsFromView(dbSession, viewWithSubView.uuid(), viewWithSubView.uuid())).containsExactlyInAnyOrder(project1.uuid(), project2.uuid()); - assertThat(underTest.selectProjectsFromView(dbSession, subView.uuid(), viewWithSubView.uuid())).containsExactlyInAnyOrder(project1.uuid()); - assertThat(underTest.selectProjectsFromView(dbSession, viewWithoutProject.uuid(), viewWithoutProject.uuid())).isEmpty(); - assertThat(underTest.selectProjectsFromView(dbSession, "Unknown", "Unknown")).isEmpty(); + assertThat(underTest.selectProjectBranchUuidsFromView(dbSession, view.uuid(), view.uuid())).containsExactlyInAnyOrder(project1.uuid()); + assertThat(underTest.selectProjectBranchUuidsFromView(dbSession, viewWithSubView.uuid(), viewWithSubView.uuid())).containsExactlyInAnyOrder(project1.uuid(), project2.uuid()); + assertThat(underTest.selectProjectBranchUuidsFromView(dbSession, subView.uuid(), viewWithSubView.uuid())).containsExactlyInAnyOrder(project1.uuid()); + assertThat(underTest.selectProjectBranchUuidsFromView(dbSession, viewWithoutProject.uuid(), viewWithoutProject.uuid())).isEmpty(); + assertThat(underTest.selectProjectBranchUuidsFromView(dbSession, "Unknown", "Unknown")).isEmpty(); } @Test @@ -915,8 +915,8 @@ public class ComponentDaoIT { ComponentDto subView2 = db.components().insertComponent(newSubPortfolio(view, "ABC", "ABC-key")); db.components().insertComponent(newProjectCopy(project3, subView2)); - assertThat(underTest.selectProjectsFromView(dbSession, subView1.uuid(), view.uuid())).containsExactlyInAnyOrder(project1.uuid(), project2.uuid()); - assertThat(underTest.selectProjectsFromView(dbSession, subView2.uuid(), view.uuid())).containsExactlyInAnyOrder(project3.uuid()); + assertThat(underTest.selectProjectBranchUuidsFromView(dbSession, subView1.uuid(), view.uuid())).containsExactlyInAnyOrder(project1.uuid(), project2.uuid()); + assertThat(underTest.selectProjectBranchUuidsFromView(dbSession, subView2.uuid(), view.uuid())).containsExactlyInAnyOrder(project3.uuid()); } @Test diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentKeyUpdaterDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentKeyUpdaterDaoIT.java index 13d60db267f..e81aa17ef53 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentKeyUpdaterDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/ComponentKeyUpdaterDaoIT.java @@ -59,7 +59,7 @@ public class ComponentKeyUpdaterDaoIT { public void updateKey_changes_the_key_of_tree_of_components() { populateSomeData(); - underTest.updateKey(dbSession, "A", "struts:core"); + underTest.updateKey(dbSession, "A", "org.struts:struts", "struts:core"); dbSession.commit(); assertThat(db.select("select uuid as \"UUID\", kee as \"KEE\" from components")) @@ -73,9 +73,7 @@ public class ComponentKeyUpdaterDaoIT { @Test public void updateKey_updates_disabled_components() { - ComponentDto project = db.components().insertComponent( - newPrivateProjectDto("A") - .setKey("my_project")); + ComponentDto project = db.components().insertPrivateProject("A", p -> p.setKey("my_project")).getMainBranchComponent(); ComponentDto directory = db.components().insertComponent( newDirectory(project, "B") .setKey("my_project:directory")); @@ -83,7 +81,7 @@ public class ComponentKeyUpdaterDaoIT { ComponentDto inactiveDirectory = db.components().insertComponent(newDirectory(project, "/inactive_directory").setKey("my_project:inactive_directory").setEnabled(false)); db.components().insertComponent(newFileDto(project, inactiveDirectory).setKey("my_project:inactive_directory/file").setEnabled(false)); - underTest.updateKey(dbSession, "A", "your_project"); + underTest.updateKey(dbSession, "A", "my_project", "your_project"); dbSession.commit(); List<ComponentDto> result = dbClient.componentDao().selectByBranchUuid("A", dbSession); @@ -107,7 +105,7 @@ public class ComponentKeyUpdaterDaoIT { assertThat(dbClient.componentDao().selectByBranchUuid(branch.uuid(), dbSession)).hasSize(prComponentCount); String newProjectKey = "newKey"; - underTest.updateKey(dbSession, project.uuid(), newProjectKey); + underTest.updateKey(dbSession, project.uuid(), project.getKey(), newProjectKey); assertThat(dbClient.componentDao().selectByKey(dbSession, oldProjectKey)).isEmpty(); assertThat(dbClient.componentDao().selectByKey(dbSession, newProjectKey)).isPresent(); @@ -133,7 +131,7 @@ public class ComponentKeyUpdaterDaoIT { assertThat(dbClient.componentDao().selectByBranchUuid(pullRequest.uuid(), dbSession)).hasSize(prComponentCount); String newProjectKey = "newKey"; - underTest.updateKey(dbSession, project.uuid(), newProjectKey); + underTest.updateKey(dbSession, project.uuid(), project.getKey(), newProjectKey); assertThat(dbClient.componentDao().selectByKey(dbSession, oldProjectKey)).isEmpty(); assertThat(dbClient.componentDao().selectByKey(dbSession, newProjectKey)).isPresent(); @@ -150,19 +148,18 @@ public class ComponentKeyUpdaterDaoIT { public void updateKey_throws_IAE_if_component_with_specified_key_does_not_exist() { populateSomeData(); - assertThatThrownBy(() -> underTest.updateKey(dbSession, "A", "foo:struts-core")) + assertThatThrownBy(() -> underTest.updateKey(dbSession, "A", "org.struts:struts", "foo:struts-core")) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Impossible to update key: a component with key \"foo:struts-core\" already exists."); } @Test public void updateKey_throws_IAE_when_sub_component_key_is_too_long() { - ComponentDto project = newPrivateProjectDto("project-uuid").setKey("old-project-key"); - db.components().insertComponent(project); + ComponentDto project = db.components().insertPrivateProject("project-uuid", p -> p.setKey("old-project-key")).getMainBranchComponent(); db.components().insertComponent(newFileDto(project).setKey("old-project-key:file")); String newLongProjectKey = Strings.repeat("a", 400); - assertThatThrownBy(() -> underTest.updateKey(dbSession, project.uuid(), newLongProjectKey)) + assertThatThrownBy(() -> underTest.updateKey(dbSession, project.uuid(), project.getKey(), newLongProjectKey)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Component key length (405) is longer than the maximum authorized (400). '" + newLongProjectKey + ":file' was provided."); } @@ -175,12 +172,11 @@ public class ComponentKeyUpdaterDaoIT { @Test public void updateKey_callsAuditPersister() { - db.components().insertComponent(newPrivateProjectDto("A").setKey("my_project")); + db.components().insertPrivateProject("A", p -> p.setKey("my_project")); - underTestWithAuditPersister.updateKey(dbSession, "A", "your_project"); + underTestWithAuditPersister.updateKey(dbSession, "A", "my_project", "your_project"); - verify(auditPersister, times(1)) - .componentKeyUpdate(any(DbSession.class), any(ComponentKeyNewValue.class), anyString()); + verify(auditPersister, times(1)).componentKeyUpdate(any(DbSession.class), any(ComponentKeyNewValue.class), anyString()); } private void populateSomeData() { diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java index b0bf23234ad..2361b2e81ed 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java @@ -164,36 +164,8 @@ public class SnapshotDaoIT { } @Test - public void selectLastAnalysisDateByProject_takes_all_branches_into_account() { - ComponentDto firstProject = db.components().insertPrivateProject().getMainBranchComponent(); - ComponentDto branch1 = db.components().insertProjectBranch(firstProject); - ComponentDto branch2 = db.components().insertProjectBranch(firstProject); - - dbClient.snapshotDao().insert(dbSession, newAnalysis(firstProject).setLast(false).setCreatedAt(1L)); - dbClient.snapshotDao().insert(dbSession, newAnalysis(branch1).setLast(false).setCreatedAt(2L)); - dbClient.snapshotDao().insert(dbSession, newAnalysis(branch2).setLast(false).setCreatedAt(10L)); - - dbClient.snapshotDao().insert(dbSession, newAnalysis(firstProject).setLast(true).setCreatedAt(7L)); - dbClient.snapshotDao().insert(dbSession, newAnalysis(branch1).setLast(true).setCreatedAt(8L)); - dbClient.snapshotDao().insert(dbSession, newAnalysis(branch2).setLast(true).setCreatedAt(9L)); - - Optional<Long> date = underTest.selectLastAnalysisDateByProject(dbSession, firstProject.uuid()); - - assertThat(date).contains(9L); - } - - @Test - public void selectLastAnalysisDateByProject_is_empty_if_no_snapshot() { - ComponentDto firstProject = db.components().insertPrivateProject().getMainBranchComponent(); - ComponentDto branch1 = db.components().insertProjectBranch(firstProject); - ComponentDto branch2 = db.components().insertProjectBranch(firstProject); - - assertThat(underTest.selectLastAnalysisDateByProject(dbSession, firstProject.uuid())).isEmpty(); - } - - @Test public void selectLastAnalysisDateByProjects_is_empty_if_no_project_passed() { - assertThat(underTest.selectLastAnalysisDateByProjects(dbSession, emptyList())).isEmpty(); + assertThat(underTest.selectLastAnalysisDateByProjectUuids(dbSession, emptyList())).isEmpty(); } @Test @@ -213,7 +185,7 @@ public class SnapshotDaoIT { dbClient.snapshotDao().insert(dbSession, newAnalysis(branch2).setLast(false).setCreatedAt(5L)); dbClient.snapshotDao().insert(dbSession, newAnalysis(branch1).setLast(true).setCreatedAt(4L)); - List<ProjectLastAnalysisDateDto> lastAnalysisByProject = underTest.selectLastAnalysisDateByProjects(dbSession, + List<ProjectLastAnalysisDateDto> lastAnalysisByProject = underTest.selectLastAnalysisDateByProjectUuids(dbSession, List.of(project1.uuid(), project2.uuid(), project3.uuid(), "non-existing")); assertThat(lastAnalysisByProject).extracting(ProjectLastAnalysisDateDto::getProjectUuid, ProjectLastAnalysisDateDto::getDate) @@ -230,7 +202,7 @@ public class SnapshotDaoIT { dbClient.snapshotDao().insert(dbSession, newAnalysis(portfolio).setLast(true).setCreatedAt(2L)); dbClient.snapshotDao().insert(dbSession, newAnalysis(branch1).setLast(true).setCreatedAt(3L)); - List<ProjectLastAnalysisDateDto> lastAnalysisByProject = underTest.selectLastAnalysisDateByProjects(dbSession, + List<ProjectLastAnalysisDateDto> lastAnalysisByProject = underTest.selectLastAnalysisDateByProjectUuids(dbSession, List.of(project1.uuid(), portfolio.uuid())); assertThat(lastAnalysisByProject).extracting(ProjectLastAnalysisDateDto::getProjectUuid, ProjectLastAnalysisDateDto::getDate) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/entity/EntityDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/entity/EntityDaoIT.java index 7b54d98409c..3f6276ac35c 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/entity/EntityDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/entity/EntityDaoIT.java @@ -27,8 +27,12 @@ import org.junit.Test; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; +import org.sonar.db.component.BranchDto; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentTesting; import org.sonar.db.component.ProjectData; import org.sonar.db.portfolio.PortfolioDto; +import org.sonar.db.project.ProjectDto; import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; @@ -42,6 +46,42 @@ public class EntityDaoIT { private final EntityDao entityDao = new EntityDao(); @Test + public void selectEntityByComponentUuid_shouldReturnProjectEntityBasedOnComponent() { + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); + BranchDto branchDto = db.components().insertProjectBranch(project); + ComponentDto fileInBranch = db.components().insertFile(branchDto); + + assertThat(entityDao.selectByComponentUuid(db.getSession(), fileInBranch.uuid()).get()) + .extracting(EntityDto::getUuid, EntityDto::getKey) + .containsOnly(project.getUuid(), project.getKey()); + } + + @Test + public void selectEntityByComponentUuid_shouldReturnPortfolioEntityBasedOnComponent() { + PortfolioDto portfolio = db.components().insertPublicPortfolioDto(); + assertThat(entityDao.selectByComponentUuid(db.getSession(), portfolio.getUuid()).get()) + .extracting(EntityDto::getUuid, EntityDto::getKey) + .containsOnly(portfolio.getUuid(), portfolio.getKey()); + } + + @Test + public void selectEntityByComponentUuid_whenPortfolioWithHierarchy_shouldReturnPortfolioEntityBasedOnComponent() { + ComponentDto projectBranch = db.components().insertPublicProject().getMainBranchComponent(); + ComponentDto portfolio = db.components().insertPublicPortfolio(); + ComponentDto subPortfolio = db.components().insertSubportfolio(portfolio); + ComponentDto projectCopy = db.components().insertComponent(ComponentTesting.newProjectCopy(projectBranch, subPortfolio)); + + assertThat(entityDao.selectByComponentUuid(db.getSession(), projectCopy.uuid()).get()) + .extracting(EntityDto::getUuid, EntityDto::getKey) + .containsOnly(portfolio.uuid(), portfolio.getKey()); + } + + @Test + public void selectEntityByComponentUuid_whenUnknown_shouldReturnEmpty() { + assertThat(entityDao.selectByComponentUuid(db.getSession(), "unknown")).isEmpty(); + } + + @Test public void selectEntitiesByKeys_shouldReturnAllEntities() { ProjectData application = db.components().insertPrivateApplication(); ProjectData project = db.components().insertPrivateProject(); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorIT.java index a55affffc50..f8105bd490d 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorIT.java @@ -166,17 +166,6 @@ public class ProjectMeasuresIndexerIteratorIT { } @Test - public void return_language_distribution_measure_from_project_with_no_branches() { - ComponentDto project = insertComponentAndProject(ComponentTesting.newPublicProjectDto(), false, - defaults(), defaults()); - - Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById(); - - assertThat(docsById.get(project.uuid()).getMeasures().getNclocByLanguages()) - .isEmpty(); - } - - @Test public void does_not_return_none_numeric_metrics() { ComponentDto project = dbTester.components().insertPrivateProject().getMainBranchComponent(); MetricDto dataMetric = dbTester.measures().insertMetric(m -> m.setValueType(DATA.name()).setKey("data")); @@ -309,18 +298,4 @@ public class ProjectMeasuresIndexerIteratorIT { it.close(); return docsById; } - - private ComponentDto insertComponentAndProject(ComponentDto component, @Nullable Boolean isPrivate, - Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) { - componentDtoPopulator.accept(component); - checkState(isPrivate == null || component.isPrivate() == isPrivate, "Illegal modification of private flag"); - dbClient.componentDao().insert(dbSession, component, true); - - ProjectDto projectDto = toProjectDto(component, System2.INSTANCE.now()); - projectDtoPopulator.accept(projectDto); - dbClient.projectDao().insert(dbSession, projectDto); - - dbTester.commit(); - return component; - } } |