@Test public void find_searches_by_exact_match_of_configKey_and_returns_most_recent_rule() { String configKey = "ABCD"; RuleDefinitionDto[] sameConfigKey = { dbTester.rules().insert(rule -> rule.setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setConfigKey(configKey).setUpdatedAt(system2.now())) }; RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); CachingRuleFinder underTest = new CachingRuleFinder(dbClient); assertThat(toRuleKey(underTest.find(RuleQuery.create().withConfigKey(configKey)))) .isEqualTo(sameConfigKey[1].getKey()); assertThat(toRuleKey(underTest.find(RuleQuery.create().withConfigKey(otherRule.getConfigKey())))) .isEqualTo(otherRule.getKey()); assertThat(underTest.find(RuleQuery.create().withConfigKey(configKey.toLowerCase()))) .isNull(); assertThat(underTest.find(RuleQuery.create().withConfigKey(randomAlphabetic(3)))) .isNull(); }
@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 findAll_returns_all_rules_with_exact_same_configkey_and_order_them_most_recent_first() { String configKey = "ABCD"; RuleDefinitionDto[] sameConfigKey = { dbTester.rules().insert(rule -> rule.setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setConfigKey(configKey).setUpdatedAt(system2.now())) }; RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); CachingRuleFinder underTest = new CachingRuleFinder(dbClient); assertThat(underTest.findAll(RuleQuery.create().withConfigKey(configKey))) .extracting(CachingRuleFinderTest::toRuleKey) .containsExactly(sameConfigKey[1].getKey(), sameConfigKey[0].getKey()); assertThat(underTest.findAll(RuleQuery.create().withConfigKey(otherRule.getConfigKey()))) .extracting(CachingRuleFinderTest::toRuleKey) .containsExactly(otherRule.getKey()); assertThat(underTest.findAll(RuleQuery.create().withConfigKey(configKey.toLowerCase()))) .isEmpty(); assertThat(underTest.findAll(RuleQuery.create().withConfigKey(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 void createActiveRule(SMInputCursor rulesCursor, Map<String, ActiveRule> activeRules, String gendarmeCategory, RulesProfile profile, RulePriority rulePriority) throws XMLStreamException { String[] includedRules = StringUtils.split(rulesCursor.getAttrValue("include"), '|'); for (int i = 0; i < includedRules.length; i++) { String configKey = includedRules[i].trim() + "@" + gendarmeCategory; ActiveRule activeRule = activeRules.get(configKey); if (activeRule == null) { Rule rule = ruleFinder.find(ruleQuery.withConfigKey(configKey)); if (rule != null) { activeRule = profile.activateRule(rule, rulePriority); activeRules.put(configKey, activeRule); } } else if (activeRule.getSeverity().equals(RulePriority.MAJOR)) { // MAJOR is the default one: maybe another priority has been defined and we must set it activeRule.setSeverity(rulePriority); } } }
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, 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); } }
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); } }
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; }