james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [james-project] 04/05: JAMES-2694 add unit tests to enforce orders of entries in zip file
Date Mon, 15 Apr 2019 07:42:21 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 5db11dfc1ae84a68148e02bb7e161f48b1146dbf
Author: RĂ©mi Kowalski <rkowalski@linagora.com>
AuthorDate: Wed Apr 3 16:17:09 2019 +0200

    JAMES-2694 add unit tests to enforce orders of entries in zip file
---
 .../org/apache/james/mailbox/backup/ZipAssert.java | 48 ++++++++++++---
 .../apache/james/mailbox/backup/ZipAssertTest.java | 70 +++++++++++++++++++---
 .../apache/james/mailbox/backup/ZipperTest.java    | 28 +++++++++
 3 files changed, 129 insertions(+), 17 deletions(-)

diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java
index e2842b8..c69326d 100644
--- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java
+++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java
@@ -31,6 +31,7 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Optional;
 import java.util.function.UnaryOperator;
 import java.util.stream.Stream;
 
@@ -151,15 +152,18 @@ public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile>
implements Aut
         this.zipFile = zipFile;
     }
 
-    public ZipAssert containsOnlyEntriesMatching(EntryChecks... entryChecks) throws Exception
{
+    private ZipAssert containsEntriesMatchingWithComparator(Optional<Comparator<ZipArchiveEntry>>
sortEntriesBy,
+                                                            Optional<Comparator<EntryChecks>>
sortEntryChecksBy,
+                                                            EntryChecks... entryChecks) throws
Exception {
         isNotNull();
-        List<EntryChecks> sortedEntryChecks = Arrays.stream(entryChecks)
-            .sorted(Comparator.comparing(checks -> checks.name))
-            .collect(Guavate.toImmutableList());
-        List<ZipArchiveEntry> entries = Collections.list(zipFile.getEntries())
-            .stream()
-            .sorted(Comparator.comparing(ZipArchiveEntry::getName))
-            .collect(Guavate.toImmutableList());
+
+        Stream<EntryChecks> entryChecksStream = Arrays.stream(entryChecks);
+        List<EntryChecks> sortedEntryChecks = sortAndCollect(sortEntryChecksBy, entryChecksStream);
+
+        Stream<ZipArchiveEntry> entryStream = Collections.list(zipFile.getEntries())
+            .stream();
+        List<ZipArchiveEntry> entries = sortAndCollect(sortEntriesBy, entryStream);
+
         if (entries.size() != entryChecks.length) {
             throwAssertionError(shouldHaveSize(zipFile, entryChecks.length, entries.size()));
         }
@@ -169,6 +173,33 @@ public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile>
implements Aut
         return myself;
     }
 
+    private <T> List<T> sortAndCollect(Optional<Comparator<T>> sortBy,
Stream<T> stream) {
+        Stream<T> sortedStream = sortBy.map(comparator -> stream.sorted(comparator))
+            .orElse(stream);
+        return sortedStream.collect(Guavate.toImmutableList());
+    }
+
+    /**
+     * Check that the zip entries in the zip file contains only the entries matching
+     * the entryChecks, with the same number of checks as entries.
+     * The order of the entries in the zip file is independent from the one of the entryChecks.
+     */
+    public ZipAssert containsOnlyEntriesMatching(EntryChecks... entryChecks) throws Exception
{
+        Comparator<ZipArchiveEntry> entryComparator = Comparator.comparing(ZipArchiveEntry::getName);
+        Comparator<EntryChecks> entryCheckComparator = Comparator.comparing(check ->
check.name);
+
+        return containsEntriesMatchingWithComparator(Optional.of(entryComparator), Optional.of(entryCheckComparator),
entryChecks);
+    }
+
+    /**
+     * Check that the zip entries in the zip file contains only the entries matching
+     * the entryChecks, with the same number of checks as entries.
+     * The order of the entries in the zip file must match the one of the entryChecks.
+     */
+    public ZipAssert containsExactlyEntriesMatching(EntryChecks... entryChecks) throws Exception
{
+        return containsEntriesMatchingWithComparator(Optional.empty(), Optional.empty(),
entryChecks);
+    }
+
     public ZipAssert hasNoEntry() {
         isNotNull();
         if (zipFile.getEntries().hasMoreElements()) {
@@ -268,6 +299,7 @@ public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile>
implements Aut
 
     /**
      * Because there are always some extra fields not belong to James, and their equals()
method doesn't work
+     *
      * @param entry
      * @return
      */
diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssertTest.java
b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssertTest.java
index adbd78e..4762307 100644
--- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssertTest.java
+++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssertTest.java
@@ -175,7 +175,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyEntriesMatchingShouldNotThrowWhenBothEmpty() throws Exception
{
+    public void containsOnlyEntriesMatchingShouldNotThrowWhenBothEmpty() throws Exception
{
         try (ZipFile zipFile = buildZipFile()) {
             assertThatCode(() -> assertThatZip(zipFile)
                     .containsOnlyEntriesMatching())
@@ -184,7 +184,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyEntriesMatchingShouldNotThrowWhenRightOrder() throws Exception
{
+    public void containsOnlyEntriesMatchingShouldNotThrowWhenRightOrder() throws Exception
{
         try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) {
             assertThatCode(() -> assertThatZip(zipFile)
                     .containsOnlyEntriesMatching(
@@ -235,7 +235,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyEntriesMatchingShouldNotThrowWhenWrongOrder() throws Exception
{
+    public void containsOnlyEntriesMatchingShouldNotThrowWhenWrongOrder() throws Exception
{
         try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) {
             assertThatCode(() -> assertThatZip(zipFile)
                     .containsOnlyEntriesMatching(
@@ -246,7 +246,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyEntriesMatchingShouldThrowWhenExpectingMoreEntries() throws
Exception {
+    public void containsOnlyEntriesMatchingShouldThrowWhenExpectingMoreEntries() throws Exception
{
         try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) {
             assertThatThrownBy(() -> assertThatZip(zipFile)
                     .containsOnlyEntriesMatching(
@@ -258,7 +258,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyEntriesMatchingShouldThrowWhenExpectingLessEntries() throws
Exception {
+    public void containsOnlyEntriesMatchingShouldThrowWhenExpectingLessEntries() throws Exception
{
         try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) {
             assertThatThrownBy(() -> assertThatZip(zipFile)
                     .containsOnlyEntriesMatching(
@@ -268,6 +268,58 @@ public class ZipAssertTest {
     }
 
     @Test
+    public void containsExactlyEntriesMatchingShouldNotThrowWhenBothEmpty() throws Exception
{
+        try (ZipFile zipFile = buildZipFile()) {
+            assertThatCode(() -> assertThatZip(zipFile)
+                .containsExactlyEntriesMatching())
+                .doesNotThrowAnyException();
+        }
+    }
+
+    @Test
+    public void containsExactlyEntriesMatchingShouldThrowWhenWrongOrder() throws Exception
{
+        try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) {
+            assertThatThrownBy(() -> assertThatZip(zipFile)
+                .containsExactlyEntriesMatching(
+                    hasName(ENTRY_NAME_2),
+                    hasName(ENTRY_NAME)))
+                .isInstanceOf(AssertionError.class);
+        }
+    }
+    @Test
+    public void containsExactlyEntriesMatchingShouldNotThrowWhenRightOrder() throws Exception
{
+        try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) {
+            assertThatCode(() -> assertThatZip(zipFile)
+                .containsExactlyEntriesMatching(
+                    hasName(ENTRY_NAME),
+                    hasName(ENTRY_NAME_2)))
+                .doesNotThrowAnyException();
+        }
+    }
+
+    @Test
+    public void containsExactlyEntriesMatchingShouldThrowWhenExpectingMoreEntries() throws
Exception {
+        try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) {
+            assertThatThrownBy(() -> assertThatZip(zipFile)
+                .containsExactlyEntriesMatching(
+                    hasName(ENTRY_NAME),
+                    hasName(ENTRY_NAME_2),
+                    hasName("extraEntry")))
+                .isInstanceOf(AssertionError.class);
+        }
+    }
+
+    @Test
+    public void containsExactlyEntriesMatchingShouldThrowWhenExpectingLessEntries() throws
Exception {
+        try (ZipFile zipFile = buildZipFile(ENTRY, ENTRY_2)) {
+            assertThatThrownBy(() -> assertThatZip(zipFile)
+                .containsExactlyEntriesMatching(
+                    hasName(ENTRY_NAME)))
+                .isInstanceOf(AssertionError.class);
+        }
+    }
+
+    @Test
     public void hasStringContentShouldNotThrowWhenIdentical() throws Exception {
         try (ZipFile zipFile = buildZipFile(ENTRY)) {
             assertThatCode(() -> assertThatZip(zipFile)
@@ -290,7 +342,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyExtraFieldsShouldNotThrowWhenBothEmpty() throws Exception
{
+    public void containsOnlyExtraFieldsShouldNotThrowWhenBothEmpty() throws Exception {
         try (ZipFile zipFile = buildZipFile(ENTRY)) {
             assertThatCode(() -> assertThatZip(zipFile)
                 .containsOnlyEntriesMatching(
@@ -301,7 +353,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyExtraFieldsShouldThrowWhenMissingExpectedField() throws Exception
{
+    public void containsOnlyExtraFieldsShouldThrowWhenMissingExpectedField() throws Exception
{
         try (ZipFile zipFile = buildZipFile(ENTRY)) {
             assertThatThrownBy(() -> assertThatZip(zipFile)
                 .containsOnlyEntriesMatching(
@@ -312,7 +364,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyExtraFieldsShouldNotThrowWhenUnexpectedField() throws Exception
{
+    public void containsOnlyExtraFieldsShouldNotThrowWhenUnexpectedField() throws Exception
{
         try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination))
{
 
             ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new
File("any"), ENTRY_NAME);
@@ -334,7 +386,7 @@ public class ZipAssertTest {
     }
 
     @Test
-    public void containsExactlyExtraFieldsShouldNotThrowWhenContainingExpectedExtraFields()
throws Exception {
+    public void containsOnlyExtraFieldsShouldNotThrowWhenContainingExpectedExtraFields()
throws Exception {
         try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination))
{
 
             ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new
File("any"), ENTRY_NAME);
diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
index 79607b3..7200cc7 100644
--- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
+++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
@@ -254,4 +254,32 @@ class ZipperTest {
                 );
         }
     }
+
+    @Test
+    void entriesInArchiveShouldBeOrderedLikeMailboxWithItsAnnotationsThenMessages() throws
Exception {
+        MailboxWithAnnotations mailbox1 = new MailboxWithAnnotations(MAILBOX_1, ImmutableList.of(ANNOTATION_1));
+        MailboxWithAnnotations mailbox2 = new MailboxWithAnnotations(MAILBOX_2, ImmutableList.of(ANNOTATION_2));
+
+        testee.archive(ImmutableList.of(mailbox1, mailbox2), Stream.of(MESSAGE_RESULT_1,
MESSAGE_RESULT_2), output);
+
+        try (ZipAssert zipAssert = assertThatZip(output)) {
+            zipAssert.containsExactlyEntriesMatching(
+                //MAILBOX 1 with its annotations
+                hasName(MAILBOX_1.getName() + "/"),
+                hasName(MAILBOX_1.getName() + "/annotations/").isDirectory(),
+                hasName(MAILBOX_1.getName() + "/annotations/" + ANNOTATION_1.getKey().asString())
+                    .hasStringContent(ANNOTATION_1_CONTENT),
+                //MAILBOX 2 with its annotations
+                hasName(MAILBOX_2.getName() + "/"),
+                hasName(MAILBOX_2.getName() + "/annotations/").isDirectory(),
+                hasName(MAILBOX_2.getName() + "/annotations/" + ANNOTATION_2.getKey().asString())
+                    .hasStringContent(ANNOTATION_2_CONTENT),
+                //the messages
+                hasName(MESSAGE_ID_1.serialize())
+                    .hasStringContent(MESSAGE_CONTENT_1),
+                hasName(MESSAGE_ID_2.serialize())
+                    .hasStringContent(MESSAGE_CONTENT_2));
+
+        }
+    }
 }


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


Mime
View raw message