JAMES-2183 Allows checking message by its id and mailbox
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4667548a Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4667548a Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4667548a Branch: refs/heads/master Commit: 4667548abe259c69cf1b3e4dc89f422da944c711 Parents: db9d06c Author: quynhn <[email protected]> Authored: Fri Oct 20 17:55:50 2017 +0700 Committer: Raphael Ouazana <[email protected]> Committed: Wed Oct 25 17:12:09 2017 +0200 ---------------------------------------------------------------------- server/testing/pom.xml | 15 +++++ .../apache/james/utils/IMAPMessageReader.java | 23 +++++++ .../james/utils/IMAPMessageReaderTest.java | 66 ++++++++++++++++++++ 3 files changed, 104 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/4667548a/server/testing/pom.xml ---------------------------------------------------------------------- diff --git a/server/testing/pom.xml b/server/testing/pom.xml index 069fab8..b1899a3 100644 --- a/server/testing/pom.xml +++ b/server/testing/pom.xml @@ -43,6 +43,21 @@ <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> </dependencies> <profiles> http://git-wip-us.apache.org/repos/asf/james-project/blob/4667548a/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java ---------------------------------------------------------------------- diff --git a/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java b/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java index e2edb6f..c5317d4 100644 --- a/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java +++ b/server/testing/src/main/java/org/apache/james/utils/IMAPMessageReader.java @@ -22,9 +22,11 @@ package org.apache.james.utils; import java.io.Closeable; import java.io.IOException; import java.util.List; +import java.util.function.Predicate; import org.apache.commons.net.imap.IMAPClient; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Splitter; public class IMAPMessageReader implements Closeable { @@ -33,6 +35,11 @@ public class IMAPMessageReader implements Closeable { private final IMAPClient imapClient; + @VisibleForTesting + IMAPMessageReader(IMAPClient imapClient) { + this.imapClient = imapClient; + } + public IMAPMessageReader(String host, int port) throws IOException { imapClient = new IMAPClient(); imapClient.connect(host, port); @@ -65,6 +72,22 @@ public class IMAPMessageReader implements Closeable { .contains("OK FETCH completed"); } + public boolean userReceivedMessageWithFlagsInMailbox(String user, String password, String mailbox, String flags) throws IOException { + connectAndSelect(user, password, mailbox); + imapClient.fetch("1:1", "ALL"); + String replyString = imapClient.getReplyString(); + return isCompletedWithFlags(flags, replyString); + } + + @VisibleForTesting + boolean isCompletedWithFlags(String flags, String replyString) { + return replyString.contains("OK FETCH completed") && + Splitter.on(" ") + .splitToList(flags) + .stream() + .allMatch(s -> replyString.contains(s)); + } + public boolean userGetNotifiedForNewMessagesWhenSelectingMailbox(String user, String password, int numOfNewMessage, String mailboxName) throws IOException { connectAndSelect(user, password, mailboxName); http://git-wip-us.apache.org/repos/asf/james-project/blob/4667548a/server/testing/src/test/java/org/apache/james/utils/IMAPMessageReaderTest.java ---------------------------------------------------------------------- diff --git a/server/testing/src/test/java/org/apache/james/utils/IMAPMessageReaderTest.java b/server/testing/src/test/java/org/apache/james/utils/IMAPMessageReaderTest.java new file mode 100644 index 0000000..797e92f --- /dev/null +++ b/server/testing/src/test/java/org/apache/james/utils/IMAPMessageReaderTest.java @@ -0,0 +1,66 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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. * + ****************************************************************/ + +package org.apache.james.utils; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.commons.net.imap.IMAPClient; +import org.junit.Test; + +public class IMAPMessageReaderTest { + private static final IMAPClient NULL_IMAP_CLIENT = null; + private IMAPMessageReader testee = new IMAPMessageReader(NULL_IMAP_CLIENT); + + @Test + public void userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSingleFlag() throws Exception { + String replyString = "* 1 FETCH (FLAGS (\\Flagged) )\n" + + "AAAC OK FETCH completed."; + + assertThat(testee.isCompletedWithFlags("\\Flagged", replyString)) + .isTrue(); + } + + @Test + public void userReceivedMessageWithFlagsInMailboxShouldReturnFalseWhenCompletedButNoFlag() throws Exception { + String replyString = "* 1 FETCH (FLAGS (\\Seen) )\n" + + "AAAC OK FETCH completed."; + + assertThat(testee.isCompletedWithFlags("\\Flagged", replyString)) + .isFalse(); + } + + @Test + public void userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSeveralFlags() throws Exception { + String replyString = "* 1 FETCH (FLAGS (\\Flagged \\Seen) )\n" + + "AAAC OK FETCH completed."; + + assertThat(testee.isCompletedWithFlags("\\Flagged \\Seen", replyString)) + .isTrue(); + } + + @Test + public void userReceivedMessageWithFlagsInMailboxShouldReturnTrueWhenSeveralFlagsInAnyOrder() throws Exception { + String replyString = "* 1 FETCH (FLAGS (\\Flagged \\Seen) )\n" + + "AAAC OK FETCH completed."; + + assertThat(testee.isCompletedWithFlags("\\Seen \\Flagged", replyString)) + .isTrue(); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
