選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RunMapperTest.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.externalissue.sarif;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.runner.RunWith;
  27. import org.mockito.Answers;
  28. import org.mockito.InjectMocks;
  29. import org.mockito.Mock;
  30. import org.mockito.MockedStatic;
  31. import org.mockito.junit.MockitoJUnitRunner;
  32. import org.slf4j.event.Level;
  33. import org.sonar.api.batch.sensor.issue.NewExternalIssue;
  34. import org.sonar.api.batch.sensor.rule.NewAdHocRule;
  35. import org.sonar.api.testfixtures.log.LogTester;
  36. import org.sonar.core.sarif.Extension;
  37. import org.sonar.core.sarif.Result;
  38. import org.sonar.core.sarif.Run;
  39. import org.sonar.scanner.externalissue.sarif.RunMapper.RunMapperResult;
  40. import static java.util.Collections.emptySet;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
  43. import static org.assertj.core.api.Assertions.assertThatNoException;
  44. import static org.mockito.Mockito.mock;
  45. import static org.mockito.Mockito.mockStatic;
  46. import static org.mockito.Mockito.when;
  47. @RunWith(MockitoJUnitRunner.class)
  48. public class RunMapperTest {
  49. private static final String WARNING = "warning";
  50. private static final String TEST_DRIVER = "Test driver";
  51. public static final String RULE_ID = "ruleId";
  52. @Mock
  53. private ResultMapper resultMapper;
  54. @Mock
  55. private RuleMapper ruleMapper;
  56. @Mock(answer = Answers.RETURNS_DEEP_STUBS)
  57. private Run run;
  58. @Mock
  59. private org.sonar.core.sarif.Rule rule;
  60. @Rule
  61. public LogTester logTester = new LogTester();
  62. @InjectMocks
  63. private RunMapper runMapper;
  64. @Before
  65. public void setUp() {
  66. when(run.getTool().getDriver().getName()).thenReturn(TEST_DRIVER);
  67. when(run.getTool().getExtensions()).thenReturn(null);
  68. when(rule.getId()).thenReturn(RULE_ID);
  69. }
  70. @Test
  71. public void mapRun_shouldMapExternalIssues() {
  72. Result result1 = mock(Result.class);
  73. Result result2 = mock(Result.class);
  74. when(run.getResults()).thenReturn(Set.of(result1, result2));
  75. NewExternalIssue externalIssue1 = mockMappedExternalIssue(result1);
  76. NewExternalIssue externalIssue2 = mockMappedExternalIssue(result2);
  77. try (MockedStatic<RulesSeverityDetector> detector = mockStatic(RulesSeverityDetector.class)) {
  78. detector.when(() -> RulesSeverityDetector.detectRulesSeverities(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  79. detector.when(() -> RulesSeverityDetector.detectRulesSeveritiesForNewTaxonomy(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  80. RunMapperResult runMapperResult = runMapper.mapRun(run);
  81. assertThat(runMapperResult.getNewExternalIssues()).containsOnly(externalIssue1, externalIssue2);
  82. assertThat(logTester.logs()).isEmpty();
  83. }
  84. }
  85. @Test
  86. public void mapRun_shouldMapExternalRules_whenDriverHasRulesAndNoExtensions() {
  87. when(run.getTool().getDriver().getRules()).thenReturn(Set.of(rule));
  88. NewAdHocRule externalRule = mockMappedExternalRule();
  89. try (MockedStatic<RulesSeverityDetector> detector = mockStatic(RulesSeverityDetector.class)) {
  90. detector.when(() -> RulesSeverityDetector.detectRulesSeverities(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  91. detector.when(() -> RulesSeverityDetector.detectRulesSeveritiesForNewTaxonomy(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  92. RunMapperResult runMapperResult = runMapper.mapRun(run);
  93. assertThat(runMapperResult.getNewAdHocRules()).containsOnly(externalRule);
  94. assertThat(logTester.logs()).isEmpty();
  95. }
  96. }
  97. @Test
  98. public void mapRun_shouldMapExternalRules_whenRulesInExtensions() {
  99. when(run.getTool().getDriver().getRules()).thenReturn(Set.of());
  100. Extension extension = mock(Extension.class);
  101. when(extension.getRules()).thenReturn(Set.of(rule));
  102. when(run.getTool().getExtensions()).thenReturn(Set.of(extension));
  103. NewAdHocRule externalRule = mockMappedExternalRule();
  104. try (MockedStatic<RulesSeverityDetector> detector = mockStatic(RulesSeverityDetector.class)) {
  105. detector.when(() -> RulesSeverityDetector.detectRulesSeverities(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  106. detector.when(() -> RulesSeverityDetector.detectRulesSeveritiesForNewTaxonomy(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  107. RunMapperResult runMapperResult = runMapper.mapRun(run);
  108. assertThat(runMapperResult.getNewAdHocRules()).containsOnly(externalRule);
  109. assertThat(logTester.logs()).isEmpty();
  110. }
  111. }
  112. @Test
  113. public void mapRun_shouldNotFail_whenExtensionsDontHaveRules() {
  114. when(run.getTool().getDriver().getRules()).thenReturn(Set.of(rule));
  115. Extension extension = mock(Extension.class);
  116. when(extension.getRules()).thenReturn(null);
  117. when(run.getTool().getExtensions()).thenReturn(Set.of(extension));
  118. try (MockedStatic<RulesSeverityDetector> detector = mockStatic(RulesSeverityDetector.class)) {
  119. detector.when(() -> RulesSeverityDetector.detectRulesSeverities(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  120. detector.when(() -> RulesSeverityDetector.detectRulesSeveritiesForNewTaxonomy(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  121. assertThatNoException().isThrownBy(() -> runMapper.mapRun(run));
  122. }
  123. }
  124. @Test
  125. public void mapRun_shouldNotFail_whenExtensionsHaveEmptyRules() {
  126. when(run.getTool().getDriver().getRules()).thenReturn(Set.of(rule));
  127. Extension extension = mock(Extension.class);
  128. when(extension.getRules()).thenReturn(Set.of());
  129. when(run.getTool().getExtensions()).thenReturn(Set.of(extension));
  130. try (MockedStatic<RulesSeverityDetector> detector = mockStatic(RulesSeverityDetector.class)) {
  131. detector.when(() -> RulesSeverityDetector.detectRulesSeverities(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  132. detector.when(() -> RulesSeverityDetector.detectRulesSeveritiesForNewTaxonomy(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  133. assertThatNoException().isThrownBy(() -> runMapper.mapRun(run));
  134. }
  135. }
  136. @Test
  137. public void mapRun_ifRunIsEmpty_returnsEmptyList() {
  138. when(run.getResults()).thenReturn(emptySet());
  139. RunMapperResult runMapperResult = runMapper.mapRun(run);
  140. assertThat(runMapperResult.getNewExternalIssues()).isEmpty();
  141. }
  142. @Test
  143. public void mapRun_ifExceptionThrownByResultMapper_logsThemAndContinueProcessing() {
  144. Result result1 = mock(Result.class);
  145. Result result2 = mock(Result.class);
  146. when(run.getResults()).thenReturn(Set.of(result1, result2));
  147. NewExternalIssue externalIssue2 = mockMappedExternalIssue(result2);
  148. when(result1.getRuleId()).thenReturn(RULE_ID);
  149. when(resultMapper.mapResult(TEST_DRIVER, WARNING, WARNING, result1)).thenThrow(new IllegalArgumentException("test"));
  150. try (MockedStatic<RulesSeverityDetector> detector = mockStatic(RulesSeverityDetector.class)) {
  151. detector.when(() -> RulesSeverityDetector.detectRulesSeverities(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  152. detector.when(() -> RulesSeverityDetector.detectRulesSeveritiesForNewTaxonomy(run, TEST_DRIVER)).thenReturn(Map.of(RULE_ID, WARNING));
  153. RunMapperResult runMapperResult = runMapper.mapRun(run);
  154. assertThat(runMapperResult.getNewExternalIssues())
  155. .containsExactly(externalIssue2);
  156. assertThat(logTester.logs(Level.WARN)).containsOnly("Failed to import an issue raised by tool Test driver, error: test");
  157. }
  158. }
  159. @Test
  160. public void mapRun_failsIfToolNotSet() {
  161. when(run.getTool()).thenReturn(null);
  162. assertThatIllegalArgumentException()
  163. .isThrownBy(() -> runMapper.mapRun(run))
  164. .withMessage("The run does not have a tool driver name defined.");
  165. }
  166. @Test
  167. public void mapRun_failsIfDriverNotSet() {
  168. when(run.getTool().getDriver()).thenReturn(null);
  169. assertThatIllegalArgumentException()
  170. .isThrownBy(() -> runMapper.mapRun(run))
  171. .withMessage("The run does not have a tool driver name defined.");
  172. }
  173. @Test
  174. public void mapRun_failsIfDriverNameIsNotSet() {
  175. when(run.getTool().getDriver().getName()).thenReturn(null);
  176. assertThatIllegalArgumentException()
  177. .isThrownBy(() -> runMapper.mapRun(run))
  178. .withMessage("The run does not have a tool driver name defined.");
  179. }
  180. private NewExternalIssue mockMappedExternalIssue(Result result) {
  181. NewExternalIssue externalIssue = mock(NewExternalIssue.class);
  182. when(result.getRuleId()).thenReturn(RULE_ID);
  183. when(resultMapper.mapResult(TEST_DRIVER, WARNING, WARNING, result)).thenReturn(externalIssue);
  184. return externalIssue;
  185. }
  186. private NewAdHocRule mockMappedExternalRule() {
  187. NewAdHocRule externalRule = mock(NewAdHocRule.class);
  188. when(ruleMapper.mapRule(rule, TEST_DRIVER, WARNING, WARNING)).thenReturn(externalRule);
  189. return externalRule;
  190. }
  191. }