}
}
- public Collection<org.sonar.api.rules.Rule> findByIds(Collection<Integer> ruleIds) {
- List<org.sonar.api.rules.Rule> rules = newArrayList();
- if (ruleIds.isEmpty()) {
- return rules;
- }
-
- try (DbSession dbSession = dbClient.openSession(false)) {
- List<RuleDto> ruleDtos = ruleDao.selectByIds(dbSession, defaultOrganizationProvider.get().getUuid(), new ArrayList<>(ruleIds));
- return convertToRuleApi(dbSession, ruleDtos);
- }
- }
-
- public Collection<org.sonar.api.rules.Rule> findByKeys(Collection<RuleKey> ruleKeys) {
- List<org.sonar.api.rules.Rule> rules = newArrayList();
- if (ruleKeys.isEmpty()) {
- return rules;
- }
-
- try (DbSession dbSession = dbClient.openSession(false)) {
- List<RuleDto> ruleDtos = ruleDao.selectByKeys(dbSession, defaultOrganizationProvider.get().getUuid(), new ArrayList<>(ruleKeys));
- return convertToRuleApi(dbSession, ruleDtos);
- }
- }
-
@Override
@CheckForNull
public org.sonar.api.rules.Rule findByKey(RuleKey key) {
*/
package org.sonar.server.rule;
-import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleQuery;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
public class DefaultRuleFinderTest {
// should_not_find_disabled_rule_by_id
assertThat(underTest.findById(rule2.getId())).isNull();
- // should_find_by_ids
- assertThat(underTest.findByIds(newArrayList(rule2.getId(), rule3.getId()))).hasSize(2);
-
// should_find_by_key
Rule rule = underTest.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
assertThat(rule).isNotNull();
assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"))).isEmpty();
}
- @Test
- public void find_ids_including_removed_rule() {
- // find rule with id 2 is REMOVED
- assertThat(underTest.findByIds(newArrayList(rule2.getId()))).hasSize(1);
- }
-
- @Test
- public void find_keys_including_removed_rule() {
- assertThat(underTest.findByKeys(newArrayList(RuleKey.of("checkstyle", "DisabledCheck")))).hasSize(1);
-
- // find rule with id 2 is REMOVED
- assertThat(underTest.findByKeys(newArrayList(RuleKey.of("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck")))).hasSize(1);
-
- assertThat(underTest.findByKeys(Collections.<RuleKey>emptyList())).isEmpty();
- }
-
@Test
public void find_id_return_null_on_removed_rule() {
// find rule with id 2 is REMOVED