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 2019/07/11 07:25:40 UTC

[james-project] 02/09: JAMES-2808 add tests for de/serialization of DeletedMessagesWithStorageInformation DTO and converter

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 f651add51f2b0011252927069e6e846b63d7fccf
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Tue Jul 9 11:30:01 2019 +0700

    JAMES-2808 add tests for de/serialization of DeletedMessagesWithStorageInformation DTO and converter
---
 mailbox/plugin/deleted-messages-vault/pom.xml      |   5 +
 ...etedMessageWithStorageInformationConverter.java |   9 +-
 ...eletedMessageWithStorageInformationDTOTest.java | 136 +++++++++++++++++++++
 .../src/test/resources/json/deleted_message.json   |  17 +++
 .../deleted_message_with_storage_information.json  |  26 ++++
 ...d_message_with_storage_information_invalid.json |  27 ++++
 .../json/deleted_message_with_subject.json         |  18 +++
 .../test/resources/json/storage_information.json   |   4 +
 8 files changed, 238 insertions(+), 4 deletions(-)

diff --git a/mailbox/plugin/deleted-messages-vault/pom.xml b/mailbox/plugin/deleted-messages-vault/pom.xml
index 8d973c0..43828e4 100644
--- a/mailbox/plugin/deleted-messages-vault/pom.xml
+++ b/mailbox/plugin/deleted-messages-vault/pom.xml
@@ -94,6 +94,11 @@
             <artifactId>guava</artifactId>
         </dependency>
         <dependency>
+            <groupId>net.javacrumbs.json-unit</groupId>
+            <artifactId>json-unit-assertj</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>nl.jqno.equalsverifier</groupId>
             <artifactId>equalsverifier</artifactId>
             <scope>test</scope>
diff --git a/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/dto/DeletedMessageWithStorageInformationConverter.java b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/dto/DeletedMessageWithStorageInformationConverter.java
index 31b77e8..c8edb91 100644
--- a/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/dto/DeletedMessageWithStorageInformationConverter.java
+++ b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/dto/DeletedMessageWithStorageInformationConverter.java
@@ -23,6 +23,7 @@ import java.time.ZonedDateTime;
 import java.util.List;
 
 import javax.inject.Inject;
+import javax.mail.internet.AddressException;
 
 import org.apache.james.blob.api.BlobId;
 import org.apache.james.blob.api.BucketName;
@@ -60,7 +61,7 @@ public class DeletedMessageWithStorageInformationConverter {
             .build();
     }
 
-    public DeletedMessage toDomainObject(DeletedMessageWithStorageInformationDTO.DeletedMessageDTO deletedMessageDTO) {
+    public DeletedMessage toDomainObject(DeletedMessageWithStorageInformationDTO.DeletedMessageDTO deletedMessageDTO) throws AddressException {
         return DeletedMessage.builder()
             .messageId(messageIdFactory.fromString(deletedMessageDTO.getMessageId()))
             .originMailboxes(deserializeOriginMailboxes(deletedMessageDTO.getOriginMailboxes()))
@@ -75,7 +76,7 @@ public class DeletedMessageWithStorageInformationConverter {
             .build();
     }
 
-    public DeletedMessageWithStorageInformation toDomainObject(DeletedMessageWithStorageInformationDTO deletedMessageWithStorageInfoDTO) {
+    public DeletedMessageWithStorageInformation toDomainObject(DeletedMessageWithStorageInformationDTO deletedMessageWithStorageInfoDTO) throws AddressException {
         return new DeletedMessageWithStorageInformation(
             toDomainObject(deletedMessageWithStorageInfoDTO.getDeletedMessage()),
             toDomainObject(deletedMessageWithStorageInfoDTO.getStorageInformation()));
@@ -87,9 +88,9 @@ public class DeletedMessageWithStorageInformationConverter {
             .collect(Guavate.toImmutableList());
     }
 
-    private ImmutableList<MailAddress> deserializeRecipients(List<String> recipients) {
+    private ImmutableList<MailAddress> deserializeRecipients(List<String> recipients) throws AddressException {
         return recipients.stream()
-            .map(Throwing.function(recipient -> new MailAddress(recipient)))
+            .map(Throwing.<String, MailAddress>function(MailAddress::new).sneakyThrow())
             .collect(Guavate.toImmutableList());
     }
 }
diff --git a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/dto/DeletedMessageWithStorageInformationDTOTest.java b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/dto/DeletedMessageWithStorageInformationDTOTest.java
new file mode 100644
index 0000000..d012867
--- /dev/null
+++ b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/dto/DeletedMessageWithStorageInformationDTOTest.java
@@ -0,0 +1,136 @@
+/****************************************************************
+ * 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.vault.dto;
+
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.apache.james.util.ClassLoaderUtils.getSystemResourceAsString;
+import static org.apache.james.vault.DeletedMessageFixture.DELETED_MESSAGE;
+import static org.apache.james.vault.DeletedMessageFixture.DELETED_MESSAGE_WITH_SUBJECT;
+import static org.apache.james.vault.dto.DeletedMessageWithStorageInformationDTO.DeletedMessageDTO;
+import static org.apache.james.vault.dto.DeletedMessageWithStorageInformationDTO.StorageInformationDTO;
+import static org.apache.james.vault.metadata.DeletedMessageVaultMetadataFixture.STORAGE_INFORMATION;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import javax.mail.internet.AddressException;
+
+import org.apache.james.blob.api.HashBlobId;
+import org.apache.james.mailbox.inmemory.InMemoryId;
+import org.apache.james.mailbox.inmemory.InMemoryMessageId;
+import org.apache.james.vault.metadata.DeletedMessageWithStorageInformation;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+
+class DeletedMessageWithStorageInformationDTOTest {
+    private static final StorageInformationDTO STORAGE_INFORMATION_DTO = StorageInformationDTO.toDTO(STORAGE_INFORMATION);
+
+    private static final DeletedMessageDTO DELETED_MESSAGE_DTO = DeletedMessageDTO.toDTO(DELETED_MESSAGE);
+
+    private static final DeletedMessageDTO DELETED_MESSAGE_WITH_SUBJECT_DTO = DeletedMessageDTO.toDTO(DELETED_MESSAGE_WITH_SUBJECT);
+
+    private static final DeletedMessageWithStorageInformation DELETED_MESSAGE_WITH_STORAGE_INFO =
+        new DeletedMessageWithStorageInformation(DELETED_MESSAGE_WITH_SUBJECT, STORAGE_INFORMATION);
+
+    private static final DeletedMessageWithStorageInformationDTO DELETED_MESSAGE_WITH_STORAGE_INFO_DTO =
+        DeletedMessageWithStorageInformationDTO.toDTO(DELETED_MESSAGE_WITH_STORAGE_INFO);
+
+    private static final String STORAGE_INFORMATION_JSON = getSystemResourceAsString("json/storage_information.json");
+
+    private static final String DELETED_MESSAGE_JSON = getSystemResourceAsString("json/deleted_message.json");
+    private static final String DELETED_MESSAGE_WITH_SUBJECT_JSON = getSystemResourceAsString("json/deleted_message_with_subject.json");
+
+    private static final String DELETED_MESSAGE_WITH_STORAGE_INFO_JSON =
+        getSystemResourceAsString("json/deleted_message_with_storage_information.json");
+
+    private ObjectMapper objectMapper;
+    private DeletedMessageWithStorageInformationConverter converter;
+
+    @BeforeEach
+    void setup() {
+        this.objectMapper = new ObjectMapper()
+            .registerModule(new Jdk8Module())
+            .setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
+
+        this.converter = new DeletedMessageWithStorageInformationConverter(
+            new HashBlobId.Factory(),
+            new InMemoryMessageId.Factory(),
+            new InMemoryId.Factory());
+    }
+
+    @Test
+    void shouldSerializeStorageInformation() throws Exception {
+        assertThatJson(objectMapper.writeValueAsString(STORAGE_INFORMATION_DTO))
+            .isEqualTo(STORAGE_INFORMATION_JSON);
+    }
+
+    @Test
+    void shouldDeserializeStorageInformation() throws Exception {
+        assertThat(converter.toDomainObject(objectMapper.readValue(STORAGE_INFORMATION_JSON, StorageInformationDTO.class)))
+            .isEqualTo(STORAGE_INFORMATION);
+    }
+
+    @Test
+    void shouldSerializeDeletedMessage() throws Exception {
+        assertThatJson(objectMapper.writeValueAsString(DELETED_MESSAGE_DTO))
+            .isEqualTo(DELETED_MESSAGE_JSON);
+    }
+
+    @Test
+    void shouldDeserializeDeletedMessage() throws Exception {
+        assertThat(converter.toDomainObject(objectMapper.readValue(DELETED_MESSAGE_JSON, DeletedMessageDTO.class)))
+            .isEqualTo(DELETED_MESSAGE);
+    }
+
+    @Test
+    void shouldSerializeDeletedMessageWithSubject() throws Exception {
+        assertThatJson(objectMapper.writeValueAsString(DELETED_MESSAGE_WITH_SUBJECT_DTO))
+            .isEqualTo(DELETED_MESSAGE_WITH_SUBJECT_JSON);
+    }
+
+    @Test
+    void shouldDeserializeDeletedMessageWithSubject() throws Exception {
+        assertThat(converter.toDomainObject(objectMapper.readValue(DELETED_MESSAGE_WITH_SUBJECT_JSON, DeletedMessageDTO.class)))
+            .isEqualTo(DELETED_MESSAGE_WITH_SUBJECT);
+    }
+
+    @Test
+    void shouldSerializeDeletedMessageWithStorageInformation() throws Exception {
+        assertThatJson(objectMapper.writeValueAsString(DELETED_MESSAGE_WITH_STORAGE_INFO_DTO))
+            .isEqualTo(DELETED_MESSAGE_WITH_STORAGE_INFO_JSON);
+    }
+
+    @Test
+    void shouldDeserializeDeletedMessageWithStorageInformation() throws Exception {
+        assertThat(converter.toDomainObject(objectMapper.readValue(DELETED_MESSAGE_WITH_STORAGE_INFO_JSON, DeletedMessageWithStorageInformationDTO.class)))
+            .isEqualTo(DELETED_MESSAGE_WITH_STORAGE_INFO);
+    }
+
+    @Test
+    void deserializingInvalidAddressesShouldThrow() {
+        assertThatThrownBy(() -> converter.toDomainObject(
+            objectMapper.readValue(getSystemResourceAsString("json/deleted_message_with_storage_information_invalid.json"),
+                DeletedMessageWithStorageInformationDTO.class)))
+            .isInstanceOf(AddressException.class);
+    }
+}
diff --git a/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message.json b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message.json
new file mode 100644
index 0000000..9fd2270
--- /dev/null
+++ b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message.json
@@ -0,0 +1,17 @@
+{
+  "messageId": "42",
+  "originMailboxes": [
+    "43",
+    "44"
+  ],
+  "owner":"bob@apache.org",
+  "deliveryDate":"2014-10-30T14:12:00Z",
+  "deletionDate":"2015-10-30T14:12:00Z",
+  "sender":"sender@localhost",
+  "recipients": [
+    "recipient1@localhost",
+    "recipient2@localhost"
+  ],
+  "hasAttachment": false,
+  "size":24
+}
\ No newline at end of file
diff --git a/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_storage_information.json b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_storage_information.json
new file mode 100644
index 0000000..3dea40b
--- /dev/null
+++ b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_storage_information.json
@@ -0,0 +1,26 @@
+{
+  "deletedMessage":
+  {
+    "messageId": "42",
+    "originMailboxes": [
+      "43",
+      "44"
+    ],
+    "owner":"bob@apache.org",
+    "deliveryDate":"2014-10-30T14:12:00Z",
+    "deletionDate":"2015-10-30T14:12:00Z",
+    "sender":"sender@localhost",
+    "recipients": [
+      "recipient1@localhost",
+      "recipient2@localhost"
+    ],
+    "subject":"subject",
+    "hasAttachment": false,
+    "size":24
+  },
+  "storageInformation":
+  {
+    "bucketName":"bucket-2019-06-01",
+    "blobId":"05dcb33b-8382-4744-923a-bc593ad84d23"
+  }
+}
\ No newline at end of file
diff --git a/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_storage_information_invalid.json b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_storage_information_invalid.json
new file mode 100644
index 0000000..4461b5b
--- /dev/null
+++ b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_storage_information_invalid.json
@@ -0,0 +1,27 @@
+{
+  "deletedMessage":
+  {
+    "messageId": "42",
+    "originMailboxes": [
+      "43",
+      "44"
+    ],
+    "owner":"bob@apache.org",
+    "deliveryDate":"2014-10-30T14:12:00Z",
+    "deletionDate":"2015-10-30T14:12:00Z",
+    "sender":"sender@localhost",
+    "recipients": [
+      "recipient1@localhost",
+      "recipient1@localhost@invalid",
+      "recipient2@localhost"
+    ],
+    "subject":"subject",
+    "hasAttachment": false,
+    "size":24
+  },
+  "storageInformation":
+  {
+    "bucketName":"bucket-2019-06-01",
+    "blobId":"05dcb33b-8382-4744-923a-bc593ad84d23"
+  }
+}
\ No newline at end of file
diff --git a/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_subject.json b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_subject.json
new file mode 100644
index 0000000..5098172
--- /dev/null
+++ b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/deleted_message_with_subject.json
@@ -0,0 +1,18 @@
+{
+  "messageId": "42",
+  "originMailboxes": [
+    "43",
+    "44"
+  ],
+  "owner":"bob@apache.org",
+  "deliveryDate":"2014-10-30T14:12:00Z",
+  "deletionDate":"2015-10-30T14:12:00Z",
+  "sender":"sender@localhost",
+  "recipients": [
+    "recipient1@localhost",
+    "recipient2@localhost"
+  ],
+  "subject":"subject",
+  "hasAttachment": false,
+  "size":24
+}
\ No newline at end of file
diff --git a/mailbox/plugin/deleted-messages-vault/src/test/resources/json/storage_information.json b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/storage_information.json
new file mode 100644
index 0000000..572848e
--- /dev/null
+++ b/mailbox/plugin/deleted-messages-vault/src/test/resources/json/storage_information.json
@@ -0,0 +1,4 @@
+{
+  "bucketName":"bucket-2019-06-01",
+  "blobId":"05dcb33b-8382-4744-923a-bc593ad84d23"
+}
\ 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