MAILBOX-310 ACLChanged should represent the diff between two ACLs
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/31925357 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/31925357 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/31925357 Branch: refs/heads/master Commit: 31925357c88f1dbaa51970ddbb93d2906f3804c3 Parents: 929f31d Author: benwa <btell...@linagora.com> Authored: Wed Oct 4 15:12:36 2017 +0700 Committer: Matthieu Baechler <matth...@apache.org> Committed: Thu Oct 5 09:48:53 2017 +0200 ---------------------------------------------------------------------- .../mailbox/acl/PositiveUserACLChanged.java | 90 +++++++++ .../mailbox/acl/PositiveUserACLChangedTest.java | 199 +++++++++++++++++++ 2 files changed, 289 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/31925357/mailbox/api/src/main/java/org/apache/james/mailbox/acl/PositiveUserACLChanged.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/acl/PositiveUserACLChanged.java b/mailbox/api/src/main/java/org/apache/james/mailbox/acl/PositiveUserACLChanged.java new file mode 100644 index 0000000..50948a4 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/acl/PositiveUserACLChanged.java @@ -0,0 +1,90 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.acl; + +import java.util.Map; +import java.util.Objects; +import java.util.stream.Stream; + +import org.apache.james.mailbox.model.MailboxACL; + +public class PositiveUserACLChanged { + private final MailboxACL oldACL; + private final MailboxACL newACL; + + public PositiveUserACLChanged(MailboxACL oldACL, MailboxACL newACL) { + this.oldACL = oldACL; + this.newACL = newACL; + } + + public MailboxACL getOldACL() { + return oldACL; + } + + public MailboxACL getNewACL() { + return newACL; + } + + public Stream<MailboxACL.Entry> addedEntries() { + Map<MailboxACL.EntryKey, MailboxACL.Rfc4314Rights> oldEntries = oldACL.ofPositiveNameType(MailboxACL.NameType.user); + + return newACL.ofPositiveNameType(MailboxACL.NameType.user) + .entrySet() + .stream() + .filter(entry -> !oldEntries.containsKey(entry.getKey())) + .map(entry -> new MailboxACL.Entry(entry.getKey(), entry.getValue())); + } + + public Stream<MailboxACL.Entry> removedEntries() { + Map<MailboxACL.EntryKey, MailboxACL.Rfc4314Rights> newEntries = newACL.ofPositiveNameType(MailboxACL.NameType.user); + + return oldACL.ofPositiveNameType(MailboxACL.NameType.user) + .entrySet() + .stream() + .filter(entry -> !newEntries.containsKey(entry.getKey())) + .map(entry -> new MailboxACL.Entry(entry.getKey(), entry.getValue())); + } + + public Stream<MailboxACL.Entry> changedEntries() { + Map<MailboxACL.EntryKey, MailboxACL.Rfc4314Rights> oldEntries = oldACL.ofPositiveNameType(MailboxACL.NameType.user); + + return newACL.ofPositiveNameType(MailboxACL.NameType.user) + .entrySet() + .stream() + .filter(entry -> oldEntries.containsKey(entry.getKey())) + .map(entry -> new MailboxACL.Entry(entry.getKey(), entry.getValue())); + } + + @Override + public final boolean equals(Object o) { + if (o instanceof PositiveUserACLChanged) { + PositiveUserACLChanged positiveUserAclChanged = (PositiveUserACLChanged) o; + + return Objects.equals(this.oldACL, positiveUserAclChanged.oldACL) + && Objects.equals(this.newACL, positiveUserAclChanged.newACL); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(oldACL, newACL); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/31925357/mailbox/api/src/test/java/org/apache/james/mailbox/acl/PositiveUserACLChangedTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/acl/PositiveUserACLChangedTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/acl/PositiveUserACLChangedTest.java new file mode 100644 index 0000000..dbf7f29 --- /dev/null +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/acl/PositiveUserACLChangedTest.java @@ -0,0 +1,199 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailbox.acl; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.mailbox.model.MailboxACL; +import org.apache.james.mailbox.model.MailboxACL.Entry; +import org.apache.james.mailbox.model.MailboxACL.EntryKey; +import org.apache.james.mailbox.model.MailboxACL.Rfc4314Rights; +import org.apache.james.mailbox.model.MailboxACL.Right; +import org.junit.Test; + +public class PositiveUserACLChangedTest { + private static final EntryKey ENTRY_KEY = EntryKey.createUserEntryKey("user"); + private static final Rfc4314Rights RIGHTS = new Rfc4314Rights(Right.Administer); + + @Test + public void addedEntriesShouldReturnEmptyWhenSameACL() { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY, + MailboxACL.EMPTY); + + assertThat(positiveUserAclChanged.addedEntries()).isEmpty(); + } + + @Test + public void removedEntriesShouldReturnEmptyWhenSameACL() { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY, + MailboxACL.EMPTY); + + assertThat(positiveUserAclChanged.removedEntries()).isEmpty(); + } + + @Test + public void changedEntriesShouldReturnEmptyWhenSameACL() { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY, + MailboxACL.EMPTY); + + assertThat(positiveUserAclChanged.changedEntries()).isEmpty(); + } + + @Test + public void addedEntriesShouldReturnNewEntryWhenAddedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY, + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(RIGHTS) + .asAddition())); + + assertThat(positiveUserAclChanged.addedEntries()) + .containsOnly(new Entry(ENTRY_KEY, RIGHTS)); + } + + @Test + public void changedEntriesShouldReturnEmptyWhenAddedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY, + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(RIGHTS) + .asAddition())); + + assertThat(positiveUserAclChanged.changedEntries()) + .isEmpty(); + } + + @Test + public void removedEntriesShouldReturnEmptyWhenAddedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY, + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(RIGHTS) + .asAddition())); + + assertThat(positiveUserAclChanged.removedEntries()) + .isEmpty(); + } + + @Test + public void addedEntriesShouldReturnEmptyWhenRemovedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(RIGHTS) + .asAddition()), + MailboxACL.EMPTY); + + assertThat(positiveUserAclChanged.addedEntries()) + .isEmpty(); + } + + @Test + public void changedEntriesShouldReturnEmptyWhenRemovedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(RIGHTS) + .asAddition()), + MailboxACL.EMPTY); + + assertThat(positiveUserAclChanged.changedEntries()) + .isEmpty(); + } + + @Test + public void removedEntriesShouldReturnEntryWhenRemovedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(RIGHTS) + .asAddition()), + MailboxACL.EMPTY); + + assertThat(positiveUserAclChanged.removedEntries()) + .containsOnly(new Entry(ENTRY_KEY, RIGHTS)); + } + + @Test + public void removedEntriesShouldReturnEmptyWhenChangedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(RIGHTS) + .asAddition()), + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(Right.Lookup) + .asAddition())); + + assertThat(positiveUserAclChanged.removedEntries()) + .isEmpty(); + } + + @Test + public void addedEntriesShouldReturnEmptyWhenChangedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(RIGHTS) + .asAddition()), + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(Right.Lookup) + .asAddition())); + + assertThat(positiveUserAclChanged.addedEntries()) + .isEmpty(); + } + + @Test + public void changedEntriesShouldReturnEntryWhenChangedEntry() throws Exception { + PositiveUserACLChanged positiveUserAclChanged = new PositiveUserACLChanged( + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(Right.Administer) + .asAddition()), + MailboxACL.EMPTY.apply( + MailboxACL.command() + .key(ENTRY_KEY) + .rights(Right.Lookup) + .asAddition())); + + assertThat(positiveUserAclChanged.changedEntries()) + .containsOnly(new Entry(ENTRY_KEY, new Rfc4314Rights(MailboxACL.Right.Lookup))); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org