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;
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;
this.defLoader = defLoader;
this.system2 = system2;
this.userSession = userSession;
+ this.ruleIndexer = ruleIndexer;
}
public String backup() {
resetRules(ruleDtos, rules, updateDate, session);
}
+ ruleIndexer.index();
session.commit();
} finally {
MyBatis.closeQuietly(session);
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());
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);
}
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;
@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;
.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())) {
.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;
}
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 {
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;
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
}
markAsExecuted(session);
session.commit();
+ ruleIndexer.index();
} finally {
dbClient.closeSession(session);
}
rule.setRemediationFunction(null);
rule.setRemediationCoefficient(null);
rule.setRemediationOffset(null);
+ rule.setUpdatedAtInMs(system2.now());
dbClient.ruleDao().update(session, rule);
countClearedRules++;
}
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;
RuleDefinitionsLoader defLoader;
@Mock
System2 system2;
+ @Mock
+ RuleIndexer ruleIndexer;
@Captor
ArgumentCaptor<RuleDto> ruleCaptor;
@Captor
when(dbClient.ruleDao()).thenReturn(ruleDao);
underTest = new DebtModelBackup(dbClient, ruleOperations, rulesXMLImporter,
- debtModelXMLExporter, defLoader, system2, userSessionRule);
+ debtModelXMLExporter, defLoader, system2, userSessionRule, ruleIndexer);
}
@Test
verifyNoMoreInteractions(ruleDao);
verify(session).commit();
+ verify(ruleIndexer).index();
RuleDto rule = ruleCaptor.getValue();
verifyNoMoreInteractions(ruleDao);
verify(session).commit();
+ verify(ruleIndexer).index();
RuleDto rule = ruleCaptor.getValue();
assertThat(rule.getDefaultRemediationFunction()).isNull();
verify(ruleDao, times(2)).update(eq(session), ruleCaptor.capture());
verifyNoMoreInteractions(ruleDao);
verify(session).commit();
+ verify(ruleIndexer).index();
RuleDto rule = ruleCaptor.getAllValues().get(1);
verifyZeroInteractions(defLoader);
verify(session).commit();
+ verify(ruleIndexer).index();
}
@Test
verify(ruleDao).selectEnabledAndNonManual(session);
verify(session).commit();
+ verify(ruleIndexer).index();
}
@Test
verify(ruleDao).selectEnabledAndNonManual(session);
verify(session).commit();
+ verify(ruleIndexer).index();
}
@Test
verify(ruleDao).selectEnabledAndNonManual(session);
verify(session).commit();
+ verify(ruleIndexer).index();
}
@Test
verify(ruleDao).selectEnabledAndNonManual(session);
verify(session).commit();
+ verify(ruleIndexer).index();
}
@Test
verify(ruleDao).selectEnabledAndNonManual(session);
verify(session).commit();
+ verify(ruleIndexer).index();
}
@Test
verify(ruleDao).selectEnabledAndNonManual(session);
verify(session, never()).commit();
+ verify(ruleIndexer, never()).index();
}
}
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;
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;
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
.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;
}
.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;
}
.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");
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;
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 {
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();
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 {
ruleDto
);
dbSession.commit();
+ ruleIndexer.index();
return ruleDto;
}