PortfolioPermissionChecker { public PortfolioPermission permissionCheck(PortfolioNode node) { return _permissions.containsKey(node) ? _permissions.get(node) : DENY; } PortfolioPermissionChecker(Portfolio portfolio, NodeChecker nodeChecker); PortfolioPermission permissionCheck(PortfolioNode node);  }
@Test public void testNonExistentNodeIsDenied() { SimplePortfolioNode node = new SimplePortfolioNode("not in the portfolio"); PortfolioPermissionChecker checker = new PortfolioPermissionChecker(new SimplePortfolio("test"), createAllowNodeChecker()); assertThat(checker.permissionCheck(node), is(DENY)); }
@Test public void testSingleNodeWithAllow() { SimplePortfolioNode node = new SimplePortfolioNode(); PortfolioPermissionChecker checker = new PortfolioPermissionChecker(new SimplePortfolio("test", node), createAllowNodeChecker()); assertThat(checker.permissionCheck(node), is(ALLOW)); }
@Test public void testSingleNodeWithDeny() { SimplePortfolioNode node = new SimplePortfolioNode(); PortfolioPermissionChecker checker = new PortfolioPermissionChecker(new SimplePortfolio("test", node), createDenyNodeChecker()); assertThat(checker.permissionCheck(node), is(DENY)); }
@Test public void testAllChildrenAllowMeansParentAllows() { SimplePortfolioNode root = nodeTree(1, nodeTree(2), nodeTree(3), nodeTree(4)); Map<Integer, PortfolioPermission> permissions = ImmutableMap.of( 1, ALLOW, 2, ALLOW, 3, ALLOW, 4, ALLOW); PortfolioPermissionChecker checker = new PortfolioPermissionChecker(new SimplePortfolio("test", root), createMappedNodeChecker(permissions)); assertThat(checker.permissionCheck(root), is(ALLOW)); }
@Test public void testAllChildrenDenyMeansParentPartials() { SimplePortfolioNode root = nodeTree(1, nodeTree(2), nodeTree(3), nodeTree(4)); Map<Integer, PortfolioPermission> permissions = ImmutableMap.of( 1, ALLOW, 2, DENY, 3, DENY, 4, DENY); PortfolioPermissionChecker checker = new PortfolioPermissionChecker(new SimplePortfolio("test", root), createMappedNodeChecker(permissions)); assertThat(checker.permissionCheck(root), is(PARTIAL)); }
@Test public void testChildrenAllowAndDenyMeansParentPartials() { SimplePortfolioNode root = nodeTree(1, nodeTree(2), nodeTree(3), nodeTree(4)); Map<Integer, PortfolioPermission> permissions = ImmutableMap.of( 1, ALLOW, 2, ALLOW, 3, DENY, 4, ALLOW); PortfolioPermissionChecker checker = new PortfolioPermissionChecker(new SimplePortfolio("test", root), createMappedNodeChecker(permissions)); assertThat(checker.permissionCheck(root), is(PARTIAL)); }
@Test public void testParentsDenialOverridesChildren() { SimplePortfolioNode root = nodeTree(1, nodeTree(2), nodeTree(3), nodeTree(4)); Map<Integer, PortfolioPermission> permissions = ImmutableMap.of( 1, DENY, 2, ALLOW, 3, ALLOW, 4, ALLOW); PortfolioPermissionChecker checker = new PortfolioPermissionChecker(new SimplePortfolio("test", root), createMappedNodeChecker(permissions)); assertThat(checker.permissionCheck(root), is(DENY)); }
@Test public void testMultilevelTree() { SimplePortfolioNode root = nodeTree(1, nodeTree(2, nodeTree(5), nodeTree(6)), nodeTree(3, nodeTree(7)), nodeTree(4, nodeTree(8), nodeTree(9))); List<PortfolioNode> childNodes = root.getChildNodes(); PortfolioNode node2 = childNodes.get(0); PortfolioNode node3 = childNodes.get(1); PortfolioNode node4 = childNodes.get(2); Map< Integer, PortfolioPermission> permissions = ImmutableMap.<Integer, PortfolioPermission>builder() .put(1, ALLOW) .put(2, ALLOW) .put(3, DENY) .put(4, ALLOW) .put(5, DENY) .put(6, ALLOW) .put(7, ALLOW) .put(8, ALLOW) .put(9, ALLOW) .build(); PortfolioPermissionChecker checker = new PortfolioPermissionChecker(new SimplePortfolio("test", root), createMappedNodeChecker(permissions)); assertThat(checker.permissionCheck(root), is(PARTIAL)); assertThat(checker.permissionCheck(node2), is(PARTIAL)); assertThat(checker.permissionCheck(node3), is(DENY)); assertThat(checker.permissionCheck(node4), is(ALLOW)); }