]> source.dussan.org Git - sonarqube.git/commitdiff
fixed RuleDaoTest for rule2 version on new DAO.
authorStephane Gamard <stephane.gamard@searchbox.com>
Thu, 8 May 2014 08:57:00 +0000 (10:57 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Thu, 8 May 2014 08:57:00 +0000 (10:57 +0200)
sonar-core/src/main/java/org/sonar/core/db/Dao.java
sonar-core/src/main/java/org/sonar/core/rule/RuleParamDto.java
sonar-server/src/main/java/org/sonar/server/db/BaseDao.java
sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleDao.java
sonar-server/src/main/java/org/sonar/server/rule2/RuleDao.java
sonar-server/src/test/java/org/sonar/server/rule2/RuleDaoTest.java [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/rule2/RuleDaoTest/insert_parameter-result.xml
sonar-server/src/test/resources/org/sonar/server/rule2/RuleDaoTest/insert_parameter.xml
sonar-server/src/test/resources/org/sonar/server/rule2/RuleDaoTest/select_parameters_by_rule_ids.xml
sonar-server/src/test/resources/org/sonar/server/rule2/RuleDaoTest/update_parameter-result.xml
sonar-server/src/test/resources/org/sonar/server/rule2/RuleDaoTest/update_parameter.xml

index fc9a0a9734d51a39e9de6eb1784977a3daa12256..139fc06d0c2f6a0ec79a60b207fde583b56095bf 100644 (file)
@@ -23,6 +23,7 @@ import org.sonar.core.persistence.DbSession;
 
 import javax.annotation.CheckForNull;
 import java.io.Serializable;
+import java.util.List;
 
 public interface Dao<E extends Dto<K>, K extends Serializable> {
 
@@ -31,10 +32,16 @@ public interface Dao<E extends Dto<K>, K extends Serializable> {
 
   E update(E item, DbSession session);
 
+  List<E> update(List<E> items, DbSession session);
+
   E insert(E item, DbSession session);
 
+   List<E> insert(List<E> items, DbSession session);
+
   void delete(E item, DbSession session);
 
+  void delete(List<E> items, DbSession session);
+
   void deleteByKey(K key, DbSession session);
 
   Iterable<K> keysOfRowsUpdatedAfter(long timestamp, DbSession session);
index 666af08e6967a91b05916ccb8793d3a845e04468..826a260a585029ef81cee6a166f9c1f6551b767e 100644 (file)
@@ -20,6 +20,9 @@
 
 package org.sonar.core.rule;
 
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
 public class RuleParamDto {
 
   private int id;
@@ -29,6 +32,10 @@ public class RuleParamDto {
   private String defaultValue;
   private String description;
 
+  public RuleParamDto() {
+
+  }
+
   public int getId() {
     return id;
   }
@@ -83,4 +90,13 @@ public class RuleParamDto {
     return this;
   }
 
+  @Override
+  public String toString() {
+    return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
+  }
+
+  public static RuleParamDto createFor(RuleDto rule) {
+    return new RuleParamDto();
+  }
+
 }
index b09cadc04b8eb7eb8926953f32a296de378013c0..5a94af5645569318c170657199187da64c49c77b 100644 (file)
@@ -23,13 +23,14 @@ import com.google.common.base.Preconditions;
 import org.sonar.core.db.Dao;
 import org.sonar.core.db.Dto;
 import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
 import org.sonar.server.search.DtoIndexAction;
 import org.sonar.server.search.IndexAction;
 import org.sonar.server.search.IndexDefinition;
 import org.sonar.server.search.KeyIndexAction;
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * naming convention for DAO
@@ -115,8 +116,6 @@ public abstract class BaseDao<M, E extends Dto<K>, K extends Serializable> imple
 
   protected abstract E doGetByKey(K key, DbSession session);
 
-  protected abstract K getKey(E item, DbSession session);
-
   protected abstract E doInsert(E item, DbSession session);
 
   protected abstract E doUpdate(E item, DbSession session);
@@ -138,6 +137,16 @@ public abstract class BaseDao<M, E extends Dto<K>, K extends Serializable> imple
     return item;
   }
 
+  @Override
+  public List<E>  update(List<E> items, DbSession session) {
+    //TODO check for bulk inserts
+    List<E> results = new ArrayList<E>();
+    for(E item:items) {
+      results.add(this.update(item, session));
+    }
+    return items;
+  }
+
   @Override
   public E insert(E item, DbSession session) {
     this.doInsert(item, session);
@@ -145,9 +154,27 @@ public abstract class BaseDao<M, E extends Dto<K>, K extends Serializable> imple
     return item;
   }
 
+  @Override
+  public List<E>  insert(List<E> items, DbSession session) {
+    //TODO check for bulk inserts
+    List<E> results = new ArrayList<E>();
+    for(E item:items) {
+      results.add(this.insert(item, session));
+    }
+    return items;
+  }
+
   @Override
   public void delete(E item, DbSession session) {
-    deleteByKey(getKey(item, session), session);
+    deleteByKey(item.getKey(), session);
+  }
+
+  @Override
+  public void delete(List<E> items, DbSession session) {
+    //TODO check for bulk inserts
+    for(E item:items) {
+      this.delete(item, session);
+    }
   }
 
   @Override
@@ -156,5 +183,4 @@ public abstract class BaseDao<M, E extends Dto<K>, K extends Serializable> imple
     doDeleteByKey(key, session);
     session.enqueue(new KeyIndexAction<K>(this.getIndexType(), IndexAction.Method.DELETE, key));
   }
-
 }
index 58f6cb771a8ee4eec1d0fc0483d45d4b62400a42..3c6b956a76838113c4fadd47a83bca99d7327587 100644 (file)
@@ -21,7 +21,6 @@
 package org.sonar.server.rule2;
 
 import com.google.common.base.Preconditions;
-import org.sonar.api.rule.RuleKey;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.qualityprofile.db.ActiveRuleDto;
 import org.sonar.core.qualityprofile.db.ActiveRuleKey;
@@ -29,7 +28,6 @@ import org.sonar.core.qualityprofile.db.ActiveRuleMapper;
 import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
 import org.sonar.core.qualityprofile.db.QualityProfileDao;
 import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.core.qualityprofile.db.QualityProfileKey;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.server.db.BaseDao;
 import org.sonar.server.search.EmbeddedIndexAction;
@@ -48,15 +46,6 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
     this.profileDao = profileDao;
   }
 
-  @Override
-  public ActiveRuleKey getKey(ActiveRuleDto item, DbSession session) {
-    if (item.getKey() != null) {
-      return item.getKey();
-    }
-    fixIdsAndKeys(item, session);
-    return item.getKey();
-  }
-
   @Override
   public Iterable<ActiveRuleKey> keysOfRowsUpdatedAfter(long timestamp, DbSession session) {
     throw new UnsupportedOperationException("Need to implement ActiveRuleDto.doGetByKey() method");
@@ -78,29 +67,6 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
     return item;
   }
 
-  private void fixIdsAndKeys(ActiveRuleDto item, DbSession session) {
-    if (item.getKey() != null) {
-      if (item.getProfileId() == null) {
-        QualityProfileDto profile = profileDao.selectByNameAndLanguage(item.getKey().qProfile().name(), item.getKey().qProfile().lang(), session);
-        // TODO fail if profile not found
-        item.setProfileId(profile.getId());
-      }
-      if (item.getRulId() == null) {
-        RuleDto rule = ruleDao.getByKey(item.getKey().ruleKey(), session);
-        // TODO fail if rule not found
-        item.setRuleId(rule.getId());
-      }
-    } else {
-      // key is null
-      if (item.getProfileId() != null && item.getRulId() != null) {
-        QualityProfileDto profile = profileDao.selectById(item.getProfileId(), session);
-        RuleDto rule = ruleDao.getById(item.getRulId(), session);
-        RuleKey ruleKey = ruleDao.getKey(rule, session);
-        item.setKey(ActiveRuleKey.of(QualityProfileKey.of(profile.getName(), profile.getLanguage()), ruleKey));
-      }
-    }
-  }
-
   @Override
   protected ActiveRuleDto doUpdate(ActiveRuleDto item, DbSession session) {
     Preconditions.checkArgument(item.getProfileId() != null, "Quality profile is not persisted (missing id)");
index b7cecd6116e2d4e3a33a95024c2d048adb9a719f..7a98fb8525ec48613fa4fce6edd20f075440d880 100644 (file)
@@ -20,6 +20,7 @@
 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;
@@ -36,6 +37,7 @@ import org.sonar.server.search.IndexAction;
 
 import javax.annotation.CheckForNull;
 import java.sql.Timestamp;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -69,14 +71,6 @@ public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> implements Ba
     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) {
@@ -94,14 +88,28 @@ public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> implements Ba
       }
     });
     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");
@@ -110,10 +118,37 @@ public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> implements Ba
     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;
   }
 }
diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/RuleDaoTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/RuleDaoTest.java
new file mode 100644 (file)
index 0000000..bffa49b
--- /dev/null
@@ -0,0 +1,409 @@
+/*
+ * 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();
+      }
+    }));
+  }
+}
index 5208b7a4a4ccf83a6761e74781979ffc02072109..8be83caf941ef811a8bc7c369245f5dc50a2f98d 100644 (file)
@@ -1,3 +1,12 @@
 <dataset>
+    <rules tags="[null]" system_tags="[null]" id="1" plugin_rule_key="NewRuleKey" plugin_name="plugin" name="new name" description="new description" status="DEPRECATED"
+           plugin_config_key="NewConfigKey" priority="0" cardinality="MULTIPLE" language="dart" created_at="2013-12-16" updated_at="2013-12-17" parent_id="3"
+           note_data="[null]" note_user_login="[null]" note_created_at="[null]" note_updated_at="[null]"
+           characteristic_id="100" default_characteristic_id="101"
+           remediation_function="linear" default_remediation_function="linear_offset"
+           remediation_coeff="1h" default_remediation_coeff="5d"
+           remediation_offset="5min" default_remediation_offset="10h"
+           effort_to_fix_description="squid.S115.effortToFix"
+            />
   <rules_parameters id="1" rule_id="1" name="max" param_type="INTEGER" default_value="30" description="My Parameter"/>
 </dataset>
index 871dedcb5e9f6c782bc573aedb4cc5029433bb03..d7ad7e33792dc90dff2de6c29c70e8e8a426a1a0 100644 (file)
@@ -1,3 +1,11 @@
 <dataset>
-
+    <rules tags="[null]" system_tags="[null]" id="1" plugin_rule_key="NewRuleKey" plugin_name="plugin" name="new name" description="new description" status="DEPRECATED"
+           plugin_config_key="NewConfigKey" priority="0" cardinality="MULTIPLE" language="dart" created_at="2013-12-16" updated_at="2013-12-17" parent_id="3"
+           note_data="[null]" note_user_login="[null]" note_created_at="[null]" note_updated_at="[null]"
+           characteristic_id="100" default_characteristic_id="101"
+           remediation_function="linear" default_remediation_function="linear_offset"
+           remediation_coeff="1h" default_remediation_coeff="5d"
+           remediation_offset="5min" default_remediation_offset="10h"
+           effort_to_fix_description="squid.S115.effortToFix"
+            />
 </dataset>
index 5d840d5998d4eeea16f2dbb6f608d64c0d2d79c4..810129bd7a80629361afe1b3a03edb9f9a68f258 100644 (file)
@@ -1,6 +1,9 @@
 <dataset>
 
-  <rules_parameters id="1" rule_id="1" name="myParameter" param_type="plop" default_value="plouf" description="My Parameter"/>
+    <rules tags="[null]" system_tags="[null]" id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY"/>
+    <rules tags="[null]" system_tags="[null]" id="2" plugin_rule_key="Unused" plugin_name="unused" name="Unused Rule" description="Not used" status="REMOVED"/>
+
+    <rules_parameters id="1" rule_id="1" name="myParameter" param_type="plop" default_value="plouf" description="My Parameter"/>
 
   <rules_parameters id="2" rule_id="2" name="otherParam" param_type="plop" default_value="plouf" description="Other Parameter"/>
 
index d61889441d92c2b2e1e88e6fe39a0371f9f034df..f34701e2f36c43cf6fa9d00a95544f7b76fc4a6f 100644 (file)
@@ -1,3 +1,5 @@
 <dataset>
-  <rules_parameters id="1" rule_id="1" name="format" param_type="STRING" default_value="^[a-z]+(\.[a-z][a-z0-9]*)*$" description="Regular expression used to check the package names against."/>
+    <rules tags="[null]" system_tags="[null]" id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY"/>
+
+    <rules_parameters id="1" rule_id="1" name="format" param_type="STRING" default_value="^[a-z]+(\.[a-z][a-z0-9]*)*$" description="Regular expression used to check the package names against."/>
 </dataset>
index 5208b7a4a4ccf83a6761e74781979ffc02072109..20c201d738d412f3bda4c1b61869d69b8aada6e7 100644 (file)
@@ -1,3 +1,5 @@
 <dataset>
-  <rules_parameters id="1" rule_id="1" name="max" param_type="INTEGER" default_value="30" description="My Parameter"/>
+    <rules tags="[null]" system_tags="[null]" id="1" plugin_rule_key="AvoidNull" plugin_name="checkstyle" name="Avoid Null" description="Should avoid NULL" status="READY"/>
+
+    <rules_parameters id="1" rule_id="1" name="max" param_type="INTEGER" default_value="30" description="My Parameter"/>
 </dataset>