From e39e1f753f03c430ffed23c426e593b68bd79bb0 Mon Sep 17 00:00:00 2001 From: benwa Date: Tue, 24 Oct 2017 11:17:11 +0700 Subject: [PATCH] JAMES-2195 Factorize JMAP request posting --- .../GetMessageListMethodStepdefs.java | 45 ++------ .../cucumber/GetMessagesMethodStepdefs.java | 105 ++++++++---------- .../integration/cucumber/HttpStepDefs.java | 56 ++++++++++ .../cucumber/SetMailboxesMethodStepdefs.java | 32 ++---- .../cucumber/SetMessagesMethodStepdefs.java | 57 ++++------ 5 files changed, 143 insertions(+), 152 deletions(-) create mode 100644 server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/HttpStepDefs.java diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/GetMessageListMethodStepdefs.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/GetMessageListMethodStepdefs.java index 68586628d4c..930cc16a972 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/GetMessageListMethodStepdefs.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/GetMessageListMethodStepdefs.java @@ -25,8 +25,6 @@ import javax.inject.Inject; -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; import org.apache.james.mailbox.model.MailboxConstants; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MessageId; @@ -34,10 +32,6 @@ import com.github.steveash.guavate.Guavate; import com.google.common.base.Joiner; -import com.jayway.jsonpath.Configuration; -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.Option; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; @@ -48,16 +42,13 @@ public class GetMessageListMethodStepdefs { private static final String ARGUMENTS = "[0][1]"; private final MainStepdefs mainStepdefs; - private final UserStepdefs userStepdefs; + private final HttpStepDefs httpStepDefs; private final GetMessagesMethodStepdefs messagesMethodStepdefs; - private HttpResponse response; - private DocumentContext jsonPath; - @Inject - private GetMessageListMethodStepdefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs, GetMessagesMethodStepdefs messagesMethodStepdefs) { + private GetMessageListMethodStepdefs(MainStepdefs mainStepdefs, HttpStepDefs httpStepDefs, GetMessagesMethodStepdefs messagesMethodStepdefs) { this.mainStepdefs = mainStepdefs; - this.userStepdefs = userStepdefs; + this.httpStepDefs = httpStepDefs; this.messagesMethodStepdefs = messagesMethodStepdefs; } @@ -72,7 +63,7 @@ public void getMessageList(String username, List mailboxes, String flag) .serialize()) .collect(Guavate.toImmutableList())); - post(String.format( + httpStepDefs.post(String.format( "[[\"getMessageList\", {\"filter\":{" + " \"inMailboxes\":[\"%s\"]," + " \"hasKeyword\":\"%s\"" + @@ -88,7 +79,7 @@ public void getMessageList(String username, String mailbox, String flag) throws .getMailbox(MailboxConstants.USER_NAMESPACE, username, mailbox) .getMailboxId(); - post(String.format( + httpStepDefs.post(String.format( "[[\"getMessageList\", {\"filter\":{" + " \"inMailboxes\":[\"%s\"]," + " \"hasKeyword\":\"%s\"" + @@ -99,7 +90,7 @@ public void getMessageList(String username, String mailbox, String flag) throws @When("^the user asks for message list with flag \"([^\"]*)\"$") public void getMessageList(String flag) throws Exception { - post(String.format( + httpStepDefs.post(String.format( "[[\"getMessageList\", {\"filter\":{" + " \"hasKeyword\":\"%s\"" + "}}, \"#0\"]]", @@ -108,32 +99,20 @@ public void getMessageList(String flag) throws Exception { @Then("^the message list is empty$") public void assertEmpty() throws Exception { - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - assertThat(jsonPath.>read(ARGUMENTS + ".messageIds")).isEmpty(); + assertThat(httpStepDefs.response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(httpStepDefs.jsonPath.>read(ARGUMENTS + ".messageIds")).isEmpty(); } @Then("^the message list has size (\\d+)") public void assertSize(int size) throws Exception { - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - assertThat(jsonPath.>read(ARGUMENTS + ".messageIds")).hasSize(size); + assertThat(httpStepDefs.response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(httpStepDefs.jsonPath.>read(ARGUMENTS + ".messageIds")).hasSize(size); } @Then("^the message list contains \"([^\"]*)\"") public void assertContains(String messsage) throws Exception { MessageId messageId = messagesMethodStepdefs.getMessageId(messsage); - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - assertThat(jsonPath.>read(ARGUMENTS + ".messageIds")).contains(messageId.serialize()); - } - - private void post(String requestBody) throws Exception { - response = Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) - .addHeader("Authorization", userStepdefs.authenticate(userStepdefs.getConnectedUser()).serialize()) - .addHeader("Accept", org.apache.http.entity.ContentType.APPLICATION_JSON.getMimeType()) - .bodyString(requestBody, org.apache.http.entity.ContentType.APPLICATION_JSON) - .execute() - .returnResponse(); - jsonPath = JsonPath.using(Configuration.defaultConfiguration() - .addOptions(Option.SUPPRESS_EXCEPTIONS)) - .parse(response.getEntity().getContent()); + assertThat(httpStepDefs.response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(httpStepDefs.jsonPath.>read(ARGUMENTS + ".messageIds")).contains(messageId.serialize()); } } diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/GetMessagesMethodStepdefs.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/GetMessagesMethodStepdefs.java index d4ca9a5cfca..524b46ca00c 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/GetMessagesMethodStepdefs.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/GetMessagesMethodStepdefs.java @@ -38,8 +38,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; import org.apache.james.jmap.DefaultMailboxes; import org.apache.james.jmap.methods.integration.cucumber.util.TableRow; import org.apache.james.jmap.model.MessagePreviewGenerator; @@ -58,10 +56,6 @@ import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; -import com.jayway.jsonpath.Configuration; -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.Option; import cucumber.api.DataTable; import cucumber.api.java.en.Given; @@ -84,16 +78,17 @@ public class GetMessagesMethodStepdefs { private final MainStepdefs mainStepdefs; private final UserStepdefs userStepdefs; + private final HttpStepDefs httpStepDefs; private final Map messageIdsByName; - private HttpResponse response; - private DocumentContext jsonPath; private List requestedMessageIds; @Inject - private GetMessagesMethodStepdefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs) { + private GetMessagesMethodStepdefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs, + HttpStepDefs httpStepDefs) { this.mainStepdefs = mainStepdefs; this.userStepdefs = userStepdefs; + this.httpStepDefs = httpStepDefs; this.messageIdsByName = new HashMap<>(); } @@ -399,7 +394,7 @@ public void postWithAccountId(String user) throws Throwable { @When("^\the user ask for messages using its accountId$") public void postWithAccountId() throws Exception { - post("[[\"getMessages\", {\"accountId\": \"1\"}, \"#0\"]]"); + httpStepDefs.post("[[\"getMessages\", {\"accountId\": \"1\"}, \"#0\"]]"); } @When("^\"([^\"]*)\" ask for messages using unknown arguments$") @@ -409,12 +404,12 @@ public void postWithUnknownArguments(String user) throws Throwable { @When("^the user ask for messages using unknown arguments$") public void postWithUnknownArguments() throws Exception { - post("[[\"getMessages\", {\"WAT\": true}, \"#0\"]]"); + httpStepDefs.post("[[\"getMessages\", {\"WAT\": true}, \"#0\"]]"); } @When("^the user ask for messages using invalid argument$") public void postWithInvalidArguments() throws Exception { - post("[[\"getMessages\", {\"ids\": null}, \"#0\"]]"); + httpStepDefs.post("[[\"getMessages\", {\"ids\": null}, \"#0\"]]"); } @When("^\"([^\"]*)\" ask for messages using invalid argument$") @@ -424,7 +419,7 @@ public void postWithInvalidArguments(String user) throws Throwable { @When("^the user ask for messages$") public void post() throws Exception { - post("[[\"getMessages\", {\"ids\": []}, \"#0\"]]"); + httpStepDefs.post("[[\"getMessages\", {\"ids\": []}, \"#0\"]]"); } @When("^\"(.*?)\" ask for messages$") @@ -461,7 +456,7 @@ private void askMessages(List messageIds) throws Exception { .map(MessageId::serialize) .map(toJsonString()) .collect(Collectors.joining(",", "[", "]" )); - post("[[\"getMessages\", {\"ids\": " + serializedIds + "}, \"#0\"]]"); + httpStepDefs.post("[[\"getMessages\", {\"ids\": " + serializedIds + "}, \"#0\"]]"); } private Function toJsonString() { @@ -488,7 +483,7 @@ public void postWithParameters(List ids, List properties) throws .map(toJsonString()) .collect(Collectors.joining(",", "[", "]" )); - post("[[\"getMessages\", {\"ids\": " + serializedIds + ", \"properties\": " + serializedProperties + "}, \"#0\"]]"); + httpStepDefs.post("[[\"getMessages\", {\"ids\": " + serializedIds + ", \"properties\": " + serializedProperties + "}, \"#0\"]]"); } private Pair entryToPair(Map.Entry entry) { @@ -499,67 +494,55 @@ private String joinKeyValue(Pair pair) { return Joiner.on(": ").join(pair); } - private void post(String requestBody) throws Exception { - response = Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) - .addHeader("Authorization", userStepdefs.authenticate(userStepdefs.getConnectedUser()).serialize()) - .addHeader("Accept", org.apache.http.entity.ContentType.APPLICATION_JSON.getMimeType()) - .bodyString(requestBody, org.apache.http.entity.ContentType.APPLICATION_JSON) - .execute() - .returnResponse(); - jsonPath = JsonPath.using(Configuration.defaultConfiguration() - .addOptions(Option.SUPPRESS_EXCEPTIONS)) - .parse(response.getEntity().getContent()); - } - @Then("^an error \"([^\"]*)\" is returned$") public void error(String type) throws Exception { - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - assertThat(jsonPath.read(NAME)).isEqualTo("error"); - assertThat(jsonPath.read(ARGUMENTS + ".type")).isEqualTo(type); + assertThat(httpStepDefs.response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(httpStepDefs.jsonPath.read(NAME)).isEqualTo("error"); + assertThat(httpStepDefs.jsonPath.read(ARGUMENTS + ".type")).isEqualTo(type); } @Then("^no error is returned$") public void noError() throws Exception { - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - assertThat(jsonPath.read(NAME)).isEqualTo("messages"); + assertThat(httpStepDefs.response.getStatusLine().getStatusCode()).isEqualTo(200); + assertThat(httpStepDefs.jsonPath.read(NAME)).isEqualTo("messages"); } @Then("^the list of unknown messages is empty$") public void assertNotFoundIsEmpty() { - assertThat(jsonPath.>read(ARGUMENTS + ".notFound")).isEmpty(); + assertThat(httpStepDefs.jsonPath.>read(ARGUMENTS + ".notFound")).isEmpty(); } @Then("^the list of messages is empty$") public void assertListIsEmpty() { - assertThat(jsonPath.>read(ARGUMENTS + ".list")).isEmpty(); + assertThat(httpStepDefs.jsonPath.>read(ARGUMENTS + ".list")).isEmpty(); } @Then("^the description is \"(.*?)\"$") public void assertDescription(String description) throws Exception { - assertThat(jsonPath.read(ARGUMENTS + ".description")).isEqualTo(description); + assertThat(httpStepDefs.jsonPath.read(ARGUMENTS + ".description")).isEqualTo(description); } @Then("^the notFound list should contain \"([^\"]*)\"$") public void assertNotFoundListContains(String ids) throws Exception { - assertThat(jsonPath.>read(ARGUMENTS + ".notFound")).contains(ids); + assertThat(httpStepDefs.jsonPath.>read(ARGUMENTS + ".notFound")).contains(ids); } @Then("^the notFound list should contain the requested message id$") public void assertNotFoundListContainsRequestedMessages() throws Exception { ImmutableList elements = requestedMessageIds.stream().map(MessageId::serialize).collect(Guavate.toImmutableList()); - assertThat(jsonPath.>read(ARGUMENTS + ".notFound")).containsExactlyElementsOf(elements); + assertThat(httpStepDefs.jsonPath.>read(ARGUMENTS + ".notFound")).containsExactlyElementsOf(elements); } @Then("^the list should contain (\\d+) message$") public void assertListContains(int numberOfMessages) throws Exception { - assertThat(jsonPath.>read(ARGUMENTS + ".list")).hasSize(numberOfMessages); + assertThat(httpStepDefs.jsonPath.>read(ARGUMENTS + ".list")).hasSize(numberOfMessages); } @Then("^the id of the message is \"([^\"]*)\"$") public void assertIdOfTheFirstMessage(String messageName) throws Exception { MessageId id = messageIdsByName.get(messageName); - assertThat(jsonPath.read(FIRST_MESSAGE + ".id")).isEqualTo(id.serialize()); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".id")).isEqualTo(id.serialize()); } @Then("^the message is in \"([^\"]*)\" mailboxes") @@ -573,7 +556,7 @@ public void assertMailboxNamesOfTheFirstMessage(String mailboxNames) throws Exce .serialize())) .distinct() .collect(Guavate.toImmutableList()); - assertThat(jsonPath.read(FIRST_MESSAGE + ".mailboxIds")) + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".mailboxIds")) .hasSize(values.size()) .containsOnlyElementsOf(values); } @@ -591,7 +574,7 @@ public void assertMailboxNamesOfTheFirstMessageWithUser(String mailboxIds) throw .serialize())) .distinct() .collect(Guavate.toImmutableList()); - assertThat(jsonPath.read(FIRST_MESSAGE + ".mailboxIds")) + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".mailboxIds")) .hasSize(values.size()) .containsOnlyElementsOf(values); } @@ -599,44 +582,44 @@ public void assertMailboxNamesOfTheFirstMessageWithUser(String mailboxIds) throw @Then("^the threadId of the message is \"([^\"]*)\"$") public void assertThreadIdOfTheFirstMessage(String threadId) throws Exception { MessageId id = messageIdsByName.get(threadId); - assertThat(jsonPath.read(FIRST_MESSAGE + ".threadId")).isEqualTo(id.serialize()); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".threadId")).isEqualTo(id.serialize()); } @Then("^the subject of the message is \"([^\"]*)\"$") public void assertSubjectOfTheFirstMessage(String subject) throws Exception { - assertThat(jsonPath.read(FIRST_MESSAGE + ".subject")).isEqualTo(subject); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".subject")).isEqualTo(subject); } @Then("^the textBody of the message is \"([^\"]*)\"$") public void assertTextBodyOfTheFirstMessage(String textBody) throws Exception { - assertThat(jsonPath.read(FIRST_MESSAGE + ".textBody")).isEqualTo(StringEscapeUtils.unescapeJava(textBody)); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".textBody")).isEqualTo(StringEscapeUtils.unescapeJava(textBody)); } @Then("^the htmlBody of the message is \"([^\"]*)\"$") public void assertHtmlBodyOfTheFirstMessage(String htmlBody) throws Exception { - assertThat(jsonPath.read(FIRST_MESSAGE + ".htmlBody")).isEqualTo(StringEscapeUtils.unescapeJava(htmlBody)); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".htmlBody")).isEqualTo(StringEscapeUtils.unescapeJava(htmlBody)); } @Then("^the isUnread of the message is \"([^\"]*)\"$") public void assertIsUnreadOfTheFirstMessage(String isUnread) throws Exception { - assertThat(jsonPath.read(FIRST_MESSAGE + ".isUnread")).isEqualTo(Boolean.valueOf(isUnread)); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".isUnread")).isEqualTo(Boolean.valueOf(isUnread)); } @Then("^the preview of the message is \"([^\"]*)\"$") public void assertPreviewOfTheFirstMessage(String preview) throws Exception { - String actual = jsonPath.read(FIRST_MESSAGE + ".preview").replace("\n", " "); + String actual = httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".preview").replace("\n", " "); assertThat(actual).isEqualToIgnoringWhitespace(StringEscapeUtils.unescapeJava(preview)); } @Then("^the preview of the message is not empty$") public void assertPreviewOfTheFirstMessageIsNotEmpty() throws Exception { - String actual = jsonPath.read(FIRST_MESSAGE + ".preview"); + String actual = httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".preview"); assertThat(actual).isNotEmpty(); } @Then("^the preview should not contain consecutive spaces or blank characters$") public void assertPreviewShouldBeNormalized() throws Exception { - String actual = jsonPath.read(FIRST_MESSAGE + ".preview"); + String actual = httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".preview"); assertThat(actual).hasSize(MessagePreviewGenerator.MAX_PREVIEW_LENGTH) .doesNotMatch(" ") .doesNotContain(StringUtils.CR) @@ -645,37 +628,37 @@ public void assertPreviewShouldBeNormalized() throws Exception { @Then("^the headers of the message contains:$") public void assertHeadersOfTheFirstMessage(DataTable headers) throws Exception { - assertThat(jsonPath.>read(FIRST_MESSAGE + ".headers")).containsAllEntriesOf(headers.asMap(String.class, String.class)); + assertThat(httpStepDefs.jsonPath.>read(FIRST_MESSAGE + ".headers")).containsAllEntriesOf(headers.asMap(String.class, String.class)); } @Then("^the date of the message is \"([^\"]*)\"$") public void assertDateOfTheFirstMessage(String date) throws Exception { - assertThat(jsonPath.read(FIRST_MESSAGE + ".date")).isEqualTo(date); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".date")).isEqualTo(date); } @Then("^the hasAttachment of the message is \"([^\"]*)\"$") public void assertHasAttachmentOfTheFirstMessage(String hasAttachment) throws Exception { - assertThat(jsonPath.read(FIRST_MESSAGE + ".hasAttachment")).isEqualTo(Boolean.valueOf(hasAttachment)); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".hasAttachment")).isEqualTo(Boolean.valueOf(hasAttachment)); } @Then("^the list of attachments of the message is empty$") public void assertAttachmentsOfTheFirstMessageIsEmpty() throws Exception { - assertThat(jsonPath.>read(ATTACHMENTS)).isEmpty(); + assertThat(httpStepDefs.jsonPath.>read(ATTACHMENTS)).isEmpty(); } @Then("^the property \"([^\"]*)\" of the message is null$") public void assertPropertyIsNull(String property) throws Exception { - assertThat(jsonPath.read(FIRST_MESSAGE + "." + property + ".date")).isNull(); + assertThat(httpStepDefs.jsonPath.read(FIRST_MESSAGE + "." + property + ".date")).isNull(); } @Then("^the list of attachments of the message contains (\\d+) attachments?$") public void assertAttachmentsHasSize(int numberOfAttachments) throws Exception { - assertThat(jsonPath.>read(ATTACHMENTS)).hasSize(numberOfAttachments); + assertThat(httpStepDefs.jsonPath.>read(ATTACHMENTS)).hasSize(numberOfAttachments); } @Then("^the list of attachments of the message contains only one attachment with cid \"([^\"]*)\"?$") public void assertAttachmentsAndItsCid(String cid) throws Exception { - assertThat(jsonPath.read(FIRST_ATTACHMENT + ".cid")).isEqualTo(cid); + assertThat(httpStepDefs.jsonPath.read(FIRST_ATTACHMENT + ".cid")).isEqualTo(cid); } @Then("^the first attachment is:$") @@ -690,28 +673,28 @@ public void assertSecondAttachment(DataTable attachmentProperties) throws Except @Then("^the preview of the message contains: (.*)$") public void assertPreviewOfMessageShouldBePrintedWithEncoding(List preview) throws Exception { - String actual = jsonPath.read(FIRST_MESSAGE + ".preview"); + String actual = httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".preview"); assertThat(actual).contains(preview); } @Then("^the keywords of the message is (.*)$") public void assertKeywordsOfMessageShouldDisplay(List keywords) throws Exception { - assertThat(jsonPath.>read(FIRST_MESSAGE + ".keywords").keySet()) + assertThat(httpStepDefs.jsonPath.>read(FIRST_MESSAGE + ".keywords").keySet()) .containsOnlyElementsOf(keywords); } @Then("^the message has no keyword$") public void assertMessageHasNoKeyword() throws Exception { - assertThat(jsonPath.>read(FIRST_MESSAGE + ".keywords")) + assertThat(httpStepDefs.jsonPath.>read(FIRST_MESSAGE + ".keywords")) .isNullOrEmpty(); } private void assertAttachment(String attachment, DataTable attachmentProperties) { attachmentProperties.asList(TableRow.class) - .forEach(entry -> assertThat(jsonPath.read(attachment + "." + entry.getKey())).isEqualTo(entry.getValue())); + .forEach(entry -> assertThat(httpStepDefs.jsonPath.read(attachment + "." + entry.getKey())).isEqualTo(entry.getValue())); } public String getBlobId() { - return jsonPath.read(FIRST_MESSAGE + ".blobId"); + return httpStepDefs.jsonPath.read(FIRST_MESSAGE + ".blobId"); } } diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/HttpStepDefs.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/HttpStepDefs.java new file mode 100644 index 00000000000..cbf57d52138 --- /dev/null +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/HttpStepDefs.java @@ -0,0 +1,56 @@ +/**************************************************************** + * 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.jmap.methods.integration.cucumber; + +import javax.inject.Inject; + +import org.apache.http.HttpResponse; +import org.apache.http.client.fluent.Request; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; + +public class HttpStepDefs { + private final MainStepdefs mainStepdefs; + private final UserStepdefs userStepdefs; + + public HttpResponse response; + public DocumentContext jsonPath; + + @Inject + public HttpStepDefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs) { + this.mainStepdefs = mainStepdefs; + this.userStepdefs = userStepdefs; + } + + public void post(String requestBody) throws Exception { + response = Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) + .addHeader("Authorization", userStepdefs.authenticate(userStepdefs.getConnectedUser()).serialize()) + .addHeader("Accept", org.apache.http.entity.ContentType.APPLICATION_JSON.getMimeType()) + .bodyString(requestBody, org.apache.http.entity.ContentType.APPLICATION_JSON) + .execute() + .returnResponse(); + jsonPath = JsonPath.using(Configuration.defaultConfiguration() + .addOptions(Option.SUPPRESS_EXCEPTIONS)) + .parse(response.getEntity().getContent()); + } +} diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMailboxesMethodStepdefs.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMailboxesMethodStepdefs.java index dfd785d4d5f..b83afacbe21 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMailboxesMethodStepdefs.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMailboxesMethodStepdefs.java @@ -29,9 +29,6 @@ import javax.inject.Inject; import javax.mail.Flags; -import org.apache.http.HttpResponse; -import org.apache.http.client.fluent.Request; -import org.apache.http.entity.ContentType; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.store.mail.model.Mailbox; @@ -55,11 +52,13 @@ public class SetMailboxesMethodStepdefs { private final MainStepdefs mainStepdefs; private final UserStepdefs userStepdefs; + private final HttpStepDefs httpStepDefs; @Inject - private SetMailboxesMethodStepdefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs) { + private SetMailboxesMethodStepdefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs, HttpStepDefs httpStepDefs) { this.mainStepdefs = mainStepdefs; this.userStepdefs = userStepdefs; + this.httpStepDefs = httpStepDefs; } @Given("^mailbox \"([^\"]*)\" with (\\d+) messages$") @@ -81,7 +80,6 @@ private void appendMessage(MailboxPath mailboxPath, int i) throws MailboxExcepti @When("^renaming mailbox \"([^\"]*)\" to \"([^\"]*)\"") public void renamingMailbox(String actualMailboxName, String newMailboxName) throws Throwable { - String username = userStepdefs.getConnectedUser(); Mailbox mailbox = mainStepdefs.mailboxProbe.getMailbox("#private", userStepdefs.getConnectedUser(), actualMailboxName); String mailboxId = mailbox.getMailboxId().serialize(); String requestBody = @@ -97,11 +95,7 @@ public void renamingMailbox(String actualMailboxName, String newMailboxName) thr " \"#0\"" + " ]" + "]"; - Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) - .addHeader("Authorization", userStepdefs.authenticate(username).serialize()) - .bodyString(requestBody, ContentType.APPLICATION_JSON) - .execute() - .discardContent(); + httpStepDefs.post(requestBody); } @When("^moving mailbox \"([^\"]*)\" to \"([^\"]*)\"$") @@ -125,12 +119,7 @@ public void movingMailbox(String actualMailboxPath, String newParentMailboxPath) " \"#0\"" + " ]" + "]"; - - Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) - .addHeader("Authorization", userStepdefs.authenticate(username).serialize()) - .bodyString(requestBody, ContentType.APPLICATION_JSON) - .execute() - .discardContent(); + httpStepDefs.post(requestBody); } @Then("^mailbox \"([^\"]*)\" contains (\\d+) messages$") @@ -141,13 +130,12 @@ public void mailboxContainsMessages(String mailboxName, int messageCount) throws String mailboxId = mailbox.getMailboxId().serialize(); Awaitility.await().atMost(Duration.FIVE_SECONDS).pollDelay(slowPacedPollInterval).pollInterval(slowPacedPollInterval).until(() -> { - HttpResponse response = Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) - .addHeader("Authorization", userStepdefs.authenticate(username).serialize()) - .bodyString("[[\"getMessageList\", {\"filter\":{\"inMailboxes\":[\"" + mailboxId + "\"]}}, \"#0\"]]", ContentType.APPLICATION_JSON) - .execute().returnResponse(); + String requestBody = "[[\"getMessageList\", {\"filter\":{\"inMailboxes\":[\"" + mailboxId + "\"]}}, \"#0\"]]"; + + httpStepDefs.post(requestBody); - assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); - DocumentContext jsonPath = JsonPath.parse(response.getEntity().getContent()); + assertThat(httpStepDefs.response.getStatusLine().getStatusCode()).isEqualTo(200); + DocumentContext jsonPath = JsonPath.parse(httpStepDefs.response.getEntity().getContent()); assertThat(jsonPath.read(NAME)).isEqualTo("messageList"); return jsonPath.>read(ARGUMENTS + ".messageIds").size() == messageCount; diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java index 64a0d0b737d..7676d9a0641 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/cucumber/SetMessagesMethodStepdefs.java @@ -25,8 +25,6 @@ import javax.inject.Inject; import javax.mail.Flags; -import org.apache.http.entity.ContentType; -import org.apache.http.client.fluent.Request; import org.apache.james.jmap.model.Keywords; import org.apache.james.mailbox.model.MailboxConstants; import org.apache.james.mailbox.model.MailboxId; @@ -43,25 +41,26 @@ public class SetMessagesMethodStepdefs { private final MainStepdefs mainStepdefs; private final UserStepdefs userStepdefs; + private final HttpStepDefs httpStepDefs; private final GetMessagesMethodStepdefs getMessagesMethodStepdefs; @Inject - private SetMessagesMethodStepdefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs, GetMessagesMethodStepdefs getMessagesMethodStepdefs) { + private SetMessagesMethodStepdefs(MainStepdefs mainStepdefs, UserStepdefs userStepdefs, HttpStepDefs httpStepDefs, GetMessagesMethodStepdefs getMessagesMethodStepdefs) { this.mainStepdefs = mainStepdefs; this.userStepdefs = userStepdefs; + this.httpStepDefs = httpStepDefs; this.getMessagesMethodStepdefs = getMessagesMethodStepdefs; } @When("^the user move \"([^\"]*)\" to mailbox \"([^\"]*)\"") public void moveMessageToMailbox(String message, String mailbox) throws Throwable { - String username = userStepdefs.getConnectedUser(); MessageId messageId = getMessagesMethodStepdefs.getMessageId(message); MailboxId mailboxId = mainStepdefs.jmapServer .getProbe(MailboxProbeImpl.class) .getMailbox(MailboxConstants.USER_NAMESPACE, userStepdefs.getConnectedUser(), mailbox) .getMailboxId(); - String requestBody = "[" + + httpStepDefs.post("[" + " [" + " \"setMessages\","+ " {" + @@ -71,12 +70,7 @@ public void moveMessageToMailbox(String message, String mailbox) throws Throwabl " }," + " \"#0\"" + " ]" + - "]"; - Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) - .addHeader("Authorization", userStepdefs.authenticate(username).serialize()) - .bodyString(requestBody, ContentType.APPLICATION_JSON) - .execute() - .discardContent(); + "]"); mainStepdefs.awaitMethod.run(); } @@ -92,7 +86,8 @@ public void copyMessageToMailbox(String userName, String message, String sourceM .getMailbox(MailboxConstants.USER_NAMESPACE, userName, destinationMailbox) .getMailboxId(); - String requestBody = "[" + + + httpStepDefs.post("[" + " [" + " \"setMessages\","+ " {" + @@ -102,39 +97,29 @@ public void copyMessageToMailbox(String userName, String message, String sourceM " }," + " \"#0\"" + " ]" + - "]"; - Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) - .addHeader("Authorization", userStepdefs.authenticate(userName).serialize()) - .bodyString(requestBody, ContentType.APPLICATION_JSON) - .execute() - .discardContent(); + "]"); mainStepdefs.awaitMethod.run(); } - @When("^\"([^\"]*)\" set flags on \"([^\"]*)\" to \"([^\"]*)\"") - public void setFlags(String username, String message, List keywords) throws Throwable { + @When("^the user set flags on \"([^\"]*)\" to \"([^\"]*)\"") + public void setFlags(String message, List keywords) throws Throwable { MessageId messageId = getMessagesMethodStepdefs.getMessageId(message); String keywordString = keywords .stream() .map(value -> "\"" + value + "\" : true") .collect(Collectors.joining(",")); - Request.Post(mainStepdefs.baseUri().setPath("/jmap").build()) - .addHeader("Authorization", userStepdefs.authenticate(username).serialize()) - .bodyString("[" + - " [" + - " \"setMessages\","+ - " {" + - " \"update\": { \"" + messageId.serialize() + "\" : {" + - " \"keywords\": {" + keywordString + "}" + - " }}" + - " }," + - " \"#0\"" + - " ]" + - "]", - ContentType.APPLICATION_JSON) - .execute() - .discardContent(); + httpStepDefs.post("[" + + " [" + + " \"setMessages\","+ + " {" + + " \"update\": { \"" + messageId.serialize() + "\" : {" + + " \"keywords\": {" + keywordString + "}" + + " }}" + + " }," + + " \"#0\"" + + " ]" + + "]"); mainStepdefs.awaitMethod.run(); }