Search in sources :

Example 1 with BadRequestException

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);
}
Also used : ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) BadRequestException(org.sonar.server.exceptions.BadRequestException) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 2 with BadRequestException

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();
    }
}
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) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 3 with BadRequestException

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;
}
Also used : ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) BadRequestException(org.sonar.server.exceptions.BadRequestException) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 4 with BadRequestException

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");
        }
    }
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) BadRequestException(org.sonar.server.exceptions.BadRequestException) OrganizationDto(org.sonar.db.organization.OrganizationDto) Test(org.junit.Test)

Example 5 with BadRequestException

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");
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) 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