PythonProfile implements BuiltInQualityProfilesDefinition { @VisibleForTesting static Set<RuleKey> getSecurityRuleKeys(String className, String ruleKeysMethodName, String ruleRepoMethodName) { try { Class<?> rulesClass = Class.forName(className); Method getRuleKeysMethod = rulesClass.getMethod(ruleKeysMethodName); Set<String> ruleKeys = (Set<String>) getRuleKeysMethod.invoke(null); Method getRepositoryKeyMethod = rulesClass.getMethod(ruleRepoMethodName); String repositoryKey = (String) getRepositoryKeyMethod.invoke(null); return ruleKeys.stream().map(k -> RuleKey.of(repositoryKey, k)).collect(Collectors.toSet()); } catch (ClassNotFoundException e) { LOG.debug(className + " is not found, " + securityRuleMessage(e)); } catch (NoSuchMethodException e) { LOG.debug("Method not found on " + className +", " + securityRuleMessage(e)); } catch (IllegalAccessException | InvocationTargetException e) { LOG.debug(e.getClass().getSimpleName() + ": " + securityRuleMessage(e)); } return Collections.emptySet(); }  @Override void define(Context context);  }
@Test public void should_contains_security_rules_if_available() { PythonRules.getRuleKeys().clear(); assertThat(getSecurityRuleKeys(SECURITY_RULES_CLASS_NAME, SECURITY_RULE_KEYS_METHOD_NAME, SECURITY_RULE_REPO_METHOD_NAME)) .isEmpty(); PythonRules.getRuleKeys().add("S3649"); assertThat(getSecurityRuleKeys(SECURITY_RULES_CLASS_NAME, SECURITY_RULE_KEYS_METHOD_NAME, SECURITY_RULE_REPO_METHOD_NAME)) .containsOnly(RuleKey.of("pythonsecurity", "S3649")); assertThat(getSecurityRuleKeys("xxx", SECURITY_RULE_KEYS_METHOD_NAME, SECURITY_RULE_REPO_METHOD_NAME)).isEmpty(); assertThat(getSecurityRuleKeys(SECURITY_RULES_CLASS_NAME, "xxx", SECURITY_RULE_REPO_METHOD_NAME)).isEmpty(); PythonRules.throwOnCall = true; assertThat(getSecurityRuleKeys(SECURITY_RULES_CLASS_NAME, SECURITY_RULE_KEYS_METHOD_NAME, SECURITY_RULE_REPO_METHOD_NAME)) .isEmpty(); PythonRules.throwOnCall = false; }