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

Commit

Permalink
Merge pull request #176 from Cognifide/136
Browse files Browse the repository at this point in the history
136
  • Loading branch information
mkrzyzanowski authored Apr 25, 2017
2 parents fd14c7c + eb59287 commit 4d4f1ff
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 64 deletions.
5 changes: 5 additions & 0 deletions bb-email/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
</dependency>

<!-- tests -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
Expand Down
122 changes: 68 additions & 54 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,15 +19,15 @@
*/
package com.cognifide.qa.bb.email;

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

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.search.SubjectTerm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,44 +67,18 @@ public void connect() {
}

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

Message[] messages = {};
try {
messages = folder.search(new SubjectTerm(subject), folder.getMessages());
} 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.
* Closes connection with mailbox.
*/
protected List<Message> getMessageList(int limit) {
Message[] messages = {};
try {
int messageCount = folder.getMessageCount();
if (messageCount < 1) {
return new ArrayList<>();
}
messages = folder.getMessages(messageCount - limit + 1, messageCount);
} catch (MessagingException e) {
LOGGER.error("error when getting email messages", e);
}
return Arrays.asList(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) {
List<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) {
List<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 @@ -135,35 +102,82 @@ public void removeAllEmails() {
}
}

/**
* Deletes all emails with subject.
*
* @param subject Subject of message.
*/
public void removeAllEmails(String subject) {
getMessageList(subject)
.forEach(this::removeEmail);
}

/**
* Deletes last n emails.
*
* @param limit emails amount.
*/
public void removeLastEmails(int limit) {
List<Message> messages = getMessageList(limit);
for (Message message : messages) {
try {
message.setFlag(Flags.Flag.DELETED, true);
} catch (MessagingException e) {
LOGGER.error("error when removing emails from server", e);
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 messageList List of Messages.
* @return List of EmailData objects.
* @param messages List of Messages.
* @return Collection of EmailData objects.
*/
private List<EmailData> createEmailDataList(List<Message> messageList) {
List<EmailData> emailDataList = new ArrayList<>();
for (Message message : messageList) {
EmailData emailData = emailDataFactory.create(message);
emailDataList.add(emailData);
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) {
try {
message.setFlag(Flags.Flag.DELETED, true);
} catch (MessagingException e) {
LOGGER.error("error when removing emails from server", e);
}
return emailDataList;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*-
* #%L
* Bobcat
* %%
* Copyright (C) 2016 Cognifide Ltd.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.cognifide.qa.bb.email;

import java.util.Optional;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.search.SearchTerm;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SubjectSearchTerm extends SearchTerm {

private static final Logger LOGGER = LoggerFactory.getLogger(SubjectSearchTerm.class);

private final String subject;

public SubjectSearchTerm(String subject) {
this.subject = subject;
}

@Override
public boolean match(Message msg) {
return Optional.ofNullable(msg)
.map(this::getSubjectFromMessage)
.map(emailSubject -> StringUtils.equals(emailSubject, subject))
.orElse(false);
}

private String getSubjectFromMessage(Message msg) {
String result = null;
try {
result = msg.getSubject();
} catch (MessagingException e) {
LOGGER.error("error when getting email subject", e);
}
return result;
}
}
107 changes: 103 additions & 4 deletions bb-email/src/test/java/com/cognifide/qa/bb/email/EmailTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -60,7 +61,7 @@ public EmailTest(String propertiesFileName) {

@Parameters
public static Collection<Object[]> parameters() {
return Arrays.asList(new Object[][] {
return Arrays.asList(new Object[][]{
{"email.imap.properties"},
{"email.imaps.properties"},
{"email.pop3s.properties"},
Expand All @@ -84,9 +85,8 @@ public void tearDown() {
public void canReceiveLatestEmails() {
int emailsNumber = 5;
List<EmailData> sentEmails = dataGenerator.generateEmailData(emailsNumber);
for (EmailData emailData : sentEmails) {
sender.sendEmail(emailData);
}

sentEmails.forEach(sender::sendEmail);

client.connect();

Expand All @@ -96,4 +96,103 @@ public void canReceiveLatestEmails() {

Assert.assertTrue("email data objects should be equal", sentEmails.equals(receivedEmails));
}

@Test
public void canReceiveMailBySubject() {
String subject = "test";
EmailData sentEmail = dataGenerator.generateEmailData(subject);

sender.sendEmail(sentEmail);

int emailsNumber = 5;
List<EmailData> sentEmails = dataGenerator.generateEmailData(emailsNumber);
sentEmails.forEach(sender::sendEmail);

client.connect();

List<EmailData> receivedEmail = client.getLatest(subject);
client.close();

Assert.assertTrue(receivedEmail.contains(sentEmail));

boolean topicMatches = receivedEmail.stream()
.map(EmailData::getSubject)
.allMatch(subject::equals);

Assert.assertTrue(topicMatches);
}

@Test
public void canRemoveAllMailsWithSubject() {
String subject = "test";
EmailData subjectMail = dataGenerator.generateEmailData(subject);
sender.sendEmail(subjectMail);

int emailsNumber = 5;
List<EmailData> sentEmails = dataGenerator.generateEmailData(emailsNumber);
sentEmails.forEach(sender::sendEmail);

client.connect();

client.removeAllEmails(subject);

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

List<EmailData> latest = client.getLatest(emailsNumber + 1);

client.close();

Assert.assertFalse(latest.contains(subjectMail));
Assert.assertTrue(latest.containsAll(sentEmails));
}

@Test
public void canRemoveLastEmails() {
int emailsNumber = 5;
int emailsToDelete = 2;
List<EmailData> sentEmails = dataGenerator.generateEmailData(emailsNumber);
sentEmails.forEach(sender::sendEmail);

client.connect();

client.removeLastEmails(emailsToDelete);

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

List<EmailData> latest = client.getLatest(emailsNumber);

client.close();

List<EmailData> limitedList =
sentEmails.stream()
.limit(emailsNumber - emailsToDelete)
.collect(Collectors.toList());

Assert.assertEquals(latest, limitedList);
}

@Test
public void canRemoveAllEmails() {
int emailsNumber = 5;
List<EmailData> sentEmails = dataGenerator.generateEmailData(emailsNumber);
sentEmails.forEach(sender::sendEmail);

client.connect();

client.removeAllEmails();

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

List<EmailData> latest = client.getLatest(emailsNumber);

client.close();

Assert.assertTrue(latest.isEmpty());
}
}
Loading

0 comments on commit 4d4f1ff

Please sign in to comment.