/** * Performs the set theoretic operation of relative complement of toRemove * {@link MailboxACL} in this {@link MailboxACL}. * * A schematic example: "user1:lr;user2:lrwt".except("user1:w;user2:t") * returns "user1:lr;user2:lrw". * * Implementations must return a new unmodifiable instance of * {@link MailboxACL}. However, implementations may decide to return this or * toRemove parameter value in case the result would be equal to the * respective one of those. * * Implementations must ensure that the result does not contain entries with * empty rigths. E.g. "user1:lr;user2:lrwt".except("user1:lr") should return * "user2:lrwt" rather than "user1:;user2:lrwt" */ public MailboxACL except(MailboxACL other) throws UnsupportedRightException { return new MailboxACL(entries.entrySet() .stream() .map(entry -> Pair.of( entry.getKey(), except(entry.getValue(), other.getEntries().get(entry.getKey())))) .filter(pair -> !pair.getValue().isEmpty()) .collect(Guavate.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue))); }
public MailboxACL except(EntryKey key, Rfc4314Rights mailboxACLRights) throws UnsupportedRightException { return except(new MailboxACL(new Entry(key, mailboxACLRights))); }
@Test public void testExceptEntryNew() throws UnsupportedRightException { /* actually no change expected */ Map<EntryKey, Rfc4314Rights> expectedEntries = new HashMap<>(u1u2g1g2ACL.getEntries()); MailboxACL result = u1u2g1g2ACL.except(MailboxACL.OWNER_KEY, MailboxACL.FULL_RIGHTS); Map<EntryKey, Rfc4314Rights> foundEntries = result.getEntries(); assertThat(expectedEntries).isEqualTo(foundEntries); }
@Test public void testExceptACLNew() throws UnsupportedRightException { /* actually no change expected */ Map<EntryKey, Rfc4314Rights> expectedEntries = new HashMap<>(u1u2g1g2ACL.getEntries()); MailboxACL toRemove = MailboxACL.OWNER_FULL_ACL; MailboxACL result = u1u2g1g2ACL.except(toRemove); Map<EntryKey, Rfc4314Rights> foundEntries = result.getEntries(); assertThat(expectedEntries).isEqualTo(foundEntries); }
@Test public void testExceptEntryFull() throws UnsupportedRightException { Map<EntryKey, Rfc4314Rights> expectedEntries = new HashMap<>(u1u2g1g2ACL.getEntries()); expectedEntries.remove(EntryKey.deserialize(USER_1)); MailboxACL result = u1u2g1g2ACL.except(EntryKey.deserialize(USER_1), MailboxACL.FULL_RIGHTS); Map<EntryKey, Rfc4314Rights> foundEntries = result.getEntries(); assertThat(foundEntries).isEqualTo(expectedEntries); }
/** * Apply the given ACL update on current ACL and return the result as a new ACL. * * @param aclUpdate Update to perform * @return Copy of current ACL updated * @throws UnsupportedRightException */ public MailboxACL apply(ACLCommand aclUpdate) throws UnsupportedRightException { switch (aclUpdate.getEditMode()) { case ADD: return union(aclUpdate.getEntryKey(), aclUpdate.getRights()); case REMOVE: return except(aclUpdate.getEntryKey(), aclUpdate.getRights()); case REPLACE: return replace(aclUpdate.getEntryKey(), aclUpdate.getRights()); } throw new RuntimeException("Unknown edit mode"); }
@Test public void testExceptEntryExisting() throws UnsupportedRightException { Map<EntryKey, Rfc4314Rights> expectedEntries = new HashMap<>(u1u2g1g2ACL.getEntries()); expectedEntries.put(EntryKey.deserialize(USER_1), Rfc4314Rights.fromSerializedRfc4314Rights(ik)); MailboxACL result = u1u2g1g2ACL.except(EntryKey.deserialize(USER_1), Rfc4314Rights.fromSerializedRfc4314Rights(ae)); Map<EntryKey, Rfc4314Rights> foundEntries = result.getEntries(); assertThat(foundEntries).isEqualTo(expectedEntries); }
@Test public void testExceptACLFull() throws UnsupportedRightException { Map<EntryKey, Rfc4314Rights> expectedEntries = new HashMap<>(u1u2g1g2ACL.getEntries()); expectedEntries.remove(EntryKey.deserialize(USER_1)); MailboxACL toRemove = new MailboxACL(new Entry(USER_1, MailboxACL.FULL_RIGHTS.serialize())); MailboxACL result = u1u2g1g2ACL.except(toRemove); Map<EntryKey, Rfc4314Rights> foundEntries = result.getEntries(); assertThat(foundEntries).isEqualTo(expectedEntries); }
@Test public void testExceptACLExisting() throws UnsupportedRightException { Map<EntryKey, Rfc4314Rights> expectedEntries = new HashMap<>(u1u2g1g2ACL.getEntries()); expectedEntries.put(EntryKey.deserialize(USER_1), Rfc4314Rights.fromSerializedRfc4314Rights(ik)); MailboxACL toRemove = new MailboxACL(new Entry(USER_1, ae)); MailboxACL result = u1u2g1g2ACL.except(toRemove); Map<EntryKey, Rfc4314Rights> foundEntries = result.getEntries(); assertThat(foundEntries).isEqualTo(expectedEntries); }