EntryPredicate { @NotNull static Predicate<PermissionEntry> createParent(@NotNull String treePath, @Nullable Tree parentTree, long permissions) { if (!Permissions.respectParentPermissions(permissions)) { return Predicates.alwaysFalse(); } if (treePath.isEmpty() || PathUtils.denotesRoot(treePath)) { return Predicates.alwaysFalse(); } else if (parentTree != null && parentTree.exists()) { return permissionEntry -> permissionEntry.appliesTo(parentTree.getPath()) && permissionEntry.matches(parentTree, null); } else { String parentPath = PathUtils.getParentPath(treePath); return permissionEntry -> permissionEntry.appliesTo(parentPath) && permissionEntry.matches(parentPath); } } private  EntryPredicate();   }
@Test public void testCreateParentPathReadPermission() { assertSame(Predicates.alwaysFalse(), EntryPredicate.createParent(tree.getPath(), null, Permissions.READ)); assertSame(Predicates.alwaysFalse(), EntryPredicate.createParent(tree.getPath(), mock(Tree.class), Permissions.READ)); }
@Test public void testCreateParentPathEmpty() { assertSame(Predicates.alwaysFalse(), EntryPredicate.createParent("", null, Permissions.ALL)); assertSame(Predicates.alwaysFalse(), EntryPredicate.createParent("", tree, Permissions.ALL)); }
@Test public void testCreateParentPathRoot() { assertSame(Predicates.alwaysFalse(), EntryPredicate.createParent(PathUtils.ROOT_PATH, tree, Permissions.ALL)); }
@Test public void testCreateParentPath() { when(pe.appliesTo(PARENT_PATH)).thenReturn(true); Predicate<PermissionEntry> predicate = EntryPredicate.createParent(TREE_PATH, null, Permissions.ALL); predicate.apply(pe); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, times(1)).appliesTo(PARENT_PATH); verify(pe, times(1)).matches(PARENT_PATH); verify(pe, never()).matches(any(Tree.class), any(PropertyState.class)); verify(pe, never()).getPrivilegeBits(); }
@Test public void testCreateParentPathTree() { when(pe.appliesTo(PARENT_PATH)).thenReturn(true); Tree parentTree = mockTree(PARENT_PATH, true); Predicate<PermissionEntry> predicate = EntryPredicate.createParent(TREE_PATH, parentTree, Permissions.ALL); predicate.apply(pe); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, times(1)).appliesTo(PARENT_PATH); verify(pe, times(1)).matches(parentTree, null); verify(pe, never()).matches(PARENT_PATH); verify(pe, never()).getPrivilegeBits(); }
@Test public void testCreateParentPathTreeNotExisting() { when(pe.appliesTo(PARENT_PATH)).thenReturn(true); Predicate<PermissionEntry> predicate = EntryPredicate.createParent(TREE_PATH, mockTree(PARENT_PATH, false), Permissions.ALL); predicate.apply(pe); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, times(1)).appliesTo(PARENT_PATH); verify(pe, times(1)).matches(PARENT_PATH); verify(pe, never()).matches(any(Tree.class), any(PropertyState.class)); verify(pe, never()).getPrivilegeBits(); }
@Test public void testCreateParentPathMismatch() { Predicate<PermissionEntry> predicate = EntryPredicate.createParent(TREE_PATH, null, Permissions.ALL); predicate.apply(pe); String parentPath = PathUtils.getParentPath(TREE_PATH); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, times(1)).appliesTo(parentPath); verify(pe, never()).matches(parentPath); verify(pe, never()).matches(any(Tree.class), any(PropertyState.class)); verify(pe, never()).getPrivilegeBits(); }
@Test public void testCreateParentPathTreeMismatch() { Tree parentTree = mockTree(PARENT_PATH, true); Predicate<PermissionEntry> predicate = EntryPredicate.createParent(TREE_PATH, parentTree, Permissions.ALL); predicate.apply(pe); String parentPath = PathUtils.getParentPath(TREE_PATH); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, times(1)).appliesTo(parentPath); verify(pe, never()).matches(parentPath); verify(pe, never()).matches(any(Tree.class), any(PropertyState.class)); verify(pe, never()).getPrivilegeBits(); }
@Test public void testCreateParentTreeReadPermission() { when(tree.exists()).thenReturn(true); assertSame(Predicates.alwaysFalse(), EntryPredicate.createParent(tree, Permissions.READ)); }
@Test public void testCreateParentTreeNotExisting() { when(pe.appliesTo(PARENT_PATH)).thenReturn(true); Predicate<PermissionEntry> predicate = EntryPredicate.createParent(tree, Permissions.ADD_NODE|Permissions.READ_NODE); predicate.apply(pe); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, times(1)).appliesTo(PARENT_PATH); verify(pe, times(1)).matches(PARENT_PATH); verify(pe, never()).matches(any(Tree.class), any(PropertyState.class)); verify(pe, never()).getPrivilegeBits(); }
@Test public void testCreateParentTreeNotExistingMismatch() { Predicate<PermissionEntry> predicate = EntryPredicate.createParent(tree, Permissions.ADD_NODE|Permissions.READ_NODE); predicate.apply(pe); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, times(1)).appliesTo(PARENT_PATH); verify(pe, never()).matches(PARENT_PATH); verify(pe, never()).matches(any(Tree.class), any(PropertyState.class)); verify(pe, never()).getPrivilegeBits(); }
@Test public void testCreateParentTreeRoot() { Tree rootTree = mockTree(PathUtils.ROOT_PATH, true); assertSame(Predicates.alwaysFalse(), EntryPredicate.createParent(rootTree, Permissions.REMOVE|Permissions.MODIFY_ACCESS_CONTROL)); }
@Test public void testCreateParentTree() { when(pe.appliesTo(PARENT_PATH)).thenReturn(true); Tree parentTree = mockTree(PARENT_PATH, true); when(tree.exists()).thenReturn(true); when(tree.getParent()).thenReturn(parentTree); Predicate<PermissionEntry> predicate = EntryPredicate.createParent(tree, Permissions.REMOVE_NODE|Permissions.READ_PROPERTY|Permissions.LOCK_MANAGEMENT); predicate.apply(pe); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, never()).matches(PARENT_PATH); verify(pe, times(1)).appliesTo(PARENT_PATH); verify(pe, times(1)).matches(parentTree, null); verify(pe, never()).matches(tree, null); verify(pe, never()).getPrivilegeBits(); }
@Test public void testCreateParentTreeMismatch() { Tree parentTree = mockTree(PARENT_PATH, true); when(tree.exists()).thenReturn(true); when(tree.getParent()).thenReturn(parentTree); Predicate<PermissionEntry> predicate = EntryPredicate.createParent(tree, Permissions.REMOVE_NODE|Permissions.READ_PROPERTY|Permissions.LOCK_MANAGEMENT); predicate.apply(pe); verify(pe, never()).matches(); verify(pe, never()).matches(TREE_PATH); verify(pe, never()).matches(PARENT_PATH); verify(pe, times(1)).appliesTo(PARENT_PATH); verify(pe, never()).matches(parentTree, null); verify(pe, never()).matches(tree, null); verify(pe, never()).getPrivilegeBits(); }