package org.sonar.server.rule.ws;
import com.google.common.base.Function;
-import com.google.common.base.Optional;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.RuleDtoFunctions;
import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.qualityprofile.QProfileLoader;
import org.sonar.server.rule.index.RuleQuery;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.singletonList;
-import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
/**
* Add details about active rules to api/rules/search and api/rules/show
searchResponse.setQProfiles(buildQProfiles(harvestedProfileKeys));
}
- private Collection<String> writeActiveRules(DbSession dbSession, SearchResponse.Builder response, RuleQuery query, Collection<RuleDto> rules) {
+ private Collection<String> writeActiveRules(DbSession dbSession, SearchResponse.Builder response, RuleQuery query, List<RuleDto> rules) {
Collection<String> qProfileKeys = newHashSet();
Rules.Actives.Builder activesBuilder = response.getActivesBuilder();
String profileKey = query.getQProfileKey();
if (profileKey != null) {
// Load details of active rules on the selected profile
+ List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profileKey);
+ ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
+
for (RuleDto rule : rules) {
ActiveRule activeRule = loader.getActiveRule(ActiveRuleKey.of(profileKey, rule.getKey()));
if (activeRule != null) {
- Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, activeRule.key());
- checkFoundWithOptional(activeRuleDto, "Active rule with key '%s' not found", activeRule.key().toString());
- List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.get().getId());
- ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamByActiveRuleKey = ArrayListMultimap.create(1, activeRuleParamDtos.size());
- activeRuleParamByActiveRuleKey.putAll(activeRule.key(), activeRuleParamDtos);
- qProfileKeys = writeActiveRules(rule.getKey(), singletonList(activeRule), activeRuleParamByActiveRuleKey, activesBuilder);
+ qProfileKeys = writeActiveRules(rule.getKey(), singletonList(activeRule), activeRuleParamsByActiveRuleKey, activesBuilder);
}
}
} else {
// Load details of all active rules
+ List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByRuleIds(dbSession, Lists.transform(rules, RuleDtoFunctions.toId()));
+ ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
+
for (RuleDto rule : rules) {
List<ActiveRule> activeRules = loader.findActiveRulesByRule(rule.getKey());
- List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
- Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
- for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
- activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
- }
-
- List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDtoToId.INSTANCE));
- ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRules.size(), 10);
- for (ActiveRuleParamDto activeRuleParamDto : activeRuleParamDtos) {
- ActiveRuleKey activeRuleKey = activeRuleIdsByKey.get(activeRuleParamDto.getId());
- activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParamDto);
- }
-
qProfileKeys = writeActiveRules(rule.getKey(), activeRules, activeRuleParamsByActiveRuleKey, activesBuilder);
}
}
return qProfileKeys;
}
+ private static Collection<String> writeActiveRules(RuleKey ruleKey, Collection<ActiveRule> activeRules,
+ ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey, Rules.Actives.Builder activesBuilder) {
+ Collection<String> qProfileKeys = newHashSet();
+ Rules.ActiveList.Builder activeRulesListResponse = Rules.ActiveList.newBuilder();
+ for (ActiveRule activeRule : activeRules) {
+ activeRulesListResponse.addActiveList(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.key())));
+ qProfileKeys.add(activeRule.key().qProfile());
+ }
+ activesBuilder
+ .getMutableActives()
+ .put(ruleKey.toString(), activeRulesListResponse.build());
+ return qProfileKeys;
+ }
+
+ private ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleDtosToActiveRuleParamDtos(DbSession dbSession, List<ActiveRuleDto> activeRuleDtos) {
+ Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
+ for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
+ activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
+ }
+ List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDtoToId.INSTANCE));
+ ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRuleDtos.size(), 10);
+ for (ActiveRuleParamDto activeRuleParamDto : activeRuleParamDtos) {
+ ActiveRuleKey activeRuleKey = activeRuleIdsByKey.get(activeRuleParamDto.getActiveRuleId());
+ activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParamDto);
+ }
+
+ return activeRuleParamsByActiveRuleKey;
+ }
+
void completeShow(DbSession dbSession, RuleDto rule, ShowResponse.Builder response) {
List<ActiveRule> activeRules = loader.findActiveRulesByRule(rule.getKey());
List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByKeys(dbSession, Lists.transform(activeRules, ActiveRuleToKey.INSTANCE));
}
}
- private static Collection<String> writeActiveRules(RuleKey ruleKey, Collection<ActiveRule> activeRules,
- ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey, Rules.Actives.Builder activesBuilder) {
- Collection<String> qProfileKeys = newHashSet();
- Rules.ActiveList.Builder activeRulesListResponse = Rules.ActiveList.newBuilder();
- for (ActiveRule activeRule : activeRules) {
- activeRulesListResponse.addActiveList(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.key())));
- qProfileKeys.add(activeRule.key().qProfile());
- }
- activesBuilder
- .getMutableActives()
- .put(ruleKey.toString(), activeRulesListResponse.build());
- return qProfileKeys;
- }
-
private static Rules.Active buildActiveRuleResponse(ActiveRule activeRule, List<ActiveRuleParamDto> parameters) {
Rules.Active.Builder activeRuleResponse = Rules.Active.newBuilder();
activeRuleResponse.setQProfile(activeRule.key().qProfile());
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.db.rule;
+
+import com.google.common.base.Function;
+import javax.annotation.Nonnull;
+
+public class RuleDtoFunctions {
+ private RuleDtoFunctions() {
+ // prevent instantiation
+ }
+
+ public static Function<RuleDto, Integer> toId() {
+ return ToId.INSTANCE;
+ }
+
+ private enum ToId implements Function<RuleDto, Integer> {
+ INSTANCE;
+
+ @Override
+ public Integer apply(@Nonnull RuleDto rule) {
+ return rule.getId();
+ }
+ }
+}
*/
package org.sonar.db.qualityprofile;
+import java.util.Collections;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
+import static com.google.common.collect.Lists.newArrayList;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
static final long NOW = 10000000L;
- static final QualityProfileDto QPROFILE_1 = QualityProfileDto.createFor("qp1").setName("QProile1");
- static final QualityProfileDto QPROFILE_2 = QualityProfileDto.createFor("qp2").setName("QProile2");
+ QualityProfileDto QPROFILE_1 = QualityProfileDto.createFor("qp1").setName("QProile1");
+ QualityProfileDto QPROFILE_2 = QualityProfileDto.createFor("qp2").setName("QProile2");
- static final RuleDto RULE_1 = RuleTesting.newDto(RuleTesting.XOO_X1);
- static final RuleDto RULE_2 = RuleTesting.newDto(RuleTesting.XOO_X2);
+ RuleDto RULE_1 = RuleTesting.newDto(RuleTesting.XOO_X1);
+ RuleDto RULE_2 = RuleTesting.newDto(RuleTesting.XOO_X2);
System2 system = mock(System2.class);
ActiveRuleDao underTest = dbTester.getDbClient().activeRuleDao();
@Before
- public void createDao() {
+ public void setUp() {
when(system.now()).thenReturn(NOW);
dbClient.qualityProfileDao().insert(dbTester.getSession(), QPROFILE_1);
assertThat(underTest.selectByKeys(dbSession, asList(activeRule1.getKey()))).hasSize(1);
assertThat(underTest.selectByKeys(dbSession, asList(ActiveRuleKey.of(QPROFILE_2.getKey(), RULE_1.getKey())))).isEmpty();
}
+
+ @Test
+ public void select_by_rule_ids() {
+ ActiveRuleDto activeRule1 = ActiveRuleDto.createFor(QPROFILE_1, RULE_1).setSeverity(Severity.BLOCKER);
+ ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(QPROFILE_1, RULE_2).setSeverity(Severity.BLOCKER);
+ ActiveRuleDto activeRule3 = ActiveRuleDto.createFor(QPROFILE_2, RULE_1).setSeverity(Severity.BLOCKER);
+ underTest.insert(dbSession, activeRule1);
+ underTest.insert(dbSession, activeRule2);
+ underTest.insert(dbSession, activeRule3);
+ dbSession.commit();
+
+ assertThat(underTest.selectByRuleIds(dbSession, Collections.singletonList(RULE_1.getId())))
+ .extracting("key").containsOnly(activeRule1.getKey(), activeRule3.getKey());
+ assertThat(underTest.selectByRuleIds(dbSession, newArrayList(RULE_1.getId(), RULE_2.getId())))
+ .extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey(), activeRule3.getKey());
+ }
}