Search in sources :

Example 61 with RuleDefinitionDto

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

the class IssueQueryFactory method create.

public IssueQuery create(SearchRequest request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        final ZoneId timeZone = parseTimeZone(request.getTimeZone()).orElse(clock.getZone());
        Collection<RuleDefinitionDto> ruleDefinitionDtos = ruleKeysToRuleId(dbSession, request.getRules());
        Collection<String> ruleUuids = ruleDefinitionDtos.stream().map(RuleDefinitionDto::getUuid).collect(Collectors.toSet());
        if (request.getRules() != null && request.getRules().stream().collect(toSet()).size() != ruleDefinitionDtos.size()) {
            ruleUuids.add("non-existing-uuid");
        }
        IssueQuery.Builder builder = IssueQuery.builder().issueKeys(request.getIssues()).severities(request.getSeverities()).statuses(request.getStatuses()).resolutions(request.getResolutions()).resolved(request.getResolved()).rules(ruleDefinitionDtos).ruleUuids(ruleUuids).assigneeUuids(request.getAssigneeUuids()).authors(request.getAuthors()).scopes(request.getScopes()).languages(request.getLanguages()).tags(request.getTags()).types(request.getTypes()).owaspTop10(request.getOwaspTop10()).sansTop25(request.getSansTop25()).cwe(request.getCwe()).sonarsourceSecurity(request.getSonarsourceSecurity()).assigned(request.getAssigned()).createdAt(parseStartingDateOrDateTime(request.getCreatedAt(), timeZone)).createdBefore(parseEndingDateOrDateTime(request.getCreatedBefore(), timeZone)).facetMode(request.getFacetMode()).timeZone(timeZone);
        List<ComponentDto> allComponents = new ArrayList<>();
        boolean effectiveOnComponentOnly = mergeDeprecatedComponentParameters(dbSession, request, allComponents);
        addComponentParameters(builder, dbSession, effectiveOnComponentOnly, allComponents, request);
        setCreatedAfterFromRequest(dbSession, builder, request, allComponents, timeZone);
        String sort = request.getSort();
        if (!isNullOrEmpty(sort)) {
            builder.sort(sort);
            builder.asc(request.getAsc());
        }
        return builder.build();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ZoneId(java.time.ZoneId) ComponentDto(org.sonar.db.component.ComponentDto) ArrayList(java.util.ArrayList) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto)

Example 62 with RuleDefinitionDto

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

the class RegisterRulesTest method do_not_remove_no_more_defined_ad_hoc_rule.

@Test
public void do_not_remove_no_more_defined_ad_hoc_rule() {
    RuleDefinitionDto rule = db.rules().insert(r -> r.setRepositoryKey("external_fake").setStatus(READY).setIsExternal(true).setIsAdHoc(true));
    execute();
    RuleDto reloaded = dbClient.ruleDao().selectByKey(db.getSession(), rule.getKey()).get();
    assertThat(reloaded.getStatus()).isEqualTo(READY);
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 63 with RuleDefinitionDto

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

the class RegisterRulesTest method rules_that_deprecate_previous_rule_must_be_recorded.

@Test
public void rules_that_deprecate_previous_rule_must_be_recorded() {
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        createRule(repo, "rule1");
        repo.done();
    });
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        createRule(repo, "newKey").addDeprecatedRuleKey("fake", "rule1").addDeprecatedRuleKey("fake", "rule2");
        repo.done();
    });
    List<RuleDefinitionDto> rules = dbClient.ruleDao().selectAllDefinitions(db.getSession());
    Set<DeprecatedRuleKeyDto> deprecatedRuleKeys = dbClient.ruleDao().selectAllDeprecatedRuleKeys(db.getSession());
    assertThat(rules).hasSize(1);
    assertThat(deprecatedRuleKeys).hasSize(2);
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 64 with RuleDefinitionDto

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

the class IssueIndexFiltersTest method filter_by_rules.

@Test
public void filter_by_rules() {
    ComponentDto project = newPrivateProjectDto();
    ComponentDto file = newFileDto(project, null);
    RuleDefinitionDto ruleDefinitionDto = newRule();
    db.rules().insert(ruleDefinitionDto);
    indexIssues(newDoc("I1", file).setRuleUuid(ruleDefinitionDto.getUuid()));
    assertThatSearchReturnsOnly(IssueQuery.builder().ruleUuids(singletonList(ruleDefinitionDto.getUuid())), "I1");
    assertThatSearchReturnsEmpty(IssueQuery.builder().ruleUuids(singletonList("uuid-abc")));
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 65 with RuleDefinitionDto

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

the class QualityProfileChangeEventServiceImplTest method distributeRuleChangeEvent.

@Test
public void distributeRuleChangeEvent() {
    QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
    // Template rule
    RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
    db.rules().insert(templateRule.getDefinition());
    // Custom rule
    RuleDefinitionDto rule1 = newCustomRule(templateRule.getDefinition()).setLanguage("xoo").setDescription("<div>line1\nline2</div>").setDescriptionFormat(MARKDOWN);
    db.rules().insert(rule1);
    ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
    ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
    activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
    Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
    ProjectDto project = db.components().insertPrivateProjectDto();
    db.qualityProfiles().associateWithProject(project, qualityProfileDto);
    underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
    ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
    verify(eventsDistributor).pushEvent(eventCaptor.capture());
    RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
    assertThat(ruleSetChangedEvent).isNotNull();
    assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent, RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects).containsExactly("RuleSetChanged", "xoo", new String[] { project.getKey() });
    assertThat(ruleSetChangedEvent.getActivatedRules()).extracting(RuleChange::getKey, RuleChange::getLanguage, RuleChange::getSeverity, RuleChange::getTemplateKey).containsExactly(tuple(rule1.getRuleKey(), "xoo", null, "template-key"));
    assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
    ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
    assertThat(actualParamChange).extracting(ParamChange::getKey, ParamChange::getValue).containsExactly("paramChangeKey", "paramChangeValue");
    assertThat(ruleSetChangedEvent.getDeactivatedRules()).isEmpty();
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ParamChange(org.sonar.core.util.ParamChange) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) 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