Search in sources :

Example 26 with SensorStorage

use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonarqube by SonarSource.

the class DefaultExternalIssueTest method fail_to_store_if_primary_location_has_no_message.

@Test
public void fail_to_store_if_primary_location_has_no_message() {
    SensorStorage storage = mock(SensorStorage.class);
    DefaultExternalIssue issue = new DefaultExternalIssue(project, storage).at(new DefaultIssueLocation().on(inputFile).at(inputFile.selectLine(1))).forRule(RuleKey.of("repo", "rule")).remediationEffortMinutes(10L).type(RuleType.BUG).severity(Severity.BLOCKER);
    assertThatThrownBy(() -> issue.save()).isInstanceOf(IllegalStateException.class).hasMessage("External issues must have a message");
}
Also used : SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

Example 27 with SensorStorage

use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonarqube by SonarSource.

the class DefaultExternalIssueTest method fail_to_store_if_no_severity.

@Test
public void fail_to_store_if_no_severity() {
    SensorStorage storage = mock(SensorStorage.class);
    DefaultExternalIssue issue = new DefaultExternalIssue(project, storage).at(new DefaultIssueLocation().on(inputFile).at(inputFile.selectLine(1)).message("Wrong way!")).forRule(RuleKey.of("repo", "rule")).remediationEffortMinutes(10L).type(RuleType.BUG);
    assertThatThrownBy(() -> issue.save()).isInstanceOf(IllegalStateException.class).hasMessageContaining("Severity is mandatory");
}
Also used : SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

Example 28 with SensorStorage

use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonarqube by SonarSource.

the class DefaultFileLinesContextTest method setUp.

@Before
public void setUp() {
    MetricFinder metricFinder = mock(MetricFinder.class);
    org.sonar.api.batch.measure.Metric<String> hitsMetric = mock(org.sonar.api.batch.measure.Metric.class);
    when(hitsMetric.valueType()).thenReturn(String.class);
    when(hitsMetric.key()).thenReturn(HITS_METRIC_KEY);
    when(metricFinder.<String>findByKey(HITS_METRIC_KEY)).thenReturn(hitsMetric);
    org.sonar.api.batch.measure.Metric<String> authorMetric = mock(org.sonar.api.batch.measure.Metric.class);
    when(authorMetric.valueType()).thenReturn(String.class);
    when(authorMetric.key()).thenReturn(AUTHOR_METRIC_KEY);
    when(metricFinder.<String>findByKey(AUTHOR_METRIC_KEY)).thenReturn(authorMetric);
    org.sonar.api.batch.measure.Metric<String> branchesMetric = mock(org.sonar.api.batch.measure.Metric.class);
    when(branchesMetric.valueType()).thenReturn(String.class);
    when(branchesMetric.key()).thenReturn(BRANCHES_METRIC_KEY);
    when(metricFinder.<String>findByKey(BRANCHES_METRIC_KEY)).thenReturn(branchesMetric);
    when(metricFinder.<String>findByKey(CoreMetrics.NCLOC_DATA_KEY)).thenReturn(CoreMetrics.NCLOC_DATA);
    when(metricFinder.<String>findByKey(CoreMetrics.EXECUTABLE_LINES_DATA_KEY)).thenReturn(CoreMetrics.EXECUTABLE_LINES_DATA);
    sensorStorage = mock(SensorStorage.class);
    file = new TestInputFileBuilder("foo", "src/foo.php").initMetadata("Foo\nbar\nbiz").build();
    fileLineMeasures = new DefaultFileLinesContext(sensorStorage, file, metricFinder);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) MetricFinder(org.sonar.api.batch.measure.MetricFinder) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Before(org.junit.Before)

Example 29 with SensorStorage

use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonarqube by SonarSource.

the class DefaultSensorContextTest method prepare.

@Before
public void prepare() throws Exception {
    activeRules = new ActiveRulesBuilder().build();
    fs = new DefaultFileSystem(temp.newFolder().toPath());
    MetricFinder metricFinder = mock(MetricFinder.class);
    when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
    when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
    settings = new MapSettings();
    sensorStorage = mock(SensorStorage.class);
    analysisMode = mock(AnalysisMode.class);
    runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER);
    adaptor = new DefaultSensorContext(mock(InputModule.class), settings, fs, activeRules, analysisMode, sensorStorage, runtime);
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) MapSettings(org.sonar.api.config.MapSettings) MetricFinder(org.sonar.api.batch.measure.MetricFinder) AnalysisMode(org.sonar.api.batch.AnalysisMode) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Before(org.junit.Before)

Example 30 with SensorStorage

use of org.sonar.api.batch.sensor.internal.SensorStorage in project sonar-java by SonarSource.

the class JavaIssueTest method testIssueCreation.

@Test
public void testIssueCreation() {
    TestInputFileBuilder tifb = new TestInputFileBuilder("module", "relPath");
    tifb.setLines(3);
    tifb.setOriginalLineOffsets(new int[] { 0, 10, 15 });
    tifb.setLastValidOffset(25);
    DefaultInputFile file = tifb.build();
    RuleKey ruleKey = RuleKey.of("squid", "ruleKey");
    SensorContext sensorContext = mock(SensorContext.class);
    SensorStorage storage = mock(SensorStorage.class);
    DefaultIssue newIssue = new DefaultIssue(storage);
    DefaultIssue newIssueOnFile = new DefaultIssue(storage);
    DefaultIssue newIssueOnLine = new DefaultIssue(storage);
    Mockito.when(sensorContext.newIssue()).thenReturn(newIssue, newIssueOnFile, newIssueOnLine);
    // issue with secondary locations
    JavaIssue javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
    javaIssue.setPrimaryLocation(file, "main message", 1, 2, 1, 6);
    javaIssue.addSecondaryLocation(file, 2, 2, 2, 4, "secondary message 1");
    javaIssue.addSecondaryLocation(file, 3, 1, 3, 5, "secondary message 2");
    javaIssue.save();
    Mockito.verify(storage, Mockito.times(1)).store(newIssue);
    assertThat(newIssue.ruleKey()).isEqualTo(ruleKey);
    assertLocation(newIssue.primaryLocation(), file, "main message", 1, 2, 1, 6);
    assertThat(newIssue.flows()).hasSize(2);
    assertLocation(newIssue.flows().get(0).locations().get(0), file, "secondary message 1", 2, 2, 2, 4);
    assertLocation(newIssue.flows().get(1).locations().get(0), file, "secondary message 2", 3, 1, 3, 5);
    // issue on file
    javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
    javaIssue.setPrimaryLocationOnFile(file, "file message");
    javaIssue.save();
    Mockito.verify(storage, Mockito.times(1)).store(newIssueOnFile);
    assertThat(newIssueOnFile.ruleKey()).isEqualTo(ruleKey);
    IssueLocation location = newIssueOnFile.primaryLocation();
    assertThat(location.inputComponent()).isEqualTo(file);
    assertThat(location.textRange()).isNull();
    assertThat(location.message()).isEqualTo("file message");
    // issue on entire line
    javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
    javaIssue.setPrimaryLocation(file, "line message", 2, -1, 2, -1);
    javaIssue.save();
    Mockito.verify(storage, Mockito.times(1)).store(newIssueOnLine);
    assertLocation(newIssueOnLine.primaryLocation(), file, "line message", 2, 0, 2, 4);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContext(org.sonar.api.batch.SensorContext) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) RuleKey(org.sonar.api.rule.RuleKey) DefaultIssue(org.sonar.api.batch.sensor.issue.internal.DefaultIssue) IssueLocation(org.sonar.api.batch.sensor.issue.IssueLocation) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Test(org.junit.Test)

Aggregations

SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)32 Test (org.junit.Test)27 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)5 NewAdHocRule (org.sonar.api.batch.sensor.rule.NewAdHocRule)5 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)4 DefaultIssue (org.sonar.api.batch.sensor.issue.internal.DefaultIssue)4 RuleKey (org.sonar.api.rule.RuleKey)4 Before (org.junit.Before)3 InputFile (org.sonar.api.batch.fs.InputFile)3 MetricFinder (org.sonar.api.batch.measure.MetricFinder)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.jupiter.api.Test)2 AnalysisMode (org.sonar.api.batch.AnalysisMode)2 SensorContext (org.sonar.api.batch.SensorContext)2 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)2 DefaultInputDir (org.sonar.api.batch.fs.internal.DefaultInputDir)2 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)2 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)2 SensorContext (org.sonar.api.batch.sensor.SensorContext)2