use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.
the class RuleCreator method validateCustomRule.
private void validateCustomRule(NewCustomRule newRule, DbSession dbSession, RuleKey templateKey) {
List<String> errors = new ArrayList<>();
validateRuleKey(errors, newRule.ruleKey());
validateName(errors, newRule);
validateDescription(errors, newRule);
String severity = newRule.severity();
if (Strings.isNullOrEmpty(severity)) {
errors.add("The severity is missing");
} else if (!Severity.ALL.contains(severity)) {
errors.add(format("Severity \"%s\" is invalid", severity));
}
if (newRule.status() == null) {
errors.add("The status is missing");
}
for (RuleParamDto ruleParam : dbClient.ruleDao().selectRuleParamsByRuleKey(dbSession, templateKey)) {
try {
validateParam(ruleParam, newRule.parameter(ruleParam.getName()));
} catch (BadRequestException validationError) {
errors.addAll(validationError.errors());
}
}
checkRequest(errors.isEmpty(), errors);
}
use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.
the class RuleActivator method bulkDeactivate.
BulkChangeResult bulkDeactivate(RuleQuery ruleQuery, String profile) {
DbSession dbSession = db.openSession(false);
BulkChangeResult result = new BulkChangeResult();
try {
Iterator<RuleKey> rules = ruleIndex.searchAll(ruleQuery);
while (rules.hasNext()) {
try {
RuleKey ruleKey = rules.next();
ActiveRuleKey key = ActiveRuleKey.of(profile, ruleKey);
List<ActiveRuleChange> changes = deactivate(dbSession, key);
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());
return result;
} finally {
dbSession.close();
}
}
use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.
the class RuleActivator method setParent.
public List<ActiveRuleChange> setParent(DbSession dbSession, String profileKey, @Nullable String parentKey) {
QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(dbSession, profileKey);
List<ActiveRuleChange> changes = new ArrayList<>();
if (parentKey == null) {
// unset if parent is defined, else nothing to do
changes.addAll(removeParent(dbSession, profile));
} else if (profile.getParentKee() == null || !parentKey.equals(profile.getParentKee())) {
QualityProfileDto parentProfile = db.qualityProfileDao().selectOrFailByKey(dbSession, parentKey);
checkRequest(!isDescendant(dbSession, profile, parentProfile), "Descendant profile '%s' can not be selected as parent of '%s'", parentKey, profileKey);
changes.addAll(removeParent(dbSession, profile));
// set new parent
profile.setParentKee(parentKey);
db.qualityProfileDao().update(dbSession, profile);
for (ActiveRuleDto parentActiveRule : db.activeRuleDao().selectByProfileKey(dbSession, parentKey)) {
try {
RuleActivation activation = new RuleActivation(parentActiveRule.getKey().ruleKey());
changes.addAll(activate(dbSession, activation, profileKey));
} catch (BadRequestException e) {
// for example because rule status is REMOVED
// TODO return errors
}
}
}
dbSession.commit();
activeRuleIndexer.index(changes);
return changes;
}
use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.
the class ProjectDataLoaderTest method load_fails_with_BRE_if_component_is_neither_a_project_or_a_module.
@Test
public void load_fails_with_BRE_if_component_is_neither_a_project_or_a_module() {
String[][] allScopesAndQualifierButProjectAndModule = { { Scopes.PROJECT, Qualifiers.VIEW }, { Scopes.PROJECT, Qualifiers.SUBVIEW }, { Scopes.FILE, Qualifiers.PROJECT }, { Scopes.DIRECTORY, Qualifiers.DIRECTORY }, { Scopes.FILE, Qualifiers.UNIT_TEST_FILE }, { Scopes.PROJECT, "DEV" }, { Scopes.PROJECT, "DEV_PRJ" } };
OrganizationDto organizationDto = dbTester.organizations().insert();
for (String[] scopeAndQualifier : allScopesAndQualifierButProjectAndModule) {
String scope = scopeAndQualifier[0];
String qualifier = scopeAndQualifier[1];
String key = "theKey_" + scope + "_" + qualifier;
String uuid = "uuid_" + uuidCounter++;
dbClient.componentDao().insert(dbSession, new ComponentDto().setOrganizationUuid(organizationDto.getUuid()).setUuid(uuid).setUuidPath(uuid + ".").setRootUuid(uuid).setScope(scope).setQualifier(qualifier).setKey(key));
dbSession.commit();
try {
underTest.load(ProjectDataQuery.create().setModuleKey(key));
fail(format("A NotFoundException should have been raised because scope (%s) or qualifier (%s) is not project", scope, qualifier));
} catch (BadRequestException e) {
assertThat(e).hasMessage("Key '" + key + "' belongs to a component which is not a Project");
}
}
}
use of org.sonar.server.exceptions.BadRequestException in project sonarqube by SonarSource.
the class SortingTest method fail_if_unknown_field.
@Test
public void fail_if_unknown_field() {
Sorting sorting = new Sorting();
sorting.add("file");
try {
sorting.fill(new SearchRequestBuilder(mock(Client.class), SearchAction.INSTANCE), "unknown", true);
fail();
} catch (BadRequestException e) {
assertThat(e.getMessage()).isEqualTo("Bad sort field: unknown");
}
}
Aggregations