Search in sources :

Example 6 with RuleDefinitionDto

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

the class RuleRepositoryImplTest method persist_new_externally_defined_Rules.

@Test
public void persist_new_externally_defined_Rules() {
    underTest = new RuleRepositoryImpl(adHocRuleCreator, db.getDbClient());
    RuleKey ruleKey = RuleKey.of("external_eslint", "no-cond-assign");
    underTest.addOrUpdateAddHocRuleIfNeeded(ruleKey, () -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));
    underTest.saveOrUpdateAddHocRules(db.getSession());
    db.commit();
    Optional<RuleDefinitionDto> ruleDefinitionDto = db.getDbClient().ruleDao().selectDefinitionByKey(db.getSession(), ruleKey);
    assertThat(ruleDefinitionDto).isPresent();
    Rule rule = underTest.getByKey(ruleKey);
    assertThat(rule).isNotNull();
    assertThat(underTest.getByUuid(ruleDefinitionDto.get().getUuid())).isNotNull();
    verify(ruleIndexer).commitAndIndex(db.getSession(), ruleDefinitionDto.get().getUuid());
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 7 with RuleDefinitionDto

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

the class RuleRepositoryImplTest method accept_new_externally_defined_Rules.

@Test
public void accept_new_externally_defined_Rules() {
    RuleKey ruleKey = RuleKey.of("external_eslint", "no-cond-assign");
    underTest.addOrUpdateAddHocRuleIfNeeded(ruleKey, () -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));
    assertThat(underTest.getByKey(ruleKey)).isNotNull();
    assertThat(underTest.getByKey(ruleKey).getType()).isNull();
    RuleDao ruleDao = dbClient.ruleDao();
    Optional<RuleDefinitionDto> ruleDefinitionDto = ruleDao.selectDefinitionByKey(dbClient.openSession(false), ruleKey);
    assertThat(ruleDefinitionDto).isNotPresent();
}
Also used : RuleDao(org.sonar.db.rule.RuleDao) RuleKey(org.sonar.api.rule.RuleKey) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 8 with RuleDefinitionDto

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

the class PersistIssuesStepTest method add_change.

@Test
public void add_change() {
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    RuleDefinitionDto rule = db.rules().insert();
    IssueDto issue = db.issues().insert(rule, project, file, i -> i.setStatus(STATUS_OPEN).setResolution(null).setCreatedAt(NOW - 1_000_000_000L).setUpdatedAt(NOW - 1_000_000_000L));
    DiskCache.CacheAppender issueCacheAppender = protoIssueCache.newAppender();
    issueCacheAppender.append(issue.toDefaultIssue().setStatus(STATUS_CLOSED).setResolution(RESOLUTION_FIXED).setSelectedAt(NOW).setNew(false).setChanged(true).setCurrentChange(new FieldDiffs().setIssueKey("ISSUE").setUserUuid("john_uuid").setDiff("technicalDebt", null, 1L).setCreationDate(new Date(NOW)))).close();
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    IssueChangeDto issueChangeDto = db.getDbClient().issueChangeDao().selectByIssueKeys(db.getSession(), singletonList(issue.getKey())).get(0);
    assertThat(issueChangeDto).extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserUuid, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey, IssueChangeDto::getIssueChangeCreationDate).containsOnly(IssueChangeDto.TYPE_FIELD_CHANGE, "john_uuid", "technicalDebt=1", issue.getKey(), NOW);
    assertThat(context.getStatistics().getAll()).contains(entry("inserts", "0"), entry("updates", "1"), entry("merged", "0"));
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FieldDiffs(org.sonar.core.issue.FieldDiffs) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DiskCache(org.sonar.ce.task.projectanalysis.util.cache.DiskCache) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Date(java.util.Date) Test(org.junit.Test)

Example 9 with RuleDefinitionDto

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

the class PersistIssuesStepTest method insert_merged_issue.

@Test
public void insert_merged_issue() {
    RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
    db.rules().insert(rule);
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project, null));
    when(system2.now()).thenReturn(NOW);
    String issueKey = "ISSUE-3";
    protoIssueCache.newAppender().append(new DefaultIssue().setKey(issueKey).setType(RuleType.CODE_SMELL).setRuleKey(rule.getKey()).setComponentUuid(file.uuid()).setComponentKey(file.getKey()).setProjectUuid(project.uuid()).setProjectKey(project.getKey()).setSeverity(BLOCKER).setStatus(STATUS_OPEN).setNew(true).setIsOnReferencedBranch(true).setIsOnChangedLine(true).setCopied(true).setType(RuleType.BUG).setCreationDate(new Date(NOW)).setSelectedAt(NOW).addComment(new DefaultIssueComment().setKey("COMMENT").setIssueKey(issueKey).setUserUuid("john_uuid").setMarkdownText("Some text").setUpdatedAt(new Date(NOW)).setCreatedAt(new Date(NOW)).setNew(true)).setCurrentChange(new FieldDiffs().setIssueKey(issueKey).setUserUuid("john_uuid").setDiff("technicalDebt", null, 1L).setCreationDate(new Date(NOW)))).close();
    TestComputationStepContext context = new TestComputationStepContext();
    underTest.execute(context);
    IssueDto result = dbClient.issueDao().selectOrFailByKey(session, issueKey);
    assertThat(result.getKey()).isEqualTo(issueKey);
    assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
    assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
    assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
    assertThat(result.getSeverity()).isEqualTo(BLOCKER);
    assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
    assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
    assertThat(result.isNewCodeReferenceIssue()).isTrue();
    List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList(issueKey));
    assertThat(changes).extracting(IssueChangeDto::getChangeType).containsExactly(IssueChangeDto.TYPE_COMMENT, IssueChangeDto.TYPE_FIELD_CHANGE);
    assertThat(context.getStatistics().getAll()).contains(entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"));
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FieldDiffs(org.sonar.core.issue.FieldDiffs) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Date(java.util.Date) DefaultIssueComment(org.sonar.core.issue.DefaultIssueComment) Test(org.junit.Test)

Example 10 with RuleDefinitionDto

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

the class SendIssueNotificationsStepTest method sends_one_issue_change_notification_every_1000_issues.

@Test
public void sends_one_issue_change_notification_every_1000_issues() {
    UserDto user = db.users().insertUser();
    ComponentDto project = newPrivateProjectDto().setDbKey(PROJECT.getDbKey()).setLongName(PROJECT.getName());
    ComponentDto file = newFileDto(project).setDbKey(FILE.getDbKey()).setLongName(FILE.getName());
    RuleDefinitionDto ruleDefinitionDto = newRule();
    RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
    List<DefaultIssue> issues = IntStream.range(0, 2001 + new Random().nextInt(10)).mapToObj(i -> newIssue(ruleDefinitionDto, project, file).setKee("uuid_" + i).setType(randomTypeExceptHotspot).toDefaultIssue().setNew(false).setChanged(true).setSendNotifications(true).setAssigneeUuid(user.getUuid())).collect(toList());
    DiskCache.CacheAppender cacheAppender = protoIssueCache.newAppender();
    issues.forEach(cacheAppender::append);
    cacheAppender.close();
    analysisMetadataHolder.setProject(Project.from(project));
    NewIssuesFactoryCaptor newIssuesFactoryCaptor = new NewIssuesFactoryCaptor(() -> mock(IssuesChangesNotification.class));
    when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenAnswer(newIssuesFactoryCaptor);
    when(notificationService.hasProjectSubscribersForTypes(PROJECT.getUuid(), NOTIF_TYPES)).thenReturn(true);
    when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
    underTest.execute(new TestComputationStepContext());
    verify(notificationFactory, times(3)).newIssuesChangesNotification(anySet(), anyMap());
    assertThat(newIssuesFactoryCaptor.issuesSetCaptor).hasSize(3);
    assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(0)).hasSize(1000);
    assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(1)).hasSize(1000);
    assertThat(newIssuesFactoryCaptor.issuesSetCaptor.get(2)).hasSize(issues.size() - 2000);
    assertThat(newIssuesFactoryCaptor.assigneeCacheCaptor).hasSize(3);
    assertThat(newIssuesFactoryCaptor.assigneeCacheCaptor).containsOnly(newIssuesFactoryCaptor.assigneeCacheCaptor.iterator().next());
    ArgumentCaptor<Collection> collectionCaptor = forClass(Collection.class);
    verify(notificationService, times(3)).deliverEmails(collectionCaptor.capture());
    assertThat(collectionCaptor.getAllValues()).hasSize(3);
    assertThat(collectionCaptor.getAllValues().get(0)).hasSize(1);
    assertThat(collectionCaptor.getAllValues().get(1)).hasSize(1);
    assertThat(collectionCaptor.getAllValues().get(2)).hasSize(1);
    verify(notificationService, times(3)).deliver(any(IssuesChangesNotification.class));
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) Collections.shuffle(java.util.Collections.shuffle) NOTIF_TYPES(org.sonar.ce.task.projectanalysis.step.SendIssueNotificationsStep.NOTIF_TYPES) Duration(org.sonar.api.utils.Duration) NewIssuesNotification(org.sonar.server.issue.notification.NewIssuesNotification) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) NewIssuesStatistics(org.sonar.server.issue.notification.NewIssuesStatistics) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) ComputationStep(org.sonar.ce.task.step.ComputationStep) Collections.singleton(java.util.Collections.singleton) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) IssueTesting.newIssue(org.sonar.db.issue.IssueTesting.newIssue) Map(java.util.Map) AnalysisMetadataHolderRule(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule) MyNewIssuesNotification(org.sonar.server.issue.notification.MyNewIssuesNotification) ReportComponent.builder(org.sonar.ce.task.projectanalysis.component.ReportComponent.builder) Mockito.doReturn(org.mockito.Mockito.doReturn) NotificationService(org.sonar.server.notification.NotificationService) ProtoIssueCache(org.sonar.ce.task.projectanalysis.issue.ProtoIssueCache) DbTester(org.sonar.db.DbTester) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) Notification(org.sonar.api.notifications.Notification) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Set(java.util.Set) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) BRANCH(org.sonar.db.component.BranchType.BRANCH) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) DistributedMetricStatsInt(org.sonar.server.issue.notification.DistributedMetricStatsInt) RuleKey(org.sonar.api.rule.RuleKey) TreeRootHolderRule(org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule) Mockito.any(org.mockito.Mockito.any) Arrays.stream(java.util.Arrays.stream) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) ComponentTesting.newBranchComponent(org.sonar.db.component.ComponentTesting.newBranchComponent) Supplier(java.util.function.Supplier) RuleTesting.newRule(org.sonar.db.rule.RuleTesting.newRule) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) Stream.concat(java.util.stream.Stream.concat) DefaultBranchImpl(org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl) Before(org.junit.Before) Component(org.sonar.ce.task.projectanalysis.component.Component) Tuple(org.assertj.core.groups.Tuple) BranchType(org.sonar.db.component.BranchType) RandomUtils.nextInt(org.apache.commons.lang.math.RandomUtils.nextInt) DefaultIssue(org.sonar.core.issue.DefaultIssue) Type(org.sonar.ce.task.projectanalysis.component.Component.Type) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) DiskCache(org.sonar.ce.task.projectanalysis.util.cache.DiskCache) Mockito.verify(org.mockito.Mockito.verify) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Collectors.toList(java.util.stream.Collectors.toList) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) ArgumentCaptor.forClass(org.mockito.ArgumentCaptor.forClass) NotificationFactory(org.sonar.ce.task.projectanalysis.notification.NotificationFactory) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) TemporaryFolder(org.junit.rules.TemporaryFolder) Project(org.sonar.server.project.Project) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleType(org.sonar.api.rules.RuleType) DefaultIssue(org.sonar.core.issue.DefaultIssue) DiskCache(org.sonar.ce.task.projectanalysis.util.cache.DiskCache) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) Random(java.util.Random) Collection(java.util.Collection) ArgumentMatchers.anyCollection(org.mockito.ArgumentMatchers.anyCollection) 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