### Question:
PmdViolationRecorder { public void saveViolation(RuleViolation pmdViolation, SensorContext context) { final InputFile inputFile = findResourceFor(pmdViolation); if (inputFile == null) { return; } final RuleKey ruleKey = findActiveRuleKeyFor(pmdViolation); if (ruleKey == null) { return; } final NewIssue issue = context.newIssue() .forRule(ruleKey); final TextRange issueTextRange = issueRangeFor(pmdViolation, inputFile); final NewIssueLocation issueLocation = issue.newLocation() .on(inputFile) .message(pmdViolation.getDescription()) .at(issueTextRange); issue.at(issueLocation) .save(); } PmdViolationRecorder(FileSystem fs, ActiveRules activeRules); void saveViolation(RuleViolation pmdViolation, SensorContext context);  }### Answer:
@Test void should_convert_pmd_violation_to_sonar_violation() { final ActiveRule rule = createRuleInActiveRules(); final File file1 = new File("src/source.java"); final DefaultInputFile inputFile1 = addToFileSystem(file1); final RuleViolation pmdViolation = createPmdViolation(file1, "RULE"); final NewIssue newIssue = mock(NewIssue.class); final NewIssueLocation issueLocation = mock(NewIssueLocation.class); when(mockContext.newIssue()).thenReturn(newIssue); when(newIssue.forRule(rule.ruleKey())).thenReturn(newIssue); when(newIssue.newLocation()).thenReturn(issueLocation); when(newIssue.at(issueLocation)).thenReturn(newIssue); when(issueLocation.on(inputFile1)).thenReturn(issueLocation); when(issueLocation.message("Description")).thenReturn(issueLocation); when(issueLocation.at(any(TextRange.class))).thenReturn(issueLocation); pmdViolationRecorder.saveViolation(pmdViolation, mockContext); verify(mockContext).newIssue(); verify(newIssue).save(); }
@Test void should_ignore_violation_on_unknown_resource() { final File unknownFile = new File("src/UNKNOWN.java"); final RuleViolation pmdViolation = createPmdViolation(unknownFile, "RULE"); pmdViolationRecorder.saveViolation(pmdViolation, mockContext); verifyZeroInteractions(mockActiveRules); verifyZeroInteractions(mockContext); verify(spiedFs).inputFile(any(FilePredicate.class)); }
@Test void should_ignore_violation_on_unknown_rule() { final File file1 = new File("test/source.java"); addToFileSystem(file1); final String ruleName = "UNKNOWN"; final RuleViolation pmdViolation = createPmdViolation(file1, ruleName); final RuleKey expectedRuleKey1 = RuleKey.of(PmdConstants.REPOSITORY_KEY, ruleName); final RuleKey expectedRuleKey2 = RuleKey.of(PmdConstants.REPOSITORY_KEY, ruleName); pmdViolationRecorder.saveViolation(pmdViolation, mockContext); verify(spiedFs).inputFile(any(FilePredicate.class)); verify(mockActiveRules).find(expectedRuleKey1); verify(mockActiveRules).find(expectedRuleKey2); verifyZeroInteractions(mockContext); }