From: Julien Lancelot Date: Thu, 25 Feb 2016 12:56:00 +0000 (+0100) Subject: SONAR-7330 QProfileBackuper is now only using dao X-Git-Tag: 5.5-M6~66 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b0ae896d7abdfec5648ffd450caae368543a7d05;p=sonarqube.git SONAR-7330 QProfileBackuper is now only using dao --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java index 86174d38ac3..b227872fc61 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java @@ -45,23 +45,21 @@ import org.sonar.api.server.ServerSide; 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) { @@ -69,28 +67,28 @@ public class QProfileBackuper { DbSession dbSession = db.openSession(false); try { profile = db.qualityProfileDao().selectOrFailByKey(dbSession, key); + List activeRules = db.activeRuleDao().selectByProfileKey(dbSession, key); + Collections.sort(activeRules, BackupActiveRuleComparator.INSTANCE); + writeXml(dbSession, writer, profile, activeRules.iterator()); } finally { - dbSession.close(); + db.closeSession(dbSession); } - List 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 activeRules) { + private void writeXml(DbSession dbSession, Writer writer, QualityProfileDto profile, Iterator 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 param : activeRule.params().entrySet()) { + for (ActiveRuleParamDto param : db.activeRuleDao().selectParamsByActiveRuleKey(dbSession, activeRule.getKey())) { xml .begin("parameter") .prop("key", param.getKey()) @@ -213,14 +211,14 @@ public class QProfileBackuper { return new SMInputFactory(xmlFactory); } - private enum BackupActiveRuleComparator implements Comparator { + private enum BackupActiveRuleComparator implements Comparator { 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(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java index 1a25af93adf..c3a0ac2ee1c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java @@ -37,14 +37,18 @@ import org.junit.rules.ExpectedException; 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; @@ -54,7 +58,7 @@ import static org.junit.Assert.fail; public class QProfileBackuperMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester().withEsIndexes(); @Rule public ExpectedException thrown = ExpectedException.none(); @Rule @@ -62,21 +66,29 @@ public class QProfileBackuperMediumTest { 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 @@ -88,9 +100,10 @@ public class QProfileBackuperMediumTest { 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()); @@ -104,6 +117,7 @@ public class QProfileBackuperMediumTest { 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); @@ -136,9 +150,14 @@ public class QProfileBackuperMediumTest { List 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 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 @@ -155,6 +174,7 @@ public class QProfileBackuperMediumTest { 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 @@ -163,9 +183,14 @@ public class QProfileBackuperMediumTest { List 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 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 @@ -183,6 +208,7 @@ public class QProfileBackuperMediumTest { 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( @@ -191,16 +217,24 @@ public class QProfileBackuperMediumTest { // parent profile is unchanged List 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 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 @@ -218,6 +252,7 @@ public class QProfileBackuperMediumTest { 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( @@ -226,16 +261,25 @@ public class QProfileBackuperMediumTest { // parent profile is updated List 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 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 @@ -253,6 +297,7 @@ public class QProfileBackuperMediumTest { 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( @@ -319,7 +364,7 @@ public class QProfileBackuperMediumTest { null); dbSession.clearCache(); - assertThat(db.deprecatedActiveRuleDao().selectAll(dbSession)).hasSize(0); + assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(0); List profiles = db.qualityProfileDao().selectAll(dbSession); assertThat(profiles).hasSize(1); assertThat(profiles.get(0).getName()).isEqualTo("P1");