Search in sources :

Example 1 with RuleDefinitionDto

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

the class SiblingComponentsWithOpenIssuesTest method should_find_sibling_components_with_open_issues_from_pullrequest.

@Test
public void should_find_sibling_components_with_open_issues_from_pullrequest() {
    ComponentDto project = db.components().insertPublicProject();
    setRoot(project);
    setBranch(BranchType.BRANCH);
    ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST), b -> b.setMergeBranchUuid(project.uuid()));
    RuleDefinitionDto rule = db.rules().insert();
    ComponentDto fileWithResolvedIssueOnPullrequest = db.components().insertComponent(ComponentTesting.newFileDto(pullRequest, null));
    db.issues().insert(rule, pullRequest, fileWithResolvedIssueOnPullrequest, i -> i.setStatus("RESOLVED"));
    underTest = new SiblingComponentsWithOpenIssues(treeRootHolder, metadataHolder, db.getDbClient());
    assertThat(underTest.getUuids(fileWithResolvedIssueOnPullrequest.getKey())).hasSize(1);
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 2 with RuleDefinitionDto

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

the class ComponentIssuesLoaderTest method loadClosedIssues_returns_only_closed_issues_with_close_date_is_from_30_days_ago.

private void loadClosedIssues_returns_only_closed_issues_with_close_date_is_from_30_days_ago(ComponentIssuesLoader underTest) {
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
    RuleDefinitionDto rule = db.rules().insert(t -> t.setType(CODE_SMELL));
    Date[] issueDates = new Date[] { addDays(NOW, -10), addDays(NOW, -31), addDays(NOW, -30), DATE_LIMIT_30_DAYS_BACK_MIDNIGHT, addDays(NOW, -29), addDays(NOW, -60) };
    IssueDto[] issues = Arrays.stream(issueDates).map(issueDate -> {
        IssueDto closedIssue = db.issues().insert(rule, project, file, t -> t.setStatus(STATUS_CLOSED).setIssueCloseDate(issueDate).setType(CODE_SMELL));
        db.issues().insertFieldDiffs(closedIssue, newToClosedDiffsWithLine(issueDate, 10));
        return closedIssue;
    }).toArray(IssueDto[]::new);
    when(system2.now()).thenReturn(NOW.getTime());
    List<DefaultIssue> defaultIssues = underTest.loadClosedIssues(file.uuid());
    assertThat(defaultIssues).extracting(DefaultIssue::key).containsOnly(issues[0].getKey(), issues[2].getKey(), issues[3].getKey(), issues[4].getKey());
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) MapSettings(org.sonar.api.config.internal.MapSettings) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) STATUS_CLOSED(org.sonar.api.issue.Issue.STATUS_CLOSED) ArrayList(java.util.ArrayList) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ImmutableList(com.google.common.collect.ImmutableList) Configuration(org.sonar.api.config.Configuration) ComponentTesting(org.sonar.db.component.ComponentTesting) DateUtils.addDays(org.sonar.api.utils.DateUtils.addDays) Nullable(javax.annotation.Nullable) DbTester(org.sonar.db.DbTester) DefaultIssue(org.sonar.core.issue.DefaultIssue) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Collections.emptyList(java.util.Collections.emptyList) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FieldDiffs(org.sonar.core.issue.FieldDiffs) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) Issue(org.sonar.api.issue.Issue) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) Collections(java.util.Collections) CODE_SMELL(org.sonar.api.rules.RuleType.CODE_SMELL) Mockito.mock(org.mockito.Mockito.mock) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date)

Example 3 with RuleDefinitionDto

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

the class ComponentIssuesLoaderTest method loadClosedIssues_returns_single_DefaultIssue_with_null_line_if_first_row_has_no_line_diff.

@Test
public void loadClosedIssues_returns_single_DefaultIssue_with_null_line_if_first_row_has_no_line_diff() {
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
    RuleDefinitionDto rule = db.rules().insert(t -> t.setType(CODE_SMELL));
    Date issueDate = addDays(NOW, -10);
    IssueDto issue = db.issues().insert(rule, project, file, t -> t.setStatus(STATUS_CLOSED).setIssueCloseDate(issueDate).setType(CODE_SMELL));
    db.issues().insertFieldDiffs(issue, newToClosedDiffsWithLine(issueDate, 10));
    db.issues().insertFieldDiffs(issue, newToClosedDiffsWithLine(addDays(issueDate, 2), null));
    db.issues().insertFieldDiffs(issue, newToClosedDiffsWithLine(addDays(issueDate, 1), 30));
    when(system2.now()).thenReturn(NOW.getTime());
    ComponentIssuesLoader underTest = newComponentIssuesLoader(newEmptySettings());
    List<DefaultIssue> defaultIssues = underTest.loadClosedIssues(file.uuid());
    assertThat(defaultIssues).hasSize(1);
    assertThat(defaultIssues.iterator().next().getLine()).isNull();
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) Test(org.junit.Test)

Example 4 with RuleDefinitionDto

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

the class ComponentIssuesLoaderTest method loadClosedIssues_returns_single_DefaultIssue_by_issue_based_on_first_row.

@Test
public void loadClosedIssues_returns_single_DefaultIssue_by_issue_based_on_first_row() {
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
    RuleDefinitionDto rule = db.rules().insert(t -> t.setType(CODE_SMELL));
    Date issueDate = addDays(NOW, -10);
    IssueDto issue = db.issues().insert(rule, project, file, t -> t.setStatus(STATUS_CLOSED).setIssueCloseDate(issueDate).setType(CODE_SMELL));
    db.issues().insertFieldDiffs(issue, newToClosedDiffsWithLine(issueDate, 10));
    db.issues().insertFieldDiffs(issue, newToClosedDiffsWithLine(addDays(issueDate, 3), 20));
    db.issues().insertFieldDiffs(issue, newToClosedDiffsWithLine(addDays(issueDate, 1), 30));
    when(system2.now()).thenReturn(NOW.getTime());
    ComponentIssuesLoader underTest = newComponentIssuesLoader(newEmptySettings());
    List<DefaultIssue> defaultIssues = underTest.loadClosedIssues(file.uuid());
    assertThat(defaultIssues).hasSize(1);
    assertThat(defaultIssues.iterator().next().getLine()).isEqualTo(20);
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Date(java.util.Date) Test(org.junit.Test)

Example 5 with RuleDefinitionDto

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

the class ComponentIssuesLoaderTest method loadLatestDiffChangesForReopeningOfClosedIssues_adds_2_diff_changes_if_most_recent_status_and_resolution_are_not_the_same_diff.

@Test
public void loadLatestDiffChangesForReopeningOfClosedIssues_adds_2_diff_changes_if_most_recent_status_and_resolution_are_not_the_same_diff() {
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
    RuleDefinitionDto rule = db.rules().insert();
    IssueDto issue = db.issues().insert(rule, project, file);
    db.issues().insertChange(issue, t -> t.setChangeData(randomDiffWith("status", "valStatus1")).setIssueChangeCreationDate(5));
    db.issues().insertChange(issue, t -> t.setChangeData(randomDiffWith("status", "valStatus2", "resolution", "valRes2")).setIssueChangeCreationDate(19));
    db.issues().insertChange(issue, t -> t.setChangeData(randomDiffWith("status", "valStatus3")).setIssueChangeCreationDate(20));
    db.issues().insertChange(issue, t -> t.setChangeData(randomDiffWith("resolution", "valRes4")).setIssueChangeCreationDate(13));
    ComponentIssuesLoader underTest = new ComponentIssuesLoader(dbClient, null, /* not used in method */
    null, /* not used in method */
    newConfiguration("0"), null);
    DefaultIssue defaultIssue = new DefaultIssue().setKey(issue.getKey());
    underTest.loadLatestDiffChangesForReopeningOfClosedIssues(ImmutableList.of(defaultIssue));
    assertThat(defaultIssue.changes()).hasSize(2);
    assertThat(defaultIssue.changes()).extracting(t -> t.get("status")).filteredOn(t -> hasValue(t, "valStatus3")).hasSize(1);
    assertThat(defaultIssue.changes()).extracting(t -> t.get("resolution")).filteredOn(t -> hasValue(t, "valRes2")).hasSize(1);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) MapSettings(org.sonar.api.config.internal.MapSettings) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) STATUS_CLOSED(org.sonar.api.issue.Issue.STATUS_CLOSED) ArrayList(java.util.ArrayList) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ImmutableList(com.google.common.collect.ImmutableList) Configuration(org.sonar.api.config.Configuration) ComponentTesting(org.sonar.db.component.ComponentTesting) DateUtils.addDays(org.sonar.api.utils.DateUtils.addDays) Nullable(javax.annotation.Nullable) DbTester(org.sonar.db.DbTester) DefaultIssue(org.sonar.core.issue.DefaultIssue) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Collections.emptyList(java.util.Collections.emptyList) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FieldDiffs(org.sonar.core.issue.FieldDiffs) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) Issue(org.sonar.api.issue.Issue) DateUtils.parseDateTime(org.sonar.api.utils.DateUtils.parseDateTime) Collections(java.util.Collections) CODE_SMELL(org.sonar.api.rules.RuleType.CODE_SMELL) Mockito.mock(org.mockito.Mockito.mock) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) Test(org.junit.Test)

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