### Question:
PmdViolationRecorder implements BatchExtension { public void saveViolation(RuleViolation pmdViolation) { InputFile inputFile = findResourceFor(pmdViolation); if (inputFile == null) { return; } Issuable issuable = perspectives.as(Issuable.class, inputFile); Rule rule = findRuleFor(pmdViolation); if (issuable == null || rule == null) { return; } IssueBuilder issueBuilder = issuable.newIssueBuilder() .ruleKey(rule.ruleKey()) .message(pmdViolation.getDescription()) .line(pmdViolation.getBeginLine()); issuable.addIssue(issueBuilder.build()); } PmdViolationRecorder(FileSystem fs, RuleFinder ruleFinder, ResourcePerspectives perspectives); void saveViolation(RuleViolation pmdViolation);  }### Answer:
@Test public void should_convert_pmd_violation_to_sonar_violation() { org.sonar.api.rules.Rule sonarRule = createRuleInRuleFinder("RULE"); File file1 = new File("src/source.java"); DefaultInputFile inputFile1 = addToFileSystem(file1); Issuable issuable = createIssuable(inputFile1); RuleViolation pmdViolation = createPmdViolation(file1, 42, "Description", "RULE"); Issue issue = mock(Issue.class); IssueBuilder issueBuilder = mock(IssueBuilder.class); when(issuable.newIssueBuilder()).thenReturn(issueBuilder); when(issueBuilder.ruleKey(sonarRule.ruleKey())).thenReturn(issueBuilder); when(issueBuilder.message("Description")).thenReturn(issueBuilder); when(issueBuilder.line(42)).thenReturn(issueBuilder); when(issueBuilder.build()).thenReturn(issue); pmdViolationRecorder.saveViolation(pmdViolation); verify(issuable).addIssue(issue); verify(issueBuilder).ruleKey(sonarRule.ruleKey()); verify(issueBuilder).message("Description"); verify(issueBuilder).line(42); verify(sonarRule, atLeastOnce()).ruleKey(); }
@Test public void should_ignore_violation_on_unknown_resource() { org.sonar.api.rules.Rule sonarRule = createRuleInRuleFinder("RULE"); File unknownFile = new File("src/UNKNOWN.java"); RuleViolation pmdViolation = createPmdViolation(unknownFile, 42, "Description", "RULE"); pmdViolationRecorder.saveViolation(pmdViolation); verifyZeroInteractions(sonarRule); }
@Test public void should_ignore_violation_on_non_issuable_resource() { org.sonar.api.rules.Rule sonarRule = createRuleInRuleFinder("RULE"); File file1 = new File("test/source.java"); addToFileSystem(file1); RuleViolation pmdViolation = createPmdViolation(file1, 42, "Description", "RULE"); pmdViolationRecorder.saveViolation(pmdViolation); verifyZeroInteractions(sonarRule); }
@Test public void should_ignore_violation_on_unknown_rule() { File file1 = new File("test/source.java"); DefaultInputFile inputFile1 = addToFileSystem(file1); Issuable issuable = createIssuable(inputFile1); RuleViolation pmdViolation = createPmdViolation(file1, 42, "Description", "UNKNOWN"); pmdViolationRecorder.saveViolation(pmdViolation); verifyZeroInteractions(issuable); }