private BatchReportWriter initReport() {
    BatchReportWriter writer = new BatchReportWriter(reportDir);
    writer.writeMetadata(
        BatchReport.Metadata.newBuilder()
            .setRootComponentRef(1)
            .setProjectKey("PROJECT_KEY")
            .setAnalysisDate(150000000L)
            .build());

    writer.writeComponent(
        BatchReport.Component.newBuilder()
            .setRef(1)
            .setType(Constants.ComponentType.PROJECT)
            .setUuid("PROJECT_A")
            .addChildRef(2)
            .build());
    writer.writeComponent(
        BatchReport.Component.newBuilder()
            .setRef(2)
            .setType(Constants.ComponentType.MODULE)
            .setUuid("BCDE")
            .addChildRef(FILE_REF)
            .build());
    writer.writeComponent(
        BatchReport.Component.newBuilder()
            .setRef(FILE_REF)
            .setType(Constants.ComponentType.FILE)
            .setUuid("FILE_A")
            .build());
    return writer;
  }
  @Test
  public void persist_report_events_with_component_children() throws Exception {
    dbTester.prepareDbUnit(getClass(), "empty.xml");

    File reportDir = temp.newFolder();
    BatchReportWriter writer = new BatchReportWriter(reportDir);
    writer.writeMetadata(
        BatchReport.Metadata.newBuilder()
            .setRootComponentRef(1)
            .setProjectKey("PROJECT_KEY")
            .setAnalysisDate(150000000L)
            .build());

    writer.writeComponent(
        BatchReport.Component.newBuilder()
            .setRef(1)
            .setType(Constants.ComponentType.PROJECT)
            .setUuid("ABCD")
            .setSnapshotId(1000L)
            .addEvent(
                BatchReport.Event.newBuilder()
                    .setName("Red (was Orange)")
                    .setCategory(Constants.EventCategory.ALERT)
                    .setDescription("Open issues > 0")
                    .build())
            .addChildRef(2)
            .build());

    writer.writeComponent(
        BatchReport.Component.newBuilder()
            .setRef(2)
            .setType(Constants.ComponentType.MODULE)
            .setUuid("BCDE")
            .setSnapshotId(1001L)
            .addEvent(
                BatchReport.Event.newBuilder()
                    .setName("Red (was Orange)")
                    .setCategory(Constants.EventCategory.ALERT)
                    .setDescription("Open issues > 0")
                    .build())
            .build());

    step.execute(
        new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class)));

    dbTester.assertDbUnit(
        getClass(), "persist_report_events_with_component_children-result.xml", "events");
  }
  @Test
  public void nothing_to_do_when_no_events_in_report() throws Exception {
    dbTester.prepareDbUnit(getClass(), "nothing_to_do_when_no_events_in_report.xml");

    File reportDir = temp.newFolder();
    BatchReportWriter writer = new BatchReportWriter(reportDir);
    writer.writeMetadata(
        BatchReport.Metadata.newBuilder()
            .setRootComponentRef(1)
            .setProjectKey("PROJECT_KEY")
            .setAnalysisDate(150000000L)
            .build());

    writer.writeComponent(
        BatchReport.Component.newBuilder()
            .setRef(1)
            .setType(Constants.ComponentType.PROJECT)
            .setUuid("ABCD")
            .build());

    step.execute(
        new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class)));

    dbTester.assertDbUnit(getClass(), "nothing_to_do_when_no_events_in_report.xml", "events");
  }
  @Test
  public void keep_one_event_by_version() throws Exception {
    dbTester.prepareDbUnit(getClass(), "keep_one_event_by_version.xml");

    File reportDir = temp.newFolder();
    BatchReportWriter writer = new BatchReportWriter(reportDir);
    writer.writeMetadata(
        BatchReport.Metadata.newBuilder()
            .setRootComponentRef(1)
            .setProjectKey("PROJECT_KEY")
            .setAnalysisDate(150000000L)
            .build());

    writer.writeComponent(
        BatchReport.Component.newBuilder()
            .setRef(1)
            .setType(Constants.ComponentType.PROJECT)
            .setUuid("ABCD")
            .setSnapshotId(1001L)
            .setVersion("1.5-SNAPSHOT")
            .build());

    step.execute(
        new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class)));

    dbTester.assertDbUnit(getClass(), "keep_one_event_by_version-result.xml", "events");
  }
  @Test
  public void persist_report_events() throws Exception {
    dbTester.prepareDbUnit(getClass(), "empty.xml");

    File reportDir = temp.newFolder();
    BatchReportWriter writer = new BatchReportWriter(reportDir);
    writer.writeMetadata(
        BatchReport.Metadata.newBuilder()
            .setRootComponentRef(1)
            .setProjectKey("PROJECT_KEY")
            .setAnalysisDate(150000000L)
            .build());

    writer.writeComponent(
        BatchReport.Component.newBuilder()
            .setRef(1)
            .setType(Constants.ComponentType.PROJECT)
            .setUuid("ABCD")
            .setSnapshotId(1000L)
            .addEvent(
                BatchReport.Event.newBuilder()
                    .setName("Red (was Orange)")
                    .setCategory(Constants.EventCategory.ALERT)
                    .setDescription("Open issues > 0")
                    .build())
            .addEvent(
                BatchReport.Event.newBuilder()
                    .setName("Changes in 'Default' (Java)")
                    .setCategory(Constants.EventCategory.PROFILE)
                    .setEventData(
                        "from=2014-10-12T08:36:25+0000;key=java-default;to=2014-10-12T10:36:25+0000")
                    .build())
            .build());

    step.execute(
        new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class)));

    dbTester.assertDbUnit(getClass(), "add_events-result.xml", "events");
  }
public class BatchReportReaderImplTest {
  private static final int COMPONENT_REF = 1;
  private static final BatchReport.Changesets CHANGESETS =
      BatchReport.Changesets.newBuilder().setComponentRef(COMPONENT_REF).build();
  private static final BatchReport.Measure MEASURE = BatchReport.Measure.newBuilder().build();
  private static final BatchReport.Component COMPONENT =
      BatchReport.Component.newBuilder().setRef(COMPONENT_REF).build();
  private static final BatchReport.Issue ISSUE = BatchReport.Issue.newBuilder().build();
  private static final BatchReport.Duplication DUPLICATION =
      BatchReport.Duplication.newBuilder().build();
  private static final BatchReport.CpdTextBlock DUPLICATION_BLOCK =
      BatchReport.CpdTextBlock.newBuilder().build();
  private static final BatchReport.Symbol SYMBOL = BatchReport.Symbol.newBuilder().build();
  private static final BatchReport.SyntaxHighlighting SYNTAX_HIGHLIGHTING_1 =
      BatchReport.SyntaxHighlighting.newBuilder().build();
  private static final BatchReport.SyntaxHighlighting SYNTAX_HIGHLIGHTING_2 =
      BatchReport.SyntaxHighlighting.newBuilder().build();
  private static final BatchReport.Coverage COVERAGE_1 = BatchReport.Coverage.newBuilder().build();
  private static final BatchReport.Coverage COVERAGE_2 = BatchReport.Coverage.newBuilder().build();
  private static final BatchReport.Test TEST_1 = BatchReport.Test.newBuilder().setName("1").build();
  private static final BatchReport.Test TEST_2 = BatchReport.Test.newBuilder().setName("2").build();
  private static final BatchReport.CoverageDetail COVERAGE_DETAIL_1 =
      BatchReport.CoverageDetail.newBuilder().setTestName("1").build();
  private static final BatchReport.CoverageDetail COVERAGE_DETAIL_2 =
      BatchReport.CoverageDetail.newBuilder().setTestName("2").build();

  @Rule public JUnitTempFolder tempFolder = new JUnitTempFolder();

  private BatchReportWriter writer;
  private BatchReportReaderImpl underTest;

  @Before
  public void setUp() {
    BatchReportDirectoryHolder holder =
        new ImmutableBatchReportDirectoryHolder(tempFolder.newDir());
    underTest = new BatchReportReaderImpl(holder);
    writer = new BatchReportWriter(holder.getDirectory());
  }

  @Test(expected = IllegalStateException.class)
  public void readMetadata_throws_ISE_if_no_metadata() {
    underTest.readMetadata();
  }

  @Test
  public void readMetadata_result_is_cached() {
    BatchReport.Metadata metadata = BatchReport.Metadata.newBuilder().build();

    writer.writeMetadata(metadata);

    BatchReport.Metadata res = underTest.readMetadata();
    assertThat(res).isEqualTo(metadata);
    assertThat(underTest.readMetadata()).isSameAs(res);
  }

  @Test
  public void readScannerLogs() throws IOException {
    File scannerLogFile = writer.getFileStructure().analysisLog();
    FileUtils.write(scannerLogFile, "log1\nlog2");

    CloseableIterator<String> logs = underTest.readScannerLogs();
    assertThat(logs).containsExactly("log1", "log2");
  }

  @Test
  public void readScannerLogs_no_logs() {
    CloseableIterator<String> logs = underTest.readScannerLogs();
    assertThat(logs.hasNext()).isFalse();
  }

  @Test
  public void readComponentMeasures_returns_empty_list_if_there_is_no_measure() {
    assertThat(underTest.readComponentMeasures(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readComponentMeasures_returns_measures() {
    writer.writeComponentMeasures(COMPONENT_REF, of(MEASURE));

    try (CloseableIterator<BatchReport.Measure> measures =
        underTest.readComponentMeasures(COMPONENT_REF)) {
      assertThat(measures.next()).isEqualTo(MEASURE);
      assertThat(measures.hasNext()).isFalse();
    }
  }

  @Test
  public void readComponentMeasures_is_not_cached() {
    writer.writeComponentMeasures(COMPONENT_REF, of(MEASURE));

    assertThat(underTest.readComponentMeasures(COMPONENT_REF))
        .isNotSameAs(underTest.readComponentMeasures(COMPONENT_REF));
  }

  @Test
  public void readChangesets_returns_null_if_no_changeset() {
    assertThat(underTest.readChangesets(COMPONENT_REF)).isNull();
  }

  @Test
  public void verify_readChangesets_returns_changesets() {
    writer.writeComponentChangesets(CHANGESETS);

    BatchReport.Changesets res = underTest.readChangesets(COMPONENT_REF);
    assertThat(res).isEqualTo(CHANGESETS);
  }

  @Test
  public void readChangesets_is_not_cached() {
    writer.writeComponentChangesets(CHANGESETS);

    assertThat(underTest.readChangesets(COMPONENT_REF))
        .isNotSameAs(underTest.readChangesets(COMPONENT_REF));
  }

  @Test(expected = IllegalStateException.class)
  public void readComponent_throws_ISE_if_file_does_not_exist() {
    underTest.readComponent(COMPONENT_REF);
  }

  @Test
  public void verify_readComponent_returns_Component() {
    writer.writeComponent(COMPONENT);

    assertThat(underTest.readComponent(COMPONENT_REF)).isEqualTo(COMPONENT);
  }

  @Test
  public void readComponent_is_not_cached() {
    writer.writeComponent(COMPONENT);

    assertThat(underTest.readComponent(COMPONENT_REF))
        .isNotSameAs(underTest.readComponent(COMPONENT_REF));
  }

  @Test
  public void readComponentIssues_returns_empty_list_if_file_does_not_exist() {
    assertThat(underTest.readComponentIssues(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readComponentIssues_returns_Issues() {
    writer.writeComponentIssues(COMPONENT_REF, of(ISSUE));

    try (CloseableIterator<BatchReport.Issue> res = underTest.readComponentIssues(COMPONENT_REF)) {
      assertThat(res.next()).isEqualTo(ISSUE);
      assertThat(res.hasNext()).isFalse();
    }
  }

  @Test
  public void readComponentIssues_it_not_cached() {
    writer.writeComponentIssues(COMPONENT_REF, of(ISSUE));

    assertThat(underTest.readComponentIssues(COMPONENT_REF))
        .isNotSameAs(underTest.readComponentIssues(COMPONENT_REF));
  }

  @Test
  public void readComponentDuplications_returns_empty_list_if_file_does_not_exist() {
    assertThat(underTest.readComponentDuplications(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readComponentDuplications_returns_Issues() {
    writer.writeComponentDuplications(COMPONENT_REF, of(DUPLICATION));

    try (CloseableIterator<BatchReport.Duplication> res =
        underTest.readComponentDuplications(COMPONENT_REF)) {
      assertThat(res.next()).isEqualTo(DUPLICATION);
      assertThat(res.hasNext()).isFalse();
    }
  }

  @Test
  public void readComponentDuplications_it_not_cached() {
    writer.writeComponentDuplications(COMPONENT_REF, of(DUPLICATION));

    assertThat(underTest.readComponentDuplications(COMPONENT_REF))
        .isNotSameAs(underTest.readComponentDuplications(COMPONENT_REF));
  }

  @Test
  public void readComponentDuplicationBlocks_returns_empty_list_if_file_does_not_exist() {
    assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readComponentDuplicationBlocks_returns_Issues() {
    writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));

    try (CloseableIterator<BatchReport.CpdTextBlock> res =
        underTest.readCpdTextBlocks(COMPONENT_REF)) {
      assertThat(res.next()).isEqualTo(DUPLICATION_BLOCK);
      assertThat(res.hasNext()).isFalse();
    }
  }

  @Test
  public void readComponentDuplicationBlocks_is_not_cached() {
    writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));

    assertThat(underTest.readCpdTextBlocks(COMPONENT_REF))
        .isNotSameAs(underTest.readCpdTextBlocks(COMPONENT_REF));
  }

  @Test
  public void readComponentSymbols_returns_empty_list_if_file_does_not_exist() {
    assertThat(underTest.readComponentSymbols(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readComponentSymbols_returns_Issues() {
    writer.writeComponentSymbols(COMPONENT_REF, of(SYMBOL));

    try (CloseableIterator<BatchReport.Symbol> res =
        underTest.readComponentSymbols(COMPONENT_REF)) {
      assertThat(res.next()).isEqualTo(SYMBOL);
      assertThat(res.hasNext()).isFalse();
    }
  }

  @Test
  public void readComponentSymbols_it_not_cached() {
    writer.writeComponentSymbols(COMPONENT_REF, of(SYMBOL));

    assertThat(underTest.readComponentSymbols(COMPONENT_REF))
        .isNotSameAs(underTest.readComponentSymbols(COMPONENT_REF));
  }

  @Test
  public void
      readComponentSyntaxHighlighting_returns_empty_CloseableIterator_when_file_does_not_exist() {
    assertThat(underTest.readComponentSyntaxHighlighting(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readComponentSyntaxHighlighting() {
    writer.writeComponentSyntaxHighlighting(
        COMPONENT_REF, of(SYNTAX_HIGHLIGHTING_1, SYNTAX_HIGHLIGHTING_2));

    CloseableIterator<BatchReport.SyntaxHighlighting> res =
        underTest.readComponentSyntaxHighlighting(COMPONENT_REF);
    assertThat(res).containsExactly(SYNTAX_HIGHLIGHTING_1, SYNTAX_HIGHLIGHTING_2);
    res.close();
  }

  @Test
  public void readComponentCoverage_returns_empty_CloseableIterator_when_file_does_not_exist() {
    assertThat(underTest.readComponentCoverage(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readComponentCoverage() {
    writer.writeComponentCoverage(COMPONENT_REF, of(COVERAGE_1, COVERAGE_2));

    CloseableIterator<BatchReport.Coverage> res = underTest.readComponentCoverage(COMPONENT_REF);
    assertThat(res).containsExactly(COVERAGE_1, COVERAGE_2);
    res.close();
  }

  @Test
  public void readFileSource_returns_absent_optional_when_file_does_not_exist() {
    assertThat(underTest.readFileSource(COMPONENT_REF)).isAbsent();
  }

  @Test
  public void verify_readFileSource() throws IOException {
    File file = writer.getSourceFile(COMPONENT_REF);
    FileUtils.writeLines(file, of("1", "2", "3"));

    CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF).get();
    assertThat(res).containsExactly("1", "2", "3");
    res.close();
  }

  @Test
  public void readTests_returns_empty_CloseableIterator_when_file_does_not_exist() {
    assertThat(underTest.readTests(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readTests() {
    writer.writeTests(COMPONENT_REF, of(TEST_1, TEST_2));

    CloseableIterator<BatchReport.Test> res = underTest.readTests(COMPONENT_REF);
    assertThat(res).containsExactly(TEST_1, TEST_2);
    res.close();
  }

  @Test
  public void readCoverageDetails_returns_empty_CloseableIterator_when_file_does_not_exist() {
    assertThat(underTest.readCoverageDetails(COMPONENT_REF)).isEmpty();
  }

  @Test
  public void verify_readCoverageDetails() {
    writer.writeCoverageDetails(COMPONENT_REF, of(COVERAGE_DETAIL_1, COVERAGE_DETAIL_2));

    CloseableIterator<BatchReport.CoverageDetail> res =
        underTest.readCoverageDetails(COMPONENT_REF);
    assertThat(res).containsExactly(COVERAGE_DETAIL_1, COVERAGE_DETAIL_2);
    res.close();
  }
}