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());
}
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();
}
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"));
}
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"));
}
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));
}
Aggregations