import org.sonar.api.utils.text.XmlWriter;
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;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex2;
@ServerSide
public class QProfileBackuper {
private final QProfileReset reset;
private final DbClient db;
- private final ActiveRuleIndex2 activeRuleIndex;
private static final Joiner RULEKEY_JOINER = Joiner.on(", ").skipNulls();
- public QProfileBackuper(QProfileReset reset, DbClient db, ActiveRuleIndex2 activeRuleIndex) {
+ public QProfileBackuper(QProfileReset reset, DbClient db) {
this.reset = reset;
this.db = db;
- this.activeRuleIndex = activeRuleIndex;
}
public void backup(String key, Writer writer) {
DbSession dbSession = db.openSession(false);
try {
profile = db.qualityProfileDao().selectOrFailByKey(dbSession, key);
+ List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, key);
+ Collections.sort(activeRules, BackupActiveRuleComparator.INSTANCE);
+ writeXml(dbSession, writer, profile, activeRules.iterator());
} finally {
- dbSession.close();
+ db.closeSession(dbSession);
}
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(activeRuleIndex.findByProfile(profile.getKey()));
- Collections.sort(activeRules, BackupActiveRuleComparator.INSTANCE);
- writeXml(writer, profile, activeRules.iterator());
}
- private static void writeXml(Writer writer, QualityProfileDto profile, Iterator<ActiveRuleDoc> activeRules) {
+ private void writeXml(DbSession dbSession, Writer writer, QualityProfileDto profile, Iterator<ActiveRuleDto> activeRules) {
XmlWriter xml = XmlWriter.of(writer).declaration();
xml.begin("profile");
xml.prop("name", profile.getName());
xml.prop("language", profile.getLanguage());
xml.begin("rules");
while (activeRules.hasNext()) {
- ActiveRule activeRule = activeRules.next();
+ ActiveRuleDto activeRule = activeRules.next();
xml.begin("rule");
- xml.prop("repositoryKey", activeRule.key().ruleKey().repository());
- xml.prop("key", activeRule.key().ruleKey().rule());
- xml.prop("priority", activeRule.severity());
+ xml.prop("repositoryKey", activeRule.getKey().ruleKey().repository());
+ xml.prop("key", activeRule.getKey().ruleKey().rule());
+ xml.prop("priority", activeRule.getSeverityString());
xml.begin("parameters");
- for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
+ for (ActiveRuleParamDto param : db.activeRuleDao().selectParamsByActiveRuleKey(dbSession, activeRule.getKey())) {
xml
.begin("parameter")
.prop("key", param.getKey())
return new SMInputFactory(xmlFactory);
}
- private enum BackupActiveRuleComparator implements Comparator<ActiveRule> {
+ private enum BackupActiveRuleComparator implements Comparator<ActiveRuleDto> {
INSTANCE;
@Override
- public int compare(ActiveRule o1, ActiveRule o2) {
+ public int compare(ActiveRuleDto o1, ActiveRuleDto o2) {
return new CompareToBuilder()
- .append(o1.key().ruleKey().repository(), o2.key().ruleKey().repository())
- .append(o1.key().ruleKey().rule(), o2.key().ruleKey().rule())
+ .append(o1.getKey().ruleKey().repository(), o2.getKey().ruleKey().repository())
+ .append(o1.getKey().ruleKey().rule(), o2.getKey().ruleKey().rule())
.toComparison();
}
}
import org.sonar.api.rule.RuleKey;
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.RowNotFoundException;
+import org.sonar.db.qualityprofile.ActiveRuleDao;
+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.db.rule.RuleTesting;
-import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
public class QProfileBackuperMediumTest {
@ClassRule
- public static ServerTester tester = new ServerTester();
+ public static ServerTester tester = new ServerTester().withEsIndexes();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
DbClient db;
DbSession dbSession;
+ RuleIndexer ruleIndexer;
+ ActiveRuleIndexer activeRuleIndexer;
@Before
public void before() {
tester.clearDbAndIndexes();
db = tester.get(DbClient.class);
dbSession = db.openSession(false);
+ ruleIndexer = tester.get(RuleIndexer.class);
+ ruleIndexer.setEnabled(true);
+ activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
+ activeRuleIndexer.setEnabled(true);
// create pre-defined rules
RuleDto xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR").setLanguage("xoo");
RuleDto xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR").setLanguage("xoo");
- 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").setDefaultValue("10").setType(RuleParamType.INTEGER.type()));
dbSession.commit();
dbSession.clearCache();
+ ruleIndexer.index();
}
@After
public void backup() throws Exception {
RuleKey blahRuleKey = RuleKey.of("blah", "my-rule");
RuleDto blahRule = RuleTesting.newDto(blahRuleKey).setSeverity("INFO").setLanguage("xoo");
- db.deprecatedRuleDao().insert(dbSession, blahRule);
+ db.ruleDao().insert(dbSession, blahRule);
dbSession.commit();
dbSession.clearCache();
+ ruleIndexer.index();
// create profile P1 with rules x2 and x1 activated
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
tester.get(RuleActivator.class).activate(dbSession, activation3, QProfileTesting.XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
+ activeRuleIndexer.index();
StringWriter output = new StringWriter();
tester.get(QProfileBackuper.class).backup(QProfileTesting.XOO_P1_KEY, output);
List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(profile.getKey()));
assertThat(activeRules).hasSize(1);
- assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER");
- assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
- assertThat(activeRules.get(0).params().get("max")).isEqualTo("7");
+ ActiveRuleDoc activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+
+ List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleDoc.key());
+ assertThat(params).hasSize(1);
+ assertThat(params.get(0).getKey()).isEqualTo("max");
+ assertThat(params.get(0).getValue()).isEqualTo("7");
}
@Test
tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
+ activeRuleIndexer.index();
// restore backup, which activates only x1
// -> update x1 and deactivate x2
List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
assertThat(activeRules).hasSize(1);
- assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER");
- assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
- assertThat(activeRules.get(0).params().get("max")).isEqualTo("7");
+ ActiveRuleDoc activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+
+ List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleDoc.key());
+ assertThat(params).hasSize(1);
+ assertThat(params.get(0).getKey()).isEqualTo("max");
+ assertThat(params.get(0).getValue()).isEqualTo("7");
}
@Test
tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
+ activeRuleIndexer.index();
// restore backup of child profile -> overrides x1
tester.get(QProfileBackuper.class).restore(new StringReader(
// parent profile is unchanged
List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
assertThat(activeRules).hasSize(1);
- assertThat(activeRules.get(0).severity()).isEqualTo("INFO");
- assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
- assertThat(activeRules.get(0).params().get("max")).isEqualTo("10");
+ ActiveRuleDoc activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.severity()).isEqualTo("INFO");
+ assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleDoc.key());
+ assertThat(params).hasSize(1);
+ assertThat(params.get(0).getKey()).isEqualTo("max");
+ assertThat(params.get(0).getValue()).isEqualTo("10");
// child profile overrides parent
activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
assertThat(activeRules).hasSize(1);
- assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER");
- assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.OVERRIDES);
- assertThat(activeRules.get(0).params().get("max")).isEqualTo("7");
+ activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.OVERRIDES);
+ params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleDoc.key());
+ assertThat(params).hasSize(1);
+ assertThat(params.get(0).getKey()).isEqualTo("max");
+ assertThat(params.get(0).getValue()).isEqualTo("7");
}
@Test
tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
+ activeRuleIndexer.index();
// restore backup of parent profile -> update x1 and propagates to child
tester.get(QProfileBackuper.class).restore(new StringReader(
// parent profile is updated
List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
assertThat(activeRules).hasSize(1);
- assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER");
- assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
- assertThat(activeRules.get(0).params().get("max")).isEqualTo("7");
+
+ ActiveRuleDoc activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleDoc.key());
+ assertThat(params).hasSize(1);
+ assertThat(params.get(0).getKey()).isEqualTo("max");
+ assertThat(params.get(0).getValue()).isEqualTo("7");
// child profile is inherited
activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
assertThat(activeRules).hasSize(1);
- assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER");
- assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.INHERITED);
- assertThat(activeRules.get(0).params().get("max")).isEqualTo("7");
+ activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.INHERITED);
+ params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleDoc.key());
+ assertThat(params).hasSize(1);
+ assertThat(params.get(0).getKey()).isEqualTo("max");
+ assertThat(params.get(0).getValue()).isEqualTo("7");
}
@Test
tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
+ activeRuleIndexer.index();
// backup of child profile contains x2 but not x1
tester.get(QProfileBackuper.class).restore(new StringReader(
null);
dbSession.clearCache();
- assertThat(db.deprecatedActiveRuleDao().selectAll(dbSession)).hasSize(0);
+ assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(0);
List<QualityProfileDto> profiles = db.qualityProfileDao().selectAll(dbSession);
assertThat(profiles).hasSize(1);
assertThat(profiles.get(0).getName()).isEqualTo("P1");