WriteToJcrTask implements Callable<Serializable> { @Override public Serializable call() throws Exception { try { final IUnifiedRepository repo = PentahoSystem.get( IUnifiedRepository.class ); final org.pentaho.reporting.libraries.base.util.IOUtils utils = org.pentaho.reporting.libraries .base.util.IOUtils.getInstance(); final ISchedulingDirectoryStrategy directoryStrategy = PentahoSystem.get( ISchedulingDirectoryStrategy.class ); final RepositoryFile outputFolder = directoryStrategy.getSchedulingDir( repo ); final ReportContentRepository repository = getReportContentRepository( outputFolder ); final ContentLocation dataLocation = repository.getRoot(); final IAsyncReportState state = parentTask.getState(); final String extension = MimeHelper.getExtension( state.getMimeType() ); final String targetExt = extension != null ? extension : TXT; final String fullPath = state.getPath(); String cleanFileName = utils.stripFileExtension( utils.getFileName( fullPath ) ); if ( StringUtil.isEmpty( cleanFileName ) ) { cleanFileName = DEFAULT_NAME; } String targetName = cleanFileName + targetExt; int copy = 1; final OutputStream outputStream; synchronized ( FORMAT ) { while ( dataLocation.exists( targetName ) ) { targetName = String.format( FORMAT, cleanFileName, copy, targetExt ); copy++; } outputStream = dataLocation.createItem( targetName ).getOutputStream(); } if ( outputStream != null ) { try { IOUtils.copy( inputStream, outputStream ); outputStream.flush(); final RepositoryFile targetFile = repo.getFile( outputFolder.getPath() + "/" + targetName ); return targetFile.getId(); } finally { IOUtils.closeQuietly( outputStream ); } } else { throw new IOException( CANT_CREATE_FILE_IN_JCR ); } } catch ( final Exception e ) { log.error( CANT_PERSIST_MSG, e ); } finally { IOUtils.closeQuietly( inputStream ); } return null; } WriteToJcrTask(
    final IAsyncReportExecution<? extends IAsyncReportState> parentTask,
    final InputStream inputStream ); @Override Serializable call();  }
@Test public void testPositiveScenario() 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 ); 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" ) ); }
@Test public void testNoNames() 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/content.txt" ) ).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( null ); when( state.getPath() ).thenReturn( "/" ); 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; } }; assertEquals( uuid.toString(), toJcrTask.call() ); assertTrue( fakeLocation.exists( "content.txt" ) ); }
@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" ) ); }
@Test public void testFail() throws Exception { 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 InputStream inputStream = mock( InputStream.class ); when( inputStream.read( any() ) ).thenThrow( new IOException( "Test" ) ); 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; } }; assertNull( toJcrTask.call() ); }
@Test public void testNullStream() throws Exception { 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 InputStream inputStream = mock( InputStream.class ); when( inputStream.read( any() ) ).thenThrow( new IOException( "Test" ) ); when( content.getStream() ).thenReturn( inputStream ); final ReportContentRepository contentRepository = mock( ReportContentRepository.class ); final ContentLocation contentLocation = mock( ContentLocation.class ); final ContentItem contentItem = mock( ContentItem.class ); when( contentItem.getOutputStream() ).thenReturn( null ); when( contentLocation.createItem( any() ) ).thenReturn( contentItem ); when( contentRepository.getRoot() ).thenReturn( contentLocation ); final WriteToJcrTask toJcrTask = new WriteToJcrTask( reportExecution, inputStream ) { @Override protected ReportContentRepository getReportContentRepository( final RepositoryFile outputFolder ) { return contentRepository; } }; assertNull( toJcrTask.call() ); }