private static boolean mergeTags(RulesDefinition.Rule ruleDef, RuleDefinitionDto dto) { boolean changed = false; if (RuleStatus.REMOVED == ruleDef.status()) { dto.setSystemTags(emptySet()); changed = true; } else if (dto.getSystemTags().size() != ruleDef.tags().size() || !dto.getSystemTags().containsAll(ruleDef.tags())) { dto.setSystemTags(ruleDef.tags()); // FIXME this can't be implemented easily with organization support: remove end-user tags that are now declared as system // RuleTagHelper.applyTags(dto, ImmutableSet.copyOf(dto.getTags())); changed = true; } return changed; }
@Test public void rule_with_property() { RulesDefinition.Repository repository = load(RuleWithProperty.class); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo("foo"); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.name()).isEqualTo("bar"); assertThat(rule.htmlDescription()).isEqualTo("Foo Bar"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.params()).hasSize(1); assertThat(rule.tags()).isEmpty(); RulesDefinition.Param prop = rule.param("property"); assertThat(prop.key()).isEqualTo("property"); assertThat(prop.description()).isEqualTo("Ignore ?"); assertThat(prop.defaultValue()).isEqualTo("false"); assertThat(prop.type()).isEqualTo(RuleParamType.STRING); }
@Test public void parse_xml() { InputStream input = getClass().getResourceAsStream("RulesDefinitionXmlLoaderTest/rules.xml"); RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name()); assertThat(repository.rules()).hasSize(2); RulesDefinition.Rule rule = repository.rule("complete"); assertThat(rule.key()).isEqualTo("complete"); assertThat(rule.name()).isEqualTo("Complete"); assertThat(rule.htmlDescription()).isEqualTo("Description of Complete"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.template()).isTrue(); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/LocalVariableName"); assertThat(rule.type()).isEqualTo(RuleType.BUG); assertThat(rule.tags()).containsOnly("misra", "spring"); assertThat(rule.params()).hasSize(2); RulesDefinition.Param ignore = rule.param("ignore"); assertThat(ignore.key()).isEqualTo("ignore"); assertThat(ignore.description()).isEqualTo("Ignore ?"); assertThat(ignore.defaultValue()).isEqualTo("false"); rule = repository.rule("minimal"); assertThat(rule.key()).isEqualTo("minimal"); assertThat(rule.name()).isEqualTo("Minimal"); assertThat(rule.htmlDescription()).isEqualTo("Description of Minimal"); assertThat(rule.params()).isEmpty(); assertThat(rule.status()).isEqualTo(RuleStatus.READY); assertThat(rule.severity()).isEqualTo(Severity.MAJOR); assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL); }
rule.severity(), rule.type().name(), repo.language(), rule.tags(), "", rule.activatedByDefault()); ruleDetailsMap.put(ruleKey.toString(), ruleDetails);
.setStatus(ruleDef.status()) .setGapDescription(ruleDef.gapDescription()) .setSystemTags(ruleDef.tags()) .setType(RuleType.valueOf(ruleDef.type().name())) .setScope(toDtoScope(ruleDef.scope()))
@Test public void test() { MyCustomPhpRulesDefinition rulesDefinition = new MyCustomPhpRulesDefinition(); RulesDefinition.Context context = new RulesDefinition.Context(); rulesDefinition.define(context); RulesDefinition.Repository repository = context.repository(REPOSITORY_KEY); assertThat(repository.name()).isEqualTo(REPOSITORY_NAME); assertThat(repository.language()).isEqualTo(PHPCustomRulesDefinition.LANGUAGE_KEY); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule customRule = repository.rule(RULE_KEY); assertThat(customRule).isNotNull(); assertThat(customRule.name()).isEqualTo(RULE_NAME); assertThat(customRule.htmlDescription()).isEqualTo("desc"); assertThat(customRule.tags()).contains("mybug"); assertThat(customRule.params()).hasSize(1); RulesDefinition.Param param = customRule.param("customParam"); assertThat(param.key()).isEqualTo("customParam"); assertThat(param.description()).isEqualTo("Custom parameter"); assertThat(param.defaultValue()).isEqualTo("value"); }
private static boolean mergeTags(RulesDefinition.Rule ruleDef, RuleDefinitionDto dto) { boolean changed = false; if (RuleStatus.REMOVED == ruleDef.status()) { dto.setSystemTags(emptySet()); changed = true; } else if (dto.getSystemTags().size() != ruleDef.tags().size() || !dto.getSystemTags().containsAll(ruleDef.tags())) { dto.setSystemTags(ruleDef.tags()); // FIXME this can't be implemented easily with organization support: remove end-user tags that are now declared as system // RuleTagHelper.applyTags(dto, ImmutableSet.copyOf(dto.getTags())); changed = true; } return changed; }
assertThat(rule.htmlDescription()).isEqualTo("Detect <code>java.lang.NullPointerException</code>"); assertThat(rule.markdownDescription()).isNull(); assertThat(rule.tags()).containsOnly("one", "two", "three", "four"); assertThat(rule.securityStandards()).containsOnly("cwe:1", "cwe:123", "cwe:2", "owaspTop10:a1", "owaspTop10:a3"); assertThat(rule.params()).isEmpty();
assertThat(rule.htmlDescription()).isEqualTo("Detect <code>java.lang.NullPointerException</code>"); assertThat(rule.markdownDescription()).isNull(); assertThat(rule.tags()).containsOnly("one", "two", "three", "four"); assertThat(rule.securityStandards()).containsOnly("cwe:1", "cwe:123", "cwe:2", "owaspTop10:a1", "owaspTop10:a3"); assertThat(rule.params()).isEmpty();
assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/ConstantName"); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.tags()).containsOnly("style", "clumsy"); assertThat(rule.params()).hasSize(1); RulesDefinition.Param param = rule.param("format");
.setStatus(ruleDef.status()) .setGapDescription(ruleDef.gapDescription()) .setSystemTags(ruleDef.tags()) .setSecurityStandards(ruleDef.securityStandards()) .setType(RuleType.valueOf(ruleDef.type().name()))
@Test public void define_rule_with_default_fields() { RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java"); newFindbugs.createRule("NPE").setName("NPE").setHtmlDescription("NPE"); newFindbugs.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); assertThat(rule.key()).isEqualTo("NPE"); assertThat(rule.severity()).isEqualTo(Severity.MAJOR); assertThat(rule.params()).isEmpty(); assertThat(rule.internalKey()).isNull(); assertThat(rule.status()).isEqualTo(RuleStatus.defaultStatus()); assertThat(rule.tags()).isEmpty(); assertThat(rule.securityStandards()).isEmpty(); assertThat(rule.debtRemediationFunction()).isNull(); }
@Test public void type_is_defined() { RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java"); newRepository.createRule("NPE").setName("NPE").setHtmlDescription("desc") .setType(RuleType.VULNERABILITY).setTags("bug", "misra"); newRepository.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); // type VULNERABILITY is kept even if the tag "bug" is present assertThat(rule.type()).isEqualTo(RuleType.VULNERABILITY); // tag "bug" is reserved and removed. assertThat(rule.tags()).containsOnly("misra"); }
@Test public void rule_with_tags() { RulesDefinition.Repository repository = load(RuleWithTags.class); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.tags()).containsOnly("misra", "clumsy"); }
@Test public void guess_type_from_tags_if_type_is_missing() { RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java"); newRepository.createRule("NPE").setName("NPE").setHtmlDescription("desc").setTags("bug", "misra"); newRepository.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); assertThat(rule.type()).isEqualTo(RuleType.BUG); // tag "bug" is reserved and removed assertThat(rule.tags()).containsOnly("misra"); } }