Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
refactored EmailClient.java
  • Loading branch information
piotr-wilczynski committed Apr 21, 2017
1 parent 78f5065 commit eb59287
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 48 deletions.
95 changes: 48 additions & 47 deletions bb-email/src/main/java/com/cognifide/qa/bb/email/EmailClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
*/
package com.cognifide.qa.bb.email;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.mail.Flags;
import javax.mail.Folder;
Expand Down Expand Up @@ -66,45 +67,18 @@ public void connect() {
}

/**
* @param subject Email message subject.
* @return Array of Messages with subject from parameter.
*/
protected Message[] getMessageList(String subject) {

Message[] messages = {};
try {
messages = folder.search(new SubjectSearchTerm(subject));
} catch (MessagingException e) {
LOGGER.error("error when getting email messages", e);
}
return messages;
}

/**
* @param limit Messages amount.
* @return Last n Messages from inbox as list.
* Closes connection with mailbox.
*/
protected Message[] getMessageList(int limit) {
Message[] messages = {};
try {
int messageCount = folder.getMessageCount();
if (messageCount < 1) {
return new Message[0];
}
int startIndex = messageCount - Math.min(limit, messageCount) + 1;
messages = folder.getMessages(startIndex, messageCount);
} catch (MessagingException e) {
LOGGER.error("error when getting email messages", e);
}
return messages;
public void close() {
connection.close();
}

/**
* @param subject Email message subject.
* @return List of EmailData with subject from parameter.
*/
public List<EmailData> getLatest(String subject) {
Message[] messageList = getMessageList(subject);
Collection<Message> messageList = getMessageList(subject);
return createEmailDataList(messageList);
}

Expand All @@ -113,17 +87,10 @@ public List<EmailData> getLatest(String subject) {
* @return Last n EmailData from inbox.
*/
public List<EmailData> getLatest(int limit) {
Message[] messageList = getMessageList(limit);
Collection<Message> messageList = getMessageList(limit);
return createEmailDataList(messageList);
}

/**
* Closes connection with mailbox.
*/
public void close() {
connection.close();
}

/**
* Deletes all emails.
*/
Expand All @@ -140,8 +107,8 @@ public void removeAllEmails() {
*
* @param subject Subject of message.
*/
public void removeAllEmailsWithTopic(String subject) {
Stream.of(getMessageList(subject))
public void removeAllEmails(String subject) {
getMessageList(subject)
.forEach(this::removeEmail);
}

Expand All @@ -151,24 +118,58 @@ public void removeAllEmailsWithTopic(String subject) {
* @param limit emails amount.
*/
public void removeLastEmails(int limit) {
Stream.of(getMessageList(limit))
getMessageList(limit)
.forEach(this::removeEmail);
}

/**
* @param subject Email message subject.
* @return Array of Messages with subject from parameter.
*/
protected List<Message> getMessageList(String subject) {

Message[] messages = {};
try {
messages = folder.search(new SubjectSearchTerm(subject));
} catch (MessagingException e) {
LOGGER.error("error when getting email messages", e);
}
return Arrays.asList(messages);
}

/**
* @param limit Messages amount.
* @return Last n Messages from inbox as list.
*/
protected List<Message> getMessageList(int limit) {
Message[] messages = {};
try {
int messageCount = folder.getMessageCount();
if (messageCount > 0) {
int startIndex = messageCount - Math.min(limit, messageCount) + 1;
messages = folder.getMessages(startIndex, messageCount);
}
} catch (MessagingException e) {
LOGGER.error("error when getting email messages", e);
}
return Arrays.asList(messages);
}

/**
* This method converts list of Message objects to list of EmailData.
*
* @param messageArray List of Messages.
* @return List of EmailData objects.
* @param messages List of Messages.
* @return Collection of EmailData objects.
*/
private List<EmailData> createEmailDataList(Message[] messageArray) {
return Stream.of(messageArray)
private List<EmailData> createEmailDataList(Collection<Message> messages) {
return messages.stream()
.map(emailDataFactory::create)
.collect(Collectors.toList());
}

/**
* This method deletes mail
*
* @param message Mail reference
*/
private void removeEmail(Message message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void canRemoveAllMailsWithSubject() {

client.connect();

client.removeAllEmailsWithTopic(subject);
client.removeAllEmails(subject);

//reconnect to apply delete of messages
client.close();
Expand Down

0 comments on commit eb59287

Please sign in to comment.