From 16824b243d6c2d445f51ef59c3f60472417a7ec4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Mon, 9 Oct 2023 11:12:35 +0200 Subject: [PATCH] SONAR-20546 when no rules activated in QP, inactivate count should have correct value --- .../ws/InheritanceActionIT.java | 21 +++++++++++++++++++ .../qualityprofile/ws/InheritanceAction.java | 5 +++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualityprofile/ws/InheritanceActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualityprofile/ws/InheritanceActionIT.java index c3bf2ca77be..ac7bb92178d 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualityprofile/ws/InheritanceActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualityprofile/ws/InheritanceActionIT.java @@ -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)); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/InheritanceAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/InheritanceAction.java index d4520969fd6..5e2184e67ca 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/InheritanceAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/ws/InheritanceAction.java @@ -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))); } } } -- 2.39.5