]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 complete tests
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 29 May 2014 08:54:56 +0000 (10:54 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 29 May 2014 08:55:06 +0000 (10:55 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/db/ActiveRuleDaoTest.java [deleted file]
sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java
sonar-server/src/test/java/org/sonar/server/tester/ServerTester.java

index 4bb2964cfecf8e6db0a4d1c571231601e4d8df83..b1346dd9e2878800ef61f49363898070439a107e 100644 (file)
@@ -72,10 +72,10 @@ public class QProfileService implements ServerComponent {
 
 
   public Multimap<String, String> bulkActivate(RuleQuery ruleQuery, QualityProfileKey profile) {
-    return ruleActivator.activateByRuleQuery(ruleQuery, profile);
+    return ruleActivator.bulkActivate(ruleQuery, profile);
   }
 
   public Multimap<String, String> bulkDeactivate(RuleQuery ruleQuery, QualityProfileKey profile) {
-    return ruleActivator.deActivateByRuleQuery(ruleQuery, profile);
+    return ruleActivator.bulkDeactivate(ruleQuery, profile);
   }
 }
index 1344623830a7ddaee01244ba90510168ae322b75..fc3e5909bcf37f31a6f4e1857a15cbf97c5f6b6a 100644 (file)
@@ -35,7 +35,6 @@ import org.sonar.api.rules.ActiveRuleParam;
 import org.sonar.api.utils.TimeProfiler;
 import org.sonar.api.utils.ValidationMessages;
 import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
 import org.sonar.core.qualityprofile.db.ActiveRuleKey;
 import org.sonar.core.qualityprofile.db.QualityProfileDto;
 import org.sonar.core.qualityprofile.db.QualityProfileKey;
@@ -108,7 +107,7 @@ public class RegisterQualityProfiles implements ServerComponent {
       }
       session.commit();
     } finally {
-      MyBatis.closeQuietly(session);
+      session.close();
       profiler.stop();
     }
   }
index b231371a5cb94d7ed3965f8f296d5e8cabf75444..4fcb3b8cd418d7fe1a308001ef6178d1b322bbab 100644 (file)
@@ -220,18 +220,18 @@ public class RuleActivator implements ServerComponent {
     }
   }
 
-  public Multimap<String, String> activateByRuleQuery(RuleQuery ruleQuery, QualityProfileKey profile) {
+  public Multimap<String, String> bulkActivate(RuleQuery ruleQuery, QualityProfileKey profile) {
     verifyPermission(UserSession.get());
     RuleIndex ruleIndex = index.get(RuleIndex.class);
     Multimap<String, String> results = ArrayListMultimap.create();
     DbSession dbSession = db.openSession(false);
 
     try {
+      // TODO pb because limited to QueryOptions.MAX_LIMIT
       RuleResult result = ruleIndex.search(ruleQuery,
         QueryOptions.DEFAULT.setOffset(0)
           .setLimit(Integer.MAX_VALUE)
-          .setFieldsToReturn(ImmutableSet.of("template", "severity"))
-      );
+          .setFieldsToReturn(ImmutableSet.of("template", "severity")));
 
       for (Rule rule : result.getHits()) {
         if (!rule.template()) {
@@ -252,7 +252,7 @@ public class RuleActivator implements ServerComponent {
     return results;
   }
 
-  public Multimap<String, String> deActivateByRuleQuery(RuleQuery ruleQuery, QualityProfileKey profile) {
+  public Multimap<String, String> bulkDeactivate(RuleQuery ruleQuery, QualityProfileKey profile) {
     verifyPermission(UserSession.get());
     RuleIndex ruleIndex = index.get(RuleIndex.class);
     Multimap<String, String> results = ArrayListMultimap.create();
@@ -261,6 +261,7 @@ public class RuleActivator implements ServerComponent {
     try {
       RuleResult result = ruleIndex.search(ruleQuery,
         QueryOptions.DEFAULT.setOffset(0)
+          // TODO pb because limited to QueryOptions.MAX_LIMIT
           .setLimit(Integer.MAX_VALUE)
           .setFieldsToReturn(ImmutableSet.of("template", "severity"))
       );
index ebf3da2978af503d4ec1f38fc65946179699a415..2bd98428b4719b76cfef4309e4370239368f7dd2 100644 (file)
@@ -99,12 +99,12 @@ public class BulkRuleActivationActions implements ServerComponent {
   }
 
   private void bulkActivate(Request request, Response response) throws Exception {
-    Multimap<String, String> results = service.activateByRuleQuery(createRuleQuery(request), readKey(request));
+    Multimap<String, String> results = service.bulkActivate(createRuleQuery(request), readKey(request));
     writeResponse(results, response);
   }
 
   private void bulkDeactivate(Request request, Response response) throws Exception {
-    Multimap<String, String> results = service.deActivateByRuleQuery(createRuleQuery(request), readKey(request));
+    Multimap<String, String> results = service.bulkDeactivate(createRuleQuery(request), readKey(request));
     writeResponse(results, response);
   }
 
index 0b06587ec6f74225e923e481e650537e4a59d4b1..5c8a474a3d3a635f989ed814a6a7e352eb2bfb9a 100644 (file)
@@ -101,6 +101,7 @@ public class ActiveRuleBackendMediumTest {
     dbSession.commit();
 
     // verify db
+    assertThat(db.activeRuleDao().getByKey(dbSession, activeRule.getKey())).isNotNull();
     List<ActiveRuleDto> persistedDtos = db.activeRuleDao().findByRule(dbSession, ruleDto);
     assertThat(persistedDtos).hasSize(1);
 
index bf78b005a9c543c1c4de737e09dbf9de8471c06f..de11c3be6b8346e7db8d5dd598d9b2fdeecc0fbc 100644 (file)
@@ -48,12 +48,12 @@ import static org.fest.assertions.Fail.fail;
 
 public class RuleActivatorMediumTest {
 
+  static final QualityProfileKey PROFILE_KEY = QualityProfileKey.of("MyProfile", "xoo");
+
   @Rule
   public ServerTester tester = new ServerTester()
     .addComponents(XooRulesDefinition.class, JavaRulesDefinition.class);
 
-  static final QualityProfileKey PROFILE_KEY = QualityProfileKey.of("MyProfile", "xoo");
-
   DbClient db;
   DbSession dbSession;
   RuleActivator ruleActivator;
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/db/ActiveRuleDaoTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/db/ActiveRuleDaoTest.java
deleted file mode 100644 (file)
index 7255f8e..0000000
+++ /dev/null
@@ -1,79 +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.db;
-
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.utils.System2;
-import org.sonar.core.persistence.AbstractDaoTestCase;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDao;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.core.rule.RuleDto;
-import org.sonar.server.rule.db.RuleDao;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class ActiveRuleDaoTest  extends AbstractDaoTestCase{
-
-  ActiveRuleDao activeRuleDao;
-  RuleDao ruleDao;
-  QualityProfileDao qualityProfileDao;
-  DbSession session;
-
-
-  @Before
-  public void setUp() throws Exception {
-    session = getMyBatis().openSession(false);
-    ruleDao = new RuleDao(System2.INSTANCE);
-    qualityProfileDao = new QualityProfileDao(getMyBatis());
-    activeRuleDao = new ActiveRuleDao(qualityProfileDao, ruleDao, System2.INSTANCE);
-  }
-
-  @After
-  public void tearDown() throws Exception {
-    session.close();
-  }
-
-  @Test
-  public void get_by_key() throws Exception {
-
-    QualityProfileDto profile = QualityProfileDto.createFor("profile","xoo");
-    qualityProfileDao.insert(session, profile);
-
-    RuleDto rule = RuleDto.createFor(RuleKey.of("repo","rule"));
-    ruleDao.insert(session, rule);
-
-    ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(profile, rule)
-      .setSeverity("BLOCKER");
-    activeRuleDao.insert(session, activeRuleDto);
-
-    session.commit();
-
-    ActiveRuleDto result = activeRuleDao.getNullableByKey(session, activeRuleDto.getKey());
-
-    assertThat(result).isNotNull();
-    assertThat(result.getKey()).isNotNull();
-  }
-}
index 4bab5f494f92a50cdee288793b021fe42003358a..f470e9e0eacf9f151122ff5f121916ef44068109 100644 (file)
@@ -32,7 +32,6 @@ 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;
@@ -64,7 +63,7 @@ public class RuleBackendMediumTest {
   public void before() {
     tester.clearDbAndIndexes();
     db = tester.get(DbClient.class);
-    dbSession = tester.get(MyBatis.class).openSession(false);
+    dbSession = tester.get(DbClient.class).openSession(false);
   }
 
   @After
@@ -317,7 +316,7 @@ public class RuleBackendMediumTest {
 
     // 2. assert find does not get REMOVED
     assertThat(index.search(new RuleQuery(), QueryOptions.DEFAULT)
-    .getRules()).hasSize(1);
+      .getRules()).hasSize(1);
   }
 
   private RuleDto newRuleDto(RuleKey ruleKey) {
index 77f4212b750042412bd9eed6f943c6b334268404..7049503d317d0b9b12b7521187cbc0fb32ccbe90 100644 (file)
@@ -32,7 +32,6 @@ 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.persistence.MyBatis;
 import org.sonar.core.qualityprofile.db.ActiveRuleDto;
 import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
 import org.sonar.core.qualityprofile.db.QualityProfileDto;
@@ -60,16 +59,16 @@ public class RuleIndexMediumTest {
   @ClassRule
   public static ServerTester tester = new ServerTester();
 
-  MyBatis myBatis = tester.get(MyBatis.class);
   RuleDao dao = tester.get(RuleDao.class);
   RuleIndex index = tester.get(RuleIndex.class);
-  DbClient dbClient = tester.get(DbClient.class);
+  DbClient db;
   DbSession dbSession;
 
   @Before
   public void before() {
     tester.clearDbAndIndexes();
-    dbSession = myBatis.openSession(false);
+    db = tester.get(DbClient.class);
+    dbSession = db.openSession(false);
   }
 
   @After
@@ -88,6 +87,7 @@ public class RuleIndexMediumTest {
     assertThat(rule.htmlDescription()).isEqualTo(ruleDto.getDescription());
     assertThat(rule.key()).isEqualTo(ruleDto.getKey());
 
+    //TODO
 //    assertThat(rule.debtSubCharacteristicKey())
 //      .isEqualTo(ruleDto.getDefaultSubCharacteristicId().toString());
     assertThat(rule.debtRemediationFunction().type().name())
@@ -286,11 +286,11 @@ public class RuleIndexMediumTest {
   @Test
   public void search_by_characteristics() throws InterruptedException {
     CharacteristicDto char1 = DebtTesting.newCharacteristicDto("char1");
-    dbClient.debtCharacteristicDao().insert(char1, dbSession);
+    db.debtCharacteristicDao().insert(char1, dbSession);
 
     CharacteristicDto char11 = DebtTesting.newCharacteristicDto("char11")
       .setParentId(char1.getId());
-    dbClient.debtCharacteristicDao().insert(char11, dbSession);
+    db.debtCharacteristicDao().insert(char11, dbSession);
     dbSession.commit();
 
     dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001"))
@@ -412,10 +412,9 @@ public class RuleIndexMediumTest {
 
   @Test
   public void fail_sort_by_language() throws InterruptedException {
-
     try {
       // Sorting on a field not tagged as sortable
-      RuleQuery query = new RuleQuery().setSortField(RuleNormalizer.RuleField.LANGUAGE);
+      new RuleQuery().setSortField(RuleNormalizer.RuleField.LANGUAGE);
       fail();
     } catch (IllegalStateException e) {
       assertThat(e.getMessage()).isEqualTo("Field 'lang' is not sortable!");
@@ -426,14 +425,14 @@ public class RuleIndexMediumTest {
   public void search_by_profile() throws InterruptedException {
     QualityProfileDto qualityProfileDto1 = QualityProfileDto.createFor("profile1", "java");
     QualityProfileDto qualityProfileDto2 = QualityProfileDto.createFor("profile2", "java");
-    dbClient.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);
+    db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);
 
     RuleDto rule1 = newRuleDto(RuleKey.of("java", "S001"));
     RuleDto rule2 = newRuleDto(RuleKey.of("java", "S002"));
     RuleDto rule3 = newRuleDto(RuleKey.of("java", "S003"));
     dao.insert(dbSession, rule1, rule2, rule3);
 
-    dbClient.activeRuleDao().insert(
+    db.activeRuleDao().insert(
       dbSession,
       ActiveRuleDto.createFor(qualityProfileDto1, rule1)
         .setSeverity("BLOCKER"),
@@ -477,7 +476,7 @@ public class RuleIndexMediumTest {
     String value = "//expression[primary/qualifiedIdentifier[count(IDENTIFIER) = 2]/IDENTIFIER[2]/@tokenValue = 'firstOf' and primary/identifierSuffix/arguments/expression[not(primary) or primary[not(qualifiedIdentifier) or identifierSuffix]]]";
 
     QualityProfileDto profile = QualityProfileDto.createFor("name", "Language");
-    dbClient.qualityProfileDao().insert(dbSession, profile);
+    db.qualityProfileDao().insert(dbSession, profile);
 
     RuleDto rule = newRuleDto(RuleKey.of("java", "S001"));
     dao.insert(dbSession, rule);
@@ -492,16 +491,13 @@ public class RuleIndexMediumTest {
       .setSeverity("BLOCKER");
 
     ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param);
-    dbClient.activeRuleDao().insert(dbSession, activeRule);
-    dbClient.activeRuleDao().addParam(dbSession, activeRule, activeRuleParam);
+    db.activeRuleDao().insert(dbSession, activeRule);
+    db.activeRuleDao().addParam(dbSession, activeRule, activeRuleParam);
     dbSession.commit();
 
     assertThat(index.getByKey(rule.getKey()).params().get(0).defaultValue()).isEqualTo(value);
-
-
   }
 
-
   @Test
   public void search_by_tag() throws InterruptedException {
     dao.insert(dbSession, newRuleDto(RuleKey.of("java", "S001")).setTags(ImmutableSet.of("tag1")));
index d9156a2dcf74d7588fb1ec2b53151150ce271659..8c7b6812118e4c9bb8c212e892a6cadf21543ff2 100644 (file)
@@ -75,7 +75,7 @@ public class ServerTester extends ExternalResource {
 
     Properties properties = new Properties();
     properties.putAll(initialProps);
-    properties.setProperty(IndexProperties.TYPE,IndexProperties.ES_TYPE.MEMORY.name());
+    properties.setProperty(IndexProperties.TYPE, IndexProperties.ES_TYPE.MEMORY.name());
     properties.setProperty(CoreProperties.SONAR_HOME, homeDir.getAbsolutePath());
     properties.setProperty(DatabaseProperties.PROP_URL, "jdbc:h2:" + homeDir.getAbsolutePath() + "/h2");
     for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {