@Test
  public void load_issues() throws Exception {
    reportReader.putFileSourceLines(FILE.getRef(), "line 1;", "line 2;");
    BatchReport.Issue reportIssue =
        BatchReport.Issue.newBuilder()
            .setLine(2)
            .setMsg("the message")
            .setRuleRepository("java")
            .setRuleKey("S001")
            .setSeverity(Constants.Severity.BLOCKER)
            .setEffortToFix(3.14)
            .build();
    reportReader.putIssues(FILE.getRef(), asList(reportIssue));
    Input<DefaultIssue> input = underTest.create(FILE);

    Collection<DefaultIssue> issues = input.getIssues();
    assertThat(issues).hasSize(1);
    DefaultIssue issue = Iterators.getOnlyElement(issues.iterator());

    // fields set by analysis report
    assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("java", "S001"));
    assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
    assertThat(issue.line()).isEqualTo(2);
    assertThat(issue.effortToFix()).isEqualTo(3.14);
    assertThat(issue.message()).isEqualTo("the message");

    // fields set by compute engine
    assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2));
    assertThat(issue.tags()).isEmpty();
    assertInitializedIssue(issue);
  }
  @Test
  public void insert_measures_from_report() {
    metricRepository.add(1, STRING_METRIC);
    metricRepository.add(2, DOUBLE_METRIC);

    reportReader.putMeasures(
        PROJECT_REF,
        Arrays.asList(
            BatchReport.Measure.newBuilder()
                .setValueType(MeasureValueType.STRING)
                .setStringValue("measure-data")
                .setVariationValue1(1.1d)
                .setVariationValue2(2.2d)
                .setVariationValue3(3.3d)
                .setVariationValue4(4.4d)
                .setVariationValue5(5.5d)
                .setDescription("measure-description")
                .setMetricKey(STRING_METRIC_KEY)
                .build()));

    reportReader.putMeasures(
        FILE_REF,
        Arrays.asList(
            BatchReport.Measure.newBuilder()
                .setValueType(MeasureValueType.DOUBLE)
                .setDoubleValue(123.123d)
                .setVariationValue1(1.1d)
                .setVariationValue2(2.2d)
                .setVariationValue3(3.3d)
                .setVariationValue4(4.4d)
                .setVariationValue5(5.5d)
                .setDescription("measure-description")
                .setMetricKey(DOUBLE_METRIC_KEY)
                .build()));

    sut.execute();
    session.commit();

    assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(2);

    List<Map<String, Object>> dtos = retrieveDtos();

    Map<String, Object> dto = dtos.get(0);
    assertThat(dto.get("snapshotId")).isEqualTo(3L);
    assertThat(dto.get("componentId")).isEqualTo(projectDto.getId());
    assertThat(dto.get("metricId")).isEqualTo(1L);
    assertThat(dto.get("textValue")).isEqualTo("measure-data");
    assertThat(dto.get("severity")).isNull();

    dto = dtos.get(PROJECT_REF);
    assertThat(dto.get("snapshotId")).isEqualTo(4L);
    assertThat(dto.get("componentId")).isEqualTo(fileDto.getId());
    assertThat(dto.get("metricId")).isEqualTo(2L);
    assertThat(dto.get("value")).isEqualTo(123.1d);
    assertThat(dto.get("severity")).isNull();
  }
  @Test
  public void load_issues_from_report() throws Exception {
    when(issueFilter.accept(any(DefaultIssue.class), eq(FILE))).thenReturn(true);
    fileSourceRepository.addLines(FILE_REF, "line 1;", "line 2;");
    ScannerReport.Issue reportIssue =
        ScannerReport.Issue.newBuilder()
            .setTextRange(TextRange.newBuilder().setStartLine(2).build())
            .setMsg("the message")
            .setRuleRepository("java")
            .setRuleKey("S001")
            .setSeverity(Constants.Severity.BLOCKER)
            .setGap(3.14)
            .build();
    reportReader.putIssues(FILE.getReportAttributes().getRef(), asList(reportIssue));
    Input<DefaultIssue> input = underTest.create(FILE);

    Collection<DefaultIssue> issues = input.getIssues();
    assertThat(issues).hasSize(1);
    DefaultIssue issue = Iterators.getOnlyElement(issues.iterator());

    // fields set by analysis report
    assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("java", "S001"));
    assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
    assertThat(issue.line()).isEqualTo(2);
    assertThat(issue.effortToFix()).isEqualTo(3.14);
    assertThat(issue.gap()).isEqualTo(3.14);
    assertThat(issue.message()).isEqualTo("the message");

    // fields set by compute engine
    assertThat(issue.checksum()).isEqualTo(input.getLineHashSequence().getHashForLine(2));
    assertThat(issue.tags()).isEmpty();
    assertInitializedIssue(issue);
  }
  @Test
  public void ignore_report_issues_on_common_rules() throws Exception {
    reportReader.putFileSourceLines(FILE.getRef(), "line 1;", "line 2;");
    BatchReport.Issue reportIssue =
        BatchReport.Issue.newBuilder()
            .setMsg("the message")
            .setRuleRepository(CommonRuleKeys.commonRepositoryForLang("java"))
            .setRuleKey("S001")
            .setSeverity(Constants.Severity.BLOCKER)
            .build();
    reportReader.putIssues(FILE.getRef(), asList(reportIssue));

    Input<DefaultIssue> input = underTest.create(FILE);

    assertThat(input.getIssues()).isEmpty();
  }
  @Test
  public void load_source_hash_sequences() throws Exception {
    reportReader.putFileSourceLines(FILE.getRef(), "line 1;", "line 2;");
    Input<DefaultIssue> input = underTest.create(FILE);

    assertThat(input.getLineHashSequence()).isNotNull();
    assertThat(input.getLineHashSequence().getHashForLine(1)).isNotEmpty();
    assertThat(input.getLineHashSequence().getHashForLine(2)).isNotEmpty();
    assertThat(input.getLineHashSequence().getHashForLine(3)).isEmpty();

    assertThat(input.getBlockHashSequence()).isNotNull();
  }
  @Test
  public void do_not_insert_function_complexity_distribution_metric_on_files() {
    metricRepository.add(1, FUNCTION_COMPLEXITY_DISTRIBUTION);

    reportReader.putMeasures(
        PROJECT_REF,
        Arrays.asList(
            BatchReport.Measure.newBuilder()
                .setValueType(MeasureValueType.STRING)
                .setStringValue("0=1;2=10")
                .setMetricKey(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)
                .build()));

    // Should not be persisted
    reportReader.putMeasures(
        FILE_REF,
        Arrays.asList(
            BatchReport.Measure.newBuilder()
                .setValueType(MeasureValueType.STRING)
                .setStringValue("0=1;2=10")
                .setMetricKey(FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)
                .build()));

    sut.execute();

    session.commit();

    assertThat(dbTester.countRowsOfTable("project_measures")).isEqualTo(1);

    List<Map<String, Object>> dtos = retrieveDtos();

    Map<String, Object> dto = dtos.get(0);
    assertThat(dto.get("snapshotId")).isEqualTo(3L);
    assertThat(dto.get("componentId")).isEqualTo(projectDto.getId());
    assertThat(dto.get("textValue")).isEqualTo("0=1;2=10");
  }
  @Test(expected = IllegalStateException.class)
  public void fail_with_ISE_when_trying_to_insert_forbidden_measures() {
    metricRepository.add(1, DUPLICATIONS_DATA);

    reportReader.putMeasures(
        FILE_REF,
        Arrays.asList(
            BatchReport.Measure.newBuilder()
                .setValueType(MeasureValueType.STRING)
                .setStringValue("{duplications}")
                .setMetricKey(DUPLICATIONS_DATA_KEY)
                .build()));

    sut.execute();
  }
  @Test
  public void load_issues_of_compute_engine_common_rules() throws Exception {
    reportReader.putFileSourceLines(FILE.getRef(), "line 1;", "line 2;");
    DefaultIssue ceIssue =
        new DefaultIssue()
            .setRuleKey(
                RuleKey.of(CommonRuleKeys.commonRepositoryForLang("java"), "InsufficientCoverage"))
            .setMessage("not enough coverage")
            .setEffortToFix(10.0);
    when(commonRuleEngine.process(FILE)).thenReturn(asList(ceIssue));

    Input<DefaultIssue> input = underTest.create(FILE);

    assertThat(input.getIssues()).containsOnly(ceIssue);
    assertInitializedIssue(input.getIssues().iterator().next());
  }
  @Test
  public void ignore_issue_from_report() throws Exception {
    when(issueFilter.accept(any(DefaultIssue.class), eq(FILE))).thenReturn(false);
    fileSourceRepository.addLines(FILE_REF, "line 1;", "line 2;");
    ScannerReport.Issue reportIssue =
        ScannerReport.Issue.newBuilder()
            .setTextRange(TextRange.newBuilder().setStartLine(2).build())
            .setMsg("the message")
            .setRuleRepository("java")
            .setRuleKey("S001")
            .setSeverity(Constants.Severity.BLOCKER)
            .setGap(3.14)
            .build();
    reportReader.putIssues(FILE.getReportAttributes().getRef(), asList(reportIssue));
    Input<DefaultIssue> input = underTest.create(FILE);

    Collection<DefaultIssue> issues = input.getIssues();
    assertThat(issues).isEmpty();
  }
  @Test
  public void empty_values_are_not_persisted() {
    metricRepository.add(1, STRING_METRIC);
    metricRepository.add(2, DOUBLE_METRIC);

    reportReader.putMeasures(
        FILE_REF,
        Arrays.asList(
            BatchReport.Measure.newBuilder()
                .setValueType(MeasureValueType.STRING)
                .setMetricKey(STRING_METRIC_KEY)
                .build(),
            BatchReport.Measure.newBuilder()
                .setValueType(MeasureValueType.DOUBLE)
                .setMetricKey(DOUBLE_METRIC_KEY)
                .build()));

    sut.execute();
    session.commit();

    assertThat(retrieveDtos()).isEmpty();
  }
  @Test
  public void bestValue_measure_of_bestValueOptimized_metrics_are_not_persisted() {
    metricRepository.add(
        1,
        new Metric.Builder(OPTIMIZED_METRIC_KEY, "Optimized metric", Metric.ValueType.BOOL)
            .setOptimizedBestValue(true)
            .setBestValue(1d)
            .create());

    reportReader.putMeasures(
        FILE_REF,
        Arrays.asList(
            BatchReport.Measure.newBuilder()
                .setValueType(MeasureValueType.BOOLEAN)
                .setBooleanValue(true)
                .setMetricKey(OPTIMIZED_METRIC_KEY)
                .build()));

    sut.execute();
    session.commit();

    assertThat(retrieveDtos()).isEmpty();
  }