*/
package org.sonar.server.qualityprofile.ws;
+import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
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));
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;
.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();
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)));
}
}
}