@Test public void should_success_finder_wrap() { // has Id assertThat(underTest.findById(rule1.getId()).getId()).isEqualTo(rule1.getId()); // should_find_by_id assertThat(underTest.findById(rule3.getId()).getConfigKey()).isEqualTo("Checker/Treewalker/AnnotationUseStyleCheck"); // should_not_find_disabled_rule_by_id assertThat(underTest.findById(rule2.getId())).isNull(); // should_find_by_key Rule rule = underTest.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"); assertThat(rule).isNotNull(); assertThat(rule.getKey()).isEqualTo(("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck")); assertThat(rule.isEnabled()).isTrue(); // find_should_return_null_if_no_results assertThat(underTest.findByKey("checkstyle", "unknown")).isNull(); assertThat(underTest.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))).isNull(); // find_repository_rules assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("checkstyle"))).hasSize(2); // find_all_enabled assertThat(underTest.findAll(RuleQuery.create())).extracting("id").containsOnly(rule1.getId(), rule3.getId(), rule4.getId()); assertThat(underTest.findAll(RuleQuery.create())).hasSize(3); // do_not_find_disabled_rules assertThat(underTest.findByKey("checkstyle", "DisabledCheck")).isNull(); // do_not_find_unknown_rules assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"))).isEmpty(); }
@Test public void find_searches_by_exact_match_of_repository_key_and_returns_most_recent_rule() { String repoKey = "ABCD"; RuleDefinitionDto[] sameRepoKey = { dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setUpdatedAt(system2.now())) }; RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); CachingRuleFinder underTest = new CachingRuleFinder(dbClient); assertThat(toRuleKey(underTest.find(RuleQuery.create().withRepositoryKey(repoKey)))) .isEqualTo(sameRepoKey[1].getKey()); assertThat(toRuleKey(underTest.find(RuleQuery.create().withRepositoryKey(otherRule.getRepositoryKey())))) .isEqualTo(otherRule.getKey()); assertThat(underTest.find(RuleQuery.create().withRepositoryKey(repoKey.toLowerCase()))) .isNull(); assertThat(underTest.find(RuleQuery.create().withRepositoryKey(randomAlphabetic(3)))) .isNull(); }
@Test public void findAll_returns_all_rules_with_exact_same_repository_key_and_order_them_most_recent_first() { String repoKey = "ABCD"; RuleDefinitionDto[] sameRepoKey = { dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setUpdatedAt(system2.now())) }; RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); CachingRuleFinder underTest = new CachingRuleFinder(dbClient); assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(repoKey))) .extracting(CachingRuleFinderTest::toRuleKey) .containsExactly(sameRepoKey[1].getKey(), sameRepoKey[0].getKey()); assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(otherRule.getRepositoryKey()))) .extracting(CachingRuleFinderTest::toRuleKey) .containsExactly(otherRule.getKey()); assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(repoKey.toLowerCase()))) .isEmpty(); assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(randomAlphabetic(3)))) .isEmpty(); }
@Test public void select_by_query() { OrganizationDto organization = db.organizations().insert(); RuleDefinitionDto rule1 = db.rules().insert(r -> r.setKey(RuleKey.of("java", "S001")).setConfigKey("S1")); db.rules().insertOrUpdateMetadata(rule1, organization); RuleDefinitionDto rule2 = db.rules().insert(r -> r.setKey(RuleKey.of("java", "S002"))); db.rules().insertOrUpdateMetadata(rule2, organization); RuleDefinitionDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED)); assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create())).hasSize(2); assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create().withKey("S001"))).hasSize(1); assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create().withConfigKey("S1"))).hasSize(1); assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create().withRepositoryKey("java"))).hasSize(2); assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create().withKey("S001").withConfigKey("S1").withRepositoryKey("java"))).hasSize(1); }
@Test public void find_searches_by_exact_match_and_match_on_all_criterias_and_returns_most_recent_match() { String repoKey = "ABCD"; String ruleKey = "EFGH"; String configKey = "IJKL"; RuleDefinitionDto[] rules = { dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setRuleKey(ruleKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())) }; RuleQuery allQuery = RuleQuery.create().withRepositoryKey(repoKey).withKey(ruleKey).withConfigKey(configKey); RuleQuery ruleAndConfigKeyQuery = RuleQuery.create().withKey(ruleKey).withConfigKey(configKey); RuleQuery repoAndConfigKeyQuery = RuleQuery.create().withRepositoryKey(repoKey).withConfigKey(configKey); RuleQuery repoAndKeyQuery = RuleQuery.create().withRepositoryKey(repoKey).withKey(ruleKey); RuleQuery configKeyQuery = RuleQuery.create().withConfigKey(configKey); RuleQuery ruleKeyQuery = RuleQuery.create().withKey(ruleKey); RuleQuery repoKeyQuery = RuleQuery.create().withRepositoryKey(repoKey); CachingRuleFinder underTest = new CachingRuleFinder(dbClient); assertThat(toRuleKey(underTest.find(allQuery))).isEqualTo(rules[0].getKey()); assertThat(toRuleKey(underTest.find(ruleAndConfigKeyQuery))).isEqualTo(rules[1].getKey()); assertThat(toRuleKey(underTest.find(repoAndConfigKeyQuery))).isEqualTo(rules[2].getKey()); assertThat(toRuleKey(underTest.find(repoAndKeyQuery))).isEqualTo(rules[0].getKey()); assertThat(toRuleKey(underTest.find(repoKeyQuery))).isEqualTo(rules[2].getKey()); assertThat(toRuleKey(underTest.find(ruleKeyQuery))).isEqualTo(rules[1].getKey()); assertThat(toRuleKey(underTest.find(configKeyQuery))).isEqualTo(rules[2].getKey()); }
dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())) }; RuleQuery allQuery = RuleQuery.create().withRepositoryKey(repoKey).withKey(ruleKey).withConfigKey(configKey); RuleQuery ruleAndConfigKeyQuery = RuleQuery.create().withKey(ruleKey).withConfigKey(configKey); RuleQuery repoAndConfigKeyQuery = RuleQuery.create().withRepositoryKey(repoKey).withConfigKey(configKey); RuleQuery repoAndKeyQuery = RuleQuery.create().withRepositoryKey(repoKey).withKey(ruleKey); RuleQuery configKeyQuery = RuleQuery.create().withConfigKey(configKey); RuleQuery ruleKeyQuery = RuleQuery.create().withKey(ruleKey); RuleQuery repoKeyQuery = RuleQuery.create().withRepositoryKey(repoKey);
private Iterable<Rule> rules() { return Iterables.concat( ruleFinder.findAll(RuleQuery.create().withRepositoryKey(FindbugsRulesDefinition.REPOSITORY_KEY)), ruleFinder.findAll(RuleQuery.create().withRepositoryKey(FbContribRulesDefinition.REPOSITORY_KEY)), ruleFinder.findAll(RuleQuery.create().withRepositoryKey(FindSecurityBugsRulesDefinition.REPOSITORY_KEY)), ruleFinder.findAll(RuleQuery.create().withRepositoryKey(FindSecurityBugsJspRulesDefinition.REPOSITORY_KEY))); }
protected GendarmeProfileImporter(String languageKey, String repositoryKey, String repositoryName, RuleFinder ruleFinder) { super(repositoryKey, repositoryName); setSupportedLanguages(languageKey); this.ruleFinder = ruleFinder; this.languageKey = languageKey; this.ruleQuery = RuleQuery.create().withRepositoryKey(repositoryKey); }
private void processRule(RulesProfile profile, String path, String moduleName, Map<String, String> properties, ValidationMessages messages) throws XMLStreamException { Rule rule; String id = properties.get("id"); String warning; if (StringUtils.isNotBlank(id)) { rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withKey(id)); warning = "Checkstyle rule with key '" + id + "' not found"; } else { String configKey = path + moduleName; rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withConfigKey(configKey)); warning = "Checkstyle rule with config key '" + configKey + "' not found"; } if (rule == null) { messages.addWarningText(warning); } else { ActiveRule activeRule = profile.activateRule(rule, null); activateProperties(activeRule, properties); } }
messages.addWarningText(message.toString()); } else { Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PHPMD_REPOSITORY_KEY).withConfigKey(configKey)); if (rule == null) { StringBuilder message = new StringBuilder("Unable to import unknown PMD rule '");
private void processRule(RulesProfile profile, String path, String moduleName, SMInputCursor moduleCursor, ValidationMessages messages) throws XMLStreamException { Map<String, String> properties = processProps(moduleCursor); Rule rule; String id = properties.get("id"); String warning; if (StringUtils.isNotBlank(id)) { rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withKey(id)); warning = "Checkstyle rule with key '" + id + "' not found"; } else { String configKey = path + moduleName; rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withConfigKey(configKey)); warning = "Checkstyle rule with config key '" + configKey + "' not found"; } if (rule == null) { messages.addWarningText(warning); } else { ActiveRule activeRule = profile.activateRule(rule, null); activateProperties(activeRule, properties); } }
private void processRule(RulesProfile profile, String path, String moduleName, Map<String, String> properties, ValidationMessages messages) { final Rule rule; final String id = properties.get("id"); final String warning; if (StringUtils.isNotBlank(id)) { rule = ruleFinder.find(RuleQuery.create() .withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withKey(id)); warning = "Checkstyle rule with key '" + id + "' not found"; } else { final String configKey = path + moduleName; rule = ruleFinder .find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY) .withConfigKey(configKey)); warning = "Checkstyle rule with config key '" + configKey + "' not found"; } if (rule == null) { messages.addWarningText(warning); } else { final ActiveRule activeRule = profile.activateRule(rule, null); activateProperties(activeRule, properties); } }
private void createViolationFromMessageAtProjectLevel(SMInputCursor messagesCursor) throws XMLStreamException { Rule currentRule = ruleFinder.find(RuleQuery.create().withRepositoryKey(repositoryKey) .withKey(messagesCursor.getAttrValue(TYPENAME))); if (currentRule != null) { // the violation is saved at project level, not on a specific resource Violation violation = Violation.create(currentRule, project); violation.setMessage(messagesCursor.collectDescendantText().trim()); violation.setSeverity(currentRule.getSeverity()); context.saveViolation(violation); } else { LOG.debug("Could not find the following rule in the FxCop rule repository: " + messagesCursor.getAttrValue(TYPENAME)); } }
Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(getKey()).withKey(ruleName));
/** * @param pmdRuleset * @param messages * @return */ protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(pmdRuleset.getName(), Php.KEY); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { boolean isRuleValid = true; String key = pmdRule.getRef(); if (key == null) { messages.addWarningText("A rule without 'ref' attribute can't be imported. see '" + pmdRule.getClazz() + "'"); isRuleValid = false; } // Attention: rule is retrieved using the key field which different of what is done on PmdImporter that uses configKey. Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PHPCS_REPOSITORY_KEY).withKey(key)); if (rule == null) { StringBuilder message = new StringBuilder("Unable to import unknown PMD rule '"); message.append(key).append("' consider adding an extension in sonar extenions directory"); messages.addWarningText(message.toString()); isRuleValid = false; } ActiveRule activeRule = profile.activateRule(rule, mapper.from(pmdRule.getPriority())); if (isRuleValid && pmdRule.getProperties() != null) { createRule(messages, pmdRule, key, rule, activeRule); } } return profile; }
private void parseRuleBlocs(SMInputCursor cursor) throws XMLStreamException { RuleQuery ruleQuery = RuleQuery.create().withRepositoryKey(repositoryKey); // Cursor is on <rule> while (cursor.getNext() != null) { if (cursor.getCurrEvent().equals(SMEvent.START_ELEMENT)) { Rule currentRule = ruleFinder.find(ruleQuery.withKey(cursor.getAttrValue("Name"))); if (currentRule != null) { String type = cursor.getAttrValue("Type"); if (StringUtils.isEmpty(type)) { parseRuleDefects(cursor, currentRule); } else { gendarmeViolationMaker.registerRuleType(currentRule, type); } } } } }
private void parseTypeBloc(String namespaceName, SMInputCursor cursor) throws XMLStreamException { // Cursor on <Type> String typeName = cursor.getAttrValue(NAME); Resource<?> typeResource = resourceBridge.getFromTypeName(namespaceName, typeName); SMInputCursor messagesCursor = cursor.descendantElementCursor(MESSAGE); while (messagesCursor.getNext() != null) { // Cursor on <Message> if (messagesCursor.getCurrEvent() == SMEvent.START_ELEMENT) { Rule currentRule = ruleFinder.find(RuleQuery.create().withRepositoryKey(repositoryKey) .withKey(messagesCursor.getAttrValue(TYPENAME))); if (currentRule != null) { // look for all potential issues searchForViolations(messagesCursor, typeResource, currentRule); } else { LOG.warn("Could not find the following rule in the FxCop rule repository: " + messagesCursor.getAttrValue(TYPENAME)); } } } }
protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { String ruleClassName = pmdRule.getClazz(); if (PmdConstants.XPATH_CLASS.equals(ruleClassName)) { messages.addWarningText("PMD XPath rule '" + pmdRule.getName() + "' can't be imported automatically. The rule must be created manually through the SonarQube web interface."); } else { String ruleRef = pmdRule.getRef(); if (ruleRef == null) { messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + ruleClassName + "'"); } else { Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(ruleRef)); if (rule != null) { ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); setParameters(activeRule, pmdRule, rule, messages); } else { messages.addWarningText("Unable to import unknown PMD rule '" + ruleRef + "'"); } } } } return profile; }
@Override public RulesProfile importProfile(Reader pmdConfigurationFile, ValidationMessages messages) { PmdRuleSet pmdRuleset = PmdRuleSets.parse(pmdConfigurationFile, messages); RulesProfile profile = RulesProfile.create(); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { String ruleClassName = pmdRule.getClazz(); if (PmdConstants.XPATH_CLASS.equals(ruleClassName)) { messages.addWarningText("PMD XPath rule '" + pmdRule.getName() + "' can't be imported automatically. The rule must be created manually through the SonarQube web interface."); } else { String ruleRef = pmdRule.getRef(); if (ruleRef == null) { messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + ruleClassName + "'"); } else { Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(ruleRef)); if (rule != null) { ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); setParameters(activeRule, pmdRule, rule, messages); } else { messages.addWarningText("Unable to import unknown PMD rule '" + ruleRef + "'"); } } } } return profile; } }
protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { if (PmdConstants.XPATH_CLASS.equals(pmdRule.getClazz())) { messages.addWarningText("PMD XPath rule '" + pmdRule.getName() + "' can't be imported automatically. The rule must be created manually through the Sonar web interface."); continue; } if (pmdRule.getRef() == null) { messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + pmdRule.getClazz() + "'"); continue; } Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(pmdRule.getRef())); if (rule != null) { ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); if (pmdRule.getProperties() != null) { for (PmdProperty prop : pmdRule.getProperties()) { if (rule.getParam(prop.getName()) == null) { messages.addWarningText("The property '" + prop.getName() + "' is not supported in the pmd rule: " + pmdRule.getRef()); continue; } activeRule.setParameter(prop.getName(), prop.getValue()); } } } else { messages.addWarningText("Unable to import unknown PMD rule '" + pmdRule.getRef() + "'"); } } return profile; }