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