UserPrincipal extends Principal { public List<Owner> getOwners() { List<Owner> owners = new LinkedList<>(); for (Permission permission : permissions) { Owner o = permission.getOwner(); if (o != null) { owners.add(o); } } return owners; } UserPrincipal(String username, Collection<Permission> permissions, boolean admin); String getUsername(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String getType(); @Override String getName(); @Override boolean hasFullAccess(); List<String> getOwnerIds(); List<String> getOwnerKeys(); List<Owner> getOwners(); @Override boolean canAccess(Object target, SubResource subResource, Access access);  }
@Test public void accessOwnerGivenPermission() { when(owner.getId()).thenReturn("1"); when(owner.getKey()).thenReturn("owner1"); when(ownerPerm.getOwner()).thenReturn(owner); List<Permission> ops = new ArrayList<>(); ops.add(ownerPerm); UserPrincipal up = new UserPrincipal("admin", ops, false); List<Owner> owners = up.getOwners(); assertNotNull(owners); assertSame(owner, owners.get(0)); }
@Test public void permsShouldNotAffectOwners() { List<Permission> perms = new ArrayList<>(); perms.add(mock(Permission.class)); UserPrincipal up = new UserPrincipal("admin", perms, false); assertTrue(up.getOwners().isEmpty()); }