@Test public void find_searches_by_exact_match_of_ruleKey_and_returns_most_recent_rule() { String ruleKey = "ABCD"; RuleDefinitionDto[] sameRuleKey = { dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setUpdatedAt(system2.now())) }; RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); CachingRuleFinder underTest = new CachingRuleFinder(dbClient); assertThat(toRuleKey(underTest.find(RuleQuery.create().withKey(ruleKey)))) .isEqualTo(sameRuleKey[1].getKey()); assertThat(toRuleKey(underTest.find(RuleQuery.create().withKey(otherRule.getRuleKey())))) .isEqualTo(otherRule.getKey()); assertThat(underTest.find(RuleQuery.create().withKey(ruleKey.toLowerCase()))) .isNull(); assertThat(underTest.find(RuleQuery.create().withKey(randomAlphabetic(3)))) .isNull(); }
@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 findAll_returns_all_rules_with_exact_same_rulekey_and_order_them_most_recent_first() { String ruleKey = "ABCD"; RuleDefinitionDto[] sameRuleKey = { dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setUpdatedAt(system2.now())) }; RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); CachingRuleFinder underTest = new CachingRuleFinder(dbClient); assertThat(underTest.findAll(RuleQuery.create().withKey(ruleKey))) .extracting(CachingRuleFinderTest::toRuleKey) .containsExactly(sameRuleKey[1].getKey(), sameRuleKey[0].getKey()); assertThat(underTest.findAll(RuleQuery.create().withKey(otherRule.getRuleKey()))) .extracting(CachingRuleFinderTest::toRuleKey) .containsExactly(otherRule.getKey()); assertThat(underTest.findAll(RuleQuery.create().withKey(ruleKey.toLowerCase()))) .isEmpty(); assertThat(underTest.findAll(RuleQuery.create().withKey(randomAlphabetic(3)))) .isEmpty(); }
@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 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)); } }
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); } }
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)); } } } }