Search in sources :

Example 76 with RuleDefinitionDto

use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.

the class PurgeDaoTest method delete_project_and_associated_data.

@Test
public void delete_project_and_associated_data() {
    RuleDefinitionDto rule = db.rules().insert();
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto module = db.components().insertComponent(newModuleDto(project));
    ComponentDto directory = db.components().insertComponent(newDirectory(module, "a/b"));
    ComponentDto file = db.components().insertComponent(newFileDto(directory));
    SnapshotDto analysis = db.components().insertSnapshot(project);
    IssueDto issue1 = db.issues().insert(rule, project, file);
    IssueChangeDto issueChange1 = db.issues().insertChange(issue1);
    IssueDto issue2 = db.issues().insert(rule, project, file);
    FileSourceDto fileSource = db.fileSources().insertFileSource(file);
    db.issues().insertNewCodeReferenceIssue(newCodeReferenceIssue(issue1));
    ComponentDto otherProject = db.components().insertPrivateProject();
    ComponentDto otherModule = db.components().insertComponent(newModuleDto(otherProject));
    ComponentDto otherDirectory = db.components().insertComponent(newDirectory(otherModule, "a/b"));
    ComponentDto otherFile = db.components().insertComponent(newFileDto(otherDirectory));
    SnapshotDto otherAnalysis = db.components().insertSnapshot(otherProject);
    IssueDto otherIssue1 = db.issues().insert(rule, otherProject, otherFile);
    IssueChangeDto otherIssueChange1 = db.issues().insertChange(otherIssue1);
    IssueDto otherIssue2 = db.issues().insert(rule, otherProject, otherFile);
    FileSourceDto otherFileSource = db.fileSources().insertFileSource(otherFile);
    db.issues().insertNewCodeReferenceIssue(newCodeReferenceIssue(otherIssue1));
    underTest.deleteProject(dbSession, project.uuid(), project.qualifier(), project.name(), project.getKey());
    dbSession.commit();
    assertThat(uuidsIn("components")).containsOnly(otherProject.uuid(), otherModule.uuid(), otherDirectory.uuid(), otherFile.uuid());
    assertThat(uuidsIn("projects")).containsOnly(otherProject.uuid());
    assertThat(uuidsIn("snapshots")).containsOnly(otherAnalysis.getUuid());
    assertThat(uuidsIn("issues", "kee")).containsOnly(otherIssue1.getKey(), otherIssue2.getKey());
    assertThat(uuidsIn("issue_changes", "kee")).containsOnly(otherIssueChange1.getKey());
    assertThat(uuidsIn("new_code_reference_issues", "issue_key")).containsOnly(otherIssue1.getKey());
    assertThat(uuidsIn("file_sources", "file_uuid")).containsOnly(otherFileSource.getFileUuid());
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FileSourceDto(org.sonar.db.source.FileSourceDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test)

Example 77 with RuleDefinitionDto

use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.

the class PurgeDaoTest method delete_disabled_components_without_issues.

@Test
public void delete_disabled_components_without_issues() {
    ComponentDto project = db.components().insertPublicProject(p -> p.setEnabled(true));
    ComponentDto enabledFileWithIssues = db.components().insertComponent(newFileDto(project).setEnabled(true));
    ComponentDto disabledFileWithIssues = db.components().insertComponent(newFileDto(project).setEnabled(false));
    ComponentDto enabledFileWithoutIssues = db.components().insertComponent(newFileDto(project).setEnabled(true));
    ComponentDto disabledFileWithoutIssues = db.components().insertComponent(newFileDto(project).setEnabled(false));
    RuleDefinitionDto rule = db.rules().insert();
    IssueDto closed1 = db.issues().insert(rule, project, enabledFileWithIssues, issue -> {
        issue.setStatus("CLOSED");
        issue.setResolution(Issue.RESOLUTION_FIXED);
        issue.setIssueCloseDate(new Date());
    });
    IssueDto closed2 = db.issues().insert(rule, project, disabledFileWithIssues, issue -> {
        issue.setStatus("CLOSED");
        issue.setResolution(Issue.RESOLUTION_FIXED);
        issue.setIssueCloseDate(new Date());
    });
    PurgeListener purgeListener = mock(PurgeListener.class);
    Set<String> disabledComponentUuids = ImmutableSet.of(disabledFileWithIssues.uuid(), disabledFileWithoutIssues.uuid());
    underTest.purge(dbSession, newConfigurationWith30Days(System2.INSTANCE, project.uuid(), project.uuid(), disabledComponentUuids), purgeListener, new PurgeProfiler());
    assertThat(db.getDbClient().componentDao().selectByProjectUuid(project.uuid(), dbSession)).extracting("uuid").containsOnly(project.uuid(), enabledFileWithIssues.uuid(), disabledFileWithIssues.uuid(), enabledFileWithoutIssues.uuid());
    verify(purgeListener).onComponentsDisabling(project.uuid(), disabledComponentUuids);
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) Date(java.util.Date) Test(org.junit.Test)

Example 78 with RuleDefinitionDto

use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.

the class PurgeDaoTest method delete_application.

@Test
public void delete_application() {
    MetricDto metric = db.measures().insertMetric();
    ComponentDto project = db.components().insertPrivateProject();
    BranchDto projectBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid()).get();
    RuleDefinitionDto rule = db.rules().insert();
    ComponentDto app = db.components().insertPrivateApplication();
    ComponentDto appBranch = db.components().insertProjectBranch(app);
    ComponentDto otherApp = db.components().insertPrivateApplication();
    ComponentDto otherAppBranch = db.components().insertProjectBranch(otherApp);
    SnapshotDto appAnalysis = db.components().insertSnapshot(app);
    SnapshotDto appBranchAnalysis = db.components().insertSnapshot(appBranch);
    SnapshotDto otherAppAnalysis = db.components().insertSnapshot(otherApp);
    SnapshotDto otherAppBranchAnalysis = db.components().insertSnapshot(otherAppBranch);
    MeasureDto appMeasure = db.measures().insertMeasure(app, appAnalysis, metric);
    MeasureDto appBranchMeasure = db.measures().insertMeasure(appBranch, appBranchAnalysis, metric);
    MeasureDto otherAppMeasure = db.measures().insertMeasure(otherApp, otherAppAnalysis, metric);
    MeasureDto otherAppBranchMeasure = db.measures().insertMeasure(otherAppBranch, otherAppBranchAnalysis, metric);
    db.components().addApplicationProject(app, project);
    db.components().addApplicationProject(otherApp, project);
    db.components().addProjectBranchToApplicationBranch(dbClient.branchDao().selectByUuid(dbSession, appBranch.uuid()).get(), projectBranch);
    db.components().addProjectBranchToApplicationBranch(dbClient.branchDao().selectByUuid(dbSession, otherAppBranch.uuid()).get(), projectBranch);
    underTest.deleteProject(dbSession, app.uuid(), app.qualifier(), project.name(), project.getKey());
    dbSession.commit();
    assertThat(uuidsIn("components")).containsOnly(project.uuid(), otherApp.uuid(), otherAppBranch.uuid());
    assertThat(uuidsIn("projects")).containsOnly(project.uuid(), otherApp.uuid());
    assertThat(uuidsIn("snapshots")).containsOnly(otherAppAnalysis.getUuid(), otherAppBranchAnalysis.getUuid());
    assertThat(uuidsIn("project_branches")).containsOnly(project.uuid(), otherApp.uuid(), otherAppBranch.uuid());
    assertThat(uuidsIn("project_measures")).containsOnly(otherAppMeasure.getUuid(), otherAppBranchMeasure.getUuid());
    assertThat(uuidsIn("app_projects", "application_uuid")).containsOnly(otherApp.uuid());
    assertThat(uuidsIn("app_branch_project_branch", "application_branch_uuid")).containsOnly(otherAppBranch.uuid());
}
Also used : MeasureDto(org.sonar.db.measure.MeasureDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) MetricDto(org.sonar.db.metric.MetricDto) BranchDto(org.sonar.db.component.BranchDto) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 79 with RuleDefinitionDto

use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.

the class PurgeDaoTest method close_issues_clean_index_and_file_sources_of_disabled_components_specified_by_uuid_in_configuration.

@Test
public void close_issues_clean_index_and_file_sources_of_disabled_components_specified_by_uuid_in_configuration() {
    RuleDefinitionDto rule = db.rules().insert();
    ComponentDto project = db.components().insertPublicProject();
    db.components().insertSnapshot(project);
    db.components().insertSnapshot(project);
    db.components().insertSnapshot(project, s -> s.setLast(false));
    ComponentDto module = db.components().insertComponent(newModuleDto(project).setEnabled(false));
    ComponentDto dir = db.components().insertComponent(newDirectory(module, "sub").setEnabled(false));
    ComponentDto srcFile = db.components().insertComponent(newFileDto(module, dir).setEnabled(false));
    ComponentDto testFile = db.components().insertComponent(newFileDto(module, dir).setEnabled(false));
    ComponentDto enabledFile = db.components().insertComponent(newFileDto(module, dir).setEnabled(true));
    IssueDto openOnFile = db.issues().insert(rule, project, srcFile, issue -> issue.setStatus("OPEN"));
    IssueDto confirmOnFile = db.issues().insert(rule, project, srcFile, issue -> issue.setStatus("CONFIRM"));
    IssueDto openOnDir = db.issues().insert(rule, project, dir, issue -> issue.setStatus("OPEN"));
    IssueDto confirmOnDir = db.issues().insert(rule, project, dir, issue -> issue.setStatus("CONFIRM"));
    IssueDto openOnEnabledComponent = db.issues().insert(rule, project, enabledFile, issue -> issue.setStatus("OPEN"));
    IssueDto confirmOnEnabledComponent = db.issues().insert(rule, project, enabledFile, issue -> issue.setStatus("CONFIRM"));
    assertThat(db.countSql("select count(*) from snapshots where purge_status = 1")).isZero();
    assertThat(db.countSql("select count(*) from issues where status = 'CLOSED'")).isZero();
    assertThat(db.countSql("select count(*) from issues where resolution = 'REMOVED'")).isZero();
    db.fileSources().insertFileSource(srcFile);
    FileSourceDto nonSelectedFileSource = db.fileSources().insertFileSource(enabledFile);
    assertThat(db.countRowsOfTable("file_sources")).isEqualTo(2);
    MetricDto metric1 = db.measures().insertMetric();
    MetricDto metric2 = db.measures().insertMetric();
    LiveMeasureDto liveMeasureMetric1OnFile = db.measures().insertLiveMeasure(srcFile, metric1);
    LiveMeasureDto liveMeasureMetric2OnFile = db.measures().insertLiveMeasure(srcFile, metric2);
    LiveMeasureDto liveMeasureMetric1OnDir = db.measures().insertLiveMeasure(dir, metric1);
    LiveMeasureDto liveMeasureMetric2OnDir = db.measures().insertLiveMeasure(dir, metric2);
    LiveMeasureDto liveMeasureMetric1OnProject = db.measures().insertLiveMeasure(project, metric1);
    LiveMeasureDto liveMeasureMetric2OnProject = db.measures().insertLiveMeasure(project, metric2);
    LiveMeasureDto liveMeasureMetric1OnNonSelected = db.measures().insertLiveMeasure(enabledFile, metric1);
    LiveMeasureDto liveMeasureMetric2OnNonSelected = db.measures().insertLiveMeasure(enabledFile, metric2);
    assertThat(db.countRowsOfTable("live_measures")).isEqualTo(8);
    PurgeListener purgeListener = mock(PurgeListener.class);
    // back to present
    Set<String> selectedComponentUuids = ImmutableSet.of(module.uuid(), srcFile.uuid(), testFile.uuid());
    underTest.purge(dbSession, newConfigurationWith30Days(system2, project.uuid(), project.uuid(), selectedComponentUuids), purgeListener, new PurgeProfiler());
    dbSession.commit();
    verify(purgeListener).onComponentsDisabling(project.uuid(), selectedComponentUuids);
    verify(purgeListener).onComponentsDisabling(project.uuid(), ImmutableSet.of(dir.uuid()));
    // set purge_status=1 for non-last snapshot
    assertThat(db.countSql("select count(*) from snapshots where purge_status = 1")).isOne();
    // close open issues of selected
    assertThat(db.countSql("select count(*) from issues where status = 'CLOSED'")).isEqualTo(4);
    for (IssueDto issue : Arrays.asList(openOnFile, confirmOnFile, openOnDir, confirmOnDir)) {
        assertThat(db.getDbClient().issueDao().selectOrFailByKey(dbSession, issue.getKey())).extracting(IssueDto::getStatus, IssueDto::getResolution).containsExactlyInAnyOrder("CLOSED", "REMOVED");
    }
    for (IssueDto issue : Arrays.asList(openOnEnabledComponent, confirmOnEnabledComponent)) {
        assertThat(db.getDbClient().issueDao().selectByKey(dbSession, issue.getKey()).get()).extracting("status", "resolution").containsExactlyInAnyOrder(issue.getStatus(), null);
    }
    // delete file sources of selected
    assertThat(db.countRowsOfTable("file_sources")).isOne();
    assertThat(db.getDbClient().fileSourceDao().selectByFileUuid(dbSession, nonSelectedFileSource.getFileUuid())).isNotNull();
    // deletes live measure of selected
    assertThat(db.countRowsOfTable("live_measures")).isEqualTo(4);
    List<LiveMeasureDto> liveMeasureDtos = db.getDbClient().liveMeasureDao().selectByComponentUuidsAndMetricUuids(dbSession, ImmutableSet.of(srcFile.uuid(), dir.uuid(), project.uuid(), enabledFile.uuid()), ImmutableSet.of(metric1.getUuid(), metric2.getUuid()));
    assertThat(liveMeasureDtos).extracting(LiveMeasureDto::getComponentUuid).containsOnly(enabledFile.uuid(), project.uuid());
    assertThat(liveMeasureDtos).extracting(LiveMeasureDto::getMetricUuid).containsOnly(metric1.getUuid(), metric2.getUuid());
}
Also used : MetricDto(org.sonar.db.metric.MetricDto) FileSourceDto(org.sonar.db.source.FileSourceDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Test(org.junit.Test)

Example 80 with RuleDefinitionDto

use of org.sonar.db.rule.RuleDefinitionDto in project sonarqube by SonarSource.

the class PurgeDaoTest method insertProjectWithBranchAndRelatedData.

private ComponentDto insertProjectWithBranchAndRelatedData() {
    RuleDefinitionDto rule = db.rules().insert();
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto branch = db.components().insertProjectBranch(project);
    ComponentDto module = db.components().insertComponent(newModuleDto(branch));
    ComponentDto subModule = db.components().insertComponent(newModuleDto(module));
    ComponentDto file = db.components().insertComponent(newFileDto(subModule));
    db.issues().insert(rule, branch, file);
    db.issues().insert(rule, branch, subModule);
    db.issues().insert(rule, branch, module);
    return project;
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto)

Aggregations

RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)654 Test (org.junit.Test)583 ComponentDto (org.sonar.db.component.ComponentDto)305 IssueDto (org.sonar.db.issue.IssueDto)219 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)144 UserDto (org.sonar.db.user.UserDto)96 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)93 DbTester (org.sonar.db.DbTester)93 System2 (org.sonar.api.utils.System2)91 UserSessionRule (org.sonar.server.tester.UserSessionRule)84 List (java.util.List)80 Rule (org.junit.Rule)77 DbClient (org.sonar.db.DbClient)68 RuleParamDto (org.sonar.db.rule.RuleParamDto)67 Mockito.mock (org.mockito.Mockito.mock)65 TestRequest (org.sonar.server.ws.TestRequest)64 Consumer (java.util.function.Consumer)63 EsTester (org.sonar.server.es.EsTester)61 Random (java.util.Random)60 RuleType (org.sonar.api.rules.RuleType)60