NodeCheckingPortfolioFilter implements PortfolioFilter { @Override public Portfolio generateRestrictedPortfolio(Portfolio portfolio) { PortfolioPermissionChecker checker = new PortfolioPermissionChecker(portfolio, _nodeChecker); PortfolioNode rootNode = portfolio.getRootNode(); Optional<? extends PortfolioNode> newRoot = buildRestrictedRootNode(checker, rootNode); if (newRoot.isPresent()) { PortfolioNode node = newRoot.get(); return node.equals(rootNode) ? portfolio : createPortfolioForNode(trimParents(node)); } else { return new SimplePortfolio("Access Denied"); } } NodeCheckingPortfolioFilter(NodeChecker nodeChecker); @Override Portfolio generateRestrictedPortfolio(Portfolio portfolio);  }
@Test public void testSingleNodePortfolioIsUnalteredWhenAllNodesAreAllowed() { SimplePortfolioNode root = nodeTree(1); Portfolio pf = new SimplePortfolio("node-1", root); Portfolio portfolio = new NodeCheckingPortfolioFilter(createAllowNodeChecker()).generateRestrictedPortfolio(pf); assertThat(portfolio == pf, is(true)); }
@Test public void testMultiNodePortfolioIsUnalteredWhenAllNodesAreAllowed() { SimplePortfolioNode root = nodeTree(1, nodeTree(2, nodeTree(4)), nodeTree(3)); Portfolio pf = new SimplePortfolio("node-1", root); Portfolio portfolio = new NodeCheckingPortfolioFilter(createAllowNodeChecker()).generateRestrictedPortfolio(pf); assertThat(portfolio == pf, is(true)); }
@Test public void testEmptyPortfolioIsReturnedWhenAllNodesAreDenied() { SimplePortfolioNode root = nodeTree(1, nodeTree(2, nodeTree(4)), nodeTree(3)); Portfolio pf = new SimplePortfolio("node-1", root); Portfolio portfolio = new NodeCheckingPortfolioFilter(createDenyNodeChecker()).generateRestrictedPortfolio(pf); assertThat(portfolio.getRootNode().size(), is(0)); assertThat(portfolio.getName(), is("Access Denied")); }
@Test public void testInaccessibleNodesAreRemoved() { SimplePortfolioNode root = nodeTree(1, nodeTree(2), nodeTree(3), nodeTree(4)); Portfolio pf = new SimplePortfolio("node-1", root); Map<Integer, PortfolioPermission> permissions = ImmutableMap.of( 1, ALLOW, 2, ALLOW, 3, DENY, 4, ALLOW); Portfolio portfolio = new NodeCheckingPortfolioFilter(createMappedNodeChecker(permissions)).generateRestrictedPortfolio(pf); assertThat(portfolio.getName(), is("node-1 [restricted]")); PortfolioNode rootNode = portfolio.getRootNode(); assertThat(rootNode.getName(), is("node-1 [restricted]")); List<PortfolioNode> children = rootNode.getChildNodes(); assertThat(children.size(), is(2)); assertThat(children.get(0).getName(), is("node-2")); assertThat(children.get(1).getName(), is("node-4")); }
@Test public void testUnnecessaryParentIsRemoved() { SimplePortfolioNode root = nodeTree(1, nodeTree(2), nodeTree(3), nodeTree(4)); Portfolio pf = new SimplePortfolio("node-1", root); Map<Integer, PortfolioPermission> permissions = ImmutableMap.of( 1, ALLOW, 2, ALLOW, 3, DENY, 4, DENY); Portfolio portfolio = new NodeCheckingPortfolioFilter(createMappedNodeChecker(permissions)).generateRestrictedPortfolio(pf); assertThat(portfolio.getName(), is("node-2")); PortfolioNode rootNode = portfolio.getRootNode(); assertThat(rootNode.getName(), is("node-2")); assertThat(rootNode.getChildNodes().size(), is(0)); }
@Test public void testMultipleUnnecessaryParentsAreRemoved() { SimplePortfolioNode root = nodeTree(1, nodeTree(2, nodeTree(5, nodeTree(6), nodeTree(7))), nodeTree(3), nodeTree(4)); Portfolio pf = new SimplePortfolio("node-1", root); Map<Integer, PortfolioPermission> permissions = ImmutableMap.<Integer, PortfolioPermission>builder() .put(1, ALLOW) .put(2, ALLOW) .put(3, DENY) .put(4, DENY) .put(5, ALLOW) .put(6, ALLOW) .put(7, ALLOW) .build(); Portfolio portfolio = new NodeCheckingPortfolioFilter(createMappedNodeChecker(permissions)).generateRestrictedPortfolio(pf); assertThat(portfolio.getName(), is("node-5")); PortfolioNode rootNode = portfolio.getRootNode(); assertThat(rootNode.getName(), is("node-5")); List<PortfolioNode> children = rootNode.getChildNodes(); assertThat(children.size(), is(2)); assertThat(children.get(0).getName(), is("node-6")); assertThat(children.get(1).getName(), is("node-7")); }
@Test public void testDeepTreeIsProcessedCorrectly() { SimplePortfolioNode root = nodeTree(1, nodeTree(2, nodeTree(4, nodeTree(9), nodeTree(10)), nodeTree(5, nodeTree(11), nodeTree(12))), nodeTree(3, nodeTree(6, nodeTree(13), nodeTree(14)), nodeTree(7, nodeTree(15)), nodeTree(8, nodeTree(16), nodeTree(17)))); Portfolio pf = new SimplePortfolio("node-1", root); Map<Integer, PortfolioPermission> permissions = ImmutableMap.<Integer, PortfolioPermission> builder() .put(1, ALLOW) .put(2, ALLOW) .put(3, ALLOW) .put(4, ALLOW) .put(5, ALLOW) .put(6, ALLOW) .put(7, DENY) .put(8, ALLOW) .put(9, ALLOW) .put(10, ALLOW) .put(11, DENY) .put(12, ALLOW) .put(13, ALLOW) .put(14, ALLOW) .put(15, DENY) .put(16, DENY) .build(); Portfolio portfolio = new NodeCheckingPortfolioFilter(createMappedNodeChecker(permissions)).generateRestrictedPortfolio(pf); assertThat(portfolio.getName(), is("node-1 [restricted]")); PortfolioNode rootNode = portfolio.getRootNode(); assertThat(rootNode.getName(), is("node-1 [restricted]")); List<PortfolioNode> n1children = rootNode.getChildNodes(); assertThat(n1children.size(), is(2)); PortfolioNode n2 = n1children.get(0); assertThat(n2.getName(), is("node-2 [restricted]")); PortfolioNode n3 = n1children.get(1); assertThat(n3.getName(), is("node-3 [restricted]")); }