Search in sources :

Example 11 with BadRequestException

use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.

the class RuleCreatorMediumTest method create_custom_rule_with_invalid_parameter.

@Test
public void create_custom_rule_with_invalid_parameter() {
    // insert template rule
    RuleDto templateRule = createTemplateRuleWithIntArrayParam();
    // Create custom rule
    NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setMarkdownDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("myIntegers", "1,polop,2"));
    try {
        creator.create(newRule);
        Fail.failBecauseExceptionWasNotThrown(BadRequestException.class);
    } catch (BadRequestException iae) {
        assertThat(iae).hasMessage("Value 'polop' must be an integer.");
    }
    dbSession.clearCache();
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) BadRequestException(org.sonar.server.exceptions.BadRequestException) Test(org.junit.Test)

Example 12 with BadRequestException

use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.

the class RuleActivator method bulkActivate.

BulkChangeResult bulkActivate(RuleQuery ruleQuery, String profileKey, @Nullable String severity) {
    DbSession dbSession = db.openSession(false);
    BulkChangeResult result = new BulkChangeResult();
    try {
        Iterator<RuleKey> rules = ruleIndex.searchAll(ruleQuery);
        while (rules.hasNext()) {
            RuleKey ruleKey = rules.next();
            try {
                RuleActivation activation = new RuleActivation(ruleKey);
                activation.setSeverity(severity);
                List<ActiveRuleChange> changes = activate(dbSession, activation, profileKey);
                result.addChanges(changes);
                if (!changes.isEmpty()) {
                    result.incrementSucceeded();
                }
            } catch (BadRequestException e) {
                // other exceptions stop the bulk activation
                result.incrementFailed();
                result.getErrors().addAll(e.errors());
            }
        }
        dbSession.commit();
        activeRuleIndexer.index(result.getChanges());
    } finally {
        dbSession.close();
    }
    return result;
}
Also used : DbSession(org.sonar.db.DbSession) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) BadRequestException(org.sonar.server.exceptions.BadRequestException)

Example 13 with BadRequestException

use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.

the class QProfileReset method doReset.

/**
   * @param dbSession
   * @param profile must exist
   */
private BulkChangeResult doReset(DbSession dbSession, QualityProfileDto profile, Collection<RuleActivation> activations) {
    Preconditions.checkNotNull(profile.getId(), "Quality profile must be persisted");
    BulkChangeResult result = new BulkChangeResult(profile);
    Set<RuleKey> ruleToBeDeactivated = Sets.newHashSet();
    // Keep reference to all the activated rules before backup restore
    for (ActiveRuleDto activeRuleDto : db.activeRuleDao().selectByProfileKey(dbSession, profile.getKee())) {
        if (activeRuleDto.getInheritance() == null) {
            // inherited rules can't be deactivated
            ruleToBeDeactivated.add(activeRuleDto.getKey().ruleKey());
        }
    }
    for (RuleActivation activation : activations) {
        try {
            List<ActiveRuleChange> changes = activator.activate(dbSession, activation, profile.getKey());
            ruleToBeDeactivated.remove(activation.getRuleKey());
            result.incrementSucceeded();
            result.addChanges(changes);
        } catch (BadRequestException e) {
            result.incrementFailed();
            result.getErrors().addAll(e.errors());
        }
    }
    List<ActiveRuleChange> changes = new ArrayList<>();
    changes.addAll(result.getChanges());
    for (RuleKey ruleKey : ruleToBeDeactivated) {
        try {
            changes.addAll(activator.deactivate(dbSession, ActiveRuleKey.of(profile.getKee(), ruleKey)));
        } catch (BadRequestException e) {
        // ignore, probably a rule inherited from parent that can't be deactivated
        }
    }
    dbSession.commit();
    activeRuleIndexer.index(changes);
    return result;
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ArrayList(java.util.ArrayList) BadRequestException(org.sonar.server.exceptions.BadRequestException) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 14 with BadRequestException

use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.

the class QProfilesWsMediumTest method activate_rule_diff_languages.

@Test
public void activate_rule_diff_languages() throws Exception {
    QualityProfileDto profile = createProfile("java");
    RuleDto rule = createRule("php", "toto");
    session.commit();
    ruIndexer.index();
    // 0. Assert No Active Rule for profile
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty();
    try {
        // 1. Activate Rule
        WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION);
        request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey());
        request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString());
        request.execute();
        session.clearCache();
        fail();
    } catch (BadRequestException e) {
        assertThat(e.getMessage()).isEqualTo("Rule blah:toto and profile pjava have different languages");
    }
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) BadRequestException(org.sonar.server.exceptions.BadRequestException) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 15 with BadRequestException

use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.

the class RuleActivatorMediumTest method fail_to_activate_if_rule_with_removed_status.

@Test
public void fail_to_activate_if_rule_with_removed_status() {
    RuleDto ruleDto = db.ruleDao().selectOrFailByKey(dbSession, XOO_X1);
    ruleDto.setStatus(RuleStatus.REMOVED);
    db.ruleDao().update(dbSession, ruleDto);
    dbSession.commit();
    dbSession.clearCache();
    RuleActivation activation = new RuleActivation(XOO_X1);
    try {
        activate(activation, XOO_P1_KEY);
        fail();
    } catch (BadRequestException e) {
        assertThat(e).hasMessage("Rule was removed: xoo:x1");
        verifyZeroActiveRules(XOO_P1_KEY);
    }
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) BadRequestException(org.sonar.server.exceptions.BadRequestException) Test(org.junit.Test)

Aggregations

BadRequestException (org.sonar.server.exceptions.BadRequestException)16 Test (org.junit.Test)11 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)5 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 RuleDto (org.sonar.db.rule.RuleDto)4 ArrayList (java.util.ArrayList)3 RuleKey (org.sonar.api.rule.RuleKey)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 DbSession (org.sonar.db.DbSession)2 EmailException (org.apache.commons.mail.EmailException)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 ComponentDto (org.sonar.db.component.ComponentDto)1 OrganizationDto (org.sonar.db.organization.OrganizationDto)1 RuleParamDto (org.sonar.db.rule.RuleParamDto)1 WsTester (org.sonar.server.ws.WsTester)1