import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;
import org.sonar.jpa.session.DatabaseSessionFactory;
+import javax.annotation.CheckForNull;
+import javax.persistence.Query;
+import java.util.Collection;
+import java.util.HashMap;
import java.util.Map;
-public final class CacheRuleFinder extends DefaultRuleFinder {
+public final class CacheRuleFinder implements RuleFinder {
private BiMap<Integer, Rule> rulesById = HashBiMap.create();
private Map<String, Map<String, Rule>> rulesByKey = Maps.newHashMap();
+ private DatabaseSessionFactory sessionFactory;
+
public CacheRuleFinder(DatabaseSessionFactory sessionFactory) {
- super(sessionFactory);
+ this.sessionFactory = sessionFactory;
}
@Override
}
return repository;
}
+
+ protected final Rule doFindById(int ruleId) {
+ DatabaseSession session = sessionFactory.getSession();
+ return session.getSingleResult(
+ session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.id=:id and r.status<>:status")
+ .setParameter("id", ruleId)
+ .setParameter("status", Rule.STATUS_REMOVED
+ ),
+ null);
+ }
+
+
+ @CheckForNull
+ public Rule findByKey(RuleKey key) {
+ return findByKey(key.repository(), key.rule());
+ }
+
+
+ @CheckForNull
+ protected final Rule doFindByKey(String repositoryKey, String key) {
+ DatabaseSession session = sessionFactory.getSession();
+ return session.getSingleResult(
+ session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.key=:key and r.pluginName=:pluginName and r.status<>:status")
+ .setParameter("key", key)
+ .setParameter("pluginName", repositoryKey)
+ .setParameter("status", Rule.STATUS_REMOVED
+ ),
+ null);
+ }
+
+ public final Rule find(RuleQuery query) {
+ DatabaseSession session = sessionFactory.getSession();
+ return session.getSingleResult(createHqlQuery(session, query), null);
+ }
+
+ public final Collection<Rule> findAll(RuleQuery query) {
+ DatabaseSession session = sessionFactory.getSession();
+ return createHqlQuery(session, query).getResultList();
+ }
+
+ private Query createHqlQuery(DatabaseSession session, RuleQuery query) {
+ StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where status<>:status ");
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("status", Rule.STATUS_REMOVED);
+ if (StringUtils.isNotBlank(query.getRepositoryKey())) {
+ hql.append("AND pluginName=:repositoryKey ");
+ params.put("repositoryKey", query.getRepositoryKey());
+ }
+ if (StringUtils.isNotBlank(query.getKey())) {
+ hql.append("AND key=:key ");
+ params.put("key", query.getKey());
+ }
+ if (StringUtils.isNotBlank(query.getConfigKey())) {
+ hql.append("AND configKey=:configKey ");
+ params.put("configKey", query.getConfigKey());
+ }
+
+ Query hqlQuery = session.createQuery(hql.toString());
+ for (Map.Entry<String, Object> entry : params.entrySet()) {
+ hqlQuery.setParameter(entry.getKey(), entry.getValue());
+ }
+ return hqlQuery;
+ }
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-package org.sonar.core.rule;
-
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RuleQuery;
-import org.sonar.jpa.session.DatabaseSessionFactory;
-
-import javax.annotation.CheckForNull;
-import javax.persistence.Query;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-public class DefaultRuleFinder implements RuleFinder {
-
- private DatabaseSessionFactory sessionFactory;
-
- public DefaultRuleFinder(DatabaseSessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- public Rule findById(int ruleId) {
- return doFindById(ruleId);
- }
-
- protected final Rule doFindById(int ruleId) {
- DatabaseSession session = sessionFactory.getSession();
- return session.getSingleResult(
- session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.id=:id and r.status<>:status")
- .setParameter("id", ruleId)
- .setParameter("status", Rule.STATUS_REMOVED
- ),
- null);
- }
-
- public Collection<Rule> findByIds(Collection<Integer> ruleIds) {
- if (ruleIds.isEmpty()) {
- return Collections.emptyList();
- }
- DatabaseSession session = sessionFactory.getSession();
- StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" r where r.id in (:ids)");
- Query hqlQuery = session.createQuery(hql.toString()).setParameter("ids", ruleIds);
- return hqlQuery.getResultList();
- }
-
- @CheckForNull
- public Rule findByKey(RuleKey key) {
- return findByKey(key.repository(), key.rule());
- }
-
- @CheckForNull
- public Rule findByKey(String repositoryKey, String key) {
- return doFindByKey(repositoryKey, key);
- }
-
- @CheckForNull
- protected final Rule doFindByKey(String repositoryKey, String key) {
- DatabaseSession session = sessionFactory.getSession();
- return session.getSingleResult(
- session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.key=:key and r.pluginName=:pluginName and r.status<>:status")
- .setParameter("key", key)
- .setParameter("pluginName", repositoryKey)
- .setParameter("status", Rule.STATUS_REMOVED
- ),
- null);
- }
-
- public final Rule find(RuleQuery query) {
- DatabaseSession session = sessionFactory.getSession();
- return session.getSingleResult(createHqlQuery(session, query), null);
- }
-
- public final Collection<Rule> findAll(RuleQuery query) {
- DatabaseSession session = sessionFactory.getSession();
- return createHqlQuery(session, query).getResultList();
- }
-
- private Query createHqlQuery(DatabaseSession session, RuleQuery query) {
- StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where status<>:status ");
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("status", Rule.STATUS_REMOVED);
- if (StringUtils.isNotBlank(query.getRepositoryKey())) {
- hql.append("AND pluginName=:repositoryKey ");
- params.put("repositoryKey", query.getRepositoryKey());
- }
- if (StringUtils.isNotBlank(query.getKey())) {
- hql.append("AND key=:key ");
- params.put("key", query.getKey());
- }
- if (StringUtils.isNotBlank(query.getConfigKey())) {
- hql.append("AND configKey=:configKey ");
- params.put("configKey", query.getConfigKey());
- }
-
- Query hqlQuery = session.createQuery(hql.toString());
- for (Map.Entry<String, Object> entry : params.entrySet()) {
- hqlQuery.setParameter(entry.getKey(), entry.getValue());
- }
- return hqlQuery;
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.core.rule;
-
-import org.junit.Test;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RuleQuery;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
-import java.util.Collection;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultRuleFinderTest extends AbstractDbUnitTestCase {
-
- @Test
- public void should_find_by_id() {
- setupData("shared");
- RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- assertThat(finder.findById(3).getConfigKey()).isEqualTo("Checker/Treewalker/AnnotationUseStyleCheck");
- }
-
- @Test
- public void should_not_find_disabled_rule_by_id() {
- setupData("shared");
- RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- assertThat(finder.findById(2)).isNull();
- }
-
- @Test
- public void should_find_by_ids() {
- setupData("shared");
- DefaultRuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- // 2 is returned even its status is REMOVED
- assertThat(finder.findByIds(newArrayList(2, 3))).hasSize(2);
- }
-
- @Test
- public void should_find_by_ids_empty() {
- Collection<Integer> newArrayList = newArrayList();
- assertThat(new DefaultRuleFinder(getSessionFactory()).findByIds(newArrayList)).isEmpty();
- }
-
- @Test
- public void should_find_by_key() {
- setupData("shared");
- RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- Rule rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
- assertThat(rule).isNotNull();
- assertThat(rule.getKey()).isEqualTo(("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"));
- assertThat(rule.isEnabled()).isTrue();
- }
-
- @Test
- public void find_should_return_null_if_no_results() {
- setupData("shared");
- RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- assertThat(finder.findByKey("checkstyle", "unknown")).isNull();
- assertThat(finder.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))).isNull();
- }
-
- @Test
- public void find_repository_rules() {
- setupData("shared");
- RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- Collection<Rule> rules = finder.findAll(RuleQuery.create().withRepositoryKey("checkstyle"));
-
- assertThat(rules).hasSize(2);
- }
-
- @Test
- public void find_all_enabled() {
- setupData("shared");
- RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- Collection<Rule> rules = finder.findAll(RuleQuery.create());
-
- assertThat(rules).onProperty("id").containsOnly(1, 3, 4);
- }
-
- @Test
- public void do_not_find_disabled_rules() {
- setupData("shared");
- RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- Rule rule = finder.findByKey("checkstyle", "DisabledCheck");
- assertThat(rule).isNull();
- }
-
- @Test
- public void do_not_find_unknown_rules() {
- setupData("shared");
- RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
- Collection<Rule> rules = finder.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"));
- assertThat(rules).isEmpty();
- }
-}
+++ /dev/null
-<dataset>
- <!-- CHECKSTYLE -->
-
- <rules tags="[null]" system_tags="[null]" id="1" name="Check Header" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
- plugin_config_key="Checker/Treewalker/HeaderCheck" plugin_name="checkstyle" description="[null]" priority="4" status="READY" is_template="[false]" template_id="[null]"/>
-
- <!-- disabled rule -->
- <rules tags="[null]" system_tags="[null]" id="2" name="Disabled checked" plugin_rule_key="DisabledCheck"
- plugin_config_key="Checker/Treewalker/DisabledCheck" plugin_name="checkstyle" description="[null]" priority="4" status="REMOVED" is_template="[false]" template_id="[null]" />
-
- <rules tags="[null]" system_tags="[null]" id="3" name="Check Annotation" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck"
- plugin_config_key="Checker/Treewalker/AnnotationUseStyleCheck" plugin_name="checkstyle" description="[null]" priority="4" status="READY" is_template="[false]" template_id="[null]" />
-
-
- <!-- PMD -->
- <rules tags="[null]" system_tags="[null]" id="4" name="Call Super First" plugin_rule_key="CallSuperFirst"
- plugin_config_key="rulesets/android.xml/CallSuperFirst" plugin_name="pmd" description="[null]" priority="2" status="READY" is_template="[false]" template_id="[null]" />
-</dataset>
<useNaming>false</useNaming>
<systemProperties>
<SONAR_HOME>${project.build.directory}/dev</SONAR_HOME>
- <rails.env>development</rails.env>
<jruby.max.runtimes>3</jruby.max.runtimes>
<sonar.jdbc.url>${sonar.jdbc.url}</sonar.jdbc.url>
- <sonar.runtime.mode>development</sonar.runtime.mode>
<sonar.es.http.host>127.0.0.1</sonar.es.http.host>
<sonar.es.http.port>9200</sonar.es.http.port>
<sonar.log.console>true</sonar.log.console>
import org.sonar.core.issue.db.IssueDto;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.rule.DefaultRuleFinder;
+import org.sonar.server.rule.DefaultRuleFinder;
import org.sonar.server.issue.actionplan.ActionPlanService;
import org.sonar.server.user.UserSession;
import org.sonar.core.qualitygate.db.QualityGateConditionDao;
import org.sonar.core.qualitygate.db.QualityGateDao;
import org.sonar.core.resource.DefaultResourcePermissions;
-import org.sonar.core.rule.DefaultRuleFinder;
+import org.sonar.server.rule.DefaultRuleFinder;
import org.sonar.core.test.TestPlanPerspectiveLoader;
import org.sonar.core.test.TestablePerspectiveLoader;
import org.sonar.core.timemachine.Periods;
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.rule;
+
+import com.google.common.collect.Lists;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.rule.index.RuleQuery;
+import org.sonar.server.search.IndexClient;
+import org.sonar.server.search.QueryOptions;
+
+import javax.annotation.CheckForNull;
+import java.util.Collection;
+import java.util.List;
+
+@Deprecated
+/**
+ * Will be removed in the future. Please use {@link org.sonar.server.rule.RuleService}
+ */
+public class DefaultRuleFinder implements RuleFinder {
+
+ private final RuleIndex index;
+
+ public DefaultRuleFinder(IndexClient indexes) {
+ this.index = indexes.get(RuleIndex.class);
+ }
+
+ @CheckForNull
+ public org.sonar.api.rules.Rule findById(int ruleId) {
+ return toRule(index.getById(ruleId));
+ }
+
+ @CheckForNull
+ public Collection<org.sonar.api.rules.Rule> findByIds(Collection<Integer> ruleIds) {
+ List<org.sonar.api.rules.Rule> rules = Lists.newArrayList();
+ if (ruleIds.isEmpty()) {
+ return rules;
+ }
+ for (Rule rule : index.getByIds(ruleIds)) {
+ rules.add(toRule(rule));
+ }
+ return rules;
+ }
+
+ @CheckForNull
+ public org.sonar.api.rules.Rule findByKey(RuleKey key) {
+ return toRule(index.getByKey(key));
+ }
+
+ @CheckForNull
+ public org.sonar.api.rules.Rule findByKey(String repositoryKey, String key) {
+ return findByKey(RuleKey.of(repositoryKey, key));
+ }
+
+ public final org.sonar.api.rules.Rule find(org.sonar.api.rules.RuleQuery query) {
+ return toRule(index.search(toQuery(query), new QueryOptions()).getHits().get(0));
+ }
+
+ public final Collection<org.sonar.api.rules.Rule> findAll(org.sonar.api.rules.RuleQuery query) {
+ List<org.sonar.api.rules.Rule> rules = Lists.newArrayList();
+ for(Rule rule:index.search(toQuery(query), new QueryOptions()).getHits()){
+ rules.add(toRule(rule));
+ }
+ return rules;
+ }
+
+ private org.sonar.api.rules.Rule toRule(Rule rule) {
+ org.sonar.api.rules.Rule apiRule = new org.sonar.api.rules.Rule();
+ apiRule.setCharacteristicId(rule.)
+ System.out.println("rule = " + rule);
+ return null;
+ }
+
+ private RuleQuery toQuery(org.sonar.api.rules.RuleQuery query) {
+ return null;
+ }
+//
+// private Query createHqlQuery(DatabaseSession session, org.sonar.api.rules.RuleQuery query) {
+// StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where status<>:status ");
+// Map<String, Object> params = new HashMap<String, Object>();
+// params.put("status", Rule.STATUS_REMOVED);
+// if (StringUtils.isNotBlank(query.getRepositoryKey())) {
+// hql.append("AND pluginName=:repositoryKey ");
+// params.put("repositoryKey", query.getRepositoryKey());
+// }
+// if (StringUtils.isNotBlank(query.getKey())) {
+// hql.append("AND key=:key ");
+// params.put("key", query.getKey());
+// }
+// if (StringUtils.isNotBlank(query.getConfigKey())) {
+// hql.append("AND configKey=:configKey ");
+// params.put("configKey", query.getConfigKey());
+// }
+//
+// Query hqlQuery = session.createQuery(hql.toString());
+// for (Map.Entry<String, Object> entry : params.entrySet()) {
+// hqlQuery.setParameter(entry.getKey(), entry.getValue());
+// }
+// return hqlQuery;
+// }
+}
package org.sonar.server.rule.index;
import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
+import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.sonar.server.search.QueryOptions;
import org.sonar.server.search.Result;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
}
return tags;
}
+
+ @Deprecated
+ @CheckForNull
+ public Rule getById(int id) {
+ SearchResponse response = getClient().prepareSearch(this.getIndexName())
+ .setTypes(this.getIndexType())
+ .setQuery(QueryBuilders.termQuery(RuleNormalizer.RuleField.ID.field(),id))
+ .setSize(1)
+ .get();
+ SearchHit hit = response.getHits().getAt(0);
+ if(hit == null){
+ return null;
+ } else {
+ return toDoc(hit.getSource());
+ }
+ }
+
+ @Deprecated
+ public List<Rule> getByIds(Collection<Integer> ids) {
+ SearchResponse response = getClient().prepareSearch(this.getIndexName())
+ .setTypes(this.getIndexType())
+ .setQuery(QueryBuilders.termQuery(RuleNormalizer.RuleField.ID.field(),ids))
+ .setSize(1)
+ .get();
+ List<Rule> rules = Lists.newArrayList();
+ for (SearchHit hit : response.getHits()) {
+ rules.add(toDoc(hit.getSource()));
+ }
+ return rules;
+ }
}
import org.sonar.core.issue.db.IssueDto;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.rule.DefaultRuleFinder;
+import org.sonar.server.rule.DefaultRuleFinder;
import org.sonar.core.user.DefaultUser;
import org.sonar.server.issue.actionplan.ActionPlanService;
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.rule;
+
+import org.fest.assertions.Assertions;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleQuery;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.rule.db.RuleDao;
+import org.sonar.server.tester.ServerTester;
+
+import java.util.Collections;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+
+@Deprecated
+public class DefaultRuleFinderMediumTest {
+
+ @ClassRule
+ public static ServerTester tester = new ServerTester();
+
+ private DefaultRuleFinder finder;
+ private DbSession session;
+
+ @Before
+ public void setup() {
+ finder = tester.get(DefaultRuleFinder.class);
+
+ session = tester.get(DbClient.class).openSession(false);
+
+ tester.get(RuleDao.class).insert(session,
+ new RuleDto()
+ .setName("Check Header")
+ .setConfigKey("Checker/Treewalker/HeaderCheck")
+ .setRuleKey("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck")
+ .setRepositoryKey("checkstyle")
+ .setSeverity(4)
+ .setStatus(RuleStatus.READY),
+ new RuleDto()
+ .setName("Disabled checked")
+ .setConfigKey("Checker/Treewalker/DisabledCheck")
+ .setRuleKey("DisabledCheck")
+ .setRepositoryKey("checkstyle")
+ .setSeverity(4)
+ .setStatus(RuleStatus.REMOVED),
+ new RuleDto()
+ .setName("Check Annotation")
+ .setConfigKey("Checker/Treewalker/AnnotationUseStyleCheck")
+ .setRuleKey("com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck")
+ .setRepositoryKey("checkstyle")
+ .setSeverity(4)
+ .setStatus(RuleStatus.READY),
+ new RuleDto()
+ .setName("Call Super First")
+ .setConfigKey("rulesets/android.xml/CallSuperFirst")
+ .setRuleKey("CallSuperFirst")
+ .setRepositoryKey("pmd")
+ .setSeverity(2)
+ .setStatus(RuleStatus.READY)
+ );
+ session.commit();
+ }
+
+ @After
+ public void after() {
+ session.close();
+ }
+
+
+ @Test
+ public void should_success_finder_wrap() {
+
+ // should_find_by_id
+ Assertions.assertThat(finder.findById(3).getConfigKey()).isEqualTo("Checker/Treewalker/AnnotationUseStyleCheck");
+
+ // should_not_find_disabled_rule_by_id
+ Assertions.assertThat(finder.findById(2)).isNull();
+
+ // should_find_by_ids
+ // 2 is returned even its status is REMOVED
+ assertThat(finder.findByIds(newArrayList(2, 3))).hasSize(2);
+
+ // should_find_by_key
+ Rule rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
+ Assertions.assertThat(rule).isNotNull();
+ Assertions.assertThat(rule.getKey()).isEqualTo(("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"));
+ Assertions.assertThat(rule.isEnabled()).isTrue();
+
+ // find_should_return_null_if_no_results
+ Assertions.assertThat(finder.findByKey("checkstyle", "unknown")).isNull();
+ Assertions.assertThat(finder.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))).isNull();
+
+ // find_repository_rules
+ Assertions.assertThat(finder.findAll(RuleQuery.create().withRepositoryKey("checkstyle"))).hasSize(2);
+
+ // find_all_enabled
+ Assertions.assertThat(finder.findAll(RuleQuery.create())).onProperty("id").containsOnly(1, 3, 4);
+
+ // do_not_find_disabled_rules
+ Assertions.assertThat(finder.findByKey("checkstyle", "DisabledCheck")).isNull();
+
+ // do_not_find_unknown_rules
+ Assertions.assertThat(finder.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"))).isEmpty();
+
+ // should_find_by_ids_empty
+ tester.clearDbAndIndexes();
+ assertThat(finder.findByIds(Collections.<Integer>emptyList())).isEmpty();
+ }
+}