import org.mockito.ArgumentCaptor;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.System2;
+import org.sonar.core.util.CloseableIterator;
+import org.sonar.core.util.UuidFactoryFast;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
}
@Test
- public void delete_project_in_ce_activity_when_deleting_project() {
+ public void delete_row_in_ce_activity_when_deleting_project() {
ComponentDto projectToBeDeleted = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
ComponentDto anotherLivingProject = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
dbClient.componentDao().insert(dbSession, projectToBeDeleted, anotherLivingProject);
- // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and on on another project
+ // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and one on another project
insertCeActivity(projectToBeDeleted);
insertCeActivity(anotherLivingProject);
dbSession.commit();
assertThat(dbTester.countRowsOfTable("ce_activity")).isEqualTo(1);
}
+ @Test
+ public void delete_row_in_ce_scanner_context_referring_to_a_row_in_ce_activity_when_deleting_project() {
+ ComponentDto projectToBeDeleted = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
+ ComponentDto anotherLivingProject = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
+ dbClient.componentDao().insert(dbSession, projectToBeDeleted, anotherLivingProject);
+
+ // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and one on another project
+ CeActivityDto toBeDeleted = insertCeActivity(projectToBeDeleted);
+ insertCeScannerContext(toBeDeleted.getUuid());
+ CeActivityDto toNotDelete = insertCeActivity(anotherLivingProject);
+ insertCeScannerContext(toNotDelete.getUuid());
+ insertCeScannerContext("non existing task");
+ dbSession.commit();
+
+ underTest.deleteProject(dbSession, projectToBeDeleted.uuid());
+ dbSession.commit();
+
+ assertThat(dbTester.select("select uuid as \"UUID\" from ce_activity"))
+ .extracting(row -> (String) row.get("UUID"))
+ .containsOnly(toNotDelete.getUuid());
+ assertThat(dbTester.select("select task_uuid as \"UUID\" from ce_scanner_context"))
+ .extracting(row -> (String) row.get("UUID"))
+ .containsOnly(toNotDelete.getUuid(), "non existing task");
+ }
+
@Test
public void delete_tasks_in_ce_queue_when_deleting_project() {
ComponentDto projectToBeDeleted = dbTester.components().insertPrivateProject();
assertThat(dbTester.countSql("select count(*) from ce_queue where component_uuid='" + projectToBeDeleted.uuid() + "'")).isEqualTo(0);
}
+ @Test
+ public void delete_row_in_ce_scanner_context_referring_to_a_row_in_ce_queue_when_deleting_project() {
+ ComponentDto projectToBeDeleted = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
+ ComponentDto anotherLivingProject = ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization());
+ dbClient.componentDao().insert(dbSession, projectToBeDeleted, anotherLivingProject);
+
+ // Insert 2 rows in CE_ACTIVITY : one for the project that will be deleted, and one on another project
+ CeQueueDto toBeDeleted = insertCeQueue(projectToBeDeleted);
+ insertCeScannerContext(toBeDeleted.getUuid());
+ CeQueueDto toNotDelete = insertCeQueue(anotherLivingProject);
+ insertCeScannerContext(toNotDelete.getUuid());
+ insertCeScannerContext("non existing task");
+ dbSession.commit();
+
+ underTest.deleteProject(dbSession, projectToBeDeleted.uuid());
+ dbSession.commit();
+
+ assertThat(dbTester.select("select uuid as \"UUID\" from ce_queue"))
+ .extracting(row -> (String) row.get("UUID"))
+ .containsOnly(toNotDelete.getUuid());
+ assertThat(dbTester.select("select task_uuid as \"UUID\" from ce_scanner_context"))
+ .extracting(row -> (String) row.get("UUID"))
+ .containsOnly(toNotDelete.getUuid(), "non existing task");
+ }
+
private ComponentDto insertProjectWithBranchAndRelatedData() {
RuleDefinitionDto rule = dbTester.rules().insert();
ComponentDto project = dbTester.components().insertMainBranch();
return dto;
}
+ private CeQueueDto insertCeQueue(ComponentDto project) {
+ CeQueueDto res = new CeQueueDto()
+ .setUuid(UuidFactoryFast.getInstance().create())
+ .setTaskType("foo")
+ .setComponentUuid(project.uuid())
+ .setStatus(Status.PENDING)
+ .setExecutionCount(0)
+ .setCreatedAt(1_2323_222L)
+ .setUpdatedAt(1_2323_222L);
+ dbClient.ceQueueDao().insert(dbSession, res);
+ dbSession.commit();
+ return res;
+ }
+
+ private void insertCeScannerContext(String uuid) {
+ dbClient.ceScannerContextDao().insert(dbSession, uuid, CloseableIterator.from(Arrays.asList("a", "b", "c").iterator()));
+ dbSession.commit();
+ }
+
private static PurgeableAnalysisDto getById(List<PurgeableAnalysisDto> snapshots, String uuid) {
return snapshots.stream()
.filter(snapshot -> uuid.equals(snapshot.getAnalysisUuid()))