You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/02/14 06:02:07 UTC

[james-project] 02/05: JAMES-3056 MailboxSoftlyAssert implementation

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 9feb468f517e41564a0e9ac5371f1e17e24f736d
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Wed Feb 12 17:42:20 2020 +0700

    JAMES-3056 MailboxSoftlyAssert implementation
    
    To be used in the cassandra mailbox inconsistency test
---
 .../james/mailbox/model/MailboxSoftlyAssert.java   |  72 ++++++++
 .../mailbox/model/MailboxSoftlyAssertTest.java     | 189 +++++++++++++++++++++
 .../org/apache/james/mailbox/model/TestId.java     |   8 +
 3 files changed, 269 insertions(+)

diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxSoftlyAssert.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxSoftlyAssert.java
new file mode 100644
index 0000000..e4986b7
--- /dev/null
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxSoftlyAssert.java
@@ -0,0 +1,72 @@
+/****************************************************************
+ * 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.model;
+
+import org.assertj.core.api.SoftAssertions;
+
+import com.google.common.base.Preconditions;
+
+public class MailboxSoftlyAssert {
+
+    @FunctionalInterface
+    public interface RequireActualMailbox {
+        MailboxAssertingStage assertThat(Mailbox actual);
+    }
+
+    public static class MailboxAssertingStage {
+        private final SoftAssertions softly;
+        private final Mailbox actual;
+
+        MailboxAssertingStage(SoftAssertions softly, Mailbox actual) {
+            Preconditions.checkNotNull(softly);
+            Preconditions.checkNotNull(actual);
+
+            this.softly = softly;
+            this.actual = actual;
+        }
+
+        public void isEqualTo(Mailbox expected) {
+            Preconditions.checkNotNull(expected);
+
+            softly.assertThat(actual.getMailboxId())
+                .withFailMessage("Expected MailboxId to be <%s> but was <%s>", expected.getMailboxId(), actual.getMailboxId())
+                .isEqualTo(expected.getMailboxId());
+            softly.assertThat(actual.getName())
+                .withFailMessage("Expected Name to be <%s> but was <%s>", expected.getName(), actual.getName())
+                .isEqualTo(expected.getName());
+            softly.assertThat(actual.getUidValidity())
+                .withFailMessage("Expected UID Validity to be <%s> but was <%s>", expected.getUidValidity(), actual.getUidValidity())
+                .isEqualTo(expected.getUidValidity());
+            softly.assertThat(actual.getUser())
+                .withFailMessage("Expected User to be <%s> but was <%s>", expected.getUser(), actual.getUser())
+                .isEqualTo(expected.getUser());
+            softly.assertThat(actual.getNamespace())
+                .withFailMessage("Expected NameSpace to be <%s> but was <%s>", expected.getNamespace(), actual.getNamespace())
+                .isEqualTo(expected.getNamespace());
+            softly.assertThat(actual.getACL())
+                .withFailMessage("Expected ACL to be <%s> but was <%s>", expected.getACL(), actual.getACL())
+                .isEqualTo(expected.getACL());
+        }
+    }
+
+    public static RequireActualMailbox softly(SoftAssertions softly) {
+        return actual -> new MailboxAssertingStage(softly, actual);
+    }
+}
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxSoftlyAssertTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxSoftlyAssertTest.java
new file mode 100644
index 0000000..d91bbcc
--- /dev/null
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxSoftlyAssertTest.java
@@ -0,0 +1,189 @@
+/****************************************************************
+ * 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.model;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.james.core.Username;
+import org.assertj.core.api.SoftAssertions;
+import org.junit.jupiter.api.Test;
+
+class MailboxSoftlyAssertTest {
+
+    private static final Username USER = Username.of("user");
+    private static final Username USER1 = Username.of("user1");
+
+    private static final long UID_VALIDITY = 42;
+    private static final TestId MAILBOX_ID = TestId.of(24);
+
+    @Test
+    void isEqualToShouldNotFailWithEqualMailbox() {
+        Mailbox mailbox1 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        Mailbox mailbox2 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+
+        SoftAssertions.assertSoftly(softly -> {
+            MailboxSoftlyAssert.softly(softly)
+                .assertThat(mailbox1)
+                .isEqualTo(mailbox2);
+        });
+    }
+
+    @Test
+    void isEqualToShouldFailWithNotEqualNamespace() {
+        Mailbox mailbox1 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        Mailbox mailbox2 = new Mailbox(new MailboxPath("other_namespace", USER, "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+
+        assertThatThrownBy(() -> {
+                SoftAssertions.assertSoftly(softly -> {
+                    MailboxSoftlyAssert.softly(softly)
+                        .assertThat(mailbox1)
+                        .isEqualTo(mailbox2);
+                });
+            })
+            .isInstanceOf(AssertionError.class)
+            .hasMessageContaining("Expected NameSpace to be <other_namespace> but was <#private>");
+    }
+
+    @Test
+    void isEqualToShouldFailWithNotEqualName() {
+        Mailbox mailbox1 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        Mailbox mailbox2 = new Mailbox(MailboxPath.forUser(USER, "other_name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+
+        assertThatThrownBy(() -> {
+                SoftAssertions.assertSoftly(softly -> {
+                    MailboxSoftlyAssert.softly(softly)
+                        .assertThat(mailbox1)
+                        .isEqualTo(mailbox2);
+                });
+            })
+            .isInstanceOf(AssertionError.class)
+            .hasMessageContaining("Expected Name to be <other_name> but was <name>");
+    }
+
+    @Test
+    void isEqualToShouldFailWithNotEqualId() {
+        Mailbox mailbox1 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        Mailbox mailbox2 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(TestId.of(MAILBOX_ID.id + 1));
+
+        assertThatThrownBy(() -> {
+                SoftAssertions.assertSoftly(softly -> {
+                    MailboxSoftlyAssert.softly(softly)
+                        .assertThat(mailbox1)
+                        .isEqualTo(mailbox2);
+                });
+            })
+            .isInstanceOf(AssertionError.class)
+            .hasMessageContaining("Expected MailboxId to be <TestId{id=25}> but was <TestId{id=24}>");
+    }
+
+    @Test
+    void isEqualToShouldFailWithNotEqualUidValidity() {
+        Mailbox mailbox1 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        Mailbox mailbox2 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY + 1);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+
+        assertThatThrownBy(() -> {
+                SoftAssertions.assertSoftly(softly -> {
+                    MailboxSoftlyAssert.softly(softly)
+                        .assertThat(mailbox1)
+                        .isEqualTo(mailbox2);
+                });
+            })
+            .isInstanceOf(AssertionError.class)
+            .hasMessageContaining("Expected UID Validity to be <43> but was <42>");
+    }
+
+    @Test
+    void isEqualToShouldFailWithNotSameSizeEntries() throws Exception {
+        Mailbox mailbox1 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        Mailbox mailbox2 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+
+        mailbox1.setACL(new MailboxACL(
+            new MailboxACL.Entry(USER.asString(), MailboxACL.Right.Write)));
+        mailbox2.setACL(new MailboxACL(
+            new MailboxACL.Entry(USER.asString(), MailboxACL.Right.Write),
+            new MailboxACL.Entry(USER1.asString(), MailboxACL.Right.Read)));
+
+        assertThatThrownBy(() -> {
+                SoftAssertions.assertSoftly(softly -> {
+                    MailboxSoftlyAssert.softly(softly)
+                        .assertThat(mailbox1)
+                        .isEqualTo(mailbox2);
+                });
+            })
+            .isInstanceOf(AssertionError.class)
+            .hasMessageContaining("Expected ACL to be <MailboxACL{entries={user=w, user1=r}}> but was <MailboxACL{entries={user=w}}");
+    }
+
+    @Test
+    void isEqualToShouldFailWithSameSizeButDifferentEntries() throws Exception {
+        Mailbox mailbox1 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        Mailbox mailbox2 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+
+        mailbox1.setACL(new MailboxACL(
+            new MailboxACL.Entry(USER.asString(), MailboxACL.Right.Write)));
+        mailbox2.setACL(new MailboxACL(
+            new MailboxACL.Entry(USER1.asString(), MailboxACL.Right.Read)));
+
+        assertThatThrownBy(() -> {
+                SoftAssertions.assertSoftly(softly -> {
+                    MailboxSoftlyAssert.softly(softly)
+                        .assertThat(mailbox1)
+                        .isEqualTo(mailbox2);
+                });
+            })
+            .isInstanceOf(AssertionError.class)
+            .hasMessageContaining("Expected ACL to be <MailboxACL{entries={user1=r}}> but was <MailboxACL{entries={user=w}}");
+    }
+
+    @Test
+    void isEqualToShouldPassWithSameSizeEntriesButDifferentOrder() throws Exception {
+        Mailbox mailbox1 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        Mailbox mailbox2 = new Mailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY);
+        mailbox1.setMailboxId(MAILBOX_ID);
+        mailbox2.setMailboxId(MAILBOX_ID);
+
+        mailbox1.setACL(new MailboxACL(
+            new MailboxACL.Entry(USER1.asString(), MailboxACL.Right.Read),
+            new MailboxACL.Entry(USER.asString(), MailboxACL.Right.Write)));
+        mailbox2.setACL(new MailboxACL(
+            new MailboxACL.Entry(USER.asString(), MailboxACL.Right.Write),
+            new MailboxACL.Entry(USER1.asString(), MailboxACL.Right.Read)));
+
+        SoftAssertions.assertSoftly(softly -> {
+            MailboxSoftlyAssert.softly(softly)
+                .assertThat(mailbox1)
+                .isEqualTo(mailbox2);
+        });
+    }
+}
diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/TestId.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/TestId.java
index f0e3875..d622d63 100644
--- a/mailbox/api/src/test/java/org/apache/james/mailbox/model/TestId.java
+++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/TestId.java
@@ -20,6 +20,8 @@ package org.apache.james.mailbox.model;
 
 import java.io.Serializable;
 
+import com.google.common.base.MoreObjects;
+
 public class TestId implements MailboxId, Serializable {
 
     public static class Factory implements MailboxId.Factory {
@@ -56,4 +58,10 @@ public class TestId implements MailboxId, Serializable {
         return id.hashCode();
     }
 
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+            .add("id", id)
+            .toString();
+    }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org