]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 complete tests of active rules
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 29 May 2014 08:35:19 +0000 (10:35 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 29 May 2014 08:35:19 +0000 (10:35 +0200)
15 files changed:
sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDto.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java [deleted file]
sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/rule/RuleDataMediumTest.java [deleted file]
sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceTest.java
sonar-server/src/test/java/org/sonar/server/tester/BackendCleanup.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/tester/DataStoreCleanup.java [deleted file]
sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java

index c8785bbdf01a449debc6ac13f4a2ba9e6c6c3ee8..c548db07c9e6a756663ee53b46d24c365802bd5d 100644 (file)
@@ -35,7 +35,7 @@ public class QualityProfileDto extends Dto<QualityProfileKey> {
   private boolean used;
 
   /**
-   * @deprecated use QualityProfileDto.createFor instead
+   * @deprecated use {@link #createFor(String, String)}
    */
   @Deprecated
   public QualityProfileDto() {
index 524af28f01e5b2a3e095f41802c3696ac80333eb..1ab20edc0d0adb4cb5a88784ee53609481330cb6 100644 (file)
@@ -203,7 +203,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
     return mapper(session).selectParamsByActiveRuleId(activeRule.getId());
   }
 
-  public ActiveRuleParamDto getParamsByKeyAndName(ActiveRuleKey key, String name, DbSession session) {
+  public ActiveRuleParamDto getParamByKeyAndName(ActiveRuleKey key, String name, DbSession session) {
     Preconditions.checkNotNull(key, "ActiveRuleKey cannot be null");
     Preconditions.checkNotNull(name, "ParameterName cannot be null");
     ActiveRuleDto activeRule = getNullableByKey(session, key);
@@ -216,6 +216,10 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
     return mapper(session).selectParamsByActiveRuleId(dto.getId());
   }
 
+  public List<ActiveRuleParamDto> findAllParams(DbSession session) {
+    return mapper(session).selectAllParams();
+  }
+
   @Deprecated
   public void removeParamByProfile(DbSession session, QProfile profile) {
     mapper(session).deleteParametersFromProfile(profile.id());
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java
new file mode 100644 (file)
index 0000000..0b06587
--- /dev/null
@@ -0,0 +1,249 @@
+/*
+ * 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.qualityprofile;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rule.Severity;
+import org.sonar.api.server.debt.DebtRemediationFunction;
+import org.sonar.check.Cardinality;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.core.rule.RuleParamDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
+import org.sonar.server.rule.RuleTesting;
+import org.sonar.server.tester.ServerTester;
+
+import java.util.Collection;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class ActiveRuleBackendMediumTest {
+
+  @ClassRule
+  public static ServerTester tester = new ServerTester();
+
+  DbClient db;
+  DbSession dbSession;
+  ActiveRuleIndex index;
+
+  @Before
+  public void before() {
+    tester.clearDbAndIndexes();
+    db = tester.get(DbClient.class);
+    dbSession = db.openSession(false);
+    index = tester.get(ActiveRuleIndex.class);
+  }
+
+  @Test
+  public void synchronize_index() throws Exception {
+    QualityProfileDto profile1 = QualityProfileDto.createFor("p1", "java");
+    db.qualityProfileDao().insert(dbSession, profile1);
+
+    RuleDto rule1 = RuleDto.createFor(RuleKey.of("java", "r1")).setSeverity(Severity.MAJOR);
+    db.ruleDao().insert(dbSession, rule1);
+
+    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile1, rule1).setSeverity("BLOCKER");
+    db.activeRuleDao().insert(dbSession, activeRule);
+    dbSession.commit();
+
+    tester.clearIndexes();
+
+    // 0. Assert that we have no rules in Is.
+    assertThat(index.getByKey(activeRule.getKey())).isNull();
+
+    // 1. Synchronize since 0
+    db.activeRuleDao().synchronizeAfter(dbSession, 0);
+
+    // 2. Assert that we have the rule in Index
+    assertThat(index.getByKey(activeRule.getKey())).isNotNull();
+
+  }
+
+  @Test
+  public void insert_and_index_active_rule() {
+    QualityProfileDto profileDto = QualityProfileDto.createFor("myprofile", "java");
+    db.qualityProfileDao().insert(dbSession, profileDto);
+    RuleKey ruleKey = RuleKey.of("javascript", "S001");
+    RuleDto ruleDto = newRuleDto(ruleKey);
+    db.ruleDao().insert(dbSession, ruleDto);
+
+    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, ruleDto)
+      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
+      .setSeverity(Severity.BLOCKER);
+    db.activeRuleDao().insert(dbSession, activeRule);
+    dbSession.commit();
+
+    // verify db
+    List<ActiveRuleDto> persistedDtos = db.activeRuleDao().findByRule(dbSession, ruleDto);
+    assertThat(persistedDtos).hasSize(1);
+
+    // verify es
+    ActiveRule hit = index.getByKey(activeRule.getKey());
+    assertThat(hit).isNotNull();
+    assertThat(hit.key()).isEqualTo(activeRule.getKey());
+    assertThat(hit.inheritance().name()).isEqualTo(activeRule.getInheritance());
+    assertThat(hit.parentKey()).isEqualTo(activeRule.getParentId());
+    assertThat(hit.severity()).isEqualTo(activeRule.getSeverityString());
+  }
+
+  @Test
+  public void insert_and_index_active_rule_param() {
+    // insert and index
+    QualityProfileDto profileDto = QualityProfileDto.createFor("myprofile", "java");
+    db.qualityProfileDao().insert(dbSession, profileDto);
+    RuleKey ruleKey = RuleKey.of("javascript", "S001");
+    RuleDto ruleDto = newRuleDto(ruleKey);
+    db.ruleDao().insert(dbSession, ruleDto);
+
+    RuleParamDto minParam = new RuleParamDto()
+      .setName("min")
+      .setType("STRING");
+    db.ruleDao().addRuleParam(dbSession, ruleDto, minParam);
+
+    RuleParamDto maxParam = new RuleParamDto()
+      .setName("max")
+      .setType("STRING");
+    db.ruleDao().addRuleParam(dbSession, ruleDto, maxParam);
+
+    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, ruleDto)
+      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
+      .setSeverity(Severity.BLOCKER);
+    db.activeRuleDao().insert(dbSession, activeRule);
+
+    ActiveRuleParamDto activeRuleMinParam = ActiveRuleParamDto.createFor(minParam)
+      .setValue("minimum");
+    db.activeRuleDao().addParam(dbSession, activeRule, activeRuleMinParam);
+
+    ActiveRuleParamDto activeRuleMaxParam = ActiveRuleParamDto.createFor(maxParam)
+      .setValue("maximum");
+    db.activeRuleDao().addParam(dbSession, activeRule, activeRuleMaxParam);
+
+    dbSession.commit();
+
+    // verify db
+    List<ActiveRuleParamDto> persistedDtos = db.activeRuleDao().findParamsByActiveRule(dbSession, activeRule);
+    assertThat(persistedDtos).hasSize(2);
+
+    // verify es
+    ActiveRule rule = index.getByKey(activeRule.getKey());
+    assertThat(rule.params()).hasSize(2);
+    assertThat(rule.params().keySet()).containsOnly("min", "max");
+    assertThat(rule.params().values()).containsOnly("minimum", "maximum");
+    assertThat(rule.params().get("min")).isEqualTo("minimum");
+  }
+
+  @Test
+  public void find_active_rules() throws Exception {
+    QualityProfileDto profile1 = QualityProfileDto.createFor("p1", "java");
+    QualityProfileDto profile2 = QualityProfileDto.createFor("p2", "java");
+    db.qualityProfileDao().insert(dbSession, profile1, profile2);
+
+    RuleDto rule1 = RuleTesting.newDto(RuleKey.of("java", "r1")).setSeverity(Severity.MAJOR);
+    RuleDto rule2 = RuleTesting.newDto(RuleKey.of("java", "r2")).setSeverity(Severity.MAJOR);
+    db.ruleDao().insert(dbSession, rule1);
+    db.ruleDao().insert(dbSession, rule2);
+
+    db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule1).setSeverity(Severity.MINOR));
+    db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule2).setSeverity(Severity.BLOCKER));
+    db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, rule2).setSeverity(Severity.CRITICAL));
+    dbSession.commit();
+
+    // 1. find by rule key
+
+    // in db
+    dbSession.clearCache();
+    assertThat(db.activeRuleDao().findByRule(dbSession, rule1)).hasSize(1);
+    assertThat(db.activeRuleDao().findByRule(dbSession, rule2)).hasSize(2);
+
+    // in es
+    List<ActiveRule> activeRules = index.findByRule(RuleKey.of("java", "r1"));
+    assertThat(activeRules).hasSize(1);
+    assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RuleKey.of("java", "r1"));
+
+    activeRules = index.findByRule(RuleKey.of("java", "r2"));
+    assertThat(activeRules).hasSize(2);
+    assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RuleKey.of("java", "r2"));
+
+    activeRules = index.findByRule(RuleKey.of("java", "r3"));
+    assertThat(activeRules).isEmpty();
+
+    // 2. find by profile
+    activeRules = index.findByProfile(profile1.getKey());
+    assertThat(activeRules).hasSize(2);
+    assertThat(activeRules.get(0).key().qProfile()).isEqualTo(profile1.getKey());
+    assertThat(activeRules.get(1).key().qProfile()).isEqualTo(profile1.getKey());
+
+    activeRules = index.findByProfile(profile2.getKey());
+    assertThat(activeRules).hasSize(1);
+    assertThat(activeRules.get(0).key().qProfile()).isEqualTo(profile2.getKey());
+
+    activeRules = index.findByProfile(QualityProfileKey.of("unknown", "unknown"));
+    assertThat(activeRules).isEmpty();
+  }
+
+  @Test
+  public void find_many_active_rules_by_profile() {
+    // insert and index
+    QualityProfileDto profileDto = QualityProfileDto.createFor("P1", "java");
+    db.qualityProfileDao().insert(dbSession, profileDto);
+    for (int i = 0; i < 100; i++) {
+      RuleDto rule = newRuleDto(RuleKey.of("javascript", "S00" + i));
+      db.ruleDao().insert(dbSession, rule);
+
+      ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, rule).setSeverity(Severity.MAJOR);
+      db.activeRuleDao().insert(dbSession, activeRule);
+    }
+    dbSession.commit();
+
+    // verify index
+    Collection<ActiveRule> activeRules = index.findByProfile(profileDto.getKey());
+    assertThat(activeRules).hasSize(100);
+  }
+
+  private RuleDto newRuleDto(RuleKey ruleKey) {
+    return new RuleDto()
+      .setRuleKey(ruleKey.rule())
+      .setRepositoryKey(ruleKey.repository())
+      .setName("Rule " + ruleKey.rule())
+      .setDescription("Description " + ruleKey.rule())
+      .setStatus(RuleStatus.READY)
+      .setConfigKey("InternalKey" + ruleKey.rule())
+      .setSeverity(Severity.INFO)
+      .setCardinality(Cardinality.SINGLE)
+      .setLanguage("js")
+      .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
+      .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
+      .setRemediationCoefficient("1h")
+      .setDefaultRemediationCoefficient("5d")
+      .setRemediationOffset("5min")
+      .setDefaultRemediationOffset("10h")
+      .setEffortToFixDescription(ruleKey.repository() + "." + ruleKey.rule() + ".effortToFix");
+  }
+}
index bb32256ca0332d3461aa828f7f7afe6f472952a5..bf78b005a9c543c1c4de737e09dbf9de8471c06f 100644 (file)
@@ -67,8 +67,9 @@ public class RuleActivatorMediumTest {
     index = tester.get(ActiveRuleIndex.class);
 
     // create quality profile
-    db.qualityProfileDao().insert(dbSession, QualityProfileDto.createFor("MyProfile", "xoo"));
+    db.qualityProfileDao().insert(dbSession, QualityProfileDto.createFor(PROFILE_KEY));
     dbSession.commit();
+    dbSession.clearCache();
   }
 
   @Test
@@ -167,10 +168,10 @@ public class RuleActivatorMediumTest {
     ruleActivator.activate(activation);
 
 
-    assertThat(db.activeRuleDao().getParamsByKeyAndName(activeRuleKey, "max", dbSession)).isNotNull();
+    assertThat(db.activeRuleDao().getParamByKeyAndName(activeRuleKey, "max", dbSession)).isNotNull();
     db.activeRuleDao().removeParamByKeyAndName(dbSession, activeRuleKey, "max");
     dbSession.commit();
-    assertThat(db.activeRuleDao().getParamsByKeyAndName(activeRuleKey, "max", dbSession)).isNull();
+    assertThat(db.activeRuleDao().getParamByKeyAndName(activeRuleKey, "max", dbSession)).isNull();
 
 
     // update
@@ -400,9 +401,10 @@ public class RuleActivatorMediumTest {
 
   private void verifyZeroActiveRules(ActiveRuleKey key) {
     // verify db
+    dbSession.clearCache();
     List<ActiveRuleDto> activeRuleDtos = db.activeRuleDao().findByProfileKey(dbSession, key.qProfile());
     assertThat(activeRuleDtos).isEmpty();
-    //TODO test params
+    assertThat(db.activeRuleDao().findAllParams(dbSession)).isEmpty();
 
     // verify es
     ActiveRule activeRule = index.getByKey(key);
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java
deleted file mode 100644 (file)
index ffe5fba..0000000
+++ /dev/null
@@ -1,228 +0,0 @@
-/*
- * 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.qualityprofile.index;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.RuleStatus;
-import org.sonar.api.rule.Severity;
-import org.sonar.api.server.debt.DebtRemediationFunction;
-import org.sonar.check.Cardinality;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.core.qualityprofile.db.QualityProfileKey;
-import org.sonar.core.rule.RuleDto;
-import org.sonar.core.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.qualityprofile.ActiveRule;
-import org.sonar.server.rule.RuleTesting;
-import org.sonar.server.tester.ServerTester;
-
-import java.util.Collection;
-import java.util.List;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class ActiveRuleIndexMediumTest {
-
-  @ClassRule
-  public static ServerTester tester = new ServerTester();
-
-  DbClient db = tester.get(DbClient.class);
-  ActiveRuleIndex index = tester.get(ActiveRuleIndex.class);
-  DbSession dbSession;
-
-  @Before
-  public void before() {
-    tester.clearDbAndEs();
-    dbSession = db.openSession(false);
-  }
-
-  @After
-  public void after() {
-    dbSession.close();
-  }
-
-  @Test
-  public void insert_and_index_active_rule() throws InterruptedException {
-    QualityProfileDto profileDto = QualityProfileDto.createFor("myprofile", "java");
-    db.qualityProfileDao().insert(dbSession, profileDto);
-    RuleKey ruleKey = RuleKey.of("javascript", "S001");
-    RuleDto ruleDto = newRuleDto(ruleKey);
-    db.ruleDao().insert(dbSession, ruleDto);
-
-    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, ruleDto)
-      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
-      .setSeverity(Severity.BLOCKER);
-    db.activeRuleDao().insert(dbSession, activeRule);
-    dbSession.commit();
-
-    // verify db
-    List<ActiveRuleDto> persistedDtos = db.activeRuleDao().findByRule(dbSession, ruleDto);
-    assertThat(persistedDtos).hasSize(1);
-
-    // verify es
-    ActiveRule hit = index.getByKey(activeRule.getKey());
-    assertThat(hit).isNotNull();
-    assertThat(hit.key()).isEqualTo(activeRule.getKey());
-    assertThat(hit.inheritance().name()).isEqualTo(activeRule.getInheritance());
-    assertThat(hit.parentKey()).isEqualTo(activeRule.getParentId());
-    assertThat(hit.severity()).isEqualTo(activeRule.getSeverityString());
-  }
-
-  @Test
-  public void insert_and_index_active_rule_param() throws InterruptedException {
-    // insert and index
-    QualityProfileDto profileDto = QualityProfileDto.createFor("myprofile", "java");
-    db.qualityProfileDao().insert(dbSession, profileDto);
-    RuleKey ruleKey = RuleKey.of("javascript", "S001");
-    RuleDto ruleDto = newRuleDto(ruleKey);
-    db.ruleDao().insert(dbSession, ruleDto);
-
-    RuleParamDto minParam = new RuleParamDto()
-      .setName("min")
-      .setType("STRING");
-    db.ruleDao().addRuleParam(dbSession, ruleDto, minParam);
-
-    RuleParamDto maxParam = new RuleParamDto()
-      .setName("max")
-      .setType("STRING");
-    db.ruleDao().addRuleParam(dbSession, ruleDto, maxParam);
-
-    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, ruleDto)
-      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
-      .setSeverity(Severity.BLOCKER);
-    db.activeRuleDao().insert(dbSession, activeRule);
-
-    ActiveRuleParamDto activeRuleMinParam = ActiveRuleParamDto.createFor(minParam)
-      .setValue("minimum");
-    db.activeRuleDao().addParam(dbSession, activeRule, activeRuleMinParam);
-
-    ActiveRuleParamDto activeRuleMaxParam = ActiveRuleParamDto.createFor(maxParam)
-      .setValue("maximum");
-    db.activeRuleDao().addParam(dbSession, activeRule, activeRuleMaxParam);
-
-    dbSession.commit();
-
-    // verify db
-    List<ActiveRuleParamDto> persistedDtos = db.activeRuleDao().findParamsByActiveRule(dbSession, activeRule);
-    assertThat(persistedDtos).hasSize(2);
-
-    // verify es
-    ActiveRule rule = index.getByKey(activeRule.getKey());
-    assertThat(rule.params()).hasSize(2);
-    assertThat(rule.params().keySet()).containsOnly("min", "max");
-    assertThat(rule.params().values()).containsOnly("minimum", "maximum");
-    assertThat(rule.params().get("min")).isEqualTo("minimum");
-  }
-
-  @Test
-  public void find_active_rules() throws Exception {
-    QualityProfileDto profile1 = QualityProfileDto.createFor("p1", "java");
-    QualityProfileDto profile2 = QualityProfileDto.createFor("p2", "java");
-    db.qualityProfileDao().insert(dbSession, profile1, profile2);
-
-    RuleDto rule1 = RuleTesting.newDto(RuleKey.of("java", "r1")).setSeverity(Severity.MAJOR);
-    RuleDto rule2 = RuleTesting.newDto(RuleKey.of("java", "r2")).setSeverity(Severity.MAJOR);
-    db.ruleDao().insert(dbSession, rule1);
-    db.ruleDao().insert(dbSession, rule2);
-
-    db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule1).setSeverity(Severity.MINOR));
-    db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule2).setSeverity(Severity.BLOCKER));
-    db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, rule2).setSeverity(Severity.CRITICAL));
-    dbSession.commit();
-
-    // 1. find by rule key
-
-    // in db
-    dbSession.clearCache();
-    assertThat(db.activeRuleDao().findByRule(dbSession, rule1)).hasSize(1);
-    assertThat(db.activeRuleDao().findByRule(dbSession, rule2)).hasSize(2);
-
-    // in es
-    List<ActiveRule> activeRules = index.findByRule(RuleKey.of("java", "r1"));
-    assertThat(activeRules).hasSize(1);
-    assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RuleKey.of("java", "r1"));
-
-    activeRules = index.findByRule(RuleKey.of("java", "r2"));
-    assertThat(activeRules).hasSize(2);
-    assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RuleKey.of("java", "r2"));
-
-    activeRules = index.findByRule(RuleKey.of("java", "r3"));
-    assertThat(activeRules).isEmpty();
-
-    // 2. find by profile
-    activeRules = index.findByProfile(profile1.getKey());
-    assertThat(activeRules).hasSize(2);
-    assertThat(activeRules.get(0).key().qProfile()).isEqualTo(profile1.getKey());
-    assertThat(activeRules.get(1).key().qProfile()).isEqualTo(profile1.getKey());
-
-    activeRules = index.findByProfile(profile2.getKey());
-    assertThat(activeRules).hasSize(1);
-    assertThat(activeRules.get(0).key().qProfile()).isEqualTo(profile2.getKey());
-
-    activeRules = index.findByProfile(QualityProfileKey.of("unknown", "unknown"));
-    assertThat(activeRules).isEmpty();
-  }
-
-  @Test
-  public void find_many_active_rules_by_profile() throws InterruptedException {
-    // insert and index
-    QualityProfileDto profileDto = QualityProfileDto.createFor("P1", "java");
-    db.qualityProfileDao().insert(dbSession, profileDto);
-    for (int i = 0; i < 100; i++) {
-      RuleDto rule = newRuleDto(RuleKey.of("javascript", "S00" + i));
-      db.ruleDao().insert(dbSession, rule);
-
-      ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, rule).setSeverity(Severity.MAJOR);
-      db.activeRuleDao().insert(dbSession, activeRule);
-    }
-    dbSession.commit();
-
-    // verify index
-    Collection<ActiveRule> activeRules = index.findByProfile(profileDto.getKey());
-    assertThat(activeRules).hasSize(100);
-  }
-
-  private RuleDto newRuleDto(RuleKey ruleKey) {
-    return new RuleDto()
-      .setRuleKey(ruleKey.rule())
-      .setRepositoryKey(ruleKey.repository())
-      .setName("Rule " + ruleKey.rule())
-      .setDescription("Description " + ruleKey.rule())
-      .setStatus(RuleStatus.READY)
-      .setConfigKey("InternalKey" + ruleKey.rule())
-      .setSeverity(Severity.INFO)
-      .setCardinality(Cardinality.SINGLE)
-      .setLanguage("js")
-      .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
-      .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
-      .setRemediationCoefficient("1h")
-      .setDefaultRemediationCoefficient("5d")
-      .setRemediationOffset("5min")
-      .setDefaultRemediationOffset("10h")
-      .setEffortToFixDescription(ruleKey.repository() + "." + ruleKey.rule() + ".effortToFix");
-  }
-}
index a5cf73465f95054d3bb483f57287931eac570d60..f558209bd5c97b368a6ae945807b713f4f1c9048 100644 (file)
@@ -64,7 +64,7 @@ public class QProfilesWsMediumTest {
 
   @Before
   public void setUp() throws Exception {
-    tester.clearDbAndEs();
+    tester.clearDbAndIndexes();
     db = tester.get(DbClient.class);
     ws = tester.get(QProfilesWs.class);
     wsTester = tester.get(WsTester.class);
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java
new file mode 100644 (file)
index 0000000..4bab5f4
--- /dev/null
@@ -0,0 +1,344 @@
+/*
+ * 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.ImmutableSet;
+import com.google.common.collect.Iterables;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rule.Severity;
+import org.sonar.api.server.debt.DebtRemediationFunction;
+import org.sonar.api.server.rule.RuleParamType;
+import org.sonar.check.Cardinality;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.core.rule.RuleParamDto;
+import org.sonar.core.technicaldebt.db.CharacteristicDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.rule.db.RuleDao;
+import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.rule.index.RuleQuery;
+import org.sonar.server.search.QueryOptions;
+import org.sonar.server.tester.ServerTester;
+
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+/**
+ * Test persistence in db and indexation in es (--> integration of DAOs and Indexes)
+ */
+public class RuleBackendMediumTest {
+
+  @ClassRule
+  public static ServerTester tester = new ServerTester();
+
+  RuleDao dao = tester.get(RuleDao.class);
+  RuleIndex index = tester.get(RuleIndex.class);
+  DbClient db;
+  DbSession dbSession;
+
+  @Before
+  public void before() {
+    tester.clearDbAndIndexes();
+    db = tester.get(DbClient.class);
+    dbSession = tester.get(MyBatis.class).openSession(false);
+  }
+
+  @After
+  public void after() {
+    dbSession.close();
+  }
+
+  @Test
+  public void insert_in_db_and_index_in_es() throws InterruptedException {
+    // insert db
+    RuleKey ruleKey = RuleKey.of("javascript", "S001");
+    dao.insert(dbSession, newRuleDto(ruleKey));
+    dbSession.commit();
+
+    // verify that rule is persisted in db
+    RuleDto persistedDto = dao.getNullableByKey(dbSession, ruleKey);
+    assertThat(persistedDto).isNotNull();
+    assertThat(persistedDto.getId()).isGreaterThanOrEqualTo(0);
+    assertThat(persistedDto.getRuleKey()).isEqualTo(ruleKey.rule());
+    assertThat(persistedDto.getLanguage()).isEqualTo("js");
+    assertThat(persistedDto.getTags()).containsOnly("tag1", "tag2");
+    assertThat(persistedDto.getSystemTags()).containsOnly("systag1", "systag2");
+    assertThat(persistedDto.getCreatedAt()).isNotNull();
+    assertThat(persistedDto.getUpdatedAt()).isNotNull();
+
+    // verify that rule is indexed in es
+    Rule hit = index.getByKey(ruleKey);
+    assertThat(hit).isNotNull();
+    assertThat(hit.key().repository()).isEqualTo(ruleKey.repository());
+    assertThat(hit.key().rule()).isEqualTo(ruleKey.rule());
+    assertThat(hit.language()).isEqualTo("js");
+    assertThat(hit.name()).isEqualTo("Rule S001");
+    assertThat(hit.htmlDescription()).isEqualTo("Description S001");
+    assertThat(hit.status()).isEqualTo(RuleStatus.READY);
+    assertThat(hit.createdAt()).isNotNull();
+    assertThat(hit.updatedAt()).isNotNull();
+    assertThat(hit.internalKey()).isEqualTo("InternalKeyS001");
+    assertThat(hit.severity()).isEqualTo("INFO");
+    assertThat(hit.template()).isFalse();
+    assertThat(hit.tags()).containsOnly("tag1", "tag2");
+    assertThat(hit.systemTags()).containsOnly("systag1", "systag2");
+  }
+
+  @Test
+  public void insert_and_index_rule_parameters() {
+    // insert db
+    RuleKey ruleKey = RuleKey.of("javascript", "S001");
+    RuleDto ruleDto = newRuleDto(ruleKey);
+    dao.insert(dbSession, ruleDto);
+    dbSession.commit();
+
+    RuleParamDto minParamDto = new RuleParamDto()
+      .setName("min")
+      .setType(RuleParamType.INTEGER.type())
+      .setDefaultValue("2")
+      .setDescription("Minimum");
+    dao.addRuleParam(dbSession, ruleDto, minParamDto);
+    RuleParamDto maxParamDto = new RuleParamDto()
+      .setName("max")
+      .setType(RuleParamType.INTEGER.type())
+      .setDefaultValue("10")
+      .setDescription("Maximum");
+    dao.addRuleParam(dbSession, ruleDto, maxParamDto);
+    dbSession.commit();
+
+    //Verify that RuleDto has date from insertion
+    RuleDto theRule = dao.getNullableByKey(dbSession, ruleKey);
+    assertThat(theRule.getCreatedAt()).isNotNull();
+    assertThat(theRule.getUpdatedAt()).isNotNull();
+
+    // verify that parameters are persisted in db
+    List<RuleParamDto> persistedDtos = dao.findRuleParamsByRuleKey(dbSession, theRule.getKey());
+    assertThat(persistedDtos).hasSize(2);
+
+    // verify that parameters are indexed in es
+
+    Rule hit = index.getByKey(ruleKey);
+    assertThat(hit).isNotNull();
+    assertThat(hit.key()).isNotNull();
+
+    RuleService service = tester.get(RuleService.class);
+    Rule rule = service.getByKey(ruleKey);
+
+    assertThat(rule.params()).hasSize(2);
+    assertThat(Iterables.getLast(rule.params(), null).key()).isEqualTo("max");
+  }
+
+  @Test
+  public void insert_and_update_rule() {
+    // insert db
+    RuleKey ruleKey = RuleKey.of("javascript", "S001");
+    RuleDto ruleDto = newRuleDto(ruleKey)
+      .setTags(ImmutableSet.of("hello"))
+      .setName("first name");
+    dao.insert(dbSession, ruleDto);
+    dbSession.commit();
+
+    // verify that parameters are indexed in es
+
+    Rule hit = index.getByKey(ruleKey);
+    assertThat(hit.tags()).containsExactly("hello");
+    assertThat(hit.name()).isEqualTo("first name");
+
+    //Update in DB
+    ruleDto.setTags(ImmutableSet.of("world"))
+      .setName("second name");
+    dao.update(dbSession, ruleDto);
+    dbSession.commit();
+
+    // verify that parameters are updated in es
+
+    hit = index.getByKey(ruleKey);
+    assertThat(hit.tags()).containsExactly("world");
+    assertThat(hit.name()).isEqualTo("second name");
+  }
+
+  @Test
+  public void insert_and_update_rule_param() throws InterruptedException {
+
+    // insert db
+    RuleKey ruleKey = RuleKey.of("javascript", "S001");
+    RuleDto ruleDto = newRuleDto(ruleKey)
+      .setTags(ImmutableSet.of("hello"))
+      .setName("first name");
+    dao.insert(dbSession, ruleDto);
+    dbSession.commit();
+
+    RuleParamDto minParamDto = new RuleParamDto()
+      .setName("min")
+      .setType(RuleParamType.INTEGER.type())
+      .setDefaultValue("2")
+      .setDescription("Minimum");
+    dao.addRuleParam(dbSession, ruleDto, minParamDto);
+
+    RuleParamDto maxParamDto = new RuleParamDto()
+      .setName("max")
+      .setType(RuleParamType.INTEGER.type())
+      .setDefaultValue("10")
+      .setDescription("Maximum");
+    dao.addRuleParam(dbSession, ruleDto, maxParamDto);
+    dbSession.commit();
+
+    // verify that parameters are indexed in es
+
+    Rule hit = index.getByKey(ruleKey);
+    assertThat(hit.params()).hasSize(2);
+
+    RuleParam param = hit.params().get(0);
+    assertThat(param.key()).isEqualTo("min");
+    assertThat(param.defaultValue()).isEqualTo("2");
+    assertThat(param.description()).isEqualTo("Minimum");
+
+
+    //Update in DB
+    minParamDto
+      .setDefaultValue("0.5")
+      .setDescription("new description");
+    dao.updateRuleParam(dbSession, ruleDto, minParamDto);
+    dbSession.commit();
+
+    // verify that parameters are updated in es
+
+    hit = index.getByKey(ruleKey);
+    assertThat(hit.params()).hasSize(2);
+
+    param = hit.params().get(0);
+    assertThat(param.key()).isEqualTo("min");
+    assertThat(param.defaultValue()).isEqualTo("0.5");
+    assertThat(param.description()).isEqualTo("new description");
+  }
+
+  @Test
+  public void insert_update_characteristics() throws Exception {
+
+    CharacteristicDto char1 = new CharacteristicDto()
+      .setEnabled(true)
+      .setKey("c1")
+      .setName("char1");
+    db.debtCharacteristicDao().insert(char1, dbSession);
+    dbSession.commit();
+
+    CharacteristicDto char11 = new CharacteristicDto()
+      .setEnabled(true)
+      .setKey("c11")
+      .setName("char11")
+      .setParentId(char1.getId());
+    db.debtCharacteristicDao().insert(char11, dbSession);
+
+    RuleKey ruleKey = RuleKey.of("test", "r1");
+    RuleDto ruleDto = newRuleDto(ruleKey)
+      .setDefaultSubCharacteristicId(char11.getId());
+    dao.insert(dbSession, ruleDto);
+    dbSession.commit();
+
+
+    // 0. assert chars in DB
+    assertThat(db.debtCharacteristicDao().selectByKey("c1", dbSession)).isNotNull();
+    assertThat(db.debtCharacteristicDao().selectByKey("c1", dbSession).getParentId()).isNull();
+    assertThat(db.debtCharacteristicDao().selectByKey("c11", dbSession)).isNotNull();
+    assertThat(db.debtCharacteristicDao().selectByKey("c11", dbSession).getParentId()).isEqualTo(char1.getId());
+
+    // 1. find char and subChar from rule
+    Rule rule = index.getByKey(ruleKey);
+    assertThat(rule.debtCharacteristicKey()).isEqualTo(char1.getKey());
+    assertThat(rule.debtSubCharacteristicKey()).isEqualTo(char11.getKey());
+
+    // 3. set Non-default characteristics
+    CharacteristicDto char2 = new CharacteristicDto()
+      .setEnabled(true)
+      .setKey("c2")
+      .setName("char2");
+    db.debtCharacteristicDao().insert(char2, dbSession);
+
+    CharacteristicDto char21 = new CharacteristicDto()
+      .setEnabled(true)
+      .setKey("c21")
+      .setName("char21")
+      .setParentId(char2.getId());
+    db.debtCharacteristicDao().insert(char21, dbSession);
+
+    ruleDto.setSubCharacteristicId(char21.getId());
+    dao.update(dbSession, ruleDto);
+
+    dbSession.commit();
+
+    // 4. Get non-default chars from Rule
+    rule = index.getByKey(ruleKey);
+    assertThat(rule.debtCharacteristicKey()).isEqualTo(char2.getKey());
+    assertThat(rule.debtSubCharacteristicKey()).isEqualTo(char21.getKey());
+  }
+
+
+  @Test
+  public void should_not_find_removed() {
+    // insert db
+    RuleKey ruleKey = RuleKey.of("javascript", "S001");
+    RuleDto ruleDto = newRuleDto(ruleKey).setStatus(RuleStatus.READY);
+
+    RuleKey removedKey = RuleKey.of("javascript", "S002");
+    RuleDto removedDto = newRuleDto(removedKey).setStatus(RuleStatus.REMOVED);
+    dao.insert(dbSession, ruleDto);
+    dao.insert(dbSession, removedDto);
+    dbSession.commit();
+
+    // 0. Assert rules are in DB
+    assertThat(dao.findAll(dbSession)).hasSize(2);
+
+    // 1. assert getBy for removed
+    assertThat(index.getByKey(removedKey)).isNotNull();
+
+    // 2. assert find does not get REMOVED
+    assertThat(index.search(new RuleQuery(), QueryOptions.DEFAULT)
+    .getRules()).hasSize(1);
+  }
+
+  private RuleDto newRuleDto(RuleKey ruleKey) {
+    return new RuleDto()
+      .setRuleKey(ruleKey.rule())
+      .setRepositoryKey(ruleKey.repository())
+      .setName("Rule " + ruleKey.rule())
+      .setDescription("Description " + ruleKey.rule())
+      .setStatus(RuleStatus.READY)
+      .setConfigKey("InternalKey" + ruleKey.rule())
+      .setSeverity(Severity.INFO)
+      .setCardinality(Cardinality.SINGLE)
+      .setLanguage("js")
+      .setTags(ImmutableSet.of("tag1", "tag2"))
+      .setSystemTags(ImmutableSet.of("systag1", "systag2"))
+      .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
+      .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
+      .setRemediationCoefficient("1h")
+      .setDefaultRemediationCoefficient("5d")
+      .setRemediationOffset("5min")
+      .setDefaultRemediationOffset("10h")
+      .setEffortToFixDescription(ruleKey.repository() + "." + ruleKey.rule() + ".effortToFix");
+  }
+}
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleDataMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleDataMediumTest.java
deleted file mode 100644 (file)
index 1b72c91..0000000
+++ /dev/null
@@ -1,344 +0,0 @@
-/*
- * 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.ImmutableSet;
-import com.google.common.collect.Iterables;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.RuleStatus;
-import org.sonar.api.rule.Severity;
-import org.sonar.api.server.debt.DebtRemediationFunction;
-import org.sonar.api.server.rule.RuleParamType;
-import org.sonar.check.Cardinality;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.rule.RuleDto;
-import org.sonar.core.rule.RuleParamDto;
-import org.sonar.core.technicaldebt.db.CharacteristicDto;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.rule.db.RuleDao;
-import org.sonar.server.rule.index.RuleIndex;
-import org.sonar.server.rule.index.RuleQuery;
-import org.sonar.server.search.QueryOptions;
-import org.sonar.server.tester.ServerTester;
-
-import java.util.List;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-/**
- * Test persistence in db and indexation in es (--> integration of DAOs and Indexes)
- */
-public class RuleDataMediumTest {
-
-  @ClassRule
-  public static ServerTester tester = new ServerTester();
-
-  RuleDao dao = tester.get(RuleDao.class);
-  RuleIndex index = tester.get(RuleIndex.class);
-  DbClient db;
-  DbSession dbSession;
-
-  @Before
-  public void before() {
-    tester.clearDbAndEs();
-    db = tester.get(DbClient.class);
-    dbSession = tester.get(MyBatis.class).openSession(false);
-  }
-
-  @After
-  public void after() {
-    dbSession.close();
-  }
-
-  @Test
-  public void insert_in_db_and_index_in_es() throws InterruptedException {
-    // insert db
-    RuleKey ruleKey = RuleKey.of("javascript", "S001");
-    dao.insert(dbSession, newRuleDto(ruleKey));
-    dbSession.commit();
-
-    // verify that rule is persisted in db
-    RuleDto persistedDto = dao.getNullableByKey(dbSession, ruleKey);
-    assertThat(persistedDto).isNotNull();
-    assertThat(persistedDto.getId()).isGreaterThanOrEqualTo(0);
-    assertThat(persistedDto.getRuleKey()).isEqualTo(ruleKey.rule());
-    assertThat(persistedDto.getLanguage()).isEqualTo("js");
-    assertThat(persistedDto.getTags()).containsOnly("tag1", "tag2");
-    assertThat(persistedDto.getSystemTags()).containsOnly("systag1", "systag2");
-    assertThat(persistedDto.getCreatedAt()).isNotNull();
-    assertThat(persistedDto.getUpdatedAt()).isNotNull();
-
-    // verify that rule is indexed in es
-    Rule hit = index.getByKey(ruleKey);
-    assertThat(hit).isNotNull();
-    assertThat(hit.key().repository()).isEqualTo(ruleKey.repository());
-    assertThat(hit.key().rule()).isEqualTo(ruleKey.rule());
-    assertThat(hit.language()).isEqualTo("js");
-    assertThat(hit.name()).isEqualTo("Rule S001");
-    assertThat(hit.htmlDescription()).isEqualTo("Description S001");
-    assertThat(hit.status()).isEqualTo(RuleStatus.READY);
-    assertThat(hit.createdAt()).isNotNull();
-    assertThat(hit.updatedAt()).isNotNull();
-    assertThat(hit.internalKey()).isEqualTo("InternalKeyS001");
-    assertThat(hit.severity()).isEqualTo("INFO");
-    assertThat(hit.template()).isFalse();
-    assertThat(hit.tags()).containsOnly("tag1", "tag2");
-    assertThat(hit.systemTags()).containsOnly("systag1", "systag2");
-  }
-
-  @Test
-  public void insert_and_index_rule_parameters() {
-    // insert db
-    RuleKey ruleKey = RuleKey.of("javascript", "S001");
-    RuleDto ruleDto = newRuleDto(ruleKey);
-    dao.insert(dbSession, ruleDto);
-    dbSession.commit();
-
-    RuleParamDto minParamDto = new RuleParamDto()
-      .setName("min")
-      .setType(RuleParamType.INTEGER.type())
-      .setDefaultValue("2")
-      .setDescription("Minimum");
-    dao.addRuleParam(dbSession, ruleDto, minParamDto);
-    RuleParamDto maxParamDto = new RuleParamDto()
-      .setName("max")
-      .setType(RuleParamType.INTEGER.type())
-      .setDefaultValue("10")
-      .setDescription("Maximum");
-    dao.addRuleParam(dbSession, ruleDto, maxParamDto);
-    dbSession.commit();
-
-    //Verify that RuleDto has date from insertion
-    RuleDto theRule = dao.getNullableByKey(dbSession, ruleKey);
-    assertThat(theRule.getCreatedAt()).isNotNull();
-    assertThat(theRule.getUpdatedAt()).isNotNull();
-
-    // verify that parameters are persisted in db
-    List<RuleParamDto> persistedDtos = dao.findRuleParamsByRuleKey(dbSession, theRule.getKey());
-    assertThat(persistedDtos).hasSize(2);
-
-    // verify that parameters are indexed in es
-
-    Rule hit = index.getByKey(ruleKey);
-    assertThat(hit).isNotNull();
-    assertThat(hit.key()).isNotNull();
-
-    RuleService service = tester.get(RuleService.class);
-    Rule rule = service.getByKey(ruleKey);
-
-    assertThat(rule.params()).hasSize(2);
-    assertThat(Iterables.getLast(rule.params(), null).key()).isEqualTo("max");
-  }
-
-  @Test
-  public void insert_and_update_rule() {
-    // insert db
-    RuleKey ruleKey = RuleKey.of("javascript", "S001");
-    RuleDto ruleDto = newRuleDto(ruleKey)
-      .setTags(ImmutableSet.of("hello"))
-      .setName("first name");
-    dao.insert(dbSession, ruleDto);
-    dbSession.commit();
-
-    // verify that parameters are indexed in es
-
-    Rule hit = index.getByKey(ruleKey);
-    assertThat(hit.tags()).containsExactly("hello");
-    assertThat(hit.name()).isEqualTo("first name");
-
-    //Update in DB
-    ruleDto.setTags(ImmutableSet.of("world"))
-      .setName("second name");
-    dao.update(dbSession, ruleDto);
-    dbSession.commit();
-
-    // verify that parameters are updated in es
-
-    hit = index.getByKey(ruleKey);
-    assertThat(hit.tags()).containsExactly("world");
-    assertThat(hit.name()).isEqualTo("second name");
-  }
-
-  @Test
-  public void insert_and_update_rule_param() throws InterruptedException {
-
-    // insert db
-    RuleKey ruleKey = RuleKey.of("javascript", "S001");
-    RuleDto ruleDto = newRuleDto(ruleKey)
-      .setTags(ImmutableSet.of("hello"))
-      .setName("first name");
-    dao.insert(dbSession, ruleDto);
-    dbSession.commit();
-
-    RuleParamDto minParamDto = new RuleParamDto()
-      .setName("min")
-      .setType(RuleParamType.INTEGER.type())
-      .setDefaultValue("2")
-      .setDescription("Minimum");
-    dao.addRuleParam(dbSession, ruleDto, minParamDto);
-
-    RuleParamDto maxParamDto = new RuleParamDto()
-      .setName("max")
-      .setType(RuleParamType.INTEGER.type())
-      .setDefaultValue("10")
-      .setDescription("Maximum");
-    dao.addRuleParam(dbSession, ruleDto, maxParamDto);
-    dbSession.commit();
-
-    // verify that parameters are indexed in es
-
-    Rule hit = index.getByKey(ruleKey);
-    assertThat(hit.params()).hasSize(2);
-
-    RuleParam param = hit.params().get(0);
-    assertThat(param.key()).isEqualTo("min");
-    assertThat(param.defaultValue()).isEqualTo("2");
-    assertThat(param.description()).isEqualTo("Minimum");
-
-
-    //Update in DB
-    minParamDto
-      .setDefaultValue("0.5")
-      .setDescription("new description");
-    dao.updateRuleParam(dbSession, ruleDto, minParamDto);
-    dbSession.commit();
-
-    // verify that parameters are updated in es
-
-    hit = index.getByKey(ruleKey);
-    assertThat(hit.params()).hasSize(2);
-
-    param = hit.params().get(0);
-    assertThat(param.key()).isEqualTo("min");
-    assertThat(param.defaultValue()).isEqualTo("0.5");
-    assertThat(param.description()).isEqualTo("new description");
-  }
-
-  @Test
-  public void insert_update_characteristics() throws Exception {
-
-    CharacteristicDto char1 = new CharacteristicDto()
-      .setEnabled(true)
-      .setKey("c1")
-      .setName("char1");
-    db.debtCharacteristicDao().insert(char1, dbSession);
-    dbSession.commit();
-
-    CharacteristicDto char11 = new CharacteristicDto()
-      .setEnabled(true)
-      .setKey("c11")
-      .setName("char11")
-      .setParentId(char1.getId());
-    db.debtCharacteristicDao().insert(char11, dbSession);
-
-    RuleKey ruleKey = RuleKey.of("test", "r1");
-    RuleDto ruleDto = newRuleDto(ruleKey)
-      .setDefaultSubCharacteristicId(char11.getId());
-    dao.insert(dbSession, ruleDto);
-    dbSession.commit();
-
-
-    // 0. assert chars in DB
-    assertThat(db.debtCharacteristicDao().selectByKey("c1", dbSession)).isNotNull();
-    assertThat(db.debtCharacteristicDao().selectByKey("c1", dbSession).getParentId()).isNull();
-    assertThat(db.debtCharacteristicDao().selectByKey("c11", dbSession)).isNotNull();
-    assertThat(db.debtCharacteristicDao().selectByKey("c11", dbSession).getParentId()).isEqualTo(char1.getId());
-
-    // 1. find char and subChar from rule
-    Rule rule = index.getByKey(ruleKey);
-    assertThat(rule.debtCharacteristicKey()).isEqualTo(char1.getKey());
-    assertThat(rule.debtSubCharacteristicKey()).isEqualTo(char11.getKey());
-
-    // 3. set Non-default characteristics
-    CharacteristicDto char2 = new CharacteristicDto()
-      .setEnabled(true)
-      .setKey("c2")
-      .setName("char2");
-    db.debtCharacteristicDao().insert(char2, dbSession);
-
-    CharacteristicDto char21 = new CharacteristicDto()
-      .setEnabled(true)
-      .setKey("c21")
-      .setName("char21")
-      .setParentId(char2.getId());
-    db.debtCharacteristicDao().insert(char21, dbSession);
-
-    ruleDto.setSubCharacteristicId(char21.getId());
-    dao.update(dbSession, ruleDto);
-
-    dbSession.commit();
-
-    // 4. Get non-default chars from Rule
-    rule = index.getByKey(ruleKey);
-    assertThat(rule.debtCharacteristicKey()).isEqualTo(char2.getKey());
-    assertThat(rule.debtSubCharacteristicKey()).isEqualTo(char21.getKey());
-  }
-
-
-  @Test
-  public void should_not_find_removed() {
-    // insert db
-    RuleKey ruleKey = RuleKey.of("javascript", "S001");
-    RuleDto ruleDto = newRuleDto(ruleKey).setStatus(RuleStatus.READY);
-
-    RuleKey removedKey = RuleKey.of("javascript", "S002");
-    RuleDto removedDto = newRuleDto(removedKey).setStatus(RuleStatus.REMOVED);
-    dao.insert(dbSession, ruleDto);
-    dao.insert(dbSession, removedDto);
-    dbSession.commit();
-
-    // 0. Assert rules are in DB
-    assertThat(dao.findAll(dbSession)).hasSize(2);
-
-    // 1. assert getBy for removed
-    assertThat(index.getByKey(removedKey)).isNotNull();
-
-    // 2. assert find does not get REMOVED
-    assertThat(index.search(new RuleQuery(), QueryOptions.DEFAULT)
-    .getRules()).hasSize(1);
-  }
-
-  private RuleDto newRuleDto(RuleKey ruleKey) {
-    return new RuleDto()
-      .setRuleKey(ruleKey.rule())
-      .setRepositoryKey(ruleKey.repository())
-      .setName("Rule " + ruleKey.rule())
-      .setDescription("Description " + ruleKey.rule())
-      .setStatus(RuleStatus.READY)
-      .setConfigKey("InternalKey" + ruleKey.rule())
-      .setSeverity(Severity.INFO)
-      .setCardinality(Cardinality.SINGLE)
-      .setLanguage("js")
-      .setTags(ImmutableSet.of("tag1", "tag2"))
-      .setSystemTags(ImmutableSet.of("systag1", "systag2"))
-      .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
-      .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
-      .setRemediationCoefficient("1h")
-      .setDefaultRemediationCoefficient("5d")
-      .setRemediationOffset("5min")
-      .setDefaultRemediationOffset("10h")
-      .setEffortToFixDescription(ruleKey.repository() + "." + ruleKey.rule() + ".effortToFix");
-  }
-}
index 4a5a61282e3b78f36e4a29d61c18fa0312076f1c..7e5a2f0f90ea0a44d4a51256c0df06eb3ffa43df 100644 (file)
@@ -49,7 +49,7 @@ public class RuleServiceMediumTest {
 
   @Before
   public void before() {
-    tester.clearDbAndEs();
+    tester.clearDbAndIndexes();
     dbSession = tester.get(DbClient.class).openSession(false);
   }
 
index b7cfb41146f94e1c872f9a31608bd437cd2032fc..dc13bce66716cdf280ec2da63ae1e6142cda97a9 100644 (file)
@@ -63,7 +63,7 @@ public class RuleUpdaterMediumTest {
 
   @Before
   public void before() {
-    tester.clearDbAndEs();
+    tester.clearDbAndIndexes();
     dbSession = db.openSession(false);
   }
 
index 03488db1ef2102103610143e39f558c60f79c023..77f4212b750042412bd9eed6f943c6b334268404 100644 (file)
@@ -68,7 +68,7 @@ public class RuleIndexMediumTest {
 
   @Before
   public void before() {
-    tester.clearDbAndEs();
+    tester.clearDbAndIndexes();
     dbSession = myBatis.openSession(false);
   }
 
index 0f9e28b88d740df12de178e38c67b958678213ee..9fab1c7e775a42013b85c78ac706041aa2420407 100644 (file)
@@ -64,7 +64,7 @@ public class RulesWebServiceTest {
 
   @Before
   public void setUp() throws Exception {
-    tester.clearDbAndEs();
+    tester.clearDbAndIndexes();
     ruleDao = tester.get(RuleDao.class);
     ws = tester.get(RulesWebService.class);
     session = tester.get(DbClient.class).openSession(false);
diff --git a/sonar-server/src/test/java/org/sonar/server/tester/BackendCleanup.java b/sonar-server/src/test/java/org/sonar/server/tester/BackendCleanup.java
new file mode 100644 (file)
index 0000000..71a32dd
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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.tester;
+
+import org.elasticsearch.client.Client;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.ServerComponent;
+import org.sonar.core.persistence.DatabaseVersion;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.server.search.ESNode;
+
+import java.sql.Connection;
+
+public class BackendCleanup implements ServerComponent {
+
+  private static final Logger LOG = LoggerFactory.getLogger(BackendCleanup.class);
+
+  private final ESNode esNode;
+  private final MyBatis myBatis;
+
+  public BackendCleanup(ESNode esNode, MyBatis myBatis) {
+    this.esNode = esNode;
+    this.myBatis = myBatis;
+  }
+
+  public void clearAll() {
+    clearDb();
+    clearIndexes();
+  }
+
+  public void clearDb() {
+    DbSession dbSession = myBatis.openSession(false);
+    Connection connection = dbSession.getConnection();
+    try {
+      LOG.info("Truncate db tables");
+      for (String table : DatabaseVersion.TABLES) {
+        try {
+          connection.createStatement().execute("TRUNCATE TABLE " + table.toLowerCase());
+          // commit is useless on some databases
+          connection.commit();
+        } catch (Exception e) {
+          throw new IllegalStateException("Fail to truncate db table " + table, e);
+        }
+      }
+
+    } finally {
+      dbSession.close();
+    }
+  }
+
+  public void clearIndexes() {
+    LOG.info("Truncate es indices");
+    Client client = esNode.client();
+    client.prepareDeleteByQuery(client.admin().cluster().prepareState().get()
+        .getState().getMetaData().concreteAllIndices())
+      .setQuery(QueryBuilders.matchAllQuery())
+      .get();
+
+  }
+}
diff --git a/sonar-server/src/test/java/org/sonar/server/tester/DataStoreCleanup.java b/sonar-server/src/test/java/org/sonar/server/tester/DataStoreCleanup.java
deleted file mode 100644 (file)
index 889a12f..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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.tester;
-
-import org.elasticsearch.client.Client;
-import org.elasticsearch.index.query.QueryBuilders;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.ServerComponent;
-import org.sonar.core.persistence.DatabaseVersion;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.server.search.ESNode;
-
-import java.sql.Connection;
-
-public class DataStoreCleanup implements ServerComponent {
-
-  private static final Logger LOG = LoggerFactory.getLogger(DataStoreCleanup.class);
-
-  private final ESNode esNode;
-  private final MyBatis myBatis;
-
-  public DataStoreCleanup(ESNode esNode, MyBatis myBatis) {
-    this.esNode = esNode;
-    this.myBatis = myBatis;
-  }
-
-  public void clearAll() {
-    clearDb();
-    clearIndexes();
-  }
-
-  public void clearDb() {
-    DbSession dbSession = myBatis.openSession(false);
-    Connection connection = dbSession.getConnection();
-    try {
-      LOG.info("Truncate db tables");
-      for (String table : DatabaseVersion.TABLES) {
-        try {
-          connection.createStatement().execute("TRUNCATE TABLE " + table.toLowerCase());
-          // commit is useless on some databases
-          connection.commit();
-        } catch (Exception e) {
-          throw new IllegalStateException("Fail to truncate db table " + table, e);
-        }
-      }
-
-    } finally {
-      dbSession.close();
-    }
-  }
-
-  public void clearIndexes() {
-    LOG.info("Truncate es indices");
-    Client client = esNode.client();
-    client.prepareDeleteByQuery(client.admin().cluster().prepareState().get()
-        .getState().getMetaData().concreteAllIndices())
-      .setQuery(QueryBuilders.matchAllQuery())
-      .get();
-
-  }
-}
index c11caa2eb743c737154f0e45528a27ef675f677c..d9156a2dcf74d7588fb1ec2b53151150ce271659 100644 (file)
@@ -51,7 +51,7 @@ public class ServerTester extends ExternalResource {
 
   private final Platform platform;
   private final File homeDir;
-  private final List components = Lists.newArrayList(DataStoreCleanup.class, WsTester.class);
+  private final List components = Lists.newArrayList(BackendCleanup.class, WsTester.class);
   private final Properties initialProps = new Properties();
 
   public ServerTester() {
@@ -156,16 +156,16 @@ public class ServerTester extends ExternalResource {
   }
 
   /**
-   * Truncate all db tables and es indices. Can be executed only if ServerTester is started.
+   * Truncate all db tables and Elasticsearch indexes. Can be executed only if ServerTester is started.
    */
-  public void clearDbAndEs() {
+  public void clearDbAndIndexes() {
     checkStarted();
-    get(DataStoreCleanup.class).clearAll();
+    get(BackendCleanup.class).clearAll();
   }
 
   public void clearIndexes() {
     checkStarted();
-    get(DataStoreCleanup.class).clearIndexes();
+    get(BackendCleanup.class).clearIndexes();
   }
 
   /**