package org.sonar.server.rule2;
import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import javax.annotation.CheckForNull;
import java.sql.Timestamp;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
throw new UnsupportedOperationException("Rules cannot be deleted");
}
- @Override
- public RuleKey getKey(RuleDto item, DbSession session) {
- if (item.getKey() != null) {
- return item.getKey();
- }
- return RuleKey.of(item.getRepositoryKey(), item.getRuleKey());
- }
-
@CheckForNull
@Deprecated
public RuleDto getById(int id, DbSession session) {
}
});
return keys;
+ }
+ /** Finder methods for Rules */
+
+ public List<RuleDto> findByNonManual(DbSession session){
+ return mapper(session).selectNonManual();
}
- public List<RuleParamDto> findRuleParamsByRuleKey(RuleKey ruleKey, DbSession dbSession) {
- return mapper(dbSession).selectParamsByRuleKey(ruleKey);
+ public List<RuleDto> findAll(DbSession session) {
+ return mapper(session).selectAll();
}
+ public List<RuleDto> findByEnabledAndNotManual(DbSession session) {
+ return mapper(session).selectEnablesAndNonManual();
+ }
+ public List<RuleDto> findByName(String name, DbSession session) {
+ //TODO change selectByName to return a list
+ return ImmutableList.of(mapper(session).selectByName(name));
+ }
+
+ /** Nested DTO RuleParams */
public void addRuleParam(RuleDto rule, RuleParamDto paramDto, DbSession session) {
Preconditions.checkNotNull(rule.getId(), "Rule id must be set");
session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.INSERT, paramDto, rule.getKey()));
}
- public void updateRuleParam(RuleDto rule, RuleParamDto paramDto, DbSession session) {
+ public RuleParamDto updateRuleParam(RuleDto rule, RuleParamDto paramDto, DbSession session) {
Preconditions.checkNotNull(rule.getId(), "Rule id must be set");
+ Preconditions.checkNotNull(paramDto.getId(), "Param is not yet persisted must be set");
paramDto.setRuleId(rule.getId());
- mapper(session).updateParameter(paramDto);
+ System.out.println("paramDto = " + paramDto);
session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.UPDATE, paramDto, rule.getKey()));
+ mapper(session).updateParameter(paramDto);
+ return paramDto;
+ }
+
+ public void removeRuleParam(RuleDto rule, RuleParamDto param, DbSession session) {
+ Preconditions.checkNotNull(param.getId(), "Param is not persisted");
+ mapper(session).deleteParameter(param.getId());
+ session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.DELETE, param, rule.getKey()));
+ }
+
+ /** Finder methods for RuleParams */
+
+ public List<RuleParamDto> findAllRuleParams(DbSession session) {
+ return mapper(session).selectAllParams();
+ }
+
+ public List<RuleParamDto> findRuleParamsByRuleKey(RuleKey key, DbSession session) {
+ return mapper(session).selectParamsByRuleKey(key);
+ }
+
+ public List<RuleParamDto> findRuleParamsByRules(List<RuleDto> ruleDtos, DbSession session) {
+ List<RuleParamDto> ruleParamDtos = new ArrayList<RuleParamDto>();
+ for(RuleDto rule:ruleDtos){
+ ruleParamDtos.addAll(findRuleParamsByRuleKey(rule.getKey(), session));
+ }
+ return ruleParamDtos;
}
}
--- /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.rule2;
+
+import com.google.common.base.Function;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.check.Cardinality;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.core.rule.RuleParamDto;
+
+import java.util.List;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+
+public class RuleDaoTest extends AbstractDaoTestCase {
+
+ private RuleDao dao;
+ private DbSession session;
+
+ @Before
+ public void before() throws Exception {
+ this.session = getMyBatis().openSession(false);
+ this.dao = new RuleDao();
+ }
+
+ @After
+ public void after() {
+ this.session.close();
+ }
+
+ @Test
+ public void select_all() throws Exception {
+ setupData("selectAll");
+ List<RuleDto> ruleDtos = dao.findAll(session);
+
+ assertThat(ruleDtos).hasSize(1);
+
+ RuleDto ruleDto = ruleDtos.get(0);
+ assertThat(ruleDto.getId()).isEqualTo(1);
+ assertThat(ruleDto.getName()).isEqualTo("Avoid Null");
+ assertThat(ruleDto.getDescription()).isEqualTo("Should avoid NULL");
+ assertThat(ruleDto.getStatus()).isEqualTo(Rule.STATUS_READY);
+ assertThat(ruleDto.getRepositoryKey()).isEqualTo("checkstyle");
+ assertThat(ruleDto.getNoteData()).isEqualTo("Rule note with accents \u00e9\u00e8\u00e0");
+ assertThat(ruleDto.getSubCharacteristicId()).isEqualTo(100);
+ assertThat(ruleDto.getDefaultSubCharacteristicId()).isEqualTo(101);
+ assertThat(ruleDto.getRemediationFunction()).isEqualTo("linear");
+ assertThat(ruleDto.getDefaultRemediationFunction()).isEqualTo("linear_offset");
+ assertThat(ruleDto.getRemediationCoefficient()).isEqualTo("1h");
+ assertThat(ruleDto.getDefaultRemediationCoefficient()).isEqualTo("5d");
+ assertThat(ruleDto.getRemediationOffset()).isEqualTo("5min");
+ assertThat(ruleDto.getDefaultRemediationOffset()).isEqualTo("10h");
+ assertThat(ruleDto.getEffortToFixDescription()).isEqualTo("squid.S115.effortToFix");
+ }
+
+ @Test
+ public void select_enables_and_non_manual() throws Exception {
+ setupData("select_enables_and_non_manual");
+ List<RuleDto> ruleDtos = dao.findByEnabledAndNotManual(session);
+
+ assertThat(ruleDtos.size()).isEqualTo(1);
+ RuleDto ruleDto = ruleDtos.get(0);
+ assertThat(ruleDto.getId()).isEqualTo(1);
+ assertThat(ruleDto.getName()).isEqualTo("Avoid Null");
+ assertThat(ruleDto.getDescription()).isEqualTo("Should avoid NULL");
+ assertThat(ruleDto.getStatus()).isEqualTo(Rule.STATUS_READY);
+ assertThat(ruleDto.getRepositoryKey()).isEqualTo("checkstyle");
+ assertThat(ruleDto.getNoteData()).isEqualTo("Rule note with accents \u00e9\u00e8\u00e0");
+ assertThat(ruleDto.getSubCharacteristicId()).isEqualTo(100);
+ assertThat(ruleDto.getDefaultSubCharacteristicId()).isEqualTo(101);
+ assertThat(ruleDto.getRemediationFunction()).isEqualTo("LINEAR");
+ assertThat(ruleDto.getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET");
+ assertThat(ruleDto.getRemediationCoefficient()).isEqualTo("1h");
+ assertThat(ruleDto.getDefaultRemediationCoefficient()).isEqualTo("5d");
+ assertThat(ruleDto.getRemediationOffset()).isEqualTo("5min");
+ assertThat(ruleDto.getDefaultRemediationOffset()).isEqualTo("10h");
+ assertThat(ruleDto.getEffortToFixDescription()).isEqualTo("squid.S115.effortToFix");
+ }
+
+ @Test
+ public void select_by_id() throws Exception {
+ setupData("selectById");
+ RuleDto ruleDto = dao.getById(2, session);
+
+ assertThat(ruleDto.getId()).isEqualTo(2);
+ assertThat(ruleDto.getName()).isEqualTo("Avoid Null");
+ assertThat(ruleDto.getDescription()).isEqualTo("Should avoid NULL");
+ assertThat(ruleDto.getStatus()).isEqualTo(Rule.STATUS_READY);
+ assertThat(ruleDto.getRepositoryKey()).isEqualTo("checkstyle");
+ }
+
+ @Test
+ public void select_by_rule_key() throws Exception {
+ setupData("select_by_rule_key");
+ assertThat(dao.getByKey(RuleKey.of("checkstyle", "AvoidComparison"), session)).isNotNull();
+ assertThat(dao.getByKey(RuleKey.of("checkstyle", "Unknown"), session)).isNull();
+ assertThat(dao.getByKey(RuleKey.of("Unknown", "AvoidComparison"), session)).isNull();
+ }
+
+ @Test
+ public void select_by_name() throws Exception {
+ setupData("select_by_name");
+ List<RuleDto> ruleDtos = dao.findByName("Avoid Null", session);
+
+ assertThat(ruleDtos).hasSize(1);
+
+ RuleDto ruleDto = Iterables.getFirst(ruleDtos, null);
+
+ assertThat(ruleDto.getId()).isEqualTo(2);
+ assertThat(ruleDto.getName()).isEqualTo("Avoid Null");
+ assertThat(ruleDto.getDescription()).isEqualTo("Should avoid NULL");
+ assertThat(ruleDto.getStatus()).isEqualTo(Rule.STATUS_READY);
+ assertThat(ruleDto.getRepositoryKey()).isEqualTo("checkstyle");
+ }
+
+ @Test
+ public void select_non_manual() throws Exception {
+ setupData("selectNonManual");
+ List<RuleDto> ruleDtos = dao.findByNonManual(session);
+ session.commit();
+ session.close();
+
+ assertThat(ruleDtos.size()).isEqualTo(1);
+ RuleDto ruleDto = ruleDtos.get(0);
+ assertThat(ruleDto.getId()).isEqualTo(1);
+ assertThat(ruleDto.getName()).isEqualTo("Avoid Null");
+ assertThat(ruleDto.getDescription()).isEqualTo("Should avoid NULL");
+ assertThat(ruleDto.getStatus()).isEqualTo(Rule.STATUS_READY);
+ assertThat(ruleDto.getRepositoryKey()).isEqualTo("checkstyle");
+ }
+
+// @Test
+// public void select_by_sub_characteristic_id(){
+// setupData("select_by_sub_characteristic_id");
+//
+// // Rules from sub characteristic (even REMOVED ones are returned)
+// List<RuleDto> ruleDtos = dao.findBySubCharacteristicId(3);
+// assertThat(ruleDtos).hasSize(3);
+// assertThat(idsFromRuleDtos(ruleDtos)).containsExactly(2, 4, 5);
+//
+// // Nothing on root characteristic
+// ruleDtos = dao.selectBySubCharacteristicId(1);
+// assertThat(ruleDtos).isEmpty();
+//
+// // Rules from disabled characteristic
+// ruleDtos = dao.selectBySubCharacteristicId(11);
+// assertThat(idsFromRuleDtos(ruleDtos)).containsExactly(3);
+// }
+
+ @Test
+ public void update() {
+ setupData("update");
+
+ RuleDto ruleToUpdate = new RuleDto()
+ .setId(1)
+ .setRuleKey("NewRuleKey")
+ .setRepositoryKey("plugin")
+ .setName("new name")
+ .setDescription("new description")
+ .setStatus(Rule.STATUS_DEPRECATED)
+ .setConfigKey("NewConfigKey")
+ .setSeverity(Severity.INFO)
+ .setCardinality(Cardinality.MULTIPLE)
+ .setLanguage("dart")
+ .setParentId(3)
+ .setNoteData("My note")
+ .setNoteUserLogin("admin")
+ .setNoteCreatedAt(DateUtils.parseDate("2013-12-19"))
+ .setNoteUpdatedAt(DateUtils.parseDate("2013-12-20"))
+ .setSubCharacteristicId(100)
+ .setDefaultSubCharacteristicId(101)
+ .setRemediationFunction("linear")
+ .setDefaultRemediationFunction("linear_offset")
+ .setRemediationCoefficient("1h")
+ .setDefaultRemediationCoefficient("5d")
+ .setRemediationOffset("5min")
+ .setDefaultRemediationOffset("10h")
+ .setEffortToFixDescription("squid.S115.effortToFix")
+ .setUpdatedAt(DateUtils.parseDate("2013-12-17"));
+
+ dao.update(ruleToUpdate, session);
+ session.commit();
+
+ checkTables("update", "rules");
+ }
+
+ @Test
+ public void insert() {
+ setupData("empty");
+
+ RuleDto ruleToInsert = new RuleDto()
+ .setId(1)
+ .setRuleKey("NewRuleKey")
+ .setRepositoryKey("plugin")
+ .setName("new name")
+ .setDescription("new description")
+ .setStatus(Rule.STATUS_DEPRECATED)
+ .setConfigKey("NewConfigKey")
+ .setSeverity(Severity.INFO)
+ .setCardinality(Cardinality.MULTIPLE)
+ .setLanguage("dart")
+ .setParentId(3)
+ .setSubCharacteristicId(100)
+ .setDefaultSubCharacteristicId(101)
+ .setRemediationFunction("linear")
+ .setDefaultRemediationFunction("linear_offset")
+ .setRemediationCoefficient("1h")
+ .setDefaultRemediationCoefficient("5d")
+ .setRemediationOffset("5min")
+ .setDefaultRemediationOffset("10h")
+ .setEffortToFixDescription("squid.S115.effortToFix")
+ .setCreatedAt(DateUtils.parseDate("2013-12-16"))
+ .setUpdatedAt(DateUtils.parseDate("2013-12-17"));
+
+ dao.insert(ruleToInsert, session);
+ session.commit();
+
+ checkTables("insert", "rules");
+ }
+
+ @Test
+ public void insert_all() {
+ setupData("empty");
+
+ RuleDto ruleToInsert1 = new RuleDto()
+ .setId(1)
+ .setRuleKey("NewRuleKey")
+ .setRepositoryKey("plugin")
+ .setName("new name")
+ .setDescription("new description")
+ .setStatus(Rule.STATUS_DEPRECATED)
+ .setConfigKey("NewConfigKey")
+ .setSeverity(Severity.INFO)
+ .setCardinality(Cardinality.MULTIPLE)
+ .setLanguage("dart")
+ .setParentId(3)
+ .setSubCharacteristicId(100)
+ .setDefaultSubCharacteristicId(101)
+ .setRemediationFunction("linear")
+ .setDefaultRemediationFunction("linear_offset")
+ .setRemediationCoefficient("1h")
+ .setDefaultRemediationCoefficient("5d")
+ .setRemediationOffset("5min")
+ .setDefaultRemediationOffset("10h")
+ .setEffortToFixDescription("squid.S115.effortToFix")
+ .setCreatedAt(DateUtils.parseDate("2013-12-16"))
+ .setUpdatedAt(DateUtils.parseDate("2013-12-17"));
+
+ RuleDto ruleToInsert2 = new RuleDto()
+ .setId(2)
+ .setRuleKey("NewRuleKey2")
+ .setRepositoryKey("plugin2")
+ .setName("new name2")
+ .setDescription("new description2")
+ .setStatus(Rule.STATUS_BETA)
+ .setConfigKey("NewConfigKey2")
+ .setSeverity(Severity.MAJOR)
+ .setCardinality(Cardinality.SINGLE)
+ .setLanguage("js")
+ .setParentId(null)
+ .setSubCharacteristicId(102)
+ .setDefaultSubCharacteristicId(103)
+ .setRemediationFunction("linear_offset")
+ .setDefaultRemediationFunction("linear")
+ .setRemediationCoefficient("5d")
+ .setDefaultRemediationCoefficient("1h")
+ .setRemediationOffset("10h")
+ .setDefaultRemediationOffset("5min")
+ .setEffortToFixDescription("squid.S115.effortToFix2")
+ .setCreatedAt(DateUtils.parseDate("2013-12-14"))
+ .setUpdatedAt(DateUtils.parseDate("2013-12-15"));
+
+ dao.insert(ImmutableList.of(ruleToInsert1, ruleToInsert2), session);
+ session.commit();
+
+ checkTables("insert_all", "rules");
+ }
+
+ @Test
+ public void select_parameters() throws Exception {
+ setupData("selectParameters");
+ List<RuleParamDto> ruleDtos = dao.findAllRuleParams(session);
+
+ assertThat(ruleDtos.size()).isEqualTo(1);
+ RuleParamDto ruleDto = ruleDtos.get(0);
+ assertThat(ruleDto.getId()).isEqualTo(1);
+ assertThat(ruleDto.getName()).isEqualTo("myParameter");
+ assertThat(ruleDto.getDescription()).isEqualTo("My Parameter");
+ assertThat(ruleDto.getType()).isEqualTo("plop");
+ assertThat(ruleDto.getDefaultValue()).isEqualTo("plouf");
+ }
+
+ @Test
+ public void select_parameters_by_rule_id() throws Exception {
+ setupData("select_parameters_by_rule_id");
+ RuleDto rule = dao.getById(1, session);
+ List<RuleParamDto> ruleDtos = dao.findRuleParamsByRuleKey(rule.getKey(), session);
+
+ assertThat(ruleDtos.size()).isEqualTo(1);
+ RuleParamDto ruleDto = ruleDtos.get(0);
+ assertThat(ruleDto.getId()).isEqualTo(1);
+ assertThat(ruleDto.getName()).isEqualTo("myParameter");
+ assertThat(ruleDto.getDescription()).isEqualTo("My Parameter");
+ assertThat(ruleDto.getType()).isEqualTo("plop");
+ assertThat(ruleDto.getRuleId()).isEqualTo(1);
+ }
+
+ @Test
+ public void select_parameters_by_rule_ids() throws Exception {
+ setupData("select_parameters_by_rule_ids");
+
+ RuleDto rule1 = dao.getById(1, session);
+ RuleDto rule2 = dao.getById(2, session);
+ assertThat(dao.findRuleParamsByRules(newArrayList(rule1, rule2), session)).hasSize(2);
+ assertThat(dao.findRuleParamsByRules(newArrayList(rule1), session)).hasSize(1);
+ }
+
+ @Test
+ public void insert_parameter() {
+ setupData("insert_parameter");
+
+ RuleDto rule1 = dao.getById(1, session);
+
+ RuleParamDto param = RuleParamDto.createFor(rule1)
+ .setName("max")
+ .setType("INTEGER")
+ .setDefaultValue("30")
+ .setDescription("My Parameter");
+
+ dao.addRuleParam(rule1, param, session);
+ session.commit();
+
+ checkTables("insert_parameter", "rules_parameters");
+ }
+
+ @Test
+ public void update_parameter() {
+ setupData("update_parameter");
+
+ RuleDto rule1 = dao.getById(1, session);
+
+ List<RuleParamDto> params = dao.findRuleParamsByRuleKey(rule1.getKey(), session);
+ assertThat(params).hasSize(1);
+
+ RuleParamDto param = Iterables.getFirst(params, null);
+
+ param.setName("format")
+ .setType("STRING")
+ .setDefaultValue("^[a-z]+(\\.[a-z][a-z0-9]*)*$")
+ .setDescription("Regular expression used to check the package names against.");
+
+ dao.updateRuleParam(rule1, param, session);
+ session.commit();
+ System.out.println("param = " + param);
+
+ checkTables("update_parameter", "rules_parameters");
+ }
+//
+// @Test
+// public void select_tags_by_rule_id() throws Exception {
+// setupData("select_tags_by_rule_id");
+//
+// assertThat(dao.selectTagsByRuleId(3)).hasSize(2);
+// }
+//
+// @Test
+// public void select_tags_by_rule_ids() throws Exception {
+// setupData("select_tags_by_rule_ids");
+//
+// assertThat(dao.selectTagsByRuleIds(newArrayList(3, 4))).hasSize(3);
+// }
+
+ private List<Integer> idsFromRuleDtos(List<RuleDto> ruleDtos){
+ return newArrayList(Iterables.transform(ruleDtos, new Function<RuleDto, Integer>() {
+ @Override
+ public Integer apply(RuleDto input) {
+ return input.getId();
+ }
+ }));
+ }
+}