/** * Copyright (c) 2014-2024 Linagora * * This program/library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or (at your * option) any later version. * * This program/library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program/library; If not, see http://www.gnu.org/licenses/ * for the GNU Lesser General Public License version 2.1. */ package org.ow2.petals.bc.mail.service; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.mail.Address; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.internet.ContentType; import javax.mail.internet.MimeMessage; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.namespace.QName; import org.junit.After; import org.junit.Before; import org.ow2.petals.components.mail.generic.version_3.Mail; import org.ow2.petals.components.mail.version_3.Body; import org.ow2.petals.junit.rules.log.handler.InMemoryLogHandler; import com.ebmwebsourcing.easycommons.lang.UncheckedException; import com.icegreen.greenmail.configuration.GreenMailConfiguration; import com.icegreen.greenmail.imap.ImapHostManager; import com.icegreen.greenmail.junit.GreenMailRule; import com.icegreen.greenmail.store.FolderException; import com.icegreen.greenmail.store.InMemoryStore; import com.icegreen.greenmail.store.MailFolder; import testprovideservice.pivot.SendMailOpRequest; /** * Abstract class for unit tests about request processing * * @author Christophe DENEUX - Linagora * */ public abstract class AbstractTest extends org.ow2.petals.bc.mail.AbstractTest { protected static final QName SVC_ITF_NAME = new QName("http://testProvideService", "testProvideInterfaceName"); protected static final QName SVC_NAME = new QName("http://testProvideService", "testServiceName"); protected static final String SVC_ENDPOINT = "testEndpointName"; protected static final String INCOMING_ACCOUNT_ADDRESS_TO = "incoming@devmail.com"; protected static final String INCOMING_ACCOUNT_ADDRESS_FROM = "incoming-noreply@devmail.com"; protected static final String INCOMING_ACCOUNT_USER = "incoming-account-user"; protected static final String INCOMING_ACCOUNT_PWD = "incoming-account-pwd"; protected static final String EXTERNAL_EMAIL_ADDRESS_FROM = "user1@devmail.com"; protected static final String EXTERNAL_EMAIL_ADDRESS_TO_1 = "user1@devmail.com"; protected static final String EXTERNAL_EMAIL_ADDRESS_TO_2 = "user2@devmail.com"; protected static final String EXTERNAL_EMAIL_ADDRESS_CC_1 = "user1-cc@devmail.com"; protected static final String EXTERNAL_EMAIL_ADDRESS_CC_2 = "user2-cc@devmail.com"; protected static final String EXTERNAL_EMAIL_ADDRESS_BCC_1 = "user1-bcc@devmail.com"; protected static final String EXTERNAL_EMAIL_ADDRESS_BCC_2 = "user2-bcc@devmail.com"; protected static final String EXTERNAL_EMAIL_ADDRESS_REPLY = "component-test@devmail.com"; protected static final String EXTERNAL_EMAIL_SUBJECT = "Integration mail test(Service unit)"; protected static final InMemoryLogHandler IN_MEMORY_LOG_HANDLER = new InMemoryLogHandler(); private static final GreenMailConfiguration MAIL_SERVER_CFG = new GreenMailConfiguration() .withUser(INCOMING_ACCOUNT_ADDRESS_TO, INCOMING_ACCOUNT_USER, INCOMING_ACCOUNT_PWD); protected static final GreenMailRule MAIL_SERVER = new GreenMailRule().withConfiguration(MAIL_SERVER_CFG); private static Marshaller MARSHALLER; protected static Unmarshaller UNMARSHALLER; static { try { final JAXBContext context = JAXBContext.newInstance(Mail.class, Body.class, org.ow2.petals.components.mail.generic.version_3.Mail.class, SendMailOpRequest.class); UNMARSHALLER = context.createUnmarshaller(); MARSHALLER = context.createMarshaller(); MARSHALLER.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); } catch (final JAXBException e) { throw new UncheckedException(e); } } /** * All log traces must be cleared before starting a unit test */ @Before public void clearLogTraces() { IN_MEMORY_LOG_HANDLER.clear(); } /** * All mails previously sent are removed */ @After public void resetMailServer() throws FolderException { final ImapHostManager imapHostManager = MAIL_SERVER.getManagers().getImapHostManager(); final InMemoryStore store = (InMemoryStore) imapHostManager.getStore(); for (final MailFolder folder : store.listMailboxes("*")) { folder.deleteAllMessages(); } } /** * Convert a JAXB element to bytes * * @param jaxbElement * The JAXB element to write as bytes */ protected byte[] toByteArray(final Object jaxbElement) throws JAXBException, IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { MARSHALLER.marshal(jaxbElement, baos); return baos.toByteArray(); } finally { baos.close(); } } /** *

* An assertion to check that mails were received by mail server with the given characteristics: *

* * * @param expectedMailSubject * The expected mail subject * @param expectedMailBody * The expected mail body * @param expectedReplyTo * The expected value of the mail attribute 'Reply-To' * @param expectedAddressesTo * The expected addresses of recipient 'To' to which the mail was sent * @param expectedAddressesCc * The expected addresses of recipient 'Cc' to which the mail was sent. {@code null} if no recipient * 'Cc'. * @param expectedAddressesBcc * The expected addresses of recipient 'Bcc' to which the mail was sent.{@code null} if no recipient * 'Bcc'. */ protected static void assertMailReceivedOnMailServers(final String expectedMailSubject, final String expectedMailBody, final String expectedReplyTo, final String[] expectedAddressesTo, final String[] expectedAddressesCc, final String[] expectedAddressesBcc) throws MessagingException, IOException { final MimeMessage[] emails = MAIL_SERVER.getReceivedMessages(); assertEquals(expectedAddressesTo.length + (expectedAddressesCc == null ? 0 : expectedAddressesCc.length) + (expectedAddressesBcc == null ? 0 : expectedAddressesBcc.length), emails.length); for (final MimeMessage email : emails) { // Check field 'TO' final Address[] recipientsTo = email.getRecipients(Message.RecipientType.TO); assertEquals(expectedAddressesTo.length, recipientsTo.length); int expectedAddressesToFound = 0; for (int i = 0; i < expectedAddressesTo.length; i++) { if (expectedAddressesTo[i].equals(recipientsTo[i].toString())) { expectedAddressesToFound++; } } assertEquals(expectedAddressesTo.length, expectedAddressesToFound); // Check field 'Cc' if (expectedAddressesCc != null) { final Address[] recipientsCc = email.getRecipients(Message.RecipientType.CC); assertEquals(expectedAddressesCc.length, recipientsCc.length); int expectedAddressesCcFound = 0; for (int i = 0; i < expectedAddressesCc.length; i++) { if (expectedAddressesCc[i].equals(recipientsCc[i].toString())) { expectedAddressesCcFound++; } } assertEquals(expectedAddressesCc.length, expectedAddressesCcFound); } // Check field 'Bcc' if (expectedAddressesBcc != null) { final Address[] recipientsBcc = email.getRecipients(Message.RecipientType.BCC); if (recipientsBcc != null) { assertEquals(expectedAddressesBcc.length, recipientsBcc.length); int expectedAddressesBccFound = 0; for (int i = 0; i < expectedAddressesBcc.length; i++) { if (expectedAddressesBcc[i].equals(recipientsBcc[i].toString())) { expectedAddressesBccFound++; } } assertEquals(expectedAddressesBcc.length, expectedAddressesBccFound); } } // Check field 'FROM' final Address[] recipientsFrom = email.getFrom(); assertEquals(1, recipientsFrom.length); assertEquals(EXTERNAL_EMAIL_ADDRESS_FROM, recipientsFrom[0].toString()); if (expectedReplyTo == null) { assertNull(email.getHeader("Reply-To")); } else { final Address[] recipientsReplyTo = email.getReplyTo(); assertEquals(1, recipientsReplyTo.length); assertEquals(expectedReplyTo, recipientsReplyTo[0].toString()); } assertEquals(expectedMailSubject, email.getSubject()); assertEquals(expectedMailBody, email.getContent().toString().trim()); assertEquals("text/plain", new ContentType(email.getContentType()).getBaseType()); } } /** *

* An assertion to check that a mail was received by mail server with the given characteristics: *

* * * @param expectedMailBody * The expected mail body * @param expectedReplyTo * The expected value of the mail attribute 'Reply-To' */ protected static void assertMailReceivedOnMailServers(final String expectedMailBody, final String expectedReplyTo) throws MessagingException, IOException { assertMailReceivedOnMailServers(EXTERNAL_EMAIL_SUBJECT, expectedMailBody, expectedReplyTo, new String[] { EXTERNAL_EMAIL_ADDRESS_TO_2 }, null, null); } /** * An assertion to check no mail was received by the mail server. */ protected static void assertNoMailReceivedOnMailServer() throws MessagingException { final MimeMessage[] emails = MAIL_SERVER.getReceivedMessages(); assertEquals(0, emails.length); } }