Search in sources :

Example 96 with RuleDefinitionDto

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

the class CachingRuleFinderTest method findAll_returns_all_rules_with_exact_same_rulekey_and_order_them_most_recent_first.

@Test
public void findAll_returns_all_rules_with_exact_same_rulekey_and_order_them_most_recent_first() {
    String ruleKey = "ABCD";
    RuleDefinitionDto[] sameRuleKey = { dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setUpdatedAt(system2.now())) };
    RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now()));
    CachingRuleFinder underTest = new CachingRuleFinder(dbClient);
    assertThat(underTest.findAll(RuleQuery.create().withKey(ruleKey))).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(sameRuleKey[1].getKey(), sameRuleKey[0].getKey());
    assertThat(underTest.findAll(RuleQuery.create().withKey(otherRule.getRuleKey()))).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(otherRule.getKey());
    assertThat(underTest.findAll(RuleQuery.create().withKey(ruleKey.toLowerCase()))).isEmpty();
    assertThat(underTest.findAll(RuleQuery.create().withKey(randomAlphabetic(3)))).isEmpty();
}
Also used : Rule(org.sonar.api.rules.Rule) Arrays(java.util.Arrays) RuleQuery(org.sonar.api.rules.RuleQuery) RuleDao(org.sonar.db.rule.RuleDao) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) DbSession(org.sonar.db.DbSession) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Nullable(javax.annotation.Nullable) Before(org.junit.Before) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) AlwaysIncreasingSystem2(org.sonar.api.impl.utils.AlwaysIncreasingSystem2) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RuleParamDto(org.sonar.db.rule.RuleParamDto) RuleKey(org.sonar.api.rule.RuleKey) Mockito.mock(org.mockito.Mockito.mock) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 97 with RuleDefinitionDto

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

the class CachingRuleFinderTest method findByKey_returns_all_loaded_rules.

@Test
public void findByKey_returns_all_loaded_rules() {
    for (int i = 0; i < ruleDefinitions.length; i++) {
        RuleDefinitionDto ruleDefinition = ruleDefinitions[i];
        RuleParamDto ruleParam = ruleParams[i];
        org.sonar.api.rules.Rule rule = underTest.findByKey(ruleDefinition.getKey());
        verifyRule(rule, ruleDefinition, ruleParam);
        assertThat(underTest.findByKey(ruleDefinition.getRepositoryKey(), ruleDefinition.getRuleKey())).isSameAs(rule);
    }
}
Also used : Rule(org.sonar.api.rules.Rule) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) Test(org.junit.Test)

Example 98 with RuleDefinitionDto

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

the class IssueIndexFacetsTest method facets_on_languages.

@Test
public void facets_on_languages() {
    ComponentDto project = newPrivateProjectDto();
    ComponentDto file = newFileDto(project, null);
    RuleDefinitionDto ruleDefinitionDto = newRule();
    db.rules().insert(ruleDefinitionDto);
    indexIssues(newDoc("I1", file).setRuleUuid(ruleDefinitionDto.getUuid()).setLanguage("xoo"));
    assertThatFacetHasOnly(IssueQuery.builder(), "languages", entry("xoo", 1L));
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 99 with RuleDefinitionDto

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

the class BulkChangeAction method toNotification.

@CheckForNull
private ChangedIssue toNotification(BulkChangeData bulkChangeData, Map<String, UserDto> userDtoByUuid, DefaultIssue issue) {
    BranchDto branchDto = bulkChangeData.branchesByProjectUuid.get(issue.projectUuid());
    if (!hasNotificationSupport(branchDto)) {
        return null;
    }
    RuleDefinitionDto ruleDefinitionDto = bulkChangeData.rulesByKey.get(issue.ruleKey());
    ComponentDto projectDto = bulkChangeData.projectsByUuid.get(issue.projectUuid());
    if (ruleDefinitionDto == null || projectDto == null) {
        return null;
    }
    Optional<UserDto> assignee = Optional.ofNullable(issue.assignee()).map(userDtoByUuid::get);
    return new ChangedIssue.Builder(issue.key()).setNewStatus(issue.status()).setNewResolution(issue.resolution()).setAssignee(assignee.map(u -> new User(u.getUuid(), u.getLogin(), u.getName())).orElse(null)).setRule(new IssuesChangesNotificationBuilder.Rule(ruleDefinitionDto.getKey(), RuleType.valueOfNullable(ruleDefinitionDto.getType()), ruleDefinitionDto.getName())).setProject(new Project.Builder(projectDto.uuid()).setKey(projectDto.getKey()).setProjectName(projectDto.name()).setBranchName(branchDto.isMain() ? null : branchDto.getKey()).build()).build();
}
Also used : PARAM_SET_SEVERITY(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SET_SEVERITY) Date(java.util.Date) ActionContext(org.sonar.server.issue.ActionContext) BUG(org.sonar.api.rules.RuleType.BUG) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) Loggers(org.sonar.api.utils.log.Loggers) SET_SEVERITY_KEY(org.sonar.server.issue.SetSeverityAction.SET_SEVERITY_KEY) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PARAM_ADD_TAGS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ADD_TAGS) WebService(org.sonar.api.server.ws.WebService) IssueChangePostProcessor(org.sonar.server.issue.IssueChangePostProcessor) Collectors.toMap(java.util.stream.Collectors.toMap) UUID_EXAMPLE_01(org.sonar.core.util.Uuids.UUID_EXAMPLE_01) BLOCKER(org.sonar.api.rule.Severity.BLOCKER) UUID_EXAMPLE_02(org.sonar.core.util.Uuids.UUID_EXAMPLE_02) SET_TYPE_KEY(org.sonar.server.issue.SetTypeAction.SET_TYPE_KEY) TRANSITION_PARAMETER(org.sonar.server.issue.TransitionAction.TRANSITION_PARAMETER) Map(java.util.Map) PARAM_ASSIGN(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ASSIGN) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) PARAM_REMOVE_TAGS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_REMOVE_TAGS) PARAM_SET_TYPE(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SET_TYPE) NotificationManager(org.sonar.server.notification.NotificationManager) PARAM_COMMENT(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMMENT) PARAM_DO_TRANSITION(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_DO_TRANSITION) TAGS_PARAMETER(org.sonar.server.issue.AbstractChangeTagsAction.TAGS_PARAMETER) System2(org.sonar.api.utils.System2) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) OPEN_AS_VULNERABILITY(org.sonar.api.issue.DefaultTransitions.OPEN_AS_VULNERABILITY) Predicate(java.util.function.Predicate) ASSIGNEE_PARAMETER(org.sonar.server.issue.AssignAction.ASSIGNEE_PARAMETER) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ImmutableMap.of(com.google.common.collect.ImmutableMap.of) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) AddTagsAction(org.sonar.server.issue.AddTagsAction) PARAM_SEND_NOTIFICATIONS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SEND_NOTIFICATIONS) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ACTION_BULK_CHANGE(org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_BULK_CHANGE) RuleKey(org.sonar.api.rule.RuleKey) Function.identity(java.util.function.Function.identity) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BranchDto(org.sonar.db.component.BranchDto) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) Issues(org.sonarqube.ws.Issues) MAX_PAGE_SIZE(org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE) SET_AS_IN_REVIEW(org.sonar.api.issue.DefaultTransitions.SET_AS_IN_REVIEW) HashMap(java.util.HashMap) PARAM_ISSUES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUES) RuleType(org.sonar.api.rules.RuleType) COMMENT_PROPERTY(org.sonar.server.issue.CommentAction.COMMENT_PROPERTY) HashSet(java.util.HashSet) Action(org.sonar.server.issue.Action) WebIssueStorage(org.sonar.server.issue.WebIssueStorage) RemoveTagsAction(org.sonar.server.issue.RemoveTagsAction) MoreCollectors.toSet(org.sonar.core.util.stream.MoreCollectors.toSet) Severity(org.sonar.api.rule.Severity) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) REOPEN(org.sonar.api.issue.DefaultTransitions.REOPEN) COMMENT_KEY(org.sonar.server.issue.CommentAction.COMMENT_KEY) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) BranchType(org.sonar.db.component.BranchType) DefaultIssue(org.sonar.core.issue.DefaultIssue) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) Consumer(java.util.function.Consumer) UserRole(org.sonar.api.web.UserRole) DO_TRANSITION_KEY(org.sonar.server.issue.TransitionAction.DO_TRANSITION_KEY) DefaultTransitions(org.sonar.api.issue.DefaultTransitions) RESOLVE_AS_REVIEWED(org.sonar.api.issue.DefaultTransitions.RESOLVE_AS_REVIEWED) Change(org.sonar.api.server.ws.Change) AssignAction(org.sonar.server.issue.AssignAction) UserSession(org.sonar.server.user.UserSession) SEVERITY_PARAMETER(org.sonar.server.issue.SetSeverityAction.SEVERITY_PARAMETER) CheckForNull(javax.annotation.CheckForNull) TYPE_PARAMETER(org.sonar.server.issue.SetTypeAction.TYPE_PARAMETER) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) BranchDto(org.sonar.db.component.BranchDto) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) UserDto(org.sonar.db.user.UserDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) CheckForNull(javax.annotation.CheckForNull)

Example 100 with RuleDefinitionDto

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

the class IssueUpdater method doSaveIssue.

private IssueDto doSaveIssue(DbSession session, DefaultIssue issue, IssueChangeContext context, Optional<RuleDefinitionDto> rule, ComponentDto project, BranchDto branchDto) {
    IssueDto issueDto = issueStorage.save(session, singletonList(issue)).iterator().next();
    if (// since this method is called after an update of the issue, date should never be null
    issue.updateDate() == null || // name of rule is displayed in notification, rule must therefor be present
    !rule.isPresent() || // notification are not supported on PRs
    !hasNotificationSupport(branchDto)) {
        return issueDto;
    }
    Optional<UserDto> assignee = Optional.ofNullable(issue.assignee()).map(assigneeUuid -> dbClient.userDao().selectByUuid(session, assigneeUuid));
    UserDto author = Optional.ofNullable(context.userUuid()).map(authorUuid -> dbClient.userDao().selectByUuid(session, authorUuid)).orElseThrow(() -> new IllegalStateException("Can not find dto for change author " + context.userUuid()));
    IssuesChangesNotificationBuilder notificationBuilder = new IssuesChangesNotificationBuilder(singleton(new ChangedIssue.Builder(issue.key()).setNewResolution(issue.resolution()).setNewStatus(issue.status()).setAssignee(assignee.map(assigneeDto -> new User(assigneeDto.getUuid(), assigneeDto.getLogin(), assigneeDto.getName())).orElse(null)).setRule(rule.map(r -> new Rule(r.getKey(), RuleType.valueOfNullable(r.getType()), r.getName())).get()).setProject(new Project.Builder(project.uuid()).setKey(project.getKey()).setProjectName(project.name()).setBranchName(branchDto.isMain() ? null : branchDto.getKey()).build()).build()), new UserChange(issue.updateDate().getTime(), new User(author.getUuid(), author.getLogin(), author.getName())));
    notificationService.scheduleForSending(notificationSerializer.serialize(notificationBuilder));
    return issueDto;
}
Also used : BranchDto(org.sonar.db.component.BranchDto) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) RuleStatus(org.sonar.api.rule.RuleStatus) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) RuleType(org.sonar.api.rules.RuleType) Collections.singletonList(java.util.Collections.singletonList) WebIssueStorage(org.sonar.server.issue.WebIssueStorage) IssueChangePostProcessor(org.sonar.server.issue.IssueChangePostProcessor) Collections.singleton(java.util.Collections.singleton) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) NotificationManager(org.sonar.server.notification.NotificationManager) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) DefaultIssue(org.sonar.core.issue.DefaultIssue) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DbClient(org.sonar.db.DbClient) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) RuleKey(org.sonar.api.rule.RuleKey) Optional(java.util.Optional) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) UserDto(org.sonar.db.user.UserDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) IssueDto(org.sonar.db.issue.IssueDto) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule)

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