From: Julien Lancelot Date: Fri, 26 Feb 2016 14:03:41 +0000 (+0100) Subject: SONAR-7330 Add RuleIndexer in missing classes X-Git-Tag: 5.5-M6~57 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a6d7179c5f8c6f63b72ea8401f02a95c566ea91c;p=sonarqube.git SONAR-7330 Add RuleIndexer in missing classes --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java b/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java index aed342810d7..1f308137e0a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java +++ b/server/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java @@ -42,6 +42,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.server.debt.DebtModelXMLExporter.RuleDebt; import org.sonar.server.rule.RuleDefinitionsLoader; import org.sonar.server.rule.RuleOperations; +import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.user.UserSession; import static com.google.common.collect.Lists.newArrayList; @@ -58,10 +59,11 @@ public class DebtModelBackup { private final RuleDefinitionsLoader defLoader; private final System2 system2; private final UserSession userSession; + private final RuleIndexer ruleIndexer; public DebtModelBackup(DbClient dbClient, RuleOperations ruleOperations, DebtRulesXMLImporter rulesXMLImporter, - DebtModelXMLExporter debtModelXMLExporter, RuleDefinitionsLoader defLoader, System2 system2, UserSession userSession) { + DebtModelXMLExporter debtModelXMLExporter, RuleDefinitionsLoader defLoader, System2 system2, UserSession userSession, RuleIndexer ruleIndexer) { this.dbClient = dbClient; this.ruleOperations = ruleOperations; this.rulesXMLImporter = rulesXMLImporter; @@ -69,6 +71,7 @@ public class DebtModelBackup { this.defLoader = defLoader; this.system2 = system2; this.userSession = userSession; + this.ruleIndexer = ruleIndexer; } public String backup() { @@ -122,6 +125,7 @@ public class DebtModelBackup { resetRules(ruleDtos, rules, updateDate, session); } + ruleIndexer.index(); session.commit(); } finally { MyBatis.closeQuietly(session); @@ -183,6 +187,7 @@ public class DebtModelBackup { restoreRules(rules(languageKey, session), rulesXMLImporter.importXML(xml, validationMessages), validationMessages, updateDate, session); session.commit(); + ruleIndexer.index(); } catch (IllegalArgumentException e) { LOG.debug("Error when restoring the model", e); validationMessages.addErrorText(e.getMessage()); @@ -200,7 +205,7 @@ public class DebtModelBackup { ruleDebt != null ? ruleDebt.function() : null, ruleDebt != null ? ruleDebt.coefficient() : null, ruleDebt != null ? ruleDebt.offset() : null, session); - rule.setUpdatedAt(updateDate); + rule.setUpdatedAtInMs(updateDate.getTime()); ruleDebts.remove(ruleDebt); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java index a7adbd6af2e..1d0dacd9488 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleCreator.java @@ -30,6 +30,7 @@ import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; import org.sonar.api.server.ServerSide; import org.sonar.api.server.rule.RuleParamType; +import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.rule.RuleDto; @@ -47,11 +48,13 @@ import static com.google.common.collect.Lists.newArrayList; @ServerSide public class RuleCreator { + private final System2 system2; private final RuleIndexer ruleIndexer; private final DbClient dbClient; private final TypeValidations typeValidations; - public RuleCreator(RuleIndexer ruleIndexer, DbClient dbClient, TypeValidations typeValidations) { + public RuleCreator(System2 system2, RuleIndexer ruleIndexer, DbClient dbClient, TypeValidations typeValidations) { + this.system2 = system2; this.ruleIndexer = ruleIndexer; this.dbClient = dbClient; this.typeValidations = typeValidations; @@ -211,7 +214,9 @@ public class RuleCreator { .setDefaultRemediationOffset(templateRuleDto.getDefaultRemediationOffset()) .setEffortToFixDescription(templateRuleDto.getEffortToFixDescription()) .setTags(templateRuleDto.getTags()) - .setSystemTags(templateRuleDto.getSystemTags()); + .setSystemTags(templateRuleDto.getSystemTags()) + .setCreatedAtInMs(system2.now()) + .setUpdatedAtInMs(system2.now()); dbClient.ruleDao().insert(dbSession, ruleDto); for (RuleParamDto templateRuleParamDto : dbClient.ruleDao().selectRuleParamsByRuleKey(dbSession, templateRuleDto.getKey())) { @@ -236,7 +241,9 @@ public class RuleCreator { .setDescription(newRule.markdownDescription()) .setDescriptionFormat(Format.MARKDOWN) .setSeverity(newRule.severity()) - .setStatus(RuleStatus.READY); + .setStatus(RuleStatus.READY) + .setCreatedAtInMs(system2.now()) + .setUpdatedAtInMs(system2.now()); dbClient.ruleDao().insert(dbSession, ruleDto); return ruleKey; } @@ -246,7 +253,8 @@ public class RuleCreator { if (newRule.isPreventReactivation()) { throw new ReactivationException(String.format("A removed rule with the key '%s' already exists", ruleDto.getKey().rule()), ruleDto.getKey()); } else { - ruleDto.setStatus(RuleStatus.READY); + ruleDto.setStatus(RuleStatus.READY) + .setUpdatedAtInMs(system2.now()); dbClient.ruleDao().update(dbSession, ruleDto); } } else { diff --git a/server/sonar-server/src/main/java/org/sonar/server/startup/ClearRulesOverloadedDebt.java b/server/sonar-server/src/main/java/org/sonar/server/startup/ClearRulesOverloadedDebt.java index 05e849a637b..49119d2f80f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/startup/ClearRulesOverloadedDebt.java +++ b/server/sonar-server/src/main/java/org/sonar/server/startup/ClearRulesOverloadedDebt.java @@ -20,12 +20,14 @@ package org.sonar.server.startup; import org.picocontainer.Startable; +import org.sonar.api.utils.System2; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.loadedtemplate.LoadedTemplateDto; import org.sonar.db.rule.RuleDto; +import org.sonar.server.rule.index.RuleIndexer; import static org.sonar.db.loadedtemplate.LoadedTemplateDto.ONE_SHOT_TASK_TYPE; @@ -45,10 +47,16 @@ public class ClearRulesOverloadedDebt implements Startable { private static final String SQALE_LICENSE_PROPERTY = "sonar.sqale.licenseHash.secured"; + private final System2 system2; + private final DbClient dbClient; - public ClearRulesOverloadedDebt(DbClient dbClient) { + private final RuleIndexer ruleIndexer; + + public ClearRulesOverloadedDebt(System2 system2, DbClient dbClient, RuleIndexer ruleIndexer) { + this.system2 = system2; this.dbClient = dbClient; + this.ruleIndexer = ruleIndexer; } @Override @@ -63,6 +71,7 @@ public class ClearRulesOverloadedDebt implements Startable { } markAsExecuted(session); session.commit(); + ruleIndexer.index(); } finally { dbClient.closeSession(session); } @@ -76,6 +85,7 @@ public class ClearRulesOverloadedDebt implements Startable { rule.setRemediationFunction(null); rule.setRemediationCoefficient(null); rule.setRemediationOffset(null); + rule.setUpdatedAtInMs(system2.now()); dbClient.ruleDao().update(session, rule); countClearedRules++; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java b/server/sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java index 5a0db3f71c9..68b6c1d78d7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java @@ -47,6 +47,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.server.debt.DebtModelXMLExporter.RuleDebt; import org.sonar.server.rule.RuleDefinitionsLoader; import org.sonar.server.rule.RuleOperations; +import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.tester.UserSessionRule; import static com.google.common.collect.Lists.newArrayList; @@ -84,6 +85,8 @@ public class DebtModelBackupTest { RuleDefinitionsLoader defLoader; @Mock System2 system2; + @Mock + RuleIndexer ruleIndexer; @Captor ArgumentCaptor ruleCaptor; @Captor @@ -106,7 +109,7 @@ public class DebtModelBackupTest { when(dbClient.ruleDao()).thenReturn(ruleDao); underTest = new DebtModelBackup(dbClient, ruleOperations, rulesXMLImporter, - debtModelXMLExporter, defLoader, system2, userSessionRule); + debtModelXMLExporter, defLoader, system2, userSessionRule, ruleIndexer); } @Test @@ -247,6 +250,7 @@ public class DebtModelBackupTest { verifyNoMoreInteractions(ruleDao); verify(session).commit(); + verify(ruleIndexer).index(); RuleDto rule = ruleCaptor.getValue(); @@ -289,6 +293,7 @@ public class DebtModelBackupTest { verifyNoMoreInteractions(ruleDao); verify(session).commit(); + verify(ruleIndexer).index(); RuleDto rule = ruleCaptor.getValue(); assertThat(rule.getDefaultRemediationFunction()).isNull(); @@ -330,6 +335,7 @@ public class DebtModelBackupTest { verify(ruleDao, times(2)).update(eq(session), ruleCaptor.capture()); verifyNoMoreInteractions(ruleDao); verify(session).commit(); + verify(ruleIndexer).index(); RuleDto rule = ruleCaptor.getAllValues().get(1); @@ -356,6 +362,7 @@ public class DebtModelBackupTest { verifyZeroInteractions(defLoader); verify(session).commit(); + verify(ruleIndexer).index(); } @Test @@ -375,6 +382,7 @@ public class DebtModelBackupTest { verify(ruleDao).selectEnabledAndNonManual(session); verify(session).commit(); + verify(ruleIndexer).index(); } @Test @@ -390,6 +398,7 @@ public class DebtModelBackupTest { verify(ruleDao).selectEnabledAndNonManual(session); verify(session).commit(); + verify(ruleIndexer).index(); } @Test @@ -413,6 +422,7 @@ public class DebtModelBackupTest { verify(ruleDao).selectEnabledAndNonManual(session); verify(session).commit(); + verify(ruleIndexer).index(); } @Test @@ -433,6 +443,7 @@ public class DebtModelBackupTest { verify(ruleDao).selectEnabledAndNonManual(session); verify(session).commit(); + verify(ruleIndexer).index(); } @Test @@ -448,6 +459,7 @@ public class DebtModelBackupTest { verify(ruleDao).selectEnabledAndNonManual(session); verify(session).commit(); + verify(ruleIndexer).index(); } @Test @@ -466,6 +478,7 @@ public class DebtModelBackupTest { verify(ruleDao).selectEnabledAndNonManual(session); verify(session, never()).commit(); + verify(ruleIndexer, never()).index(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorMediumTest.java index bd79147fc66..a3ccbff4b11 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleCreatorMediumTest.java @@ -21,6 +21,7 @@ package org.sonar.server.rule; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; +import java.util.Date; import java.util.List; import org.assertj.core.api.Fail; import org.junit.After; @@ -41,6 +42,7 @@ import org.sonar.db.rule.RuleTesting; import org.sonar.server.es.SearchOptions; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.rule.index.RuleIndex2; +import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.rule.index.RuleQuery; import org.sonar.server.tester.ServerTester; import org.sonar.server.tester.UserSessionRule; @@ -62,11 +64,14 @@ public class RuleCreatorMediumTest { RuleDao dao = tester.get(RuleDao.class); RuleCreator creator = tester.get(RuleCreator.class); RuleIndex2 ruleIndex = tester.get(RuleIndex2.class); + RuleIndexer ruleIndexer; @Before public void before() { tester.clearDbAndIndexes(); dbSession = tester.get(DbClient.class).openSession(false); + ruleIndexer = tester.get(RuleIndexer.class); + ruleIndexer.setEnabled(true); } @After @@ -682,11 +687,14 @@ public class RuleCreatorMediumTest { .setDefaultRemediationOffset("5min") .setEffortToFixDescription("desc") .setTags(Sets.newHashSet("usertag1", "usertag2")) - .setSystemTags(Sets.newHashSet("tag1", "tag4")); + .setSystemTags(Sets.newHashSet("tag1", "tag4")) + .setCreatedAtInMs(new Date().getTime()) + .setUpdatedAtInMs(new Date().getTime()); dao.insert(dbSession, templateRule); RuleParamDto ruleParamDto = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"); dao.insertRuleParam(dbSession, templateRule, ruleParamDto); dbSession.commit(); + ruleIndexer.index(); return templateRule; } @@ -698,12 +706,15 @@ public class RuleCreatorMediumTest { .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) .setDefaultRemediationCoefficient("1h") .setDefaultRemediationOffset("5min") - .setEffortToFixDescription("desc"); + .setEffortToFixDescription("desc") + .setCreatedAtInMs(new Date().getTime()) + .setUpdatedAtInMs(new Date().getTime()); dao.insert(dbSession, templateRule); RuleParamDto ruleParamDto = RuleParamDto.createFor(templateRule) .setName("myIntegers").setType("INTEGER,multiple=true,values=1;2;3").setDescription("My Integers").setDefaultValue("1"); dao.insertRuleParam(dbSession, templateRule, ruleParamDto); dbSession.commit(); + ruleIndexer.index(); return templateRule; } @@ -715,7 +726,9 @@ public class RuleCreatorMediumTest { .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) .setDefaultRemediationCoefficient("1h") .setDefaultRemediationOffset("5min") - .setEffortToFixDescription("desc"); + .setEffortToFixDescription("desc") + .setCreatedAtInMs(new Date().getTime()) + .setUpdatedAtInMs(new Date().getTime()); dao.insert(dbSession, templateRule); RuleParamDto ruleParam1Dto = RuleParamDto.createFor(templateRule) .setName("first").setType("INTEGER").setDescription("First integer").setDefaultValue("0"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/startup/ClearRulesOverloadedDebtTest.java b/server/sonar-server/src/test/java/org/sonar/server/startup/ClearRulesOverloadedDebtTest.java index 21e113b27f4..6c8f2d5be46 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/startup/ClearRulesOverloadedDebtTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/startup/ClearRulesOverloadedDebtTest.java @@ -21,8 +21,10 @@ package org.sonar.server.startup; import java.util.Date; import javax.annotation.Nullable; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; +import org.sonar.api.config.Settings; import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.LogTester; @@ -34,8 +36,12 @@ import org.sonar.db.property.PropertyDto; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleTesting; +import org.sonar.server.es.EsTester; +import org.sonar.server.rule.index.RuleIndexDefinition; +import org.sonar.server.rule.index.RuleIndexer; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; import static org.sonar.db.loadedtemplate.LoadedTemplateDto.ONE_SHOT_TASK_TYPE; public class ClearRulesOverloadedDebtTest { @@ -46,8 +52,13 @@ public class ClearRulesOverloadedDebtTest { private static final RuleKey RULE_KEY_2 = RuleTesting.XOO_X2; private static final RuleKey RULE_KEY_3 = RuleTesting.XOO_X3; + System2 system2 = mock(System2.class); + @Rule - public DbTester tester = DbTester.create(System2.INSTANCE); + public DbTester tester = DbTester.create(system2); + + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new RuleIndexDefinition(new Settings())); @Rule public LogTester logTester = new LogTester(); @@ -55,8 +66,9 @@ public class ClearRulesOverloadedDebtTest { DbClient dbClient = tester.getDbClient(); DbSession dbSession = tester.getSession(); RuleDao ruleDao = new RuleDao(); + RuleIndexer ruleIndexer = new RuleIndexer(dbClient, esTester.client()); - ClearRulesOverloadedDebt underTest = new ClearRulesOverloadedDebt(dbClient); + ClearRulesOverloadedDebt underTest = new ClearRulesOverloadedDebt(system2, dbClient, ruleIndexer); @Test public void remove_overridden_debt() throws Exception { @@ -136,6 +148,7 @@ public class ClearRulesOverloadedDebtTest { ruleDto ); dbSession.commit(); + ruleIndexer.index(); return ruleDto; }