Skip to content

Commit

Permalink
JAMES-2249 JMAP cucumber test: factorise getting mailbox ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc DUZAN authored and aduprat committed Dec 12, 2017
1 parent 986458f commit 34d523e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 130 deletions.
Expand Up @@ -25,13 +25,7 @@


import javax.inject.Inject; import javax.inject.Inject;


import org.apache.james.mailbox.model.MailboxConstants;
import org.apache.james.mailbox.model.MailboxId;
import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.model.MessageId;
import org.apache.james.modules.MailboxProbeImpl;

import com.github.steveash.guavate.Guavate;
import com.google.common.base.Joiner;


import cucumber.api.java.en.Then; import cucumber.api.java.en.Then;
import cucumber.api.java.en.When; import cucumber.api.java.en.When;
Expand All @@ -56,50 +50,24 @@ private GetMessageListMethodStepdefs(MainStepdefs mainStepdefs, HttpClient httpC


@When("^\"([^\"]*)\" asks for message list in mailboxes \"([^\"]*)\" with flag \"([^\"]*)\"$") @When("^\"([^\"]*)\" asks for message list in mailboxes \"([^\"]*)\" with flag \"([^\"]*)\"$")
public void getMessageListWithFlag(String username, List<String> mailboxes, String flag) throws Exception { public void getMessageListWithFlag(String username, List<String> mailboxes, String flag) throws Exception {
String mailboxIds = Joiner.on("\",\"")
.join(mailboxes.stream()
.map(mailbox -> mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, username, mailbox)
.getMailboxId()
.serialize())
.collect(Guavate.toImmutableList()));

httpClient.post(String.format( httpClient.post(String.format(
"[[\"getMessageList\", {\"filter\":{" + "[[\"getMessageList\", {\"filter\":{" +
" \"inMailboxes\":[\"%s\"]," + " \"inMailboxes\":[\"%s\"]," +
" \"hasKeyword\":\"%s\"" + " \"hasKeyword\":\"%s\"" +
"}}, \"#0\"]]", "}}, \"#0\"]]",
mailboxIds, mainStepdefs.getMailboxIds(username, mailboxes),
flag)); flag));
} }


@When("^\"([^\"]*)\" asks for message list in (?:mailboxes|mailbox) \"([^\"]*)\"$") @When("^\"([^\"]*)\" asks for message list in (?:mailboxes|mailbox) \"([^\"]*)\"$")
public void getMessageList(String username, List<String> mailboxes) throws Exception { public void getMessageList(String username, List<String> mailboxes) throws Exception {
String mailboxIds = Joiner.on("\",\"") getMessageListFromMailboxIds(mainStepdefs.getMailboxIds(username, mailboxes));
.join(mailboxes.stream()
.map(mailbox -> mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, username, mailbox)
.getMailboxId()
.serialize())
.collect(Guavate.toImmutableList()));

getMessageListFromMailboxIds(mailboxIds);
} }


@When("^\"([^\"]*)\" asks for message list in delegated (?:mailboxes|mailbox) \"([^\"]*)\" from \"([^\"]*)\"$") @When("^\"([^\"]*)\" asks for message list in delegated (?:mailboxes|mailbox) \"([^\"]*)\" from \"([^\"]*)\"$")
public void getMessageListFromDelegated(String sharee, List<String> mailboxes, String sharer) throws Exception { public void getMessageListFromDelegated(String sharee, List<String> mailboxes, String sharer) throws Exception {
String mailboxIds = Joiner.on("\",\"") userStepdefs.execWithUser(sharee, () ->
.join(mailboxes.stream() getMessageListFromMailboxIds(mainStepdefs.getMailboxIds(sharer, mailboxes)));
.map(mailbox -> mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, sharer, mailbox)
.getMailboxId()
.serialize())
.collect(Guavate.toImmutableList()));

userStepdefs.execWithUser(sharee, () -> getMessageListFromMailboxIds(mailboxIds));
} }


private void getMessageListFromMailboxIds(String mailboxIds) throws Exception { private void getMessageListFromMailboxIds(String mailboxIds) throws Exception {
Expand All @@ -112,17 +80,15 @@ private void getMessageListFromMailboxIds(String mailboxIds) throws Exception {


@When("^\"([^\"]*)\" asks for message list in mailbox \"([^\"]*)\" with flag \"([^\"]*)\"$") @When("^\"([^\"]*)\" asks for message list in mailbox \"([^\"]*)\" with flag \"([^\"]*)\"$")
public void getMessageList(String username, String mailbox, String flag) throws Exception { public void getMessageList(String username, String mailbox, String flag) throws Exception {
MailboxId mailboxId = mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, username, mailbox)
.getMailboxId();


httpClient.post(String.format( httpClient.post(String.format(
"[[\"getMessageList\", {\"filter\":{" + "[[\"getMessageList\", {\"filter\":{" +
" \"inMailboxes\":[\"%s\"]," + " \"inMailboxes\":[\"%s\"]," +
" \"hasKeyword\":\"%s\"" + " \"hasKeyword\":\"%s\"" +
"}}, \"#0\"]]", "}}, \"#0\"]]",
mailboxId.serialize(), mainStepdefs
.getMailboxId(username, mailbox)
.serialize(),
flag)); flag));
} }


Expand Down
Expand Up @@ -92,8 +92,11 @@ private GetMessagesMethodStepdefs(MainStepdefs mainStepdefs, UserStepdefs userSt
@Given("^the user has a message \"([^\"]*)\" in \"([^\"]*)\" and \"([^\"]*)\" mailboxes with subject \"([^\"]*)\", content \"([^\"]*)\"$") @Given("^the user has a message \"([^\"]*)\" in \"([^\"]*)\" and \"([^\"]*)\" mailboxes with subject \"([^\"]*)\", content \"([^\"]*)\"$")
public void appendMessageInTwoMailboxes(String messageName, String mailbox1, String mailbox2, String subject, String content) throws Exception { public void appendMessageInTwoMailboxes(String messageName, String mailbox1, String mailbox2, String subject, String content) throws Exception {
MessageId id = appendMessage(mailbox1, ContentType.noContentType(), subject, content, NO_HEADERS); MessageId id = appendMessage(mailbox1, ContentType.noContentType(), subject, content, NO_HEADERS);
MailboxId mailboxId1 = mainStepdefs.mailboxProbe.getMailbox(MailboxConstants.USER_NAMESPACE, userStepdefs.getConnectedUser(), mailbox1).getMailboxId();
MailboxId mailboxId2 = mainStepdefs.mailboxProbe.getMailbox(MailboxConstants.USER_NAMESPACE, userStepdefs.getConnectedUser(), mailbox2).getMailboxId(); String user = userStepdefs.getConnectedUser();

MailboxId mailboxId1 = mainStepdefs.getMailboxId(user, mailbox1);
MailboxId mailboxId2 = mainStepdefs.getMailboxId(user, mailbox2);


mainStepdefs.jmapServer.getProbe(JmapGuiceProbe.class).setInMailboxes(id, userStepdefs.getConnectedUser(), mailboxId1, mailboxId2); mainStepdefs.jmapServer.getProbe(JmapGuiceProbe.class).setInMailboxes(id, userStepdefs.getConnectedUser(), mailboxId1, mailboxId2);
messageIdStepdefs.addMessageId(messageName, id); messageIdStepdefs.addMessageId(messageName, id);
Expand Down Expand Up @@ -536,7 +539,8 @@ public void assertNotFoundListContains(String id) {
@Then("^the notFound list should contain the requested message id$") @Then("^the notFound list should contain the requested message id$")
public void assertNotFoundListContainsRequestedMessages() { public void assertNotFoundListContainsRequestedMessages() {
ImmutableList<String> elements = requestedMessageIds.stream().map(MessageId::serialize).collect(Guavate.toImmutableList()); ImmutableList<String> elements = requestedMessageIds.stream().map(MessageId::serialize).collect(Guavate.toImmutableList());
assertThat(httpClient.jsonPath.<List<String>>read(ARGUMENTS + ".notFound")).containsExactlyElementsOf(elements); assertThat(httpClient.jsonPath.<List<String>>read(ARGUMENTS + ".notFound"))
.containsExactlyElementsOf(elements);
} }




Expand All @@ -553,35 +557,27 @@ public void assertIdOfTheFirstMessage(String messageName) {


@Then("^the message is in \"([^\"]*)\" mailboxes") @Then("^the message is in \"([^\"]*)\" mailboxes")
public void assertMailboxNamesOfTheFirstMessage(String mailboxNames) { public void assertMailboxNamesOfTheFirstMessage(String mailboxNames) {
List<String> values = Splitter.on(",") List<String> mailboxIds = mainStepdefs.getMailboxIdsList(userStepdefs.getConnectedUser(),
.splitToList(mailboxNames).stream() Splitter.on(",").splitToList(mailboxNames));
.map(Throwing.function(name -> mainStepdefs.jmapServer
.getProbe(MailboxProbeImpl.class)
.getMailbox(MailboxConstants.USER_NAMESPACE, userStepdefs.getConnectedUser(), name)
.getMailboxId()
.serialize()))
.distinct()
.collect(Guavate.toImmutableList());
assertThat(httpClient.jsonPath.<JSONArray>read(FIRST_MESSAGE + ".mailboxIds")) assertThat(httpClient.jsonPath.<JSONArray>read(FIRST_MESSAGE + ".mailboxIds"))
.hasSize(values.size()) .containsExactlyInAnyOrder(mailboxIds.toArray());
.containsOnlyElementsOf(values);
} }


@Then("^\"([^\"]*)\" should see message \"([^\"]*)\" in mailboxes:$") @Then("^\"([^\"]*)\" should see message \"([^\"]*)\" in mailboxes:$")
public void assertMailboxesOfMessage(String user, String messageId, DataTable userMailboxes) throws Exception { public void assertMailboxesOfMessage(String user, String messageId, DataTable userMailboxes) throws Exception {
userStepdefs.execWithUser(user, () -> postWithAListOfIds(ImmutableList.of(messageId))); userStepdefs.execWithUser(user, () -> postWithAListOfIds(ImmutableList.of(messageId)));


List<String> values = userMailboxes.asMap(String.class, String.class).entrySet().stream() List<String> mailboxIds = userMailboxes.asMap(String.class, String.class).entrySet().stream()
.map(Throwing.function(userMailbox -> mainStepdefs.jmapServer .map(Throwing.function(userMailbox ->
.getProbe(MailboxProbeImpl.class) mainStepdefs
.getMailbox(MailboxConstants.USER_NAMESPACE, userMailbox.getKey(), userMailbox.getValue()) .getMailboxId(userMailbox.getKey(), userMailbox.getValue())
.getMailboxId() .serialize()))
.serialize()))
.distinct() .distinct()
.collect(Guavate.toImmutableList()); .collect(Guavate.toImmutableList());

assertThat(httpClient.jsonPath.<JSONArray>read(FIRST_MESSAGE + ".mailboxIds")) assertThat(httpClient.jsonPath.<JSONArray>read(FIRST_MESSAGE + ".mailboxIds"))
.hasSize(values.size()) .containsExactlyInAnyOrder(mailboxIds.toArray());
.containsOnlyElementsOf(values);
} }


@Then("^the threadId of the message is \"([^\"]*)\"$") @Then("^the threadId of the message is \"([^\"]*)\"$")
Expand Down
Expand Up @@ -19,8 +19,13 @@


package org.apache.james.jmap.methods.integration.cucumber; package org.apache.james.jmap.methods.integration.cucumber;


import com.github.steveash.guavate.Guavate;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.apache.james.GuiceJamesServer; import org.apache.james.GuiceJamesServer;
import org.apache.james.mailbox.model.MailboxConstants;
import org.apache.james.mailbox.model.MailboxId;
import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.model.MessageId;
import org.apache.james.mailbox.store.probe.ACLProbe; import org.apache.james.mailbox.store.probe.ACLProbe;
import org.apache.james.mailbox.store.probe.MailboxProbe; import org.apache.james.mailbox.store.probe.MailboxProbe;
Expand All @@ -35,6 +40,10 @@


import cucumber.runtime.java.guice.ScenarioScoped; import cucumber.runtime.java.guice.ScenarioScoped;


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

@ScenarioScoped @ScenarioScoped
public class MainStepdefs { public class MainStepdefs {


Expand Down Expand Up @@ -67,4 +76,27 @@ public void tearDown() {
jmapServer.stop(); jmapServer.stop();
} }


public MailboxId getMailboxId(String username, String mailbox) {
return getMailboxId(MailboxConstants.USER_NAMESPACE, username, mailbox);
}

public MailboxId getMailboxId(String namespace, String username, String mailbox) {
return mailboxProbe
.getMailbox(namespace, username, mailbox)
.getMailboxId();
}
public String getMailboxIds(String username, List<String> mailboxes) {
return Joiner.on("\",\"")
.join(getMailboxIdsList(username, mailboxes.stream()));
}

public ImmutableList<String> getMailboxIdsList(String username, Stream<String> mailboxes) {
return mailboxes.map(mailbox -> getMailboxId(username, mailbox)
.serialize())
.collect(Guavate.toImmutableList());
}

public ImmutableList<String> getMailboxIdsList(String username, Collection<String> mailboxes) {
return getMailboxIdsList(username, mailboxes.stream());
}
} }

0 comments on commit 34d523e

Please sign in to comment.