*/
package org.sonar.server.qualityprofile;
+import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Set;
+import javax.annotation.Nonnull;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
-import org.sonar.core.util.NonNullInputFunction;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.ActiveRuleDto;
+import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
@ServerSide
public class QProfileComparison {
private final DbClient dbClient;
- private final QProfileLoader profileLoader;
-
- public QProfileComparison(DbClient dbClient, QProfileLoader profileLoader) {
+ public QProfileComparison(DbClient dbClient) {
this.dbClient = dbClient;
- this.profileLoader = profileLoader;
}
public QProfileComparisonResult compare(String leftKey, String rightKey) {
QProfileComparisonResult result = new QProfileComparisonResult();
DbSession dbSession = dbClient.openSession(false);
-
try {
- compare(leftKey, rightKey, dbSession, result);
+ compare(dbSession, leftKey, rightKey, result);
} finally {
- dbSession.close();
+ dbClient.closeSession(dbSession);
}
-
return result;
}
- private void compare(String leftKey, String rightKey, DbSession session, QProfileComparisonResult result) {
+ private void compare(DbSession session, String leftKey, String rightKey, QProfileComparisonResult result) {
result.left = dbClient.qualityProfileDao().selectByKey(session, leftKey);
Preconditions.checkArgument(result.left != null, String.format("Could not find left profile '%s'", leftKey));
result.right = dbClient.qualityProfileDao().selectByKey(session, rightKey);
Preconditions.checkArgument(result.right != null, String.format("Could not find right profile '%s'", leftKey));
- Map<RuleKey, ActiveRuleDoc> leftActiveRulesByRuleKey = loadActiveRules(leftKey);
- Map<RuleKey, ActiveRuleDoc> rightActiveRulesByRuleKey = loadActiveRules(rightKey);
+ Map<RuleKey, ActiveRuleDto> leftActiveRulesByRuleKey = loadActiveRules(session, leftKey);
+ Map<RuleKey, ActiveRuleDto> rightActiveRulesByRuleKey = loadActiveRules(session, rightKey);
Set<RuleKey> allRules = Sets.newHashSet();
allRules.addAll(leftActiveRulesByRuleKey.keySet());
} else if (!rightActiveRulesByRuleKey.containsKey(ruleKey)) {
result.inLeft.put(ruleKey, leftActiveRulesByRuleKey.get(ruleKey));
} else {
- compareActivationParams(leftActiveRulesByRuleKey.get(ruleKey), rightActiveRulesByRuleKey.get(ruleKey), result);
+ compareActivationParams(session, leftActiveRulesByRuleKey.get(ruleKey), rightActiveRulesByRuleKey.get(ruleKey), result);
}
}
}
- private void compareActivationParams(ActiveRule leftRule, ActiveRule rightRule, QProfileComparisonResult result) {
- RuleKey key = leftRule.key().ruleKey();
- if (leftRule.params().equals(rightRule.params()) && leftRule.severity().equals(rightRule.severity())) {
+ private void compareActivationParams(DbSession session, ActiveRuleDto leftRule, ActiveRuleDto rightRule, QProfileComparisonResult result) {
+ RuleKey key = leftRule.getKey().ruleKey();
+ Map<String, String> leftParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleKey(session, leftRule.getKey()));
+ Map<String, String> rightParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleKey(session, rightRule.getKey()));
+ if (leftParams.equals(rightParams) && leftRule.getSeverityString().equals(rightRule.getSeverityString())) {
result.same.put(key, leftRule);
} else {
ActiveRuleDiff diff = new ActiveRuleDiff();
- diff.leftSeverity = leftRule.severity();
- diff.rightSeverity = rightRule.severity();
+ diff.leftSeverity = leftRule.getSeverityString();
+ diff.rightSeverity = rightRule.getSeverityString();
- diff.paramDifference = Maps.difference(leftRule.params(), rightRule.params());
+ diff.paramDifference = Maps.difference(leftParams, rightParams);
result.modified.put(key, diff);
}
}
- private Map<RuleKey, ActiveRuleDoc> loadActiveRules(String profileKey) {
- return Maps.uniqueIndex(profileLoader.findActiveRulesByProfile(profileKey), new NonNullInputFunction<ActiveRuleDoc, RuleKey>() {
- @Override
- protected RuleKey doApply(ActiveRuleDoc input) {
- return input.key().ruleKey();
- }
- });
+ private Map<RuleKey, ActiveRuleDto> loadActiveRules(DbSession session, String profileKey) {
+ return Maps.uniqueIndex(dbClient.activeRuleDao().selectByProfileKey(session, profileKey), ActiveRuleToRuleKey.INSTANCE);
}
public static class QProfileComparisonResult {
private QualityProfileDto left;
private QualityProfileDto right;
- private Map<RuleKey, ActiveRule> inLeft = Maps.newHashMap();
- private Map<RuleKey, ActiveRule> inRight = Maps.newHashMap();
+ private Map<RuleKey, ActiveRuleDto> inLeft = Maps.newHashMap();
+ private Map<RuleKey, ActiveRuleDto> inRight = Maps.newHashMap();
private Map<RuleKey, ActiveRuleDiff> modified = Maps.newHashMap();
- private Map<RuleKey, ActiveRule> same = Maps.newHashMap();
+ private Map<RuleKey, ActiveRuleDto> same = Maps.newHashMap();
public QualityProfileDto left() {
return left;
return right;
}
- public Map<RuleKey, ActiveRule> inLeft() {
+ public Map<RuleKey, ActiveRuleDto> inLeft() {
return inLeft;
}
- public Map<RuleKey, ActiveRule> inRight() {
+ public Map<RuleKey, ActiveRuleDto> inRight() {
return inRight;
}
return modified;
}
- public Map<RuleKey, ActiveRule> same() {
+ public Map<RuleKey, ActiveRuleDto> same() {
return same;
}
}
}
+ private enum ActiveRuleToRuleKey implements Function<ActiveRuleDto, RuleKey> {
+ INSTANCE;
+
+ @Override
+ public RuleKey apply(@Nonnull ActiveRuleDto input) {
+ return input.getKey().ruleKey();
+ }
+ }
+
+ private static Map<String, String> paramDtoToMap(List<ActiveRuleParamDto> params) {
+ Map<String, String> map = new HashMap<>();
+ for (ActiveRuleParamDto dto : params) {
+ map.put(dto.getKey(), dto.getValue());
+ }
+ return map;
+ }
+
}
import com.google.common.collect.MapDifference.ValueDifference;
import com.google.common.collect.Maps;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.api.server.ws.WebService.NewController;
import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.core.util.NonNullInputFunction;
-import org.sonar.server.qualityprofile.ActiveRule;
+import org.sonar.db.qualityprofile.ActiveRuleDto;
+import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.qualityprofile.QProfileComparison;
import org.sonar.server.qualityprofile.QProfileComparison.ActiveRuleDiff;
import org.sonar.server.qualityprofile.QProfileComparison.QProfileComparisonResult;
import org.sonar.server.rule.RuleRepositories.Repository;
import org.sonar.server.rule.RuleService;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
public class CompareAction implements QProfileWsAction {
private static final String ATTRIBUTE_LEFT = "left";
.prop(ATTRIBUTE_NAME, profile.getName());
}
- private void writeRules(JsonWriter json, Map<RuleKey, ActiveRule> activeRules, Map<RuleKey, Rule> rulesByKey) {
+ private void writeRules(JsonWriter json, Map<RuleKey, ActiveRuleDto> activeRules, Map<RuleKey, Rule> rulesByKey) {
json.beginArray();
- for (Entry<RuleKey, ActiveRule> activeRule : activeRules.entrySet()) {
+ for (Entry<RuleKey, ActiveRuleDto> activeRule : activeRules.entrySet()) {
RuleKey key = activeRule.getKey();
- ActiveRule value = activeRule.getValue();
+ ActiveRuleDto value = activeRule.getValue();
json.beginObject();
writeRule(json, key, rulesByKey);
- json.prop(ATTRIBUTE_SEVERITY, value.severity());
+ json.prop(ATTRIBUTE_SEVERITY, value.getSeverityString());
json.endObject();
}
json.endArray();
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
-import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.RuleParamType;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
+import org.sonar.db.rule.RuleTesting;
import org.sonar.server.qualityprofile.QProfileComparison.ActiveRuleDiff;
import org.sonar.server.qualityprofile.QProfileComparison.QProfileComparisonResult;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
-import org.sonar.db.rule.RuleTesting;
import org.sonar.server.tester.ServerTester;
-import org.sonar.server.tester.UserSessionRule;
import static org.assertj.core.api.Assertions.assertThat;
public class QProfileComparisonMediumTest {
@ClassRule
- public static ServerTester tester = new ServerTester().addXoo();
- @Rule
- public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
+ public static ServerTester tester = new ServerTester().withEsIndexes().addXoo();
DbClient db;
DbSession dbSession;
- ActiveRuleIndex index;
RuleActivator ruleActivator;
QProfileComparison comparison;
xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR");
xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR");
- db.deprecatedRuleDao().insert(dbSession, xooRule1, xooRule2);
- db.deprecatedRuleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
+ db.ruleDao().insert(dbSession, xooRule1);
+ db.ruleDao().insert(dbSession, xooRule2);
+ db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("max").setType(RuleParamType.INTEGER.type()));
- db.deprecatedRuleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
+ db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("min").setType(RuleParamType.INTEGER.type()));
left = QProfileTesting.newXooP1();
right = QProfileTesting.newXooP2();
db.qualityProfileDao().insert(dbSession, left, right);
+
dbSession.commit();
- dbSession.clearCache();
}
@After
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.server.rule.RulesDefinition;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.QProfileName;
import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.tester.ServerTester;
public class CompareActionMediumTest {
@ClassRule
- public static ServerTester tester = new ServerTester().withStartupTasks().addXoo()
+ public static ServerTester tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo()
.addComponents(new RulesDefinition() {
@Override
public void define(Context context) {
.setLanguage(lang)
.setSeverity(Severity.BLOCKER)
.setStatus(RuleStatus.READY);
- db.deprecatedRuleDao().insert(session, rule);
+ db.ruleDao().insert(session, rule);
RuleParamDto param = RuleParamDto.createFor(rule).setName("param_" + id).setType(RuleParamType.STRING.toString());
- db.deprecatedRuleDao().insertRuleParam(session, rule, param);
+ db.ruleDao().insertRuleParam(session, rule, param);
return rule;
}
private RuleDto createRuleWithParam(String lang, String id) {
RuleDto rule = createRule(lang, id);
RuleParamDto param = RuleParamDto.createFor(rule).setName("param_" + id).setType(RuleParamType.STRING.toString());
- db.deprecatedRuleDao().insertRuleParam(session, rule, param);
+ db.ruleDao().insertRuleParam(session, rule, param);
return rule;
}
private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) {
ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
.setSeverity(rule.getSeverityString());
- db.deprecatedActiveRuleDao().insert(session, activeRule);
+ db.activeRuleDao().insert(session, activeRule);
return activeRule;
}
private ActiveRuleDto createActiveRuleWithParam(RuleDto rule, QualityProfileDto profile, String value) {
ActiveRuleDto activeRule = createActiveRule(rule, profile);
- RuleParamDto paramDto = db.deprecatedRuleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
+ RuleParamDto paramDto = db.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value);
- db.deprecatedActiveRuleDao().insertParam(session, activeRule, activeRuleParam);
+ db.activeRuleDao().insertParam(session, activeRule, activeRuleParam);
return activeRule;
}
private ActiveRuleDto createActiveRuleWithSeverity(RuleDto rule, QualityProfileDto profile, String severity) {
ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
.setSeverity(severity);
- db.deprecatedActiveRuleDao().insert(session, activeRule);
+ db.activeRuleDao().insert(session, activeRule);
return activeRule;
}
}