]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - Added count method to Base and QProfiles
authorStephane Gamard <stephane.gamard@searchbox.com>
Wed, 11 Jun 2014 15:02:05 +0000 (17:02 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 11 Jun 2014 15:02:23 +0000 (17:02 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleBackendMediumTest.java

index 6aaa2898b31d277a0b54f82bf09141004a822e20..fe5a7dda15f43e0bd369f2be8cfba016a2fb055d 100644 (file)
@@ -200,4 +200,8 @@ public class QProfileService implements ServerComponent {
     UserSession.get().checkLoggedIn();
     UserSession.get().checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
   }
+
+  public Long countActiveRulesByProfile(QualityProfileKey key) {
+    return index.get(ActiveRuleIndex.class).countByQualityProfileKey(key);
+  }
 }
index 06936511e31d3b607b045e1c1f287d0e52b0ef9b..a174f40ace3636d59ba0fce7632ca50adebfe66b 100644 (file)
@@ -38,6 +38,7 @@
 */
 package org.sonar.server.qualityprofile.index;
 
+import org.elasticsearch.action.count.CountRequestBuilder;
 import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.common.settings.ImmutableSettings;
@@ -169,4 +170,10 @@ public class ActiveRuleIndex extends BaseIndex<ActiveRule, ActiveRuleDto, Active
   }
 
 
+  public Long countByQualityProfileKey(QualityProfileKey key) {
+    CountRequestBuilder request = getClient().prepareCount(getIndexName())
+      .setQuery(QueryBuilders.termQuery(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), key.toString()))
+      .setRouting(key.toString());
+    return request.get().getCount();
+  }
 }
index 762f0286db1017005439d6d97fa4785cf5bc19a5..90782b195769f48f92998a72a549e72e3b718c8c 100644 (file)
@@ -535,4 +535,10 @@ public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serial
     }
     return filter;
   }
+
+  public Long countAll() {
+    return getClient().prepareCount(this.getIndexName())
+      .setTypes(this.getIndexType())
+      .get().getCount();
+  }
 }
index b9c2803a53b36f5855bc539e1cca3044d1bf068b..cb4143f7da360a6d72384192aa03a914fc9587e3 100644 (file)
@@ -230,6 +230,29 @@ public class ActiveRuleBackendMediumTest {
     assertThat(activeRules).hasSize(100);
   }
 
+  @Test
+  public void count_by_profile() {
+    QualityProfileDto profileDto1 = QualityProfileDto.createFor("p1", "java");
+    QualityProfileDto profileDto2 = QualityProfileDto.createFor("p2", "java");
+    db.qualityProfileDao().insert(dbSession, profileDto1, profileDto2);
+
+    RuleKey ruleKey = RuleKey.of("javascript", "S001");
+    RuleDto ruleDto = newRuleDto(ruleKey);
+    db.ruleDao().insert(dbSession, ruleDto);
+
+    ActiveRuleDto activeRule1 = ActiveRuleDto.createFor(profileDto1, ruleDto).setSeverity(Severity.MAJOR);
+    ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(profileDto2, ruleDto).setSeverity(Severity.MAJOR);
+    db.activeRuleDao().insert(dbSession, activeRule1, activeRule2);
+    dbSession.commit();
+
+    // 0. Test base case
+    assertThat(index.countAll()).isEqualTo(2);
+
+    // 1. Assert by profileKey
+    assertThat(index.countByQualityProfileKey(profileDto1.getKey())).isEqualTo(1);
+  }
+
+
   private RuleDto newRuleDto(RuleKey ruleKey) {
     return new RuleDto()
       .setRuleKey(ruleKey.rule())