From f83091c81a09ba469e1e6b53f1da77c726d28336 Mon Sep 17 00:00:00 2001 From: lukasz-jarocki-sonarsource Date: Wed, 12 Jul 2023 11:17:24 +0200 Subject: [PATCH] SONAR-19850 Fix tests after enabling different uuids for project and branch --- .../component/ProjectPersisterIT.java | 18 +++++++++--------- .../ProjectExportTaskProcessor.java | 4 +++- .../it/java/org/sonar/db/purge/PurgeDaoIT.java | 8 +++++++- .../ws/azure/ImportAzureProjectActionIT.java | 2 +- .../ImportBitbucketCloudRepoActionIT.java | 2 +- .../ImportBitbucketServerProjectActionIT.java | 2 +- .../ws/github/ImportGithubProjectActionIT.java | 2 +- .../ws/gitlab/ImportGitLabProjectActionIT.java | 2 +- .../server/ce/queue/ReportSubmitterIT.java | 2 +- .../server/component/ComponentUpdaterIT.java | 2 +- .../server/project/ws/CreateActionIT.java | 2 +- .../server/component/ComponentUpdater.java | 10 ---------- .../server/project/ws/BulkDeleteAction.java | 15 +++++++-------- 13 files changed, 34 insertions(+), 37 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/ProjectPersisterIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/ProjectPersisterIT.java index 45d0ef5e8a5..99b0f40403e 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/ProjectPersisterIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/component/ProjectPersisterIT.java @@ -77,9 +77,9 @@ public class ProjectPersisterIT { ProjectDto p1 = dbTester.components().insertPublicProject("PROJECT_UUID", p -> p.setKey(ROOT.getKey()).setName(ROOT.getName()).setDescription("OLD_DESC")).getProjectDto(); - assertProject("OLD_DESC", ROOT.getName(), p1.getUpdatedAt()); + assertProject(p1.getUuid(), "OLD_DESC", ROOT.getName(), p1.getUpdatedAt()); underTest.persist(dbTester.getSession()); - assertProject(ROOT.getDescription(), ROOT.getName(), 1000L); + assertProject(ROOT.getUuid(), ROOT.getDescription(), ROOT.getName(), 1000L); } @Test @@ -87,9 +87,9 @@ public class ProjectPersisterIT { ProjectDto p1 = dbTester.components().insertPublicProject("PROJECT_UUID", p -> p.setKey(ROOT.getKey()).setName("OLD_NAME").setDescription(ROOT.getDescription())).getProjectDto(); - assertProject(ROOT.getDescription(), "OLD_NAME", p1.getUpdatedAt()); + assertProject(p1.getUuid(), ROOT.getDescription(), "OLD_NAME", p1.getUpdatedAt()); underTest.persist(dbTester.getSession()); - assertProject(ROOT.getDescription(), ROOT.getName(), 1000L); + assertProject(ROOT.getUuid(), ROOT.getDescription(), ROOT.getName(), 1000L); } @Test @@ -97,15 +97,15 @@ public class ProjectPersisterIT { ProjectDto p1 = dbTester.components().insertPublicProject( c -> c.setUuid("PROJECT_UUID").setKey(ROOT.getKey()).setName(ROOT.getName()).setDescription(ROOT.getDescription())).getProjectDto(); - assertProject(ROOT.getDescription(), ROOT.getName(), p1.getUpdatedAt()); + assertProject(p1.getUuid(), ROOT.getDescription(), ROOT.getName(), p1.getUpdatedAt()); underTest.persist(dbTester.getSession()); - assertProject(ROOT.getDescription(), ROOT.getName(), p1.getUpdatedAt()); + assertProject(p1.getUuid(), ROOT.getDescription(), ROOT.getName(), p1.getUpdatedAt()); } - private void assertProject(String description, String name, long updated) { + private void assertProject(String uuid, String description, String name, long updated) { assertThat(dbTester.getDbClient().projectDao().selectProjectByKey(dbTester.getSession(), ROOT.getKey()).get()) - .extracting(ProjectDto::getName, ProjectDto::getDescription, ProjectDto::getUpdatedAt) - .containsExactly(name, description, updated); + .extracting(ProjectDto::getUuid, ProjectDto::getName, ProjectDto::getDescription, ProjectDto::getUpdatedAt) + .containsExactly(uuid, name, description, updated); } } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java index e18d97a589b..fc94024b6ce 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/taskprocessor/ProjectExportTaskProcessor.java @@ -52,7 +52,9 @@ public class ProjectExportTaskProcessor implements CeTaskProcessor { private void processProjectExport(CeTask task) { CeTask.Component exportComponent = mandatoryComponent(task, PROJECT_EXPORT); - ProjectDescriptor projectExportDescriptor = new ProjectDescriptor(task.getEntity().get().getUuid(), + CeTask.Component entity = task.getEntity() + .orElseThrow(() -> new IllegalStateException("Compute engine task for project export doesn't contain entity")); + ProjectDescriptor projectExportDescriptor = new ProjectDescriptor(entity.getUuid(), mandatoryKey(exportComponent), mandatoryName(exportComponent)); try (TaskContainer taskContainer = new TaskContainerImpl(componentContainer, diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java index 9094851bb2a..ec0bb78cd87 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java @@ -100,7 +100,10 @@ import static java.util.Collections.singletonList; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.groups.Tuple.tuple; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import static org.sonar.db.ce.CeTaskTypes.REPORT; @@ -1219,6 +1222,7 @@ public class PurgeDaoIT { @Test public void should_delete_old_closed_issues() { + PurgeListener listener = mock(PurgeListener.class); RuleDto rule = db.rules().insert(); ProjectData projectData = db.components().insertPublicProject(); ComponentDto mainBranch = projectData.getMainBranchComponent(); @@ -1241,7 +1245,7 @@ public class PurgeDaoIT { db.issues().insertNewCodeReferenceIssue(newCodeReferenceIssue(notClosed)); when(system2.now()).thenReturn(new Date().getTime()); - underTest.purge(dbSession, newConfigurationWith30Days(system2, mainBranch.uuid(), projectData.projectUuid()), PurgeListener.EMPTY, new PurgeProfiler()); + underTest.purge(dbSession, newConfigurationWith30Days(system2, mainBranch.uuid(), projectData.projectUuid()), listener, new PurgeProfiler()); dbSession.commit(); // old closed got deleted @@ -1260,6 +1264,8 @@ public class PurgeDaoIT { Optional oldClosedFromQuery = db.getDbClient().issueDao().selectByKey(dbSession, oldClosed.getKey()); assertThat(oldClosedFromQuery).isEmpty(); + + verify(listener, only()).onIssuesRemoval(projectData.projectUuid(), List.of(oldClosed.getKee())); } @Test diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java index 3c9c3338e67..3cb3ffb9f38 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectActionIT.java @@ -93,7 +93,7 @@ public class ImportAzureProjectActionIT { private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE, mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(), - defaultBranchNameResolver, true); + defaultBranchNameResolver); private final Encryption encryption = mock(Encryption.class); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java index 79053120c52..08a147ec483 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionIT.java @@ -93,7 +93,7 @@ public class ImportBitbucketCloudRepoActionIT { DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class); private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE, mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(), - defaultBranchNameResolver, true); + defaultBranchNameResolver); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java index a615c9a50be..8b674f338f3 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionIT.java @@ -100,7 +100,7 @@ public class ImportBitbucketServerProjectActionIT { private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE, mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(), - defaultBranchNameResolver, true); + defaultBranchNameResolver); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java index df056b47dd6..dd35ab3cb4b 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectActionIT.java @@ -94,7 +94,7 @@ public class ImportGithubProjectActionIT { private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE, permissionTemplateService, new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(), - defaultBranchNameResolver, true); + defaultBranchNameResolver); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java index db88cbdc36e..e257bcc044f 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectActionIT.java @@ -84,7 +84,7 @@ public class ImportGitLabProjectActionIT { private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE, mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(), - defaultBranchNameResolver, true); + defaultBranchNameResolver); private final GitlabHttpClient gitlabHttpClient = mock(GitlabHttpClient.class); private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java index 748ed942153..6bdf633aba7 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java @@ -93,7 +93,7 @@ public class ReportSubmitterIT { private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), mock(System2.class), permissionTemplateService, - new FavoriteUpdater(db.getDbClient()), projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver, true); + new FavoriteUpdater(db.getDbClient()), projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver); private final BranchSupport ossEditionBranchSupport = new BranchSupport(null); private final ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentUpdater, permissionTemplateService, db.getDbClient(), ossEditionBranchSupport, diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java index 5223e7123f4..0e72ba37443 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java @@ -80,7 +80,7 @@ public class ComponentUpdaterIT { private final ComponentUpdater underTest = new ComponentUpdater(db.getDbClient(), i18n, system2, permissionTemplateService, new FavoriteUpdater(db.getDbClient()), - projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver, true); + projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver); @Before public void before() { diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java index df1fe14ba12..75dd6ae7d4b 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/CreateActionIT.java @@ -107,7 +107,7 @@ public class CreateActionIT { new CreateAction( db.getDbClient(), userSession, new ComponentUpdater(db.getDbClient(), i18n, system2, permissionTemplateService, new FavoriteUpdater(db.getDbClient()), - projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver, true), + projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver), projectDefaultVisibility, defaultBranchNameResolver, newCodeDefinitionResolver)); @Before diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java index 843183bd137..75118c589f1 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java @@ -65,20 +65,11 @@ public class ComponentUpdater { private final Indexers indexers; private final UuidFactory uuidFactory; private final DefaultBranchNameResolver defaultBranchNameResolver; - private final boolean useDifferentUuids; @Autowired public ComponentUpdater(DbClient dbClient, I18n i18n, System2 system2, PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater, Indexers indexers, UuidFactory uuidFactory, DefaultBranchNameResolver defaultBranchNameResolver) { - this(dbClient, i18n, system2, permissionTemplateService, favoriteUpdater, indexers, uuidFactory, defaultBranchNameResolver, false); - } - - @VisibleForTesting - public ComponentUpdater(DbClient dbClient, I18n i18n, System2 system2, - PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater, - Indexers indexers, UuidFactory uuidFactory, DefaultBranchNameResolver defaultBranchNameResolver, - boolean useDifferentUuids) { this.dbClient = dbClient; this.i18n = i18n; this.system2 = system2; @@ -87,7 +78,6 @@ public class ComponentUpdater { this.indexers = indexers; this.uuidFactory = uuidFactory; this.defaultBranchNameResolver = defaultBranchNameResolver; - this.useDifferentUuids = useDifferentUuids; } /** diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java index c71fcb9974c..379c1774e81 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java @@ -154,23 +154,22 @@ public class BulkDeleteAction implements ProjectsWsAction { ComponentQuery query = buildDbQuery(searchRequest); Set componentDtos = new HashSet<>(dbClient.componentDao().selectByQuery(dbSession, query, 0, Integer.MAX_VALUE)); List entities = dbClient.entityDao().selectByKeys(dbSession, componentDtos.stream().map(ComponentDto::getKey).collect(toSet())); + Set entityUuids = entities.stream().map(EntityDto::getUuid).collect(toSet()); + Map mainBranchUuidByEntityUuid = dbClient.branchDao().selectMainBranchesByProjectUuids(dbSession, entityUuids).stream() + .collect(Collectors.toMap(BranchDto::getProjectUuid, BranchDto::getUuid)); try { entities.forEach(p -> componentCleanerService.deleteEntity(dbSession, p)); } finally { - callDeleteListeners(dbSession, entities); + callDeleteListeners(dbSession, mainBranchUuidByEntityUuid, entities); } } response.noContent(); } - private void callDeleteListeners(DbSession dbSession, List entities) { - Set entityUuids = entities.stream().map(EntityDto::getUuid).collect(toSet()); - Map mainBranchUuidByEntityUuid = dbClient.branchDao().selectMainBranchesByProjectUuids(dbSession, entityUuids).stream() - .collect(Collectors.toMap(BranchDto::getProjectUuid, BranchDto::getUuid)); - - ImmutableSet deletedProjects = entities.stream().map(entity -> new DeletedProject(Project.from(entity), mainBranchUuidByEntityUuid.get(entity.getUuid()))) - .collect(toImmutableSet()); + private void callDeleteListeners(DbSession dbSession, Map mainBranchUuidByEntityUuid , List entities) { + Set deletedProjects = entities.stream().map(entity -> new DeletedProject(Project.from(entity), + mainBranchUuidByEntityUuid.get(entity.getUuid()))).collect(toSet()); projectLifeCycleListeners.onProjectsDeleted(deletedProjects); } -- 2.39.5