Skip to content

Commit

Permalink
JAMES-1650 Provide messages count (total and unread) in GetMailboxes …
Browse files Browse the repository at this point in the history
…response. Contributed by Vignon <fvignon@linagora.com>

git-svn-id: https://svn.apache.org/repos/asf/james/project/trunk@1722878 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbaechler committed Jan 4, 2016
1 parent cc4d186 commit fd86da3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
Expand Up @@ -30,9 +30,9 @@
import org.apache.james.jmap.model.mailbox.SortOrder;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.MessageManager;
import org.apache.james.mailbox.MessageManager.MetaData.FetchGroup;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.exception.MailboxNotFoundException;
import org.apache.james.mailbox.model.MailboxPath;
import org.apache.james.mailbox.store.mail.MailboxMapperFactory;
import org.apache.james.mailbox.store.mail.model.MailboxId;
Expand Down Expand Up @@ -98,11 +98,13 @@ private GetMailboxesResponse getMailboxesResponse(MailboxSession mailboxSession)
private Optional<Mailbox> mailboxFromMailboxPath(MailboxPath mailboxPath, MailboxSession mailboxSession) {
try {
Optional<Role> role = Role.from(mailboxPath.getName());
MessageManager.MetaData mailboxMetaData = getMailboxMetaData(mailboxPath, mailboxSession);
return Optional.ofNullable(Mailbox.builder()
.id(getMailboxId(mailboxPath, mailboxSession))
.name(mailboxPath.getName())
.role(role)
.unreadMessages(unreadMessages(mailboxPath, mailboxSession))
.unreadMessages(mailboxMetaData.getUnseenCount())
.totalMessages(mailboxMetaData.getMessageCount())
.sortOrder(SortOrder.getSortOrder(role))
.build());
} catch (MailboxException e) {
Expand All @@ -111,17 +113,16 @@ private Optional<Mailbox> mailboxFromMailboxPath(MailboxPath mailboxPath, Mailbo
}
}

private String getMailboxId(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxException, MailboxNotFoundException {
private String getMailboxId(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxException {
return mailboxMapperFactory.getMailboxMapper(mailboxSession)
.findMailboxByPath(mailboxPath)
.getMailboxId()
.serialize();
}

private long unreadMessages(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxException {
private MessageManager.MetaData getMailboxMetaData(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxException {
return mailboxManager.getMailbox(mailboxPath, mailboxSession)
.getMetaData(DONT_RESET_RECENT, mailboxSession, FetchGroup.UNSEEN_COUNT)
.getUnseenCount();
.getMetaData(DONT_RESET_RECENT, mailboxSession, FetchGroup.UNSEEN_COUNT);
}

}
Expand Up @@ -34,6 +34,7 @@
import org.apache.james.mailbox.acl.MailboxACLResolver;
import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
import org.apache.james.mailbox.acl.UnionMailboxACLResolver;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.inmemory.InMemoryId;
import org.apache.james.mailbox.inmemory.InMemoryMailboxSessionMapperFactory;
import org.apache.james.mailbox.model.MailboxPath;
Expand Down Expand Up @@ -167,4 +168,56 @@ public void getMailboxesShouldReturnMailboxesWithSortOrder() throws Exception {
Tuple.tuple("TEMPLATES", 80));
}

}
@Test
public void getMailboxesShouldReturnEmptyMailboxByDefault() throws MailboxException {
MailboxPath mailboxPath = new MailboxPath("#private", USERNAME, "name");
MailboxSession mailboxSession = mailboxManager.createSystemSession(USERNAME, LOGGER);
mailboxManager.createMailbox(mailboxPath, mailboxSession);

GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder()
.build();

GetMailboxesResponse getMailboxesResponse = getMailboxesMethod.process(getMailboxesRequest, mailboxSession);
assertThat(getMailboxesResponse.getList())
.extracting(Mailbox::getTotalMessages, Mailbox::getUnreadMessages)
.containsOnly(Tuple.tuple(0L, 0L));
}

@Test
public void getMailboxesShouldReturnCorrectTotalMessagesCount() throws MailboxException {
MailboxPath mailboxPath = new MailboxPath("#private", USERNAME, "name");
MailboxSession mailboxSession = mailboxManager.createSystemSession(USERNAME, LOGGER);
mailboxManager.createMailbox(mailboxPath, mailboxSession);
MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, mailboxSession);
messageManager.appendMessage(new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), mailboxSession, false, new Flags());
messageManager.appendMessage(new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(), mailboxSession, false, new Flags());

GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder()
.build();

GetMailboxesResponse getMailboxesResponse = getMailboxesMethod.process(getMailboxesRequest, mailboxSession);
assertThat(getMailboxesResponse.getList())
.extracting(Mailbox::getTotalMessages)
.containsExactly(2L);
}

@Test
public void getMailboxesShouldReturnCorrectUnreadMessagesCount() throws MailboxException {
MailboxPath mailboxPath = new MailboxPath("#private", USERNAME, "name");
MailboxSession mailboxSession = mailboxManager.createSystemSession(USERNAME, LOGGER);
mailboxManager.createMailbox(mailboxPath, mailboxSession);
MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, mailboxSession);
Flags defaultUnseenFlag = new Flags();
Flags readMessageFlag = new Flags();
readMessageFlag.add(Flags.Flag.SEEN);
messageManager.appendMessage(new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), mailboxSession, false, defaultUnseenFlag );
messageManager.appendMessage(new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(), mailboxSession, false, defaultUnseenFlag );
messageManager.appendMessage(new ByteArrayInputStream("Subject: test3\r\n\r\ntestmail".getBytes()), new Date(), mailboxSession, false, readMessageFlag);
GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder()
.build();
GetMailboxesResponse getMailboxesResponse = getMailboxesMethod.process(getMailboxesRequest, mailboxSession);
assertThat(getMailboxesResponse.getList())
.extracting(Mailbox::getUnreadMessages)
.containsExactly(2L);
}
}

0 comments on commit fd86da3

Please sign in to comment.