import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
+import org.assertj.core.api.ThrowableAssertAlternative;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
+import static org.sonar.api.rule.RuleStatus.REMOVED;
+import static org.sonar.api.rule.Severity.CRITICAL;
+import static org.sonar.db.rule.RuleTesting.newRule;
+import static org.sonar.server.rule.RuleUpdate.createForCustomRule;
+import static org.sonar.server.rule.RuleUpdate.createForPluginRule;
public class RuleUpdaterTest {
private RuleIndex ruleIndex = new RuleIndex(es.client(), system2);
private RuleIndexer ruleIndexer = new RuleIndexer(es.client(), db.getDbClient());
private DbSession dbSession = db.getSession();
- private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private RuleUpdater underTest = new RuleUpdater(db.getDbClient(), ruleIndexer, system2);
@Test
public void do_not_update_rule_with_removed_status() {
- db.rules().insert(RuleTesting.newRule(RULE_KEY).setStatus(RuleStatus.REMOVED));
+ db.rules().insert(newRule(RULE_KEY).setStatus(RuleStatus.REMOVED));
dbSession.commit();
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setTags(Sets.newHashSet("java9"))
.setOrganization(db.getDefaultOrganization());
- try {
- underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
- fail();
- } catch (IllegalArgumentException e) {
- assertThat(e).hasMessage("Rule with REMOVED status cannot be updated: squid:S001");
- }
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Rule with REMOVED status cannot be updated: squid:S001");
+
+ underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
}
@Test
db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleId(ruleDto.getId()));
dbSession.commit();
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY);
+ RuleUpdate update = createForPluginRule(RULE_KEY);
assertThat(update.isEmpty()).isTrue();
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleId(ruleDto.getId()));
dbSession.commit();
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setMarkdownNote("my *note*")
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleId(ruleDto.getId()));
dbSession.commit();
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setMarkdownNote(null)
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
dbSession.commit();
// java8 is a system tag -> ignore
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setTags(Sets.newHashSet("bug", "java8"))
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
db.rules().insertOrUpdateMetadata(ruleDto.getMetadata());
dbSession.commit();
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setTags(null)
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
@Test
public void override_debt() {
- db.rules().insert(RuleTesting.newRule(RULE_KEY)
+ db.rules().insert(newRule(RULE_KEY)
.setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
.setDefRemediationGapMultiplier("1d")
.setDefRemediationBaseEffort("5min"));
dbSession.commit();
DefaultDebtRemediationFunction fn = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "1min");
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setDebtRemediationFunction(fn)
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
@Test
public void override_debt_only_offset() {
- db.rules().insert(RuleTesting.newRule(RULE_KEY)
+ db.rules().insert(newRule(RULE_KEY)
.setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR.name())
.setDefRemediationGapMultiplier("1d")
.setDefRemediationBaseEffort(null));
dbSession.commit();
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setDebtRemediationFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, "2d", null))
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
@Test
public void override_debt_from_linear_with_offset_to_constant() {
- db.rules().insert(RuleTesting.newRule(RULE_KEY)
+ db.rules().insert(newRule(RULE_KEY)
.setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
.setDefRemediationGapMultiplier("1d")
.setDefRemediationBaseEffort("5min"));
dbSession.commit();
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setDebtRemediationFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "10min"))
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleId(ruleDto.getId()));
dbSession.commit();
- RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ RuleUpdate update = createForPluginRule(RULE_KEY)
.setDebtRemediationFunction(null)
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
db.rules().insertRuleParam(customRule, param -> param.setName("format").setType("STRING").setDescription("Format").setDefaultValue(null));
// Update custom rule
- RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
+ RuleUpdate update = createForCustomRule(customRule.getKey())
.setName("New name")
.setMarkdownDescription("New description")
.setSeverity("MAJOR")
dbSession.commit();
// Update custom rule without setting a value for the parameter
- RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
+ RuleUpdate update = createForCustomRule(customRule.getKey())
.setName("New name")
.setMarkdownDescription("New description")
.setSeverity("MAJOR")
dbSession.commit();
// Update custom rule parameter 'regex', add 'message' and remove 'format'
- RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
+ RuleUpdate update = createForCustomRule(customRule.getKey())
.setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"))
.setOrganization(db.getDefaultOrganization());
underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
dbSession.commit();
// Update custom rule
- RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
+ RuleUpdate update = createForCustomRule(customRule.getKey())
.setName("")
.setMarkdownDescription("New desc")
.setOrganization(db.getDefaultOrganization());
- try {
- underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The name is missing");
- }
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("The name is missing");
+
+ underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
}
@Test
dbSession.commit();
- // Update custom rule
- RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
- .setName("New name")
- .setMarkdownDescription("")
- .setOrganization(db.getDefaultOrganization());
- try {
- underTest.update(dbSession, update, db.getDefaultOrganization(), userSessionRule);
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The description is missing");
- }
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("The description is missing");
+
+ underTest.update(dbSession,
+ createForCustomRule(customRule.getKey()).setName("New name").setMarkdownDescription("").setOrganization(db.getDefaultOrganization()),
+ db.getDefaultOrganization(), userSessionRule);
}
@Test
public void fail_to_update_plugin_rule_if_name_is_set() {
- // Create rule rule
- RuleDefinitionDto ruleDto = RuleTesting.newRule(RuleKey.of("squid", "S01"));
- db.rules().insert(ruleDto);
-
+ RuleDefinitionDto ruleDefinition = db.rules().insert(newRule(RuleKey.of("squid", "S01")));
dbSession.commit();
- try {
- // Update rule
- RuleUpdate.createForPluginRule(ruleDto.getKey())
- .setName("New name");
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not a custom rule");
- }
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Not a custom rule");
+
+ createForPluginRule(ruleDefinition.getKey()).setName("New name");
}
@Test
public void fail_to_update_plugin_rule_if_description_is_set() {
- // Create rule rule
- RuleDefinitionDto ruleDto = RuleTesting.newRule(RuleKey.of("squid", "S01"));
- db.rules().insert(ruleDto);
-
+ RuleDefinitionDto ruleDefinition = db.rules().insert(newRule(RuleKey.of("squid", "S01")));
dbSession.commit();
- try {
- // Update rule
- RuleUpdate.createForPluginRule(ruleDto.getKey())
- .setMarkdownDescription("New description");
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not a custom rule");
- }
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Not a custom rule");
+
+ createForPluginRule(ruleDefinition.getKey()).setMarkdownDescription("New description");
}
@Test
public void fail_to_update_plugin_rule_if_severity_is_set() {
- // Create rule rule
- RuleDefinitionDto ruleDto = RuleTesting.newRule(RuleKey.of("squid", "S01"));
- db.rules().insert(ruleDto);
-
+ RuleDefinitionDto ruleDefinition = db.rules().insert(newRule(RuleKey.of("squid", "S01")));
dbSession.commit();
- try {
- // Update rule
- RuleUpdate.createForPluginRule(ruleDto.getKey())
- .setSeverity(Severity.CRITICAL);
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not a custom rule");
- }
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Not a custom rule");
+
+ createForPluginRule(ruleDefinition.getKey()).setSeverity(CRITICAL);
}
private static Map<String, RuleParamDto> paramsByKey(List<RuleParamDto> params) {