private final DbSession dbSession = dbTester.getSession();
private final UuidFactory uuidFactory = new SequenceUuidFactory();
- private final RuleCreator underTest = new RuleCreator(system2, new RuleIndexer(es.client(), dbTester.getDbClient()), dbTester.getDbClient(), newFullTypeValidations(), uuidFactory);
+ private final RuleCreator underTest = new RuleCreator(system2, new RuleIndexer(es.client(), dbTester.getDbClient()), dbTester.getDbClient(), newFullTypeValidations(),
+ uuidFactory);
@Test
public void create_custom_rule() {
.setName("My custom")
.setMarkdownDescription("some description")
.setSeverity(Severity.MAJOR)
+ .setImpacts(List.of(new NewCustomRule.Impact(RELIABILITY, MEDIUM)))
.setStatus(RuleStatus.READY);
NewCustomRule secondRule = NewCustomRule.createForCustomRule(RuleKey.parse("java:CUSTOM_RULE_2"), templateRule.getKey())
.setSeverity(Severity.MAJOR)
.setStatus(RuleStatus.READY);
- List<RuleKey> customRuleKeys = underTest.create(dbSession, Arrays.asList(firstRule, secondRule))
+ List<RuleKey> customRuleKeys = underTest.restore(dbSession, Arrays.asList(firstRule, secondRule))
.stream()
.map(RuleDto::getKey)
.toList();
assertThat(rules).asList()
.extracting("ruleKey")
.containsOnly("CUSTOM_RULE_1", "CUSTOM_RULE_2");
+
+ RuleDto customRule1 = rules.stream().filter(e -> e.getRuleKey().equals("CUSTOM_RULE_1")).findFirst().orElseThrow();
+ assertThat(customRule1.getSeverityString()).isEqualTo(Severity.MAJOR);
+ assertThat(customRule1.getDefaultImpactsMap()).containsExactlyInAnyOrderEntriesOf(Map.of(RELIABILITY, MEDIUM));
+
}
@Test
.setSeverity(Severity.MAJOR)
.setStatus(RuleStatus.READY)
.setParameters(Map.of("regex", "a.*"));
-
- assertThatThrownBy(() -> underTest.create(dbSession, singletonList(newRule)))
+ List<NewCustomRule> newRules = singletonList(newRule);
+ assertThatThrownBy(() -> underTest.restore(dbSession, newRules))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("This rule is not a template rule: java:S001");
}
.setStatus(RuleStatus.READY);
List<NewCustomRule> newRules = singletonList(newRule);
- assertThatThrownBy(() -> underTest.create(dbSession, newRules))
+ assertThatThrownBy(() -> underTest.restore(dbSession, newRules))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The template key doesn't exist: java:S001");
}
.orElseThrow(() -> new IllegalArgumentException(format(TEMPLATE_KEY_NOT_EXIST_FORMAT, templateKey)));
checkArgument(templateRule.isTemplate(), "This rule is not a template rule: %s", templateKey.toString());
checkArgument(templateRule.getStatus() != RuleStatus.REMOVED, TEMPLATE_KEY_NOT_EXIST_FORMAT, templateKey.toString());
- validateCustomRule(newRule, dbSession, templateKey);
+ validateCustomRule(newRule, dbSession, templateKey, true);
Optional<RuleDto> definition = loadRule(dbSession, newRule.ruleKey());
RuleDto ruleDto = definition.map(dto -> updateExistingRule(dto, newRule, dbSession))
return ruleDto;
}
- public List<RuleDto> create(DbSession dbSession, List<NewCustomRule> newRules) {
+ public List<RuleDto> restore(DbSession dbSession, List<NewCustomRule> newRules) {
Set<RuleKey> templateKeys = newRules.stream().map(NewCustomRule::templateKey).collect(Collectors.toSet());
Map<RuleKey, RuleDto> templateRules = dbClient.ruleDao().selectByKeys(dbSession, templateKeys)
.stream()
List<RuleDto> customRules = newRules.stream()
.map(newCustomRule -> {
RuleDto templateRule = templateRules.get(newCustomRule.templateKey());
- validateCustomRule(newCustomRule, dbSession, templateRule.getKey());
+ validateCustomRule(newCustomRule, dbSession, templateRule.getKey(), false);
return createCustomRule(newCustomRule, templateRule, dbSession);
})
.toList();
return customRules;
}
- private void validateCustomRule(NewCustomRule newRule, DbSession dbSession, RuleKey templateKey) {
+ private void validateCustomRule(NewCustomRule newRule, DbSession dbSession, RuleKey templateKey, boolean checkImpacts) {
List<String> errors = new ArrayList<>();
validateRuleKey(errors, newRule.ruleKey(), templateKey);
if (severity != null && !Severity.ALL.contains(severity)) {
errors.add(format("Severity \"%s\" is invalid", severity));
}
- if (!CollectionUtils.isEmpty(newRule.getImpacts()) && (StringUtils.isNotBlank(newRule.severity()) || newRule.type() != null)) {
+ if (checkImpacts && !CollectionUtils.isEmpty(newRule.getImpacts()) && (StringUtils.isNotBlank(newRule.severity()) || newRule.type() != null)) {
errors.add("The rule cannot have both impacts and type/severity specified");
}
Arguments.of(true, true, true),
Arguments.of(false, false, false),
Arguments.of(false, null, false),
- Arguments.of(false, true, true)
- );
+ Arguments.of(false, true, true));
}
-
@Test
void restore_custom_rule() {
- when(ruleCreator.create(any(), anyList())).then(invocation -> Collections.singletonList(db.rules().insert(RuleKey.of("sonarjs", "s001"))));
+ when(ruleCreator.restore(any(), anyList())).then(invocation -> Collections.singletonList(db.rules().insert(RuleKey.of("sonarjs", "s001"))));
Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
"<profile>" +
}
@Test
- void restore_should_override_impacts(){
+ void restore_should_override_impacts() {
String ruleUuid = db.rules().insert(RuleKey.of("sonarjs", "s001")).getUuid();
Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
.toList();
if (!customRulesToCreate.isEmpty()) {
- return db.ruleDao().selectByKeys(dbSession, ruleCreator.create(dbSession, customRulesToCreate).stream().map(RuleDto::getKey).toList())
+ return db.ruleDao().selectByKeys(dbSession, ruleCreator.restore(dbSession, customRulesToCreate).stream().map(RuleDto::getKey).toList())
.stream()
.collect(Collectors.toMap(RuleDto::getKey, identity()));
}