@Test public void testCreateLinkNode() throws AccessDeniedException, RepositoryException { Node fileNode = createFileNode(); Session session = mock(Session.class); Session adminSession = mock(Session.class); SlingRepository slingRepository = mock(SlingRepository.class); String linkPath = "/path/to/link"; String sitePath = "/path/to/site"; when(session.getUserID()).thenReturn("alice"); when(fileNode.getSession()).thenReturn(session); NodeType[] nodeTypes = new NodeType[0]; when(fileNode.getMixinNodeTypes()).thenReturn(nodeTypes); when(session.getItem(fileNode.getPath())).thenReturn(fileNode); when(adminSession.getItem(fileNode.getPath())).thenReturn(fileNode); when(slingRepository.loginAdministrative(null)).thenReturn(adminSession); when(adminSession.hasPendingChanges()).thenReturn(true); when(session.hasPendingChanges()).thenReturn(true); // link Node linkNode = mock(Node.class); when(session.itemExists(linkPath)).thenReturn(true); when(session.getItem(linkPath)).thenReturn(linkNode); FileUtils.createLink(fileNode, linkPath, null, slingRepository); verify(fileNode).addMixin(FilesConstants.REQUIRED_MIXIN); verify(session).save(); verify(adminSession).save(); verify(adminSession).logout(); }
private Node copyFile( String zipEntryName, String fileName, String sitePath, String contentType, Session session, ZipFile zip) { final String id = uniqueId(); final String path = FileUtils.getHashedPath(FilesConstants.USER_FILESTORE, id); Node node = null; try { final InputStream in = zip.getInputStream(zip.getEntry(zipEntryName)); node = FileUtils.saveFile(session, path, id, in, fileName, contentType, slingRepository); final String linkPath = sitePath + "/_files/" + fileName; FileUtils.createLink(session, node, linkPath, sitePath, slingRepository); } catch (RepositoryException e) { throw new Error(e); } catch (IOException e) { throw new Error(e); } return node; }