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 b876083f70915f70aa053e8f6da5990c2fbc47a3 Author: Benoit Tellier <[email protected]> AuthorDate: Fri Feb 22 11:19:34 2019 +0700 MAILBOX-381 Add DeletedMessage POJO --- mailbox/plugin/deleted-messages-vault/pom.xml | 5 + .../org/apache/james/vault/DeletedMessage.java | 239 +++++++++++++++++++++ .../org/apache/james/vault/DeletedMessageTest.java | 107 +++++++++ 3 files changed, 351 insertions(+) diff --git a/mailbox/plugin/deleted-messages-vault/pom.xml b/mailbox/plugin/deleted-messages-vault/pom.xml index 24ca964..d931dd7 100644 --- a/mailbox/plugin/deleted-messages-vault/pom.xml +++ b/mailbox/plugin/deleted-messages-vault/pom.xml @@ -41,6 +41,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>apache-james-mailbox-memory</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> diff --git a/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/DeletedMessage.java b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/DeletedMessage.java new file mode 100644 index 0000000..0e3a104 --- /dev/null +++ b/mailbox/plugin/deleted-messages-vault/src/main/java/org/apache/james/vault/DeletedMessage.java @@ -0,0 +1,239 @@ +/**************************************************************** + * 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; + +import java.io.InputStream; +import java.time.ZonedDateTime; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Supplier; + +import org.apache.james.core.MailAddress; +import org.apache.james.core.MaybeSender; +import org.apache.james.core.User; +import org.apache.james.mailbox.model.MailboxId; +import org.apache.james.mailbox.model.MessageId; + +import com.google.common.collect.ImmutableList; + +public class DeletedMessage { + public static class Builder { + @FunctionalInterface + interface RequireMessageId<T> { + T messageId(MessageId messageId); + } + + @FunctionalInterface + interface RequireUser<T> { + T user(User user); + } + + @FunctionalInterface + interface RequireOriginMailboxes<T> { + T originMailboxes(List<MailboxId> mailboxIds); + + default T originMailboxes(MailboxId... mailboxIds) { + return originMailboxes(ImmutableList.copyOf(mailboxIds)); + } + } + + @FunctionalInterface + interface RequireDeliveryDate<T> { + T deliveryDate(ZonedDateTime deliveryDate); + } + + @FunctionalInterface + interface RequireDeletionDate<T> { + T deletionDate(ZonedDateTime deletionDate); + } + + @FunctionalInterface + interface RequireSender<T> { + T sender(MaybeSender sender); + } + + @FunctionalInterface + interface RequireRecipients<T> { + T recipients(List<MailAddress> recipients); + + default T recipients(MailAddress... recipients) { + return recipients(ImmutableList.copyOf(recipients)); + } + } + + @FunctionalInterface + interface RequireHasAttachment<T> { + T hasAttachment(boolean value); + + default T hasAttachment() { + return hasAttachment(true); + } + + default T hasNoAttachments() { + return hasAttachment(false); + } + } + + @FunctionalInterface + interface RequireContent<T> { + T content(Supplier<InputStream> content); + } + + public static class FinalStage { + private final MessageId messageId; + private final List<MailboxId> originMailboxes; + private final User owner; + private final ZonedDateTime deliveryDate; + private final ZonedDateTime deletionDate; + private final MaybeSender sender; + private final List<MailAddress> recipients; + private final boolean hasAttachment; + private final Supplier<InputStream> content; + private Optional<String> subject; + + public FinalStage(MessageId messageId, List<MailboxId> originMailboxes, User owner, ZonedDateTime deliveryDate, + ZonedDateTime deletionDate, MaybeSender sender, List<MailAddress> recipients, boolean hasAttachment, + Supplier<InputStream> content) { + this.messageId = messageId; + this.originMailboxes = originMailboxes; + this.owner = owner; + this.deliveryDate = deliveryDate; + this.deletionDate = deletionDate; + this.sender = sender; + this.recipients = recipients; + this.hasAttachment = hasAttachment; + this.content = content; + this.subject = Optional.empty(); + } + + public FinalStage subject(String subject) { + this.subject = Optional.of(subject); + return this; + } + + public DeletedMessage build() { + return new DeletedMessage(messageId, originMailboxes, owner, content, deliveryDate, deletionDate, sender, + recipients, subject, hasAttachment); + } + } + } + + public static Builder.RequireMessageId<Builder.RequireOriginMailboxes<Builder.RequireUser<Builder.RequireDeliveryDate< + Builder.RequireDeletionDate<Builder.RequireContent<Builder.RequireSender<Builder.RequireRecipients< + Builder.RequireHasAttachment<Builder.FinalStage>>>>>>>>> builder() { + + return messageId -> originMailboxes -> user -> deliveryDate -> deletionDate -> content -> sender -> recipients -> + hasAttachment -> new Builder.FinalStage(messageId, originMailboxes, user, deliveryDate, deletionDate, sender, + recipients, hasAttachment, content); + + } + + private final MessageId messageId; + private final List<MailboxId> originMailboxes; + private final User owner; + private final ZonedDateTime deliveryDate; + private final ZonedDateTime deletionDate; + private final MaybeSender sender; + private final List<MailAddress> recipients; + private final Optional<String> subject; + private final boolean hasAttachment; + private final Supplier<InputStream> content; + + public DeletedMessage(MessageId messageId, List<MailboxId> originMailboxes, User owner, Supplier<InputStream> content, + ZonedDateTime deliveryDate, ZonedDateTime deletionDate, MaybeSender sender, List<MailAddress> recipients, + Optional<String> subject, boolean hasAttachment) { + this.messageId = messageId; + this.originMailboxes = originMailboxes; + this.owner = owner; + this.content = content; + this.deliveryDate = deliveryDate; + this.deletionDate = deletionDate; + this.sender = sender; + this.recipients = recipients; + this.subject = subject; + this.hasAttachment = hasAttachment; + } + + public MessageId getMessageId() { + return messageId; + } + + public List<MailboxId> getOriginMailboxes() { + return originMailboxes; + } + + public User getOwner() { + return owner; + } + + public Supplier<InputStream> getContent() { + return content; + } + + public ZonedDateTime getDeliveryDate() { + return deliveryDate; + } + + public ZonedDateTime getDeletionDate() { + return deletionDate; + } + + public MaybeSender getSender() { + return sender; + } + + public List<MailAddress> getRecipients() { + return recipients; + } + + public Optional<String> getSubject() { + return subject; + } + + public boolean hasAttachment() { + return hasAttachment; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof DeletedMessage) { + DeletedMessage that = (DeletedMessage) o; + + return Objects.equals(this.hasAttachment, that.hasAttachment) + && Objects.equals(this.messageId, that.messageId) + && Objects.equals(this.originMailboxes, that.originMailboxes) + && Objects.equals(this.owner, that.owner) + && Objects.equals(this.content, that.content) + && Objects.equals(this.deliveryDate, that.deliveryDate) + && Objects.equals(this.deletionDate, that.deletionDate) + && Objects.equals(this.sender, that.sender) + && Objects.equals(this.recipients, that.recipients) + && Objects.equals(this.subject, that.subject); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(messageId, originMailboxes, owner, content, deliveryDate, deletionDate, sender, recipients, + subject, hasAttachment); + } +} diff --git a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageTest.java b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageTest.java new file mode 100644 index 0000000..1ea1b6a --- /dev/null +++ b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageTest.java @@ -0,0 +1,107 @@ +/**************************************************************** + * 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; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.time.ZonedDateTime; + +import org.apache.james.core.MailAddress; +import org.apache.james.core.MaybeSender; +import org.apache.james.core.User; +import org.apache.james.mailbox.inmemory.InMemoryId; +import org.apache.james.mailbox.inmemory.InMemoryMessageId; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class DeletedMessageTest { + private static final InMemoryMessageId MESSAGE_ID = InMemoryMessageId.of(42); + private static final InMemoryId MAILBOX_ID_1 = InMemoryId.of(43); + private static final InMemoryId MAILBOX_ID_2 = InMemoryId.of(44); + private static final User USER = User.fromUsername("[email protected]"); + private static final ZonedDateTime DELIVERY_DATE = ZonedDateTime.parse("2014-10-30T14:12:00Z"); + private static final ZonedDateTime DELETION_DATE = ZonedDateTime.parse("2015-10-30T14:12:00Z"); + private static final byte[] CONTENT = "header: value\r\n\r\ncontent".getBytes(StandardCharsets.UTF_8); + private static final String SUBJECT = "subject"; + + @Test + void deletedMessageShouldMatchBeanContract() { + EqualsVerifier.forClass(DeletedMessage.class) + .verify(); + } + + @Test + void buildShouldReturnDeletedMessageWithAllCompulsoryFields() throws Exception { + MaybeSender sender = MaybeSender.of(new MailAddress("[email protected]")); + MailAddress recipient1 = new MailAddress("[email protected]"); + MailAddress recipient2 = new MailAddress("[email protected]"); + DeletedMessage deletedMessage = DeletedMessage.builder() + .messageId(MESSAGE_ID) + .originMailboxes(MAILBOX_ID_1, MAILBOX_ID_2) + .user(USER) + .deliveryDate(DELIVERY_DATE) + .deletionDate(DELETION_DATE) + .content(() -> new ByteArrayInputStream(CONTENT)) + .sender(sender) + .recipients(recipient1, recipient2) + .hasAttachment(false) + .build(); + + SoftAssertions.assertSoftly( + soft -> { + soft.assertThat(deletedMessage.getMessageId()).isEqualTo(MESSAGE_ID); + soft.assertThat(deletedMessage.getOriginMailboxes()).containsOnly(MAILBOX_ID_1, MAILBOX_ID_2); + soft.assertThat(deletedMessage.getOwner()).isEqualTo(USER); + soft.assertThat(deletedMessage.getDeliveryDate()).isEqualTo(DELIVERY_DATE); + soft.assertThat(deletedMessage.getDeletionDate()).isEqualTo(DELETION_DATE); + soft.assertThat(deletedMessage.getContent().get()).hasSameContentAs(new ByteArrayInputStream(CONTENT)); + soft.assertThat(deletedMessage.getSender()).isEqualTo(sender); + soft.assertThat(deletedMessage.getRecipients()).containsOnly(recipient1, recipient2); + soft.assertThat(deletedMessage.hasAttachment()).isFalse(); + soft.assertThat(deletedMessage.getSubject()).isEmpty(); + } + ); + } + + @Test + void buildShouldReturnDeletedMessageWithSubject() throws Exception { + MaybeSender sender = MaybeSender.of(new MailAddress("[email protected]")); + MailAddress recipient1 = new MailAddress("[email protected]"); + MailAddress recipient2 = new MailAddress("[email protected]"); + DeletedMessage deletedMessage = DeletedMessage.builder() + .messageId(MESSAGE_ID) + .originMailboxes(MAILBOX_ID_1, MAILBOX_ID_2) + .user(USER) + .deliveryDate(DELIVERY_DATE) + .deletionDate(DELETION_DATE) + .content(() -> new ByteArrayInputStream(CONTENT)) + .sender(sender) + .recipients(recipient1, recipient2) + .hasAttachment(false) + .subject(SUBJECT) + .build(); + + assertThat(deletedMessage.getSubject()).contains(SUBJECT); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
