import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.rule.RuleDto;
@ServerSide
public class RuleDeleter {
+ private final System2 system2;
private final RuleIndexer ruleIndexer;
private final DbClient dbClient;
private final RuleActivator ruleActivator;
- public RuleDeleter(RuleIndexer ruleIndexer, DbClient dbClient, RuleActivator ruleActivator) {
+ public RuleDeleter(System2 system2, RuleIndexer ruleIndexer, DbClient dbClient, RuleActivator ruleActivator) {
+ this.system2 = system2;
this.ruleIndexer = ruleIndexer;
this.dbClient = dbClient;
this.ruleActivator = ruleActivator;
}
rule.setStatus(RuleStatus.REMOVED);
+ rule.setUpdatedAt(system2.now());
dbClient.ruleDao().update(dbSession, rule);
dbSession.commit();
- ruleIndexer.setEnabled(true).index();
+ ruleIndexer.index();
} finally {
dbSession.close();
import org.sonar.server.qualityprofile.RuleActivator;
import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndex;
+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;
// TODO replace ServerTester by EsTester / DbTester
public class RuleDeleterMediumTest {
+ static final long PAST = 10000L;
+
@ClassRule
public static ServerTester tester = new ServerTester().withEsIndexes();
DbClient db = tester.get(DbClient.class);
RuleDao dao = tester.get(RuleDao.class);
RuleIndex index = tester.get(RuleIndex.class);
+ RuleIndexer ruleIndexer = tester.get(RuleIndexer.class);
+ ActiveRuleIndexer activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
RuleDeleter deleter = tester.get(RuleDeleter.class);
DbSession dbSession = tester.get(DbClient.class).openSession(false);
@Before
public void before() {
tester.clearDbAndIndexes();
+ ruleIndexer.setEnabled(true);
+ activeRuleIndexer.setEnabled(true);
}
@After
@Test
public void delete_custom_rule() {
// Create template rule
- RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("xoo", "T1")).setLanguage("xoo");
+ RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("xoo", "T1"))
+ .setLanguage("xoo")
+ .setCreatedAt(PAST)
+ .setUpdatedAt(PAST);
dao.insert(dbSession, templateRule);
// Create custom rule
- RuleDto customRule = RuleTesting.newCustomRule(templateRule).setLanguage("xoo");
+ RuleDto customRule = RuleTesting.newCustomRule(templateRule)
+ .setLanguage("xoo")
+ .setCreatedAt(PAST)
+ .setUpdatedAt(PAST);
dao.insert(dbSession, customRule);
// Create a quality profile
db.qualityProfileDao().insert(dbSession, profileDto);
dbSession.commit();
dbSession.clearCache();
+ ruleIndexer.index();
+ activeRuleIndexer.index();
// Activate the custom rule
activate(new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER), QProfileTesting.XOO_P1_KEY);
RuleDto customRuleReloaded = dao.selectOrFailByKey(dbSession, customRule.getKey());
assertThat(customRuleReloaded).isNotNull();
assertThat(customRuleReloaded.getStatus()).isEqualTo(RuleStatus.REMOVED);
+ assertThat(customRuleReloaded.getUpdatedAt()).isNotEqualTo(PAST);
// Verify there's no more active rule from custom rule
List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(ActiveRuleIndex.class).findByProfile(profileDto.getKey()));
@Test
public void delete_manual_rule() {
// Create manual rule
- RuleDto manualRule = RuleTesting.newManualRule("Manual_Rule");
+ RuleDto manualRule = RuleTesting.newManualRule("Manual_Rule")
+ .setCreatedAt(PAST)
+ .setUpdatedAt(PAST);
dao.insert(dbSession, manualRule);
-
dbSession.commit();
+ ruleIndexer.index();
// Delete manual rule
deleter.delete(manualRule.getKey());
RuleDto result = dao.selectOrFailByKey(dbSession, manualRule.getKey());
assertThat(result).isNotNull();
assertThat(result.getStatus()).isEqualTo(RuleStatus.REMOVED);
+ assertThat(result.getUpdatedAt()).isNotEqualTo(PAST);
// Verify in index
assertThat(index.search(new RuleQuery(), new SearchOptions()).getIds()).isEmpty();