From cf948edcb5bf89894be7bbb2d3deb106523cd350 Mon Sep 17 00:00:00 2001 From: HoussemNasri Date: Mon, 18 Nov 2024 13:08:47 +0100 Subject: [PATCH] [ISSUE-1257] Delete ContactsCollection - Removed ContactsCollection usage from mailetcontainer.xml in tests - Removed references of ContactsCollection from documentation - Removed all code belonging to ContactsCollection --- .../pages/tmail-backend/configure/index.adoc | 4 +- .../features/contactAutocomplete.adoc | 15 +- .../src/test/resources/mailetcontainer.xml | 5 +- .../src/test/resources/mailetcontainer.xml | 5 +- .../src/test/resources/mailetcontainer.xml | 5 +- ...letcontainer_with_amqpforward_openpass.xml | 5 +- .../tmail/mailets/ContactsCollection.scala | 107 ---- .../mailets/ContactsCollectionTest.scala | 477 ------------------ 8 files changed, 26 insertions(+), 597 deletions(-) delete mode 100644 tmail-backend/mailets/src/main/scala/com/linagora/tmail/mailets/ContactsCollection.scala delete mode 100644 tmail-backend/mailets/src/test/scala/com/linagora/tmail/mailets/ContactsCollectionTest.scala diff --git a/docs/modules/ROOT/pages/tmail-backend/configure/index.adoc b/docs/modules/ROOT/pages/tmail-backend/configure/index.adoc index e4373f2551..f97174a726 100644 --- a/docs/modules/ROOT/pages/tmail-backend/configure/index.adoc +++ b/docs/modules/ROOT/pages/tmail-backend/configure/index.adoc @@ -249,11 +249,11 @@ The mailet configuration requires the following parameters: * `exchange_type`: type of the exchange. Can be "direct", "fanout", "topic", "headers". Optional, defaults to "direct". === Example -This configuration forwards contacts collected by the `ContactsCollection` mailet to OpenPaas through the AMQP protocol. +This configuration forwards contacts extracted by the `ContactExtractor` mailet to OpenPaas through the AMQP protocol. [source,xml] ---- - + extractedContacts diff --git a/docs/modules/ROOT/pages/tmail-backend/features/contactAutocomplete.adoc b/docs/modules/ROOT/pages/tmail-backend/features/contactAutocomplete.adoc index a901286c83..4879afa7f9 100644 --- a/docs/modules/ROOT/pages/tmail-backend/features/contactAutocomplete.adoc +++ b/docs/modules/ROOT/pages/tmail-backend/features/contactAutocomplete.adoc @@ -13,15 +13,16 @@ The user is able to autocomplete over his personal contacts and domain contacts == Storing account contacts when sending mails -An administrator needs to rely on the `ContactsCollection` mailet in `mailetcontainer.xml` configuration file -in order to extract the recipient contacts of a message and index them asynchronously. This mailet also stores -them as JSON in a specified message attribute. - -For example: +An administrator can use a combination of the `ContactExtractor` and `IndexContacts` mailets in the `mailetcontainer.xml` configuration file +in order to extract the recipient contacts of a message and index them asynchronously. +For example, in the configuration below, TMail will extract the list of recipients every time the current user sends an email and store them in the `ExtractedContacts` attribute. Then, the `IndexContacts` mailet will index them to enable the autocomplete feature. .... - - ExtractedContacts + + ExtractedContacts + + + ExtractedContacts .... diff --git a/tmail-backend/integration-tests/healthcheck/distributed-healthcheck-integration-tests/src/test/resources/mailetcontainer.xml b/tmail-backend/integration-tests/healthcheck/distributed-healthcheck-integration-tests/src/test/resources/mailetcontainer.xml index f5a2b4ed07..71e599dde9 100644 --- a/tmail-backend/integration-tests/healthcheck/distributed-healthcheck-integration-tests/src/test/resources/mailetcontainer.xml +++ b/tmail-backend/integration-tests/healthcheck/distributed-healthcheck-integration-tests/src/test/resources/mailetcontainer.xml @@ -87,7 +87,10 @@ - + + ContactAttribute1 + + ContactAttribute1 diff --git a/tmail-backend/integration-tests/jmap/distributed-jmap-integration-tests/src/test/resources/mailetcontainer.xml b/tmail-backend/integration-tests/jmap/distributed-jmap-integration-tests/src/test/resources/mailetcontainer.xml index 79d266bfdf..e2864f8478 100644 --- a/tmail-backend/integration-tests/jmap/distributed-jmap-integration-tests/src/test/resources/mailetcontainer.xml +++ b/tmail-backend/integration-tests/jmap/distributed-jmap-integration-tests/src/test/resources/mailetcontainer.xml @@ -87,7 +87,10 @@ - + + ContactAttribute1 + + ContactAttribute1 diff --git a/tmail-backend/integration-tests/jmap/memory-jmap-integration-tests/src/test/resources/mailetcontainer.xml b/tmail-backend/integration-tests/jmap/memory-jmap-integration-tests/src/test/resources/mailetcontainer.xml index b3fb282644..a762cf3a5b 100644 --- a/tmail-backend/integration-tests/jmap/memory-jmap-integration-tests/src/test/resources/mailetcontainer.xml +++ b/tmail-backend/integration-tests/jmap/memory-jmap-integration-tests/src/test/resources/mailetcontainer.xml @@ -95,7 +95,10 @@ - + + ContactAttribute1 + + ContactAttribute1 diff --git a/tmail-backend/integration-tests/jmap/memory-jmap-integration-tests/src/test/resources/mailetcontainer_with_amqpforward_openpass.xml b/tmail-backend/integration-tests/jmap/memory-jmap-integration-tests/src/test/resources/mailetcontainer_with_amqpforward_openpass.xml index 4d7341685b..3c2219a179 100644 --- a/tmail-backend/integration-tests/jmap/memory-jmap-integration-tests/src/test/resources/mailetcontainer_with_amqpforward_openpass.xml +++ b/tmail-backend/integration-tests/jmap/memory-jmap-integration-tests/src/test/resources/mailetcontainer_with_amqpforward_openpass.xml @@ -106,7 +106,10 @@ - + + ContactAttribute1 + + ContactAttribute1 diff --git a/tmail-backend/mailets/src/main/scala/com/linagora/tmail/mailets/ContactsCollection.scala b/tmail-backend/mailets/src/main/scala/com/linagora/tmail/mailets/ContactsCollection.scala deleted file mode 100644 index 3a188e8765..0000000000 --- a/tmail-backend/mailets/src/main/scala/com/linagora/tmail/mailets/ContactsCollection.scala +++ /dev/null @@ -1,107 +0,0 @@ -package com.linagora.tmail.mailets - -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module -import com.google.common.collect.{ImmutableList, ImmutableSet} -import com.linagora.tmail.james.jmap.EmailAddressContactInjectKeys -import com.linagora.tmail.james.jmap.contact.{ContactFields, TmailContactUserAddedEvent} -import jakarta.inject.{Inject, Named} -import jakarta.mail.Message -import org.apache.commons.collections.CollectionUtils -import org.apache.james.core.{MailAddress, Username} -import org.apache.james.events.Event.EventId -import org.apache.james.events.{EventBus, RegistrationKey} -import org.apache.james.mime4j.dom.address.{Address, Group, Mailbox} -import org.apache.james.mime4j.field.address.LenientAddressParser -import org.apache.james.transport.mailets.ContactExtractor.ExtractedContacts -import org.apache.mailet.base.GenericMailet -import org.apache.mailet.{Attribute, AttributeName, AttributeValue, Mail, MailetException} -import reactor.core.scala.publisher.{SFlux, SMono} - -import scala.jdk.CollectionConverters._ -import scala.jdk.OptionConverters._ -import scala.util.Try - -/** - *

ContactsCollection allows extracting the recipient's contact of a message - * and dispatch ContactUserAddedEvent to eventBus, in order to index contact asynchronously. - * This mailet also stores them as JSON in a specified message attribute.

- * - *

Here is the JSON format:

- *

- * {
- *   "userEmail" : "sender@james.org",
- *   "emails" : [ "to@james.org", "cc@james.org" ]
- * }
- * 
- * - *

Sample configuration:

- * - *

- * <mailet match="All" class="ContactsCollection">
- *   <attribute>ExtractedContacts</attribute>
- * </mailet>
- * 
- */ - -class ContactsCollection @Inject()(@Named(EmailAddressContactInjectKeys.AUTOCOMPLETE) eventBus: EventBus) extends GenericMailet { - - private val NO_REGISTRATION_KEYS: ImmutableSet[RegistrationKey] = ImmutableSet.of - private val OBJECT_MAPPER: ObjectMapper = new ObjectMapper().registerModule(new Jdk8Module) - private var attributeName: AttributeName = _ - - override def init(): Unit = - attributeName = Option(getInitParameter("attribute")) - .map(AttributeName.of) - .getOrElse(throw new MailetException("No value for `attribute` parameter was provided.")) - - override def service(mail: Mail): Unit = - if (CollectionUtils.isNotEmpty(mail.getRecipients)) { - SMono.justOrEmpty(mail.getMaybeSender.asOptional().toScala) - .map(sender => extractRecipientsContacts(mail) -> sender) - .flatMap { - case (contacts, sender) => SFlux.zip(appendAttributeToMail(sender, mail, contacts), dispatchEvents(sender, contacts)) - .`then`() - }.block() - } - - private def extractRecipientsContacts(mail: Mail): Seq[ContactFields] = - Seq(Message.RecipientType.TO, Message.RecipientType.CC, Message.RecipientType.BCC) - .flatMap(recipientType => Option(mail.getMessage.getHeader(recipientType.toString, ","))) - .flatMap(mergedHeaderValue => LenientAddressParser.DEFAULT - .parseAddressList(mergedHeaderValue) - .asScala - .flatMap(address => convertAddressToMailboxStream(address)) - .flatMap(mime4jAddress => extractContactField(mime4jAddress)) - .toSeq - .asInstanceOf[Seq[ContactFields]]) - .distinctBy(_.address) - - private def convertAddressToMailboxStream(address: Address): List[Mailbox] = - address match { - case mailbox: Mailbox => List(mailbox) - case group: Group => group.getMailboxes.asScala.toList - case _ => List.empty - } - - private def extractContactField(mime4jAddress: Mailbox): Option[ContactFields] = - Try(ContactFields(new MailAddress(mime4jAddress.getAddress), firstname = Option(mime4jAddress.getName).getOrElse(""))) - .toOption - - private def dispatchEvents(sender: MailAddress, contacts: Seq[ContactFields]): SMono[Unit] = - SFlux.fromIterable(contacts) - .flatMap(contact => SMono.fromPublisher(eventBus.dispatch(TmailContactUserAddedEvent( - eventId = EventId.random(), - username = Username.fromMailAddress(sender), - contact = contact), - NO_REGISTRATION_KEYS))) - .collectSeq() - .`then`() - - private def appendAttributeToMail(sender: MailAddress, mail: Mail, contacts: Seq[ContactFields]): SMono[String] = - SMono.just(sender) - .map(mailAddress => new ExtractedContacts(mailAddress.asString, ImmutableList.copyOf(contacts.map(_.address.asString()).asJava))) - .map(extractedContact => OBJECT_MAPPER.writeValueAsString(extractedContact)) - .doOnNext(extractedContactJson => mail.setAttribute(new Attribute(attributeName, AttributeValue.of(extractedContactJson)))) -} - diff --git a/tmail-backend/mailets/src/test/scala/com/linagora/tmail/mailets/ContactsCollectionTest.scala b/tmail-backend/mailets/src/test/scala/com/linagora/tmail/mailets/ContactsCollectionTest.scala deleted file mode 100644 index 777b168307..0000000000 --- a/tmail-backend/mailets/src/test/scala/com/linagora/tmail/mailets/ContactsCollectionTest.scala +++ /dev/null @@ -1,477 +0,0 @@ -package com.linagora.tmail.mailets - -import java.util -import java.util.Optional - -import com.linagora.tmail.james.jmap.contact.{ContactFields, TmailContactUserAddedEvent} -import com.linagora.tmail.mailets.ContactsCollectionTest.{ATTRIBUTE_NAME, MAILET_CONFIG, RECIPIENT, RECIPIENT2, RECIPIENT3, SENDER} -import net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson -import org.apache.james.core.builder.MimeMessageBuilder -import org.apache.james.core.{MailAddress, Username} -import org.apache.james.events.EventListener.ReactiveGroupEventListener -import org.apache.james.events.delivery.InVmEventDelivery -import org.apache.james.events.{Event, EventBus, Group, InVMEventBus, MemoryEventDeadLetters, RetryBackoffConfiguration} -import org.apache.james.metrics.tests.RecordingMetricFactory -import org.apache.mailet.base.test.{FakeMail, FakeMailetConfig} -import org.apache.mailet.{AttributeName, Mail, MailetConfig, MailetException} -import org.assertj.core.api.Assertions.{assertThat, assertThatCode, assertThatThrownBy} -import org.junit.jupiter.api.{BeforeEach, Test} -import org.reactivestreams.Publisher -import reactor.core.scala.publisher.SMono - -import scala.jdk.CollectionConverters._ -import scala.jdk.OptionConverters._ - -object ContactsCollectionTest { - val ATTRIBUTE_NAME: AttributeName = AttributeName.of("AttributeValue1") - val MAILET_CONFIG: MailetConfig = FakeMailetConfig.builder() - .mailetName("ContactsCollection") - .setProperty("attribute", ATTRIBUTE_NAME.asString()) - .build() - - val SENDER: String = "sender1@domain.tld" - val RECIPIENT: String = "recipient1@domain.tld" - val RECIPIENT2: String = "recipient2@domain.tld" - val RECIPIENT3: String = "recipient3@domain.tld" -} - -class TestEventListener(events: util.ArrayList[Event] = new util.ArrayList[Event]()) extends ReactiveGroupEventListener { - - override def reactiveEvent(event: Event): Publisher[Void] = SMono.just(events.add(event)).`then`() - - override def getDefaultGroup: Group = new Group - - def eventReceived(): util.List[Event] = events - - def contactReceived(): util.List[ContactFields] = events.asScala - .map(_.asInstanceOf[TmailContactUserAddedEvent]) - .map(_.contact).asJava -} - -class ContactsCollectionTest { - var mailet: ContactsCollection = _ - var eventBus: EventBus = _ - var eventListener: TestEventListener = new TestEventListener() - - @BeforeEach - def setup(): Unit = { - eventBus = new InVMEventBus(new InVmEventDelivery(new RecordingMetricFactory), RetryBackoffConfiguration.DEFAULT, new MemoryEventDeadLetters()) - eventBus.register(eventListener) - mailet = new ContactsCollection(eventBus) - } - - @Test - def initShouldThrowWhenNoAttributeParameter(): Unit = { - val mailetConfig: FakeMailetConfig = FakeMailetConfig.builder() - .mailetName("ContactsCollection") - .build() - - assertThatThrownBy(() => mailet.init(mailetConfig)) - .isInstanceOf(classOf[MailetException]) - } - - @Test - def initShouldNotThrowWithAllParameters(): Unit = { - val mailetConfig: FakeMailetConfig = FakeMailetConfig.builder() - .mailetName("ContactsCollection") - .setProperty("attribute", "attributeValue") - .build() - - assertThatCode(() => mailet.init(mailetConfig)) - .doesNotThrowAnyException() - } - - @Test - def serviceShouldDispatchEventWhenHasRecipient(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(RECIPIENT) - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT))) - } - - @Test - def serviceShouldDispatchEventWhenHasInvalidRecipient(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient("invalidRecipient", RECIPIENT3) - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT3))) - } - - @Test - def parsingAddressShouldRelyOnMime4J(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(s"destinataires inconnus: $RECIPIENT;") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient("whateverRecipient@domain.tld") - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT))) - } - - @Test - def shouldNotFailWhenEmptyGroupAddress(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient("destinataires inconnus: ;") - .addBccRecipient(RECIPIENT) - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT))) - } - - @Test - def shouldSupportGroupAddress(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(s"destinataires inconnus: $RECIPIENT, $RECIPIENT2;") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipients(RECIPIENT, RECIPIENT2) - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT)), ContactFields(new MailAddress(RECIPIENT2))) - } - - @Test - def mixedCaseGroupAddressInManyFields(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(s"group1: $RECIPIENT;") - .addCcRecipient(s"group2: $RECIPIENT2;") - .addBccRecipient(s"group3: $RECIPIENT3;") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipients(RECIPIENT, RECIPIENT2, RECIPIENT3) - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT)), ContactFields(new MailAddress(RECIPIENT2)), ContactFields(new MailAddress(RECIPIENT3))) - } - - @Test - def mixedCaseGroupAddressAndMailboxAddress(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(s"group1: $RECIPIENT, $RECIPIENT2;", RECIPIENT3) - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipients(RECIPIENT, RECIPIENT2, RECIPIENT3) - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT)), ContactFields(new MailAddress(RECIPIENT2)), ContactFields(new MailAddress(RECIPIENT3))) - } - - @Test - def shouldMergeContactsWhenDuplicated(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addHeader("To", s"group1: $RECIPIENT, $RECIPIENT2; $RECIPIENT3") - .addHeader("Cc", s"$RECIPIENT, $RECIPIENT2, $RECIPIENT3") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipients(RECIPIENT, RECIPIENT2, RECIPIENT3) - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT)), ContactFields(new MailAddress(RECIPIENT2)), ContactFields(new MailAddress(RECIPIENT3))) - } - - @Test - def shouldParseAddressNameIfPossible(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient("Christophe Hamerling ") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient("chri.hamerling@linagora.com") - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress("chri.hamerling@linagora.com"), "Christophe Hamerling")) - } - - @Test - def shouldParseAddressNamesInGroupIfPossible(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient("group: Christophe Hamerling , Alpine Ubuntu ;") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipients("chri.hamerling@linagora.com", "alpine.ubuntu@linagora.com") - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress("chri.hamerling@linagora.com"), "Christophe Hamerling"), - ContactFields(new MailAddress("alpine.ubuntu@linagora.com"), "Alpine Ubuntu")) - } - - @Test - def eventHasBeenDispatchedShouldHasUsernameIsSender(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(RECIPIENT) - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - - assertThat(eventListener.eventReceived()) - .hasSize(1) - assertThat(eventListener.eventReceived().get(0).getUsername) - .isEqualTo(Username.of(SENDER)) - } - - @Test - def serviceShouldDispatchSeveralEventWhenHasSeveralRecipient(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(RECIPIENT, "recipient2@domain.tld") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT)), ContactFields(new MailAddress("recipient2@domain.tld"))) - } - - @Test - def serviceShouldNotDispatchEventWhenMailHasNotRecipient(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .build() - - mailet.service(mail) - assertThat(eventListener.eventReceived()).isEmpty() - } - - @Test - def serviceShouldAddTheAttribute(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(RECIPIENT) - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - - val attributeValue = getAttributeValue(mail) - assertThat(attributeValue).isPresent - assertThatJson(attributeValue.get).isEqualTo(s"""{"userEmail":"$SENDER","emails":["$RECIPIENT"]}""") - } - - @Test - def serviceShouldNotAddAttributeWhenMailHasNotRecipient(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .build() - - mailet.service(mail) - assertThat(getAttributeValue(mail)).isEmpty - } - - @Test - def serviceShouldCollectContactsFromCCRecipient(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(RECIPIENT) - .addCcRecipient("cc@domain.tld") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - - assertThatJson(getAttributeValue(mail).get) - .isEqualTo(s"""{"userEmail":"$SENDER","emails":["$RECIPIENT", "cc@domain.tld"]}""") - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT)), ContactFields(new MailAddress("cc@domain.tld"))) - } - - @Test - def serviceShouldCollectContactsFromBCCRecipient(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(RECIPIENT) - .addBccRecipient("bcc@domain.tld") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - - assertThatJson(getAttributeValue(mail).get) - .isEqualTo(s"""{"userEmail":"$SENDER","emails":["$RECIPIENT", "bcc@domain.tld"]}""") - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT)), ContactFields(new MailAddress("bcc@domain.tld"))) - } - - - @Test - def serviceShouldPreserveRecipientsEmailAddress(): Unit = { - mailet.init(MAILET_CONFIG) - - val mail: FakeMail = FakeMail.builder() - .name("mail1") - .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() - .setSender(SENDER) - .addToRecipient(s"RecipientName1 <$RECIPIENT>") - .addCcRecipient("cc@domain.tld") - .setSubject("Subject 01") - .setText("Content mail 123")) - .sender(SENDER) - .recipient(RECIPIENT) - .build() - - mailet.service(mail) - - assertThatJson(getAttributeValue(mail).get) - .isEqualTo(s"""{"userEmail":"$SENDER","emails":["$RECIPIENT", "cc@domain.tld"]}""") - - assertThat(eventListener.contactReceived()) - .containsExactlyInAnyOrder(ContactFields(new MailAddress(RECIPIENT), firstname = "RecipientName1"), ContactFields(new MailAddress("cc@domain.tld"))) - } - - private def getAttributeValue(mail: Mail): Optional[String] = - mail.getAttribute(ATTRIBUTE_NAME) - .toScala.map(_.getValue.value().asInstanceOf[String]) - .toJava -}