@CheckForNull public String getDescription(String ruleKey) { RuleDetails details = ruleDetails(ruleKey); if (details == null) { return null; } if (details.getExtendedDescription().isEmpty()) { return details.getHtmlDescription(); } return details.getHtmlDescription() + "<br/><br/>" + details.getExtendedDescription(); }
private void copyRuleHtmlDescriptions(Function<String, RuleDetails> ruleDescriptionProducer, IssuesReport report) { try { Set<String> ruleKeys = report.getSummary().getTotalByRuleKey().keySet(); Path target = reportDir.resolve("sonarlintreport_rules"); Files.createDirectories(target); copyDependency(target, "rule.css"); for (String ruleKey : ruleKeys) { RuleDetails ruleDetails = ruleDescriptionProducer.apply(ruleKey); String htmlDescription = ruleDetails.getHtmlDescription(); String extendedDescription = ruleDetails.getExtendedDescription(); if (!extendedDescription.isEmpty()) { htmlDescription += "\n<div>" + extendedDescription + "</div>"; } FileUtils.write(target.resolve(Util.escapeFileName(ruleKey) + ".html").toFile(), "<!doctype html><html><head><link href=\"rule.css\" rel=\"stylesheet\" type=\"text/css\" /></head><body><h1><big>" + ruleDetails.getName() + "</big> (" + ruleKey + ")</h1><div class=\"rule-desc\">" + htmlDescription + "</div></body></html>", StandardCharsets.UTF_8); } } catch (IOException e) { throw new IllegalStateException("Unable to copy rule descriptions", e); } }
@Test public void should_get_description() { RuleDetails ruleDetails = mock(RuleDetails.class); when(ruleDetails.getExtendedDescription()).thenReturn("desc"); when(ruleDetails.getHtmlDescription()).thenReturn("html"); when(engine.getRuleDetails("rule1")).thenReturn(ruleDetails); assertThat(facade.getDescription("rule1")).isEqualTo("html<br/><br/>desc"); assertThat(facade.getDescription("invalid")).isNull(); }
@Test public void should_get_description() { RuleDetails ruleDetails = mock(RuleDetails.class); when(ruleDetails.getExtendedDescription()).thenReturn("desc"); when(ruleDetails.getHtmlDescription()).thenReturn("html"); when(engine.getRuleDetails("rule1")).thenReturn(ruleDetails); assertThat(facade.getDescription("rule1")).isEqualTo("html<br/><br/>desc"); assertThat(facade.getDescription("invalid")).isNull(); }