/** * 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: *
*text/plain
.* An assertion to check that a mail was received by mail server with the given characteristics: *
*text/plain
.