Skip to content

Commit

Permalink
JAMES-1914 Check that active IMAP connexion get notified after JMAP m…
Browse files Browse the repository at this point in the history
…ove when using NOOP
  • Loading branch information
Luc DUZAN authored and aduprat committed Feb 1, 2017
1 parent 36e3480 commit 21f77da
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Expand Up @@ -21,9 +21,12 @@

import java.io.Closeable;
import java.io.IOException;
import java.util.List;

import org.apache.commons.net.imap.IMAPClient;

import com.google.common.base.Splitter;

public class IMAPMessageReader implements Closeable {

private final IMAPClient imapClient;
Expand All @@ -33,21 +36,24 @@ public IMAPMessageReader(String host, int port) throws IOException {
imapClient.connect(host, port);
}

public void connectAndSelect(String user, String password, String mailbox) throws IOException{
imapClient.login(user, password);
imapClient.select(mailbox);
}

public boolean userReceivedMessage(String user, String password) throws IOException {
return userReceivedMessageInMailbox(user, password, "INBOX");
}

public boolean userReceivedMessageInMailbox(String user, String password, String mailbox) throws IOException {
imapClient.login(user, password);
imapClient.select(mailbox);
connectAndSelect(user, password, mailbox);
imapClient.fetch("1:1", "ALL");
return imapClient.getReplyString()
.contains("OK FETCH completed");
}

public boolean userGetNotifiedForNewMessagesWhenSelectingMailbox(String user, String password, int numOfNewMessage, String mailboxName) throws IOException {
imapClient.login(user, password);
imapClient.select(mailboxName);
connectAndSelect(user, password, mailboxName);

return imapClient.getReplyString().contains("OK [UNSEEN " + numOfNewMessage +"]");
}
Expand All @@ -57,14 +63,14 @@ public boolean userDoesNotReceiveMessage(String user, String password) throws IO
}

public boolean userDoesNotReceiveMessageInMailbox(String user, String password, String mailboxName) throws IOException {
imapClient.login(user, password);
imapClient.select(mailboxName);
connectAndSelect(user, password, mailboxName);
imapClient.fetch("1:1", "ALL");
return imapClient.getReplyString()
.contains("BAD FETCH failed. Invalid messageset");
}

public String readFirstMessageInInbox(String user, String password) throws IOException {

return readFirstMessageInInbox(user, password, "(BODY[])");
}

Expand All @@ -79,6 +85,20 @@ private String readFirstMessageInInbox(String user, String password, String para
return imapClient.getReplyString();
}

public boolean userGetNotifiedForNewMessages(int uid) throws IOException {
imapClient.noop();

String replyString = imapClient.getReplyString();
List<String> parts = Splitter.on('\n')
.trimResults()
.omitEmptyStrings()
.splitToList(replyString);
return parts.size() == 3
&& parts.get(2).contains("OK NOOP completed.")
&& parts.contains("* " + uid + " EXISTS")
&& parts.contains("* " + uid + " RECENT");
}

@Override
public void close() throws IOException {
imapClient.close();
Expand Down
Expand Up @@ -21,10 +21,14 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.apache.james.mailets.utils.IMAPMessageReader;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.runtime.java.guice.ScenarioScoped;

Expand All @@ -33,11 +37,13 @@ public class ImapStepdefs {

private final MainStepdefs mainStepdefs;
private final UserStepdefs userStepdefs;
private final Map<String, IMAPMessageReader> imapConnections;

@Inject
private ImapStepdefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs) {
this.mainStepdefs = mainStepdefs;
this.userStepdefs = userStepdefs;
this.imapConnections = new HashMap<>();
}

@Then("^the user has a IMAP message in mailbox \"([^\"]*)\"$")
Expand All @@ -51,7 +57,7 @@ public void hasMessageInMailbox(String mailbox) throws Throwable {
.isTrue();
}

@Then("^the user has a IMAP notification about (\\d+) new message on mailbox \"([^\"]*)\"$")
@Then("^the user has a IMAP notification about (\\d+) new message when selecting mailbox \"([^\"]*)\"$")
public void hasANotificationAboutNewMessagesInMailbox(int numOfNewMessage, String mailbox) throws Throwable {
IMAPMessageReader imapMessageReader = new IMAPMessageReader("127.0.0.1", 1143);

Expand All @@ -72,4 +78,22 @@ public void hasNoMessageInMailbox(String mailbox) throws Throwable {
mailbox))
.isTrue();
}

@Given("^the user has a open IMAP connexion with mailbox \"([^\"]*)\" selected")
public void openImapConnexionAndSelectMailbox(String mailbox) throws Throwable {
IMAPMessageReader imapMessageReader = new IMAPMessageReader("127.0.0.1", 1143);

String login = userStepdefs.lastConnectedUser;
String password = userStepdefs.passwordByUser.get(login);

imapMessageReader.connectAndSelect(login, password, mailbox);
imapConnections.put(mailbox, imapMessageReader);
}

@Then("^the user has a IMAP RECENT and notification about new message with uid (\\d+) on connexion for mailbox \"([^\"]*)\"$")
public void checkNotificationForNewMessageOnActiveConnexion(int uid, String mailbox) throws Throwable {
IMAPMessageReader imapMessageReader = imapConnections.get(mailbox);
assertThat(imapMessageReader).isNotNull();
assertThat(imapMessageReader.userGetNotifiedForNewMessages(uid)).isTrue();
}
}
Expand Up @@ -21,7 +21,13 @@ Feature: IMAP compatibility of JMAP setMessages method used to update mailboxes
Then the user has a IMAP message in mailbox "mailbox"
And the user has a IMAP message in mailbox "source"

Scenario: If a message is moved byJMAP, IMAP connexion will be notified
Scenario: If a message is moved by JMAP, IMAP client will be notified when selecting mailbox
Given the user has a message "m1" in "source" mailbox with subject "My awesome subject", content "This is the content"
When the user move "m1" to mailbox "mailbox"
Then the user has a IMAP notification about 1 new message on mailbox "mailbox"
Then the user has a IMAP notification about 1 new message when selecting mailbox "mailbox"

Scenario: If a message is moved by JMAP, IMAP client that have selected the destination mailbox will be notified
Given the user has a message "m1" in "source" mailbox with subject "My awesome subject", content "This is the content"
Given the user has a open IMAP connexion with mailbox "mailbox" selected
When the user move "m1" to mailbox "mailbox"
Then the user has a IMAP RECENT and notification about new message with uid 1 on connexion for mailbox "mailbox"

0 comments on commit 21f77da

Please sign in to comment.