DefaultDeleteHelper implements IDeleteHelper { public List<RepositoryFile> getAllDeletedFiles( final Session session, final PentahoJcrConstants pentahoJcrConstants ) throws RepositoryException { if ( isAdmin() ) { List<RepositoryFile> deletedFiles = new ArrayList<>(); ITenant tenant = JcrTenantUtils.getTenant(); for ( String user : getUserList() ) { String homePath = getHomePath( tenant, user ); try { Node userHomeFolderNode = (Node) session.getItem( homePath ); if ( userHomeFolderNode.hasNode( FOLDER_NAME_TRASH ) ) { Node trashNode = userHomeFolderNode.getNode( FOLDER_NAME_TRASH ); List<RepositoryFile> deletedForUser = getDeletedFiles( session, pentahoJcrConstants, trashNode, user ); deletedFiles.addAll( deletedForUser ); } } catch ( PathNotFoundException e ) { logger.warn( MessageFormat.format( Messages.getInstance() .getString( "DefaultDeleteHelper.PATH_NOT_FOUND_EXCEPTION" ), homePath, user ), e ); } } Collections.sort( deletedFiles ); return deletedFiles; } return getDeletedFiles( session, pentahoJcrConstants ); } DefaultDeleteHelper( final ILockHelper lockHelper, final IPathConversionHelper pathConversionHelper ); void deleteFile( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                          final Serializable fileId ); List<RepositoryFile> getDeletedFiles( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                                               final String origParentFolderPath, final String filter ); List<RepositoryFile> getDeletedFiles( final Session session, final PentahoJcrConstants pentahoJcrConstants ); List<RepositoryFile> getAllDeletedFiles( final Session session,
      final PentahoJcrConstants pentahoJcrConstants ); void permanentlyDeleteFile( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                                     final Serializable fileId ); void undeleteFile( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                            final Serializable fileId ); String getOriginalParentFolderPath( final Session session, final PentahoJcrConstants pentahoJcrConstants,
                                             final Serializable fileId ); static final String JCR_ROOT_VERSION; }
@Test public void testGetAllDeletedFiles() throws Exception { final String path1 = "path1"; final Calendar date1 = Calendar.getInstance(); final Node deletedNode1 = createDeletedNode( path1, date1 ); final String path2 = "path2"; final Calendar date2 = Calendar.getInstance(); final Node deletedNode2 = createDeletedNode( path2, date2 ); final Node nodeTrash = mock( Node.class ); when( nodeTrash.getNodes() ).thenAnswer( invoc -> { final NodeIterator nodeIterator = mock( NodeIterator.class ); when( nodeIterator.hasNext() ).thenReturn( true, true, false ); when( nodeIterator.nextNode() ).thenReturn( deletedNode1, deletedNode2 ); return nodeIterator; } ); final Node nodeOtherFolder = mock( Node.class ); when( nodeOtherFolder.hasNode( anyString() ) ).thenReturn( true ); when( nodeOtherFolder.getNode( anyString() ) ).thenReturn( nodeTrash ); final String pathUsr = "pathUser"; final Calendar dateUsr = Calendar.getInstance(); final Node deletedNodeUsr = createDeletedNode( pathUsr, dateUsr ); final Node nodeTrashUsr = mock( Node.class ); when( nodeTrashUsr.getNodes() ).thenAnswer( invoc -> { NodeIterator nodeIteratorUsr = mock( NodeIterator.class ); when( nodeIteratorUsr.hasNext() ).thenReturn( true, false ); when( nodeIteratorUsr.nextNode() ).thenReturn( deletedNodeUsr ); return nodeIteratorUsr; } ); final Node nodeUserFolder = mock( Node.class ); when( nodeUserFolder.hasNode( anyString() ) ).thenReturn( true ); when( nodeUserFolder.getNode( anyString() ) ).thenReturn( nodeTrashUsr ); final boolean[] admin = { false }; defaultDeleteHelper = new DefaultDeleteHelper( lockHelper, pathConversionHelper ) { @Override protected boolean isAdmin() { return admin[0]; } @Override protected List<String> getUserList() { return Arrays.asList( "test", "other" ); } }; when( session.getItem( endsWith( "/other" ) ) ).thenReturn( nodeOtherFolder ); when( session.getItem( endsWith( "/test" ) ) ).thenReturn( nodeUserFolder ); final List<RepositoryFile> deletedFiles = defaultDeleteHelper.getAllDeletedFiles( session, pentahoJcrConstants ); assertNotNull( deletedFiles ); assertEquals( 1, deletedFiles.size() ); assertEquals( pathUsr, deletedFiles.get( 0 ).getOriginalParentFolderPath() ); admin[0] = true; final List<RepositoryFile> deletedFilesAdmin = defaultDeleteHelper.getAllDeletedFiles( session, pentahoJcrConstants ); assertNotNull( deletedFilesAdmin ); assertEquals( 3, deletedFilesAdmin.size() ); }
@Test public void testGetAllDeletedFiles_deletedUserHomeFolderNode() throws Exception { final String pathUser1 = "pathUser1"; final Calendar dateUser1 = Calendar.getInstance(); final Node deletedNodeUser1 = createDeletedNode( pathUser1, dateUser1 ); final Node nodeTrashUser1 = mock( Node.class ); when( nodeTrashUser1.getNodes() ).thenAnswer( invoc -> { NodeIterator nodeIteratorUser1 = mock( NodeIterator.class ); when( nodeIteratorUser1.hasNext() ).thenReturn( true, false ); when( nodeIteratorUser1.nextNode() ).thenReturn( deletedNodeUser1 ); return nodeIteratorUser1; } ); final Node nodeUser1 = mock( Node.class ); when( nodeUser1.hasNode( anyString() ) ).thenReturn( true ); when( nodeUser1.getNode( anyString() ) ).thenReturn( nodeTrashUser1 ); final String pathUser2 = "pathUser2"; final Calendar dateUser2 = Calendar.getInstance(); final Node deletedNodeUser2 = createDeletedNode( pathUser2, dateUser2 ); final Node nodeTrashUser2 = mock( Node.class ); when( nodeTrashUser2.getNodes() ).thenAnswer( invoc -> { NodeIterator nodeIteratorUser2 = mock( NodeIterator.class ); when( nodeIteratorUser2.hasNext() ).thenReturn( true, false ); when( nodeIteratorUser2.nextNode() ).thenReturn( deletedNodeUser2 ); return nodeIteratorUser2; } ); final Node nodeUser2 = mock( Node.class ); when( nodeUser2.hasNode( anyString() ) ).thenReturn( true ); when( nodeUser2.getNode( anyString() ) ).thenReturn( nodeTrashUser2 ); defaultDeleteHelper = new DefaultDeleteHelper( lockHelper, pathConversionHelper ) { @Override protected boolean isAdmin() { return true; } @Override protected List<String> getUserList() { return Arrays.asList( "user1", "userInvalidPath", "user2" ); } }; when( session.getItem( endsWith( "/userInvalidPath" ) ) ).thenThrow( new PathNotFoundException() ); when( session.getItem( endsWith( "/user2" ) ) ).thenReturn( nodeUser2 ); when( session.getItem( endsWith( "/user1" ) ) ).thenReturn( nodeUser1 ); final List<RepositoryFile> deletedFilesAdmin = defaultDeleteHelper.getAllDeletedFiles( session, pentahoJcrConstants ); assertNotNull( deletedFilesAdmin ); assertEquals( 2, deletedFilesAdmin.size() ); }