@Test
public void testAlreadyExists() throws Exception {

  final UUID uuid = UUID.randomUUID();
  final RepositoryFile file = mock( RepositoryFile.class );
  when( file.getId() ).thenReturn( uuid.toString() );
  when( PentahoSystem.get( IUnifiedRepository.class ).getFile( "/test/report.pdf" ) ).thenReturn(
    file );
  when( PentahoSystem.get( IUnifiedRepository.class ).getFile( "/test/report(1).pdf" ) ).thenReturn(
    file );
  final FakeLocation fakeLocation = new FakeLocation();
  final IFixedSizeStreamingContent content = mock( IFixedSizeStreamingContent.class );
  final IAsyncReportExecution reportExecution = mock( IAsyncReportExecution.class );
  final IAsyncReportState state = mock( IAsyncReportState.class );
  when( state.getMimeType() ).thenReturn( "application/pdf" );
  when( state.getPath() ).thenReturn( "report.prpt" );
  when( reportExecution.getState() ).thenReturn( state );
  final NullInputStream inputStream = new NullInputStream( 100 );
  when( content.getStream() ).thenReturn( inputStream );
  final ReportContentRepository contentRepository = mock( ReportContentRepository.class );
  when( contentRepository.getRoot() ).thenReturn( fakeLocation );
  final WriteToJcrTask toJcrTask = new WriteToJcrTask( reportExecution, inputStream ) {
    @Override protected ReportContentRepository getReportContentRepository( final RepositoryFile outputFolder ) {
      return contentRepository;
    }
  };
  assertNotNull( toJcrTask.call() );
  assertTrue( fakeLocation.exists( "report.pdf" ) );
  assertNotNull( toJcrTask.call() );
  assertTrue( fakeLocation.exists( "report.pdf" ) );
  assertTrue( fakeLocation.exists( "report(1).pdf" ) );
}