PermissionPredicate implements PrintablePredicate<REQ> { @Override public boolean test(REQ req) { return Stream.of(req) .filter(Objects::nonNull) .map(PrincipalAvailable.class::cast) .map(PrincipalAvailable::getPrincipal) .filter(UserPrincipal.class::isInstance) .map(UserPrincipal.class::cast) .anyMatch(up -> up.hasPermission(permission)); } PermissionPredicate(String permission); @Override boolean test(REQ req); @Override String toString();  }
@Test public void test() { PermissionPredicate<PrincipalRequest> pred = new PermissionPredicate<>("deleteRepository"); PrincipalRequest req = new PrincipalRequest(); TestUser user = new TestUser("kawasima", new HashSet<>(Arrays.asList("deleteRepository", "createRepository"))); req.setPrincipal(user); assertThat(pred.test(req)).isTrue(); TestUser user2 = new TestUser("kawasima", new HashSet<>(Collections.singletonList("readRepository"))); req.setPrincipal(user2); assertThat(pred.test(req)).isFalse(); }