diff --git a/docs/src/docs/modules/mail-connector.adoc b/docs/src/docs/modules/mail-connector.adoc index 688a7a6a2c..b4b1cf0da4 100644 --- a/docs/src/docs/modules/mail-connector.adoc +++ b/docs/src/docs/modules/mail-connector.adoc @@ -92,7 +92,8 @@ The `POP3MailConnector` provides access to a POP3 server. Emails are read and pr POP3MailConnector pop3MailConnector = new POP3MailConnector(); // wait until the email with subject 'test' arrived in the InboxFolder -EmailQuery query = new EmailQuery().setSearchTerm(new SubjectTerm(subject)); +SubjectTerm subjectTerm = new SubjectTerm(subject); +EmailQuery query = new EmailQuery().setSearchTerm(subjectTerm); Email email = pop3MailConnector.query(query).findFirst().get(); // delete all emails with this subject from the server while setting timeout and max number of polls explicitly @@ -103,10 +104,13 @@ Email email = pop3MailConnector.query(query).findFirst().get(); // delete mails matching the given criteria String recipient = email.getRecipients().get(0)); -String subject = email.getSubject(); -String messageId = null; -pop3MailConnector.deleteMessage(recipient, RecipientType.TO, subject, messageId); +SearchTerm searchTerm = new AndTerm(new SearchTerm[]{ + subjectTerm, + new RecipientTerm(RecipientType.TO, new InternetAddress(recipient)) +}); + +pop3MailConnector.deleteMessage(searchTerm); ---- .Working with attachments @@ -286,7 +290,7 @@ connector.query(query) ---- == MailUtils -This helper class contains methods which facilitate reoccurring tasks when working with mails, e.g. encrypting, decrypting, and comparing mails. +This helper class contains methods which facilitate reoccurring tasks when working with mails, e.g. encrypting, decrypting and comparing mails. .Encryption, Decryption and Comparison [source,java] diff --git a/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/imap/ImapMailConnector.java b/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/imap/ImapMailConnector.java index 4e7577cce0..449ad23789 100644 --- a/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/imap/ImapMailConnector.java +++ b/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/imap/ImapMailConnector.java @@ -21,26 +21,24 @@ */ package eu.tsystems.mms.tic.testframework.mailconnector.imap; -import eu.tsystems.mms.tic.testframework.common.PropertyManager; import eu.tsystems.mms.tic.testframework.exceptions.SystemException; -import eu.tsystems.mms.tic.testframework.logging.Loggable; import eu.tsystems.mms.tic.testframework.mailconnector.util.AbstractInboxConnector; + +import java.util.Properties; + import jakarta.mail.Flags; import jakarta.mail.Folder; import jakarta.mail.Message; import jakarta.mail.MessagingException; -import jakarta.mail.NoSuchProviderException; import jakarta.mail.Store; -import java.util.Properties; - /** * MailConnector using the IMAP Protocol. Creates a session with values from mailconnection.properties. * * @author mibu */ -public class ImapMailConnector extends AbstractInboxConnector implements Loggable { +public class ImapMailConnector extends AbstractInboxConnector { /** * Constructor, creates a ImapMailConnector object. @@ -54,14 +52,14 @@ public ImapMailConnector() { * Called from constructor. Initializes the ImapMailConnector. */ private void init() { - setServer(PropertyManager.getProperty("IMAP_SERVER", null)); - setPort(PropertyManager.getProperty("IMAP_SERVER_PORT", null)); - setInboxFolder(PropertyManager.getProperty("IMAP_FOLDER_INBOX", null)); - setUsername(PropertyManager.getProperty("IMAP_USERNAME", null)); - setPassword(PropertyManager.getProperty("IMAP_PASSWORD", null)); - setSslEnabled(PropertyManager.getBooleanProperty("IMAP_SSL_ENABLED", true)); + setServer(PROPERTY_MANAGER.getProperty("IMAP_SERVER", null)); + setPort(PROPERTY_MANAGER.getProperty("IMAP_SERVER_PORT", null)); + setInboxFolder(PROPERTY_MANAGER.getProperty("IMAP_FOLDER_INBOX", null)); + setUsername(PROPERTY_MANAGER.getProperty("IMAP_USERNAME", null)); + setPassword(PROPERTY_MANAGER.getProperty("IMAP_PASSWORD", null)); + setSslEnabled(PROPERTY_MANAGER.getBooleanProperty("IMAP_SSL_ENABLED", true)); - setDebug(PropertyManager.getBooleanProperty("DEBUG_SETTING", false)); + setDebug(PROPERTY_MANAGER.getBooleanProperty("DEBUG_SETTING", false)); } /** @@ -108,9 +106,6 @@ private void pMarkAllMailsAsSeen() throws SystemException { message.setFlag(Flags.Flag.SEEN, true); } store.close(); - } catch (final NoSuchProviderException e) { - log().error(e.getMessage()); - throw new SystemException(e); } catch (final MessagingException e) { log().error(e.getMessage()); throw new SystemException(e); diff --git a/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/pop3/POP3MailConnector.java b/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/pop3/POP3MailConnector.java index a0978dfb90..fe96e86467 100644 --- a/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/pop3/POP3MailConnector.java +++ b/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/pop3/POP3MailConnector.java @@ -21,8 +21,8 @@ */ package eu.tsystems.mms.tic.testframework.mailconnector.pop3; -import eu.tsystems.mms.tic.testframework.common.PropertyManager; import eu.tsystems.mms.tic.testframework.mailconnector.util.AbstractInboxConnector; + import java.util.Properties; /** @@ -44,15 +44,15 @@ public POP3MailConnector() { * Called from constructor. Initializes the ImapMailConnector. */ private void init() { - setServer(PropertyManager.getProperty("POP3_SERVER", null)); - setPort(PropertyManager.getProperty("POP3_SERVER_PORT", null)); - setInboxFolder(PropertyManager.getProperty("POP3_FOLDER_INBOX", null)); - setUsername(PropertyManager.getProperty("POP3_USERNAME", null)); - setPassword(PropertyManager.getProperty("POP3_PASSWORD", null)); + setServer(PROPERTY_MANAGER.getProperty("POP3_SERVER", null)); + setPort(PROPERTY_MANAGER.getProperty("POP3_SERVER_PORT", null)); + setInboxFolder(PROPERTY_MANAGER.getProperty("POP3_FOLDER_INBOX", null)); + setUsername(PROPERTY_MANAGER.getProperty("POP3_USERNAME", null)); + setPassword(PROPERTY_MANAGER.getProperty("POP3_PASSWORD", null)); // Password may needs to be encoded - setDebug(PropertyManager.getBooleanProperty("DEBUG_SETTING", false)); - setSslEnabled(PropertyManager.getBooleanProperty("POP3_SSL_ENABLED", true)); + setDebug(PROPERTY_MANAGER.getBooleanProperty("DEBUG_SETTING", false)); + setSslEnabled(PROPERTY_MANAGER.getBooleanProperty("POP3_SSL_ENABLED", true)); } /** diff --git a/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/util/AbstractInboxConnector.java b/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/util/AbstractInboxConnector.java index 6a9aa61e7e..c76ed83763 100644 --- a/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/util/AbstractInboxConnector.java +++ b/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/util/AbstractInboxConnector.java @@ -21,9 +21,11 @@ */ package eu.tsystems.mms.tic.testframework.mailconnector.util; +import eu.tsystems.mms.tic.testframework.common.PropertyManagerProvider; import eu.tsystems.mms.tic.testframework.exceptions.SystemException; import eu.tsystems.mms.tic.testframework.logging.Loggable; import eu.tsystems.mms.tic.testframework.utils.TimerUtils; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -32,6 +34,9 @@ import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; + +import org.apache.commons.lang3.ArrayUtils; + import jakarta.mail.Flags; import jakarta.mail.Folder; import jakarta.mail.Message; @@ -43,7 +48,6 @@ import jakarta.mail.internet.AddressException; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; - import jakarta.mail.search.AndTerm; import jakarta.mail.search.ComparisonTerm; import jakarta.mail.search.FromTerm; @@ -52,71 +56,13 @@ import jakarta.mail.search.SearchTerm; import jakarta.mail.search.SentDateTerm; import jakarta.mail.search.SubjectTerm; -import org.apache.commons.lang3.ArrayUtils; /** * abstract class to handle mail connector * * @author sepr */ -public abstract class AbstractInboxConnector extends AbstractMailConnector implements Loggable { - /** - * Wait until messages with search criteria are received. - * - * @param searchCriterias The subject which message should contain. - * @return The message. - * - * @throws AddressException thrown if an error by waiting for the message occurs. - * @deprecated Use {@link #query(EmailQuery)} instead - */ - @Deprecated - public List waitForMails(List searchCriterias) throws AddressException { - EmailQuery query = new EmailQuery(); - return waitForMails(searchCriterias, query.getRetryCount(), Math.round(query.getPauseMs()/1000f)); - } - - /** - * Wait until messages with search term are received. - * - * @param searchTerm The criterias which the message should contain. - * @return The message. - * @deprecated Use {@link #query(EmailQuery)} instead - */ - @Deprecated - public List waitForMails(final SearchTerm searchTerm) { - EmailQuery query = new EmailQuery(); - return waitForMails(searchTerm, query.getRetryCount(), Math.round(query.getPauseMs()/1000f)); - } - - /** - * Wait until messages with search term are received in the given folder. - * @param searchTerm - * @param folderName - * @return - * @deprecated Use {@link #query(EmailQuery)} instead - */ - @Deprecated - public List waitForMails(final SearchTerm searchTerm, final String folderName) { - EmailQuery query = new EmailQuery(); - return waitForMails(searchTerm, query.getRetryCount(), Math.round(query.getPauseMs()/1000f), folderName); - } - - /** - * Wait until messages with search criteria are received. - * - * @param searchCriterias The subject which message should contain. - * @param maxReadTries - * @param pollingTimerSeconds - * @return The message. - * - * @throws AddressException thrown if an error by waiting for the message occurs. - * @deprecated Use {@link #query(EmailQuery)} instead - */ - @Deprecated - public List waitForMails(List searchCriterias, int maxReadTries, int pollingTimerSeconds) throws AddressException { - final SearchTerm searchTerm = translateSearchCriterias(searchCriterias); - return waitForMails(searchTerm, maxReadTries, pollingTimerSeconds); - } +public abstract class AbstractInboxConnector extends AbstractMailConnector implements Loggable, PropertyManagerProvider { /** * translate Array of SearchCriteria to Array of SearchTerms @@ -205,6 +151,7 @@ private SearchTerm pTranslateSearchCriteriaToSearchTerm(final SearchCriteria sea * @return The message. * @deprecated Use {@link #query(EmailQuery)} instead */ + @Deprecated public List waitForMails(final SearchTerm searchTerm, int maxReadTries, int pollingTimerSeconds) { return waitForMails(searchTerm, maxReadTries, pollingTimerSeconds, getInboxFolder()); } @@ -219,11 +166,12 @@ public List waitForMails(final SearchTerm searchTerm, int maxReadTries, i * @deprecated Use {@link #query(EmailQuery)} instead * @throws RuntimeException When there are no emails present. */ + @Deprecated public List waitForMails(final SearchTerm searchTerm, int maxReadTries, int pollingTimerSeconds, final String folderName) { EmailQuery query = new EmailQuery() .setSearchTerm(searchTerm) .setRetryCount(maxReadTries) - .setPauseMs(pollingTimerSeconds*1000) + .setPauseMs(pollingTimerSeconds*1000L) .setFolderName(folderName); List emailList = query(query).collect(Collectors.toList()); @@ -315,14 +263,14 @@ private Stream waitForMessages(final SearchTerm searchTerm, int max /** * Get the message count from {@link #getInboxFolder()}. */ - public long getMessageCount() { + public int getMessageCount() { return this.pGetMessageCount(getInboxFolder()); } /** * Get the message count from a specified folder name. */ - public long getMessageCount(String folderName) { + public int getMessageCount(String folderName) { return this.pGetMessageCount(folderName); } @@ -361,9 +309,10 @@ private int pGetMessageCount(String folderName) { * @param recipientType The type of the recipient. * @param subject The subject of the mail. Can be null if mail has no subject. * @param messageId The id of the message. Can be null. - * + * @deprecated Use {@link #deleteMessage(SearchTerm)} instead * @return true if message was deleted, else false */ + @Deprecated public boolean deleteMessage( final String recipient, final Message.RecipientType recipientType, @@ -387,43 +336,63 @@ public boolean deleteMessage(final Email mail) { /** * deletes messages by given search criterias * - * @param messagesCriterias List of search criteria list - inner list represents searchcriterias to identify one + * @param searchTerms List of search criteria list - inner list represents searchcriterias to identify one * message * * @return true if messages were deleted - * - * @throws AddressException thrown if an error occurred in the translation of the searchCriterias to SearchTerm. */ - @Deprecated - public boolean deleteMessage(List> messagesCriterias) throws AddressException { + public boolean deleteMessages(final List searchTerms) { boolean isDeleted = false; - for (List messageCriterias : messagesCriterias) { - final SearchTerm searchTerm = translateSearchCriterias(messageCriterias); + for (final SearchTerm searchTerm : searchTerms) { isDeleted = deleteMessage(searchTerm); - } + return isDeleted; } /** - * deletes messages by given search criterias + * Deletes a message. * - * @param searchTerms List of search criteria list - inner list represents searchcriterias to identify one - * message + * @param recipient The recipient String. Can be null. + * @param recipientType The type of the recipient. + * @param subject The subject of the mail. Can be null if mail has no subject. + * @param messageId The id of the message. Can be null. * - * @return true if messages were deleted + * @return true if message was deleted, else false */ - public boolean deleteMessages(final List searchTerms) { + @Deprecated + private boolean pDeleteMessage(final String recipient, final Message.RecipientType recipientType, + final String subject, final String messageId) { - boolean isDeleted = false; + boolean deleted = false; - for (final SearchTerm searchTerm : searchTerms) { - isDeleted = deleteMessage(searchTerm); - } + Store store; + try { + store = getSession().getStore(); + store.connect(); + final Folder folder = store.getFolder(getInboxFolder()); + folder.open(Folder.READ_WRITE); + final Message[] messages = folder.getMessages(); + log().info("Checking messages from " + getInboxFolder() + " for MessageID:"); - return isDeleted; + if (messages.length > 0) { + for (final Message message : messages) { + deleted = compareMessageAndDelete(message, recipient, recipientType, subject, messageId); + } + } else { + log().info("None."); + } + // leads to error "folder not open" when reading message content + folder.close(true); + store.close(); + + } catch (final MessagingException e) { + log().error(e.getMessage()); + throw new RuntimeException(e); + } + return deleted; } /** @@ -479,9 +448,10 @@ public int moveMessage(final String targetFolder, final SearchTerm... searchTerm * message * * @return count of moved mails. - * + * @deprecated Use {@link #moveMessage(String, SearchTerm...)} instead * @throws AddressException thrown if an error occurred in the translation of the searchCriterias to SearchTerm. */ + @Deprecated public int moveMessage(String targetFolder, SearchCriteria... searchCriterias) throws AddressException { final SearchTerm[] searchTerms = getSearchTermsFromSearchCriterias(searchCriterias); @@ -530,54 +500,6 @@ private int pMoveMessage(final String targetFolderName, final SearchTerm... sear return count; } - /** - * deletes messages with fitting parameters - * - * @param deleteCriteriaValues String List containing the desired values - * @param deleteCriteriaType Delete Criteria Type - Recipient, Subject or MessageID - * - * @return boolean - true if messages were deleted - */ - @Deprecated - public boolean deleteMessages(List deleteCriteriaValues, DeleteCriteriaType deleteCriteriaType) { - - List booleanValues = new ArrayList<>(); - - for (String deleteCriteriaValue : deleteCriteriaValues) { - boolean isDeleted; - switch (deleteCriteriaType) { - case RECIPIENT: - isDeleted = deleteMessage(deleteCriteriaValue, Message.RecipientType.TO, null, null); - break; - case SUBJECT: - isDeleted = deleteMessage(null, Message.RecipientType.TO, deleteCriteriaValue, null); - break; - case MESSAGEID: - isDeleted = deleteMessage(null, Message.RecipientType.TO, null, deleteCriteriaValue); - break; - default: - throw new SystemException("Not supported: " + deleteCriteriaType); - } - booleanValues.add(isDeleted); - } - - for (Boolean booleanValue : booleanValues) { - if (!booleanValue) { - return false; - } - } - - return true; - } - - /** - * @deprecated Use {@link #deleteMessage(SearchTerm)} instead - */ - @Deprecated - public boolean deleteMessage(List deleteCriteriaValues, DeleteCriteriaType deleteCriteriaType) { - return deleteMessages(deleteCriteriaValues, deleteCriteriaType); - } - /** * delete all message in the InboxFolder * @return @@ -609,7 +531,7 @@ private boolean pDeleteMessage(final SearchTerm searchTerm, final String folderN messages = folder.getMessages(); } - log().info("Checking messages from " + getInboxFolder() + " for MessageID:"); + log().info("Checking messages from '{}'", getInboxFolder()); if (messages.length > 0) { String msg = "Message found, DELETING"; @@ -620,49 +542,7 @@ private boolean pDeleteMessage(final SearchTerm searchTerm, final String folderN } log().info(msg); message.setFlag(Flags.Flag.DELETED, true); - } - } else { - log().info("None."); - } - // leads to error "folder not open" when reading message content - folder.close(true); - store.close(); - - } catch (final MessagingException e) { - log().error(e.getMessage()); - throw new RuntimeException(e); - } - return deleted; - } - - /** - * Deletes a message. - * - * @param recipient The recipient String. Can be null. - * @param recipientType The type of the recipient. - * @param subject The subject of the mail. Can be null if mail has no subject. - * @param messageId The id of the message. Can be null. - * - * @return true if message was deleted, else false - */ - @Deprecated - private boolean pDeleteMessage(final String recipient, final Message.RecipientType recipientType, - final String subject, final String messageId) { - - boolean deleted = false; - - Store store; - try { - store = getSession().getStore(); - store.connect(); - final Folder folder = store.getFolder(getInboxFolder()); - folder.open(Folder.READ_WRITE); - final Message[] messages = folder.getMessages(); - log().info("Checking messages from " + getInboxFolder() + " for MessageID:"); - - if (messages.length > 0) { - for (final Message message : messages) { - deleted = compareMessageAndDelete(message, recipient, recipientType, subject, messageId); + deleted = true; } } else { log().info("None."); diff --git a/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/util/EmailQuery.java b/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/util/EmailQuery.java index c481f55b10..7c79b01ad3 100644 --- a/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/util/EmailQuery.java +++ b/mail-connector/src/main/java/eu/tsystems/mms/tic/testframework/mailconnector/util/EmailQuery.java @@ -21,16 +21,17 @@ package eu.tsystems.mms.tic.testframework.mailconnector.util; -import eu.tsystems.mms.tic.testframework.common.PropertyManager; -import jakarta.mail.search.AndTerm; -import jakarta.mail.search.SearchTerm; +import eu.tsystems.mms.tic.testframework.common.PropertyManagerProvider; import java.util.List; +import jakarta.mail.search.AndTerm; +import jakarta.mail.search.SearchTerm; + /** * EMail query object */ -public class EmailQuery { +public class EmailQuery implements PropertyManagerProvider { /** * Key for pollingfrequency Property. */ @@ -40,8 +41,9 @@ public class EmailQuery { */ private static final String MAX_READ_TRIES_PROPERTY = "MAX_READ_TRIES"; - private int retryCount = Integer.parseInt(PropertyManager.getProperty(MAX_READ_TRIES_PROPERTY, "20")); - private long pauseMs = Integer.parseInt(PropertyManager.getProperty(POLLING_TIMER_SECONDS_PROPERTY, "10")) * 1000; + private int retryCount = Long.valueOf(PROPERTY_MANAGER.getLongProperty(MAX_READ_TRIES_PROPERTY, 20L)).intValue(); + private long pauseMs = PROPERTY_MANAGER.getLongProperty(POLLING_TIMER_SECONDS_PROPERTY, 10L) * 1000; + private SearchTerm searchTerm; private String folderName; diff --git a/mail-connector/src/test/java/eu/tsystems/mms/tic/testframework/mailconnector/test/MailConnectorTest.java b/mail-connector/src/test/java/eu/tsystems/mms/tic/testframework/mailconnector/test/MailConnectorTest.java index 0b171cc06a..087496b020 100644 --- a/mail-connector/src/test/java/eu/tsystems/mms/tic/testframework/mailconnector/test/MailConnectorTest.java +++ b/mail-connector/src/test/java/eu/tsystems/mms/tic/testframework/mailconnector/test/MailConnectorTest.java @@ -21,10 +21,7 @@ */ package eu.tsystems.mms.tic.testframework.mailconnector.test; -import com.icegreen.greenmail.util.DummySSLSocketFactory; -import com.icegreen.greenmail.util.GreenMail; -import com.icegreen.greenmail.util.ServerSetupTest; -import eu.tsystems.mms.tic.testframework.common.PropertyManager; +import eu.tsystems.mms.tic.testframework.common.PropertyManagerProvider; import eu.tsystems.mms.tic.testframework.logging.Loggable; import eu.tsystems.mms.tic.testframework.mailconnector.imap.ImapMailConnector; import eu.tsystems.mms.tic.testframework.mailconnector.pop3.POP3MailConnector; @@ -37,6 +34,29 @@ import eu.tsystems.mms.tic.testframework.testing.AssertProvider; import eu.tsystems.mms.tic.testframework.testing.TesterraTest; import eu.tsystems.mms.tic.testframework.utils.FileUtils; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.security.Security; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.commons.lang3.RandomStringUtils; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import com.icegreen.greenmail.util.DummySSLSocketFactory; +import com.icegreen.greenmail.util.GreenMail; +import com.icegreen.greenmail.util.ServerSetupTest; import jakarta.mail.Address; import jakarta.mail.Message.RecipientType; import jakarta.mail.MessagingException; @@ -55,32 +75,13 @@ import jakarta.mail.search.SearchTerm; import jakarta.mail.search.SentDateTerm; import jakarta.mail.search.SubjectTerm; -import org.apache.commons.lang3.RandomStringUtils; -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Method; -import java.math.BigDecimal; -import java.nio.charset.StandardCharsets; -import java.security.Security; -import java.util.ArrayList; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.stream.Stream; /** * Integration Tests for TesterraMailConnector. * * @author mrgi, tbmi */ -public class MailConnectorTest extends TesterraTest implements Loggable, AssertProvider { +public class MailConnectorTest extends TesterraTest implements Loggable, PropertyManagerProvider, AssertProvider { // CONSTANTS /** @@ -174,11 +175,11 @@ public void shutDownServer() { @BeforeClass public void initProperties() { - PropertyManager.loadProperties("mailconnection.properties"); + PROPERTY_MANAGER.loadProperties("mailconnection.properties"); Security.setProperty("ssl.SocketFactory.provider", DummySSLSocketFactory.class.getName()); mailServerAll = new GreenMail(ServerSetupTest.ALL); - mailServerAll.setUser(RECIPIENT, PropertyManager.getProperty("SMTP_USERNAME"), PropertyManager.getProperty("SMTP_PASSWORD")); + mailServerAll.setUser(RECIPIENT, PROPERTY_MANAGER.getProperty("SMTP_USERNAME"), PROPERTY_MANAGER.getProperty("SMTP_PASSWORD")); mailServerAll.start(); pop3 = new POP3MailConnector(); @@ -281,15 +282,13 @@ public void testT03_sendAndWaitForMessageWithPlainTextAttachment() throws Except } @DataProvider(name = "binaryAttachmentFiles") - public Iterator dataProvider() { - - List files = new ArrayList<>(); - files.add("attachment.pdf"); - files.add("attachment.jpg"); - files.add("attachment.png"); - files.add("attachment.zip"); - - return files.iterator(); + public Object[][] dataProvider() { + return new Object[][] { + {"attachment.pdf"}, + {"attachment.jpg"}, + {"attachment.png"}, + {"attachment.zip"} + }; } /** @@ -420,8 +419,8 @@ public void testT05_sendAndWaitForMessageEncryptedWithCertificate() throws Excep public void testT06_sendAndWaitFromSSLMessage() throws MessagingException { final String subject = STR_MAIL_SUBJECT + "testT06_sendAndWaitForSSLMessage"; - final String sslPortPop3 = PropertyManager.getProperty("POP3_SERVER_PORT_SSL", null); - final String sslPortSmtp = PropertyManager.getProperty("SMTP_SERVER_PORT_SSL", null); + final String sslPortPop3 = PROPERTY_MANAGER.getProperty("POP3_SERVER_PORT_SSL", null); + final String sslPortSmtp = PROPERTY_MANAGER.getProperty("SMTP_SERVER_PORT_SSL", null); // SETUP 1 - Create SSL connectors and message. final SMTPMailConnector smtpSSL = new SMTPMailConnector(); @@ -586,8 +585,24 @@ public void testT12_sendAndWaitForMessageWithoutAttachment_SubjectSentDate() thr */ @Test public void testT11_deleteAllMessages() { + final String subjectFirstMessage = STR_MAIL_SUBJECT + "testT11_deleteAllMessages_FirstMail"; + final String subjectSecondMessage = STR_MAIL_SUBJECT + "testT11_deleteAllMessages_SecondMail"; + + // create messages + final MimeMessage msg1 = this.createDefaultMessage(session, subjectFirstMessage); + final MimeMessage msg2 = this.createDefaultMessage(session, subjectSecondMessage); + + // send messages + smtp.sendMessage(msg1); + smtp.sendMessage(msg2); + + // verify Mail Box contains 2 Mails for deletion + Assert.assertTrue(pop3.getMessageCount() > 1 , "Mail box contains mails for deletion"); + + // delete all messages + pop3.deleteAllMessages(); - pop3.deleteMessage(null, null, null, null); + // verify empty inbox final boolean inboxEmpty = (pop3.getMessageCount() == 0); Assert.assertTrue(inboxEmpty, "Mail box is empty"); } @@ -699,16 +714,15 @@ private Email waitForMessage(final SearchTerm searchTerm) throws AssertionError } private Email waitForMessage(final SearchTerm searchTerm, final AbstractInboxConnector abstractInboxConnector, final String folderName) throws AssertionError { - Email receivedMsg = null; // TEST - Fail, if no message was received. - try { - receivedMsg = abstractInboxConnector.waitForMails(searchTerm, folderName).get(0); - } catch (Exception e) { - Assert.fail(ERR_NO_MSG_RECEIVED); - } + final EmailQuery query = new EmailQuery() + .setSearchTerm(searchTerm) + .setFolderName(folderName); + final List emailList = abstractInboxConnector.query(query).collect(Collectors.toList()); + ASSERT.assertTrue(!emailList.isEmpty(), "messages found."); - return receivedMsg; + return emailList.get(0); } private Email waitForMessage(final SearchTerm searchTerm, final AbstractInboxConnector abstractInboxConnector) throws AssertionError { @@ -731,10 +745,8 @@ private void deleteMessage(final Email msg, final AbstractInboxConnector abstrac new SubjectTerm(subject), new RecipientTerm(RecipientType.TO, new InternetAddress(recipient))); - abstractInboxConnector.deleteMessage(searchTerm); - - final boolean inboxEmpty = (abstractInboxConnector.getMessageCount() == 0); - Assert.assertTrue(inboxEmpty, "Mail box is empty"); + final boolean messageDeleted = abstractInboxConnector.deleteMessage(searchTerm); + Assert.assertTrue(messageDeleted, "Message deleted"); } private void deleteMessage(final Email msg, final AbstractInboxConnector abstractInboxConnector, final String folderName) throws AssertionError, AddressException { @@ -745,10 +757,8 @@ private void deleteMessage(final Email msg, final AbstractInboxConnector abstrac new SubjectTerm(subject), new RecipientTerm(RecipientType.TO, new InternetAddress(recipient))); - abstractInboxConnector.deleteMessage(searchTerm, folderName); - - final boolean inboxEmpty = (abstractInboxConnector.getMessageCount() == 0); - Assert.assertTrue(inboxEmpty, "Mail box is empty"); + final boolean messageDeleted = abstractInboxConnector.deleteMessage(searchTerm, folderName); + Assert.assertTrue(messageDeleted, "Message deleted"); } @Test @@ -768,6 +778,7 @@ public void testPollingTimeout() { Assert.assertEquals(emails.count(), 0); long endTime = System.currentTimeMillis() - startTime; - ASSERT.assertGreaterEqualThan(new BigDecimal(endTime), new BigDecimal(initPause * initRetry), "Invalid polling time"); + ASSERT.assertGreaterEqualThan(new BigDecimal(endTime), + new BigDecimal(initPause * initRetry), "Invalid polling time"); } }