UpdateSchedulingLocationListener implements ISchedulingListener { @Override public void onSchedulingCompleted( final Serializable fileId ) { try { final IUnifiedRepository repo = PentahoSystem.get( IUnifiedRepository.class ); final RepositoryFile savedFile = repo.getFileById( fileId ); final RepositoryFile outputFolder = repo.getFileById( targetfolderId ); final org.pentaho.reporting.libraries.base.util.IOUtils utils = org.pentaho.reporting.libraries .base.util.IOUtils.getInstance(); if ( savedFile != null && !savedFile.isFolder() && outputFolder != null && outputFolder.isFolder() && !StringUtil .isEmpty( newName ) ) { final String fileExtension = utils.getFileExtension( savedFile.getName() ); final String folderPath = outputFolder.getPath() + "/"; String newPath = folderPath + newName + fileExtension; int count = 1; synchronized ( FORMAT ) { String uuidAsString = getUuidAsString(); while ( null != repo.getFile( newPath + uuidAsString ) ) { uuidAsString = getUuidAsString(); } repo.moveFile( savedFile.getId(), newPath + uuidAsString, MOVE_MSG ); while ( null != repo.getFile( newPath ) ) { newPath = String .format( FORMAT, folderPath + newName, count, fileExtension ); count++; } repo.moveFile( savedFile.getId(), newPath, MOVE_MSG ); } } } catch ( final Exception e ) { log.error( ERROR_MSG, e ); } } UpdateSchedulingLocationListener( final Serializable targetfolderId, final String newName ); @Override void onSchedulingCompleted( final Serializable fileId );  }
@Test public void onSchedulingCompleted() throws Exception { final UpdateSchedulingLocationListener listener = new UpdateSchedulingLocationListener( "/notexists", "test" ); listener.onSchedulingCompleted( "notexists.prpt" ); listener.onSchedulingCompleted( "null" ); verify( repository, times( 4 ) ).getFileById( any() ); verify( repository, times( 0 ) ).moveFile( any(), any(), any() ); final UpdateSchedulingLocationListener listener3 = new UpdateSchedulingLocationListener( "null", "test" ); listener3.onSchedulingCompleted( "notexists.prpt" ); listener3.onSchedulingCompleted( "null" ); listener3.onSchedulingCompleted( "test.prpt" ); verify( repository, times( 0 ) ).moveFile( any(), any(), any() ); final UpdateSchedulingLocationListener listener2 = new UpdateSchedulingLocationListener( "/test", "test" ); listener2.onSchedulingCompleted( "/test" ); listener2.onSchedulingCompleted( "null" ); verify( repository, times( 0 ) ).moveFile( any(), any(), any() ); listener2.onSchedulingCompleted( "test.prpt" ); verify( repository, times( 2 ) ).moveFile( any(), any(), any() ); }
@Test public void testNameClash() throws Exception { final RepositoryFile mock = mock( RepositoryFile.class ); when( repository.getFile( "null/1" ) ).thenReturn( mock ); final UpdateSchedulingLocationListener updateSchedulingLocationListener = new UpdateSchedulingLocationListener( "/test", "1" ); updateSchedulingLocationListener.onSchedulingCompleted( "test.prpt" ); verify( repository ).moveFile( null, "null/1(1)", "Moved to the location selected by user" ); }