]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20546 when no rules activated in QP, inactivate count should have correct value
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Mon, 9 Oct 2023 09:12:35 +0000 (11:12 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 10 Oct 2023 20:02:45 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualityprofile/ws/InheritanceActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/InheritanceAction.java

index c3bf2ca77bee47aaad6d00e888c02b3d89b1ecc3..ac7bb92178dc47f043943786c89d358c35761fa0 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualityprofile.ws;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
@@ -169,6 +170,26 @@ public class InheritanceActionIT {
     assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getActiveRuleCount).containsExactly(2L);
   }
 
+  @Test
+  public void handle_whenNoRulesActivated_shouldReturnExpectedInactivateRulesForLanguage() throws IOException {
+    String language = "java";
+    QProfileDto qualityProfile = db.qualityProfiles().insert(p -> p.setLanguage(language));
+    RuleDto rule = db.rules().insert(r -> r.setLanguage(language));
+
+    InputStream response = ws.newRequest()
+      .setMediaType(PROTOBUF)
+      .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
+      .setParam(PARAM_QUALITY_PROFILE, qualityProfile.getName())
+      .execute()
+      .getInputStream();
+    InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
+
+    assertThat(result.getProfile().getKey()).isEqualTo(qualityProfile.getKee());
+    assertThat(result.getProfile().getActiveRuleCount()).isZero();
+    assertThat(result.getProfile().getInactiveRuleCount()).isEqualTo(1);
+
+  }
+
   @Test
   public void inheritance_ignores_removed_rules() throws Exception {
     RuleDto rule = db.rules().insert(r -> r.setStatus(RuleStatus.REMOVED));
index d4520969fd61596368de7d2b6020c8aa40a4f093..5e2184e67ca74b82242d02fd37cac36e3fb2d41b 100644 (file)
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
@@ -134,7 +135,7 @@ public class InheritanceAction implements QProfileWsAction {
       .setName(qualityProfile.getName())
       .setActiveRuleCount(statistics.countRulesByProfileKey.getOrDefault(key, 0L))
       .setOverridingRuleCount(statistics.countOverridingRulesByProfileKey.getOrDefault(key, 0L))
-      .setInactiveRuleCount(statistics.countInactiveRuleByProfileKey.getOrDefault(key, 0L))
+      .setInactiveRuleCount(statistics.countInactiveRuleByProfileKey.get(key))
       .setIsBuiltIn(qualityProfile.isBuiltIn());
     ofNullable(qualityProfile.getParentKee()).ifPresent(builder::setParent);
     return builder.build();
@@ -151,7 +152,7 @@ public class InheritanceAction implements QProfileWsAction {
       countRulesByProfileKey = dao.countActiveRulesByQuery(dbSession, builder.setProfiles(profiles).build());
       countOverridingRulesByProfileKey = dao.countActiveRulesByQuery(dbSession, builder.setProfiles(profiles).setInheritance(OVERRIDES).build());
       long totalRuleAvailable = dbClient.ruleDao().countByLanguage(dbSession, language);
-      countRulesByProfileKey.forEach((profileKey, activeRules) -> countInactiveRuleByProfileKey.put(profileKey, totalRuleAvailable - activeRules));
+      profiles.forEach(p -> countInactiveRuleByProfileKey.put(p.getKee(), totalRuleAvailable - Optional.ofNullable(countRulesByProfileKey.get(p.getKee())).orElse(0L)));
     }
   }
 }