http://git-wip-us.apache.org/repos/asf/james-project/blob/517a4cfe/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java b/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java deleted file mode 100644 index aeca639..0000000 --- a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java +++ /dev/null @@ -1,429 +0,0 @@ -/**************************************************************** - * 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; - -import static com.jayway.restassured.RestAssured.given; -import static com.jayway.restassured.config.EncoderConfig.encoderConfig; -import static com.jayway.restassured.config.RestAssuredConfig.newConfig; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.isEmptyOrNullString; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.nullValue; - -import java.io.ByteArrayInputStream; -import java.util.Date; - -import javax.mail.Flags; - -import org.apache.james.backends.cassandra.EmbeddedCassandra; -import org.apache.james.jmap.JmapAuthentication; -import org.apache.james.jmap.JmapServer; -import org.apache.james.jmap.api.access.AccessToken; -import org.apache.james.mailbox.cassandra.CassandraId; -import org.apache.james.mailbox.elasticsearch.EmbeddedElasticSearch; -import org.apache.james.mailbox.model.MailboxConstants; -import org.apache.james.mailbox.model.MailboxPath; -import org.apache.james.mailbox.store.mail.model.Mailbox; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TemporaryFolder; - -import com.google.common.base.Charsets; -import com.jayway.restassured.RestAssured; -import com.jayway.restassured.http.ContentType; - -public abstract class GetMailboxesMethodTest { - private static final String NAME = "[0][0]"; - private static final String ARGUMENTS = "[0][1]"; - - private TemporaryFolder temporaryFolder = new TemporaryFolder(); - private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(); - private EmbeddedCassandra cassandra = EmbeddedCassandra.createStartServer(); - private JmapServer jmapServer = jmapServer(temporaryFolder, embeddedElasticSearch, cassandra); - - protected abstract JmapServer jmapServer(TemporaryFolder temporaryFolder, EmbeddedElasticSearch embeddedElasticSearch, EmbeddedCassandra cassandra); - - @Rule - public RuleChain chain = RuleChain - .outerRule(temporaryFolder) - .around(embeddedElasticSearch) - .around(jmapServer); - - private AccessToken accessToken; - private String username; - - @Before - public void setup() throws Exception { - RestAssured.port = jmapServer.getPort(); - RestAssured.config = newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)); - - String domain = "domain.tld"; - username = "username@" + domain; - String password = "password"; - jmapServer.serverProbe().addDomain(domain); - jmapServer.serverProbe().addUser(username, password); - accessToken = JmapAuthentication.authenticateJamesUser(username, password); - } - - @Test - public void getMailboxesShouldErrorNotSupportedWhenRequestContainsNonNullAccountId() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"accountId\": \"1\"}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("error")) - .body(ARGUMENTS + ".type", equalTo("Not yet implemented")); - } - - @Test - public void getMailboxesShouldReturnEmptyWhenIdsDoesntMatch() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "name"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"ids\": [\"notAMailboxId\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", hasSize(0)); - } - - @Test - public void getMailboxesShouldReturnMailboxesWhenIdsMatch() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "INBOX"); - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "myMailbox"); - - Mailbox<CassandraId> mailbox = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE, username, "INBOX"); - Mailbox<CassandraId> mailbox2 = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE, username, "myMailbox"); - - String mailboxId = mailbox.getMailboxId().serialize(); - String mailboxId2 = mailbox2.getMailboxId().serialize(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"ids\": [\"" + mailboxId + "\", \"" + mailboxId2 + "\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", hasSize(2)) - .body(ARGUMENTS + ".list[0].id", equalTo(mailboxId)) - .body(ARGUMENTS + ".list[1].id", equalTo(mailboxId2)); - } - - @Test - public void getMailboxesShouldReturnOnlyMatchingMailboxesWhenIdsGiven() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "INBOX"); - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "myMailbox"); - - Mailbox<CassandraId> mailbox = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE, username, "INBOX"); - - String mailboxId = mailbox.getMailboxId().serialize(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"ids\": [\"" + mailboxId + "\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", hasSize(1)) - .body(ARGUMENTS + ".list[0].id", equalTo(mailboxId)); - } - - @Test - public void getMailboxesShouldReturnEmptyWhenIdsIsEmpty() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "INBOX"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"ids\": []}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", empty()); - } - - @Test - public void getMailboxesShouldReturnAllMailboxesWhenIdsIsNull() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "INBOX"); - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "myMailbox"); - - Mailbox<CassandraId> mailbox = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE, username, "INBOX"); - Mailbox<CassandraId> mailbox2 = jmapServer.serverProbe().getMailbox(MailboxConstants.USER_NAMESPACE, username, "myMailbox"); - - String mailboxId = mailbox.getMailboxId().serialize(); - String mailboxId2 = mailbox2.getMailboxId().serialize(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"ids\": null}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", hasSize(2)) - .body(ARGUMENTS + ".list[0].id", equalTo(mailboxId)) - .body(ARGUMENTS + ".list[1].id", equalTo(mailboxId2)); - } - - @Test - public void getMailboxesShouldErrorInvalidArgumentsWhenRequestIsInvalid() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"ids\": true}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("error")) - .body(ARGUMENTS + ".type", equalTo("invalidArguments")) - .body(ARGUMENTS + ".description", equalTo("Can not deserialize instance of java.util.ArrayList out of VALUE_TRUE token\n" - + " at [Source: {\"ids\":true}; line: 1, column: 2] (through reference chain: org.apache.james.jmap.model.Builder[\"ids\"])")); - } - - @Test - public void getMailboxesShouldReturnEmptyListWhenNoMailboxes() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", empty()); - } - - @Test - public void getMailboxesShouldReturnDefaultMailboxesWhenAuthenticatedUserDoesntHaveAnAccountYet() throws Exception { - - String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMzM3QGRvbWFpbi50bGQiLCJuYW1lIjoiTmV3IFVzZXIif" - + "Q.fxNWNzksXyCij2ooVi-QfGe9vTicF2N9FDtWSJdjWTjhwoQ_i0dgiT8clp4dtOJzy78hB2UkAW-iq7z3PR_Gz0qFah7EbYoEs" - + "5lQs1UlhNGCRTvIsyR8qHUXtA6emw9x0nuMnswtyXhzoA-cEHCArrMxMeWhTYi2l4od3G8Irrvu1Yc5hKLwLgPdnImbKyB5a89T" - + "vzuZE8-FVyMmhlaJA2T1GpbsaUnfE1ki_bBzqMHTD_Ob7oSVzz2UOiOeL-ombn1X9GbYQ2I-Ob4V84WHONYxw0VjPHlj9saZ2n7" - + "2RJTBsIo6flJT-MchaEvTYBvuV_wlCCQYjI1g7mdeD6aXfw"; - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", "Bearer " + token) - .body("[[\"getMailboxes\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", hasSize(3)) - .body(ARGUMENTS + ".list.name", hasItems("INBOX", "Outbox", "Sent")); - } - - @Test - public void getMailboxesShouldErrorWithBadJWTToken() { - - String badAuthToken = "BADTOKENOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.T04BTk" + - "LXkJj24coSZkK13RfG25lpvmSl2MJ7N10KpBk9_-95EGYZdog-BDAn3PJzqVw52z-Bwjh4VOj1-j7cURu0cT4jXehhUrlCxS4n7QHZ" + - "DN_bsEYGu7KzjWTpTsUiHe-rN7izXVFxDGG1TGwlmBCBnPW-EFCf9ylUsJi0r2BKNdaaPRfMIrHptH1zJBkkUziWpBN1RNLjmvlAUf" + - "49t1Tbv21ZqYM5Ht2vrhJWczFbuC-TD-8zJkXhjTmA1GVgomIX5dx1cH-dZX1wANNmshUJGHgepWlPU-5VIYxPEhb219RMLJIELMY2" + - "qNOR8Q31ydinyqzXvCSzVJOf6T60-w"; - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", "Bearer " + badAuthToken) - .body("[[\"getMailboxes\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(401); - } - - @Test - public void getMailboxesShouldReturnMailboxesWhenAvailable() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "name"); - - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "name"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list[0].name", equalTo("name")) - .body(ARGUMENTS + ".list[0].parentId", nullValue()) - .body(ARGUMENTS + ".list[0].role", nullValue()) - .body(ARGUMENTS + ".list[0].sortOrder", equalTo(1000)) - .body(ARGUMENTS + ".list[0].mustBeOnlyMailbox", equalTo(false)) - .body(ARGUMENTS + ".list[0].mayReadItems", equalTo(false)) - .body(ARGUMENTS + ".list[0].mayAddItems", equalTo(false)) - .body(ARGUMENTS + ".list[0].mayRemoveItems", equalTo(false)) - .body(ARGUMENTS + ".list[0].mayCreateChild", equalTo(false)) - .body(ARGUMENTS + ".list[0].mayRename", equalTo(false)) - .body(ARGUMENTS + ".list[0].mayDelete", equalTo(false)) - .body(ARGUMENTS + ".list[0].totalMessages", equalTo(1)) - .body(ARGUMENTS + ".list[0].unreadMessages", equalTo(1)) - .body(ARGUMENTS + ".list[0].unreadThreads", equalTo(0)); - } - - @Test - public void getMailboxesShouldReturnFilteredMailboxesPropertiesWhenRequestContainsFilterProperties() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "name"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"properties\" : [\"unreadMessages\", \"sortOrder\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list[0].id", not(isEmptyOrNullString())) - .body(ARGUMENTS + ".list[0].name", nullValue()) - .body(ARGUMENTS + ".list[0].parentId", nullValue()) - .body(ARGUMENTS + ".list[0].role", nullValue()) - .body(ARGUMENTS + ".list[0].sortOrder", equalTo(1000)) - .body(ARGUMENTS + ".list[0].mustBeOnlyMailbox", nullValue()) - .body(ARGUMENTS + ".list[0].mayReadItems", nullValue()) - .body(ARGUMENTS + ".list[0].mayAddItems", nullValue()) - .body(ARGUMENTS + ".list[0].mayRemoveItems", nullValue()) - .body(ARGUMENTS + ".list[0].mayCreateChild", nullValue()) - .body(ARGUMENTS + ".list[0].mayRename", nullValue()) - .body(ARGUMENTS + ".list[0].mayDelete", nullValue()) - .body(ARGUMENTS + ".list[0].totalMessages", nullValue()) - .body(ARGUMENTS + ".list[0].unreadMessages", equalTo(0)) - .body(ARGUMENTS + ".list[0].unreadThreads", nullValue()); - } - - @Test - public void getMailboxesShouldReturnIdWhenRequestContainsEmptyPropertyListFilter() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "name"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"properties\" : []}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list[0].id", not(isEmptyOrNullString())) - .body(ARGUMENTS + ".list[0].name", nullValue()); - } - - @Test - public void getMailboxesShouldIgnoreUnknownPropertiesWhenRequestContainsUnknownPropertyListFilter() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "name"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {\"properties\" : [\"unknown\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list[0].id", not(isEmptyOrNullString())) - .body(ARGUMENTS + ".list[0].name", nullValue()); - } - - @Test - public void getMailboxesShouldReturnMailboxesWithSortOrder() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "inbox"); - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "trash"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", hasSize(2)) - .body(ARGUMENTS + ".list[0].name", equalTo("inbox")) - .body(ARGUMENTS + ".list[0].sortOrder", equalTo(10)) - .body(ARGUMENTS + ".list[1].name", equalTo("trash")) - .body(ARGUMENTS + ".list[1].sortOrder", equalTo(60)); - } - - @Test - public void getMailboxesShouldReturnMailboxesWithRolesInLowerCase() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "outbox"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("mailboxes")) - .body(ARGUMENTS + ".list", hasSize(1)) - .body(ARGUMENTS + ".list[0].role", equalTo("outbox")); - } - -}
http://git-wip-us.apache.org/repos/asf/james-project/blob/517a4cfe/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java b/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java deleted file mode 100644 index 4013840..0000000 --- a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java +++ /dev/null @@ -1,516 +0,0 @@ -/**************************************************************** - * 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; - -import static com.jayway.restassured.RestAssured.given; -import static com.jayway.restassured.RestAssured.with; -import static com.jayway.restassured.config.EncoderConfig.encoderConfig; -import static com.jayway.restassured.config.RestAssuredConfig.newConfig; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasSize; - -import java.io.ByteArrayInputStream; -import java.time.LocalDate; -import java.util.Date; -import java.util.List; - -import javax.mail.Flags; - -import org.apache.james.backends.cassandra.EmbeddedCassandra; -import org.apache.james.jmap.JmapAuthentication; -import org.apache.james.jmap.JmapServer; -import org.apache.james.jmap.api.access.AccessToken; -import org.apache.james.mailbox.elasticsearch.EmbeddedElasticSearch; -import org.apache.james.mailbox.model.MailboxConstants; -import org.apache.james.mailbox.model.MailboxPath; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TemporaryFolder; - -import com.google.common.base.Charsets; -import com.jayway.restassured.RestAssured; -import com.jayway.restassured.http.ContentType; - -public abstract class GetMessageListMethodTest { - private static final String NAME = "[0][0]"; - private static final String ARGUMENTS = "[0][1]"; - - private TemporaryFolder temporaryFolder = new TemporaryFolder(); - private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(); - private EmbeddedCassandra cassandra = EmbeddedCassandra.createStartServer(); - private JmapServer jmapServer = jmapServer(temporaryFolder, embeddedElasticSearch, cassandra); - - protected abstract JmapServer jmapServer(TemporaryFolder temporaryFolder, EmbeddedElasticSearch embeddedElasticSearch, EmbeddedCassandra cassandra); - - @Rule - public RuleChain chain = RuleChain - .outerRule(temporaryFolder) - .around(embeddedElasticSearch) - .around(jmapServer); - - private AccessToken accessToken; - private String username; - private String domain; - - @Before - public void setup() throws Exception { - RestAssured.port = jmapServer.getPort(); - RestAssured.config = newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)); - - this.domain = "domain.tld"; - this.username = "username@" + domain; - String password = "password"; - jmapServer.serverProbe().addDomain(domain); - jmapServer.serverProbe().addUser(username, password); - this.accessToken = JmapAuthentication.authenticateJamesUser(username, password); - } - - @Test - public void getMessageListShouldErrorInvalidArgumentsWhenRequestIsInvalid() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"filter\": true}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("error")) - .body(ARGUMENTS + ".type", equalTo("invalidArguments")) - .body(ARGUMENTS + ".description", equalTo("Can not instantiate value of type [simple type, class org.apache.james.jmap.model.FilterCondition$Builder] from Boolean value (true); no single-boolean/Boolean-arg constructor/factory method\n" + - " at [Source: {\"filter\":true}; line: 1, column: 2] (through reference chain: org.apache.james.jmap.model.Builder[\"filter\"])")); - } - - @Test - public void getMessageListShouldReturnAllMessagesWhenSingleMailboxNoParameters() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", containsInAnyOrder("[email protected]|mailbox|1", "[email protected]|mailbox|2")); - } - - @Test - public void getMessageListShouldReturnAllMessagesWhenMultipleMailboxesAndNoParameters() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox2"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox2"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", containsInAnyOrder("[email protected]|mailbox|1", "[email protected]|mailbox2|1")); - } - - @Test - public void getMessageListShouldReturnAllMessagesOfCurrentUserOnlyWhenMultipleMailboxesAndNoParameters() throws Exception { - String otherUser = "other@" + domain; - String password = "password"; - jmapServer.serverProbe().addUser(otherUser, password); - - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox2"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox2"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, otherUser, "mailbox"); - jmapServer.serverProbe().appendMessage(otherUser, new MailboxPath(MailboxConstants.USER_NAMESPACE, otherUser, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", containsInAnyOrder("[email protected]|mailbox|1", "[email protected]|mailbox2|1")); - } - - @Test - public void getMessageListShouldFilterMessagesWhenInMailboxesFilterMatches() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - String mailboxId = - with() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {}, \"#0\"]]") - .post("/jmap") - .path("[0][1].list[0].id"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"filter\":{\"inMailboxes\":[\"" + mailboxId + "\"]}}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", contains("[email protected]|mailbox|1")); - } - - @Test - public void getMessageListShouldFilterMessagesWhenMultipleInMailboxesFilterMatches() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox2"); - embeddedElasticSearch.awaitForElasticSearch(); - - List<String> mailboxIds = - with() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMailboxes\", {}, \"#0\"]]") - .post("/jmap") - .path("[0][1].list.id"); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body(String.format("[[\"getMessageList\", {\"filter\":{\"inMailboxes\":[\"%s\", \"%s\"]}}, \"#0\"]]", mailboxIds.get(0), mailboxIds.get(1))) - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", contains("[email protected]|mailbox|1")); - } - - @Test - public void getMessageListShouldFilterMessagesWhenInMailboxesFilterDoesntMatches() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"filter\":{\"inMailboxes\":[\"mailbox2\"]}}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(ARGUMENTS + ".messageIds", empty()); - } - - @Test - public void getMessageListShouldSortMessagesWhenSortedByDateDefault() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"sort\":[\"date\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", contains("[email protected]|mailbox|1", "[email protected]|mailbox|2")); - } - - @Test - public void getMessageListShouldSortMessagesWhenSortedByDateAsc() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"sort\":[\"date asc\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", contains("[email protected]|mailbox|2", "[email protected]|mailbox|1")); - } - - @Test - public void getMessageListShouldSortMessagesWhenSortedByDateDesc() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"sort\":[\"date desc\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", contains("[email protected]|mailbox|1", "[email protected]|mailbox|2")); - } - - @Test - public void getMessageListShouldWorkWhenCollapseThreadIsFalse() { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"collapseThreads\":false}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")); - } - - @Test - public void getMessageListShouldWorkWhenCollapseThreadIsTrue() { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"collapseThreads\":true}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")); - } - - @Test - public void getMessageListShouldReturnAllMessagesWhenPositionIsNotGiven() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", containsInAnyOrder("[email protected]|mailbox|1", "[email protected]|mailbox|2")); - } - - @Test - public void getMessageListShouldReturnSkipMessagesWhenPositionIsGiven() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"position\":1}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", contains("[email protected]|mailbox|2")); - } - - @Test - public void getMessageListShouldReturnAllMessagesWhenLimitIsNotGiven() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", containsInAnyOrder("[email protected]|mailbox|1", "[email protected]|mailbox|2")); - } - - @Test - public void getMessageListShouldReturnLimitMessagesWhenLimitGiven() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"limit\":1}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", contains("[email protected]|mailbox|1")); - } - - @Test - public void getMessageListShouldReturnLimitMessagesWithDefaultValueWhenLimitIsNotGiven() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test2\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test3\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test4\r\n\r\ntestmail".getBytes()), new Date(date.toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messageList")) - .body(ARGUMENTS + ".messageIds", containsInAnyOrder("[email protected]|mailbox|1", "[email protected]|mailbox|2", "[email protected]|mailbox|3")); - } - - @Test - public void getMessageListShouldChainFetchingMessagesWhenAskedFor() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - LocalDate date = LocalDate.now(); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes()), new Date(date.plusDays(1).toEpochDay()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessageList\", {\"fetchMessages\":true}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body("[0][0]", equalTo("messageList")) - .body("[1][0]", equalTo("messages")) - .body("[0][1].messageIds", hasSize(1)) - .body("[0][1].messageIds[0]", equalTo("[email protected]|mailbox|1")) - .body("[1][1].list", hasSize(1)) - .body("[1][1].list[0].id", equalTo("[email protected]|mailbox|1")); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/517a4cfe/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java b/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java deleted file mode 100644 index 234c6b9..0000000 --- a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java +++ /dev/null @@ -1,339 +0,0 @@ -/**************************************************************** - * 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; - -import static com.jayway.restassured.RestAssured.given; -import static com.jayway.restassured.config.EncoderConfig.encoderConfig; -import static com.jayway.restassured.config.RestAssuredConfig.newConfig; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.nullValue; - -import java.io.ByteArrayInputStream; -import java.time.ZonedDateTime; -import java.util.Date; - -import javax.mail.Flags; - -import org.apache.james.backends.cassandra.EmbeddedCassandra; -import org.apache.james.jmap.JmapAuthentication; -import org.apache.james.jmap.JmapServer; -import org.apache.james.jmap.api.access.AccessToken; -import org.apache.james.mailbox.elasticsearch.EmbeddedElasticSearch; -import org.apache.james.mailbox.model.MailboxConstants; -import org.apache.james.mailbox.model.MailboxPath; -import org.elasticsearch.common.collect.ImmutableMap; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.RuleChain; -import org.junit.rules.TemporaryFolder; - -import com.google.common.base.Charsets; -import com.jayway.restassured.RestAssured; -import com.jayway.restassured.http.ContentType; - -public abstract class GetMessagesMethodTest { - private static final String NAME = "[0][0]"; - private static final String ARGUMENTS = "[0][1]"; - - private TemporaryFolder temporaryFolder = new TemporaryFolder(); - private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(); - private EmbeddedCassandra cassandra = EmbeddedCassandra.createStartServer(); - private JmapServer jmapServer = jmapServer(temporaryFolder, embeddedElasticSearch, cassandra); - - protected abstract JmapServer jmapServer(TemporaryFolder temporaryFolder, EmbeddedElasticSearch embeddedElasticSearch, EmbeddedCassandra cassandra); - - @Rule - public RuleChain chain = RuleChain - .outerRule(temporaryFolder) - .around(embeddedElasticSearch) - .around(jmapServer); - - private AccessToken accessToken; - private String username; - - @Before - public void setup() throws Exception { - RestAssured.port = jmapServer.getPort(); - RestAssured.config = newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)); - - String domain = "domain.tld"; - username = "username@" + domain; - String password = "password"; - jmapServer.serverProbe().addDomain(domain); - jmapServer.serverProbe().addUser(username, password); - jmapServer.serverProbe().createMailbox("#private", "username", "inbox"); - accessToken = JmapAuthentication.authenticateJamesUser(username, password); - } - - @Test - public void getMessagesShouldErrorNotSupportedWhenRequestContainsNonNullAccountId() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"accountId\": \"1\"}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("error")) - .body(ARGUMENTS + ".type", equalTo("Not yet implemented")); - } - - @Test - public void getMessagesShouldIgnoreUnknownArguments() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"WAT\": true}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(ARGUMENTS + ".notFound", empty()) - .body(ARGUMENTS + ".list", empty()); - } - - @Test - public void getMessagesShouldErrorInvalidArgumentsWhenRequestContainsInvalidArgument() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": null}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("error")) - .body(ARGUMENTS + ".type", equalTo("invalidArguments")) - .body(ARGUMENTS + ".description", equalTo("N/A (through reference chain: org.apache.james.jmap.model.Builder[\"ids\"])")); - } - - @Test - public void getMessagesShouldReturnEmptyListWhenNoMessage() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": []}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messages")) - .body(ARGUMENTS + ".list", empty()); - } - - @Test - public void getMessagesShouldReturnNoFoundIndicesWhenMessageNotFound() throws Exception { - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": [\"username|inbox|12\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messages")) - .body(ARGUMENTS + ".notFound", contains("username|inbox|12")); - } - - @Test - public void getMessagesShouldReturnMessagesWhenAvailable() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "inbox"); - - ZonedDateTime dateTime = ZonedDateTime.parse("2014-10-30T14:12:00Z"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "inbox"), - new ByteArrayInputStream("Subject: my test subject\r\n\r\ntestmail".getBytes()), Date.from(dateTime.toInstant()), false, new Flags()); - - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": [\"" + username + "|inbox|1\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messages")) - .body(ARGUMENTS + ".list", hasSize(1)) - .body(ARGUMENTS + ".list[0].id", equalTo(username + "|inbox|1")) - .body(ARGUMENTS + ".list[0].threadId", equalTo(username + "|inbox|1")) - .body(ARGUMENTS + ".list[0].subject", equalTo("my test subject")) - .body(ARGUMENTS + ".list[0].textBody", equalTo("testmail")) - .body(ARGUMENTS + ".list[0].isUnread", equalTo(true)) - .body(ARGUMENTS + ".list[0].preview", equalTo("testmail")) - .body(ARGUMENTS + ".list[0].headers", equalTo(ImmutableMap.of("subject", "my test subject"))) - .body(ARGUMENTS + ".list[0].date", equalTo("2014-10-30T14:12:00Z")); - } - - @Test - public void getMessagesShouldReturnMessageWhenHtmlMessage() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "inbox"); - - ZonedDateTime dateTime = ZonedDateTime.parse("2014-10-30T14:12:00Z"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "inbox"), - new ByteArrayInputStream("Content-Type: text/html\r\nSubject: my test subject\r\n\r\nThis is a <b>HTML</b> mail".getBytes()), Date.from(dateTime.toInstant()), false, new Flags()); - - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": [\"" + username + "|inbox|1\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messages")) - .body(ARGUMENTS + ".list", hasSize(1)) - .body(ARGUMENTS + ".list[0].id", equalTo(username + "|inbox|1")) - .body(ARGUMENTS + ".list[0].threadId", equalTo(username + "|inbox|1")) - .body(ARGUMENTS + ".list[0].subject", equalTo("my test subject")) - .body(ARGUMENTS + ".list[0].htmlBody", equalTo("This is a <b>HTML</b> mail")) - .body(ARGUMENTS + ".list[0].isUnread", equalTo(true)) - .body(ARGUMENTS + ".list[0].preview", equalTo("This is a <b>HTML</b> mail")) - .body(ARGUMENTS + ".list[0].headers", equalTo(ImmutableMap.of("content-type", "text/html", "subject", "my test subject"))) - .body(ARGUMENTS + ".list[0].date", equalTo("2014-10-30T14:12:00Z")); - } - - @Test - public void getMessagesShouldReturnFilteredPropertiesMessagesWhenAsked() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "inbox"); - - ZonedDateTime dateTime = ZonedDateTime.parse("2014-10-30T14:12:00Z"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "inbox"), - new ByteArrayInputStream("Subject: my test subject\r\n\r\ntestmail".getBytes()), Date.from(dateTime.toInstant()), false, new Flags()); - - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": [\"" + username + "|inbox|1\"], \"properties\": [\"id\", \"subject\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messages")) - .body(ARGUMENTS + ".list", hasSize(1)) - .body(ARGUMENTS + ".list[0].id", equalTo(username + "|inbox|1")) - .body(ARGUMENTS + ".list[0].subject", equalTo("my test subject")) - .body(ARGUMENTS + ".list[0].textBody", nullValue()) - .body(ARGUMENTS + ".list[0].isUnread", nullValue()) - .body(ARGUMENTS + ".list[0].preview", nullValue()) - .body(ARGUMENTS + ".list[0].headers", nullValue()) - .body(ARGUMENTS + ".list[0].date", nullValue()); - } - - @Test - public void getMessagesShouldReturnFilteredHeaderPropertyWhenAsked() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "inbox"); - - ZonedDateTime dateTime = ZonedDateTime.parse("2014-10-30T14:12:00Z"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "inbox"), - new ByteArrayInputStream(("From: [email protected]\r\n" - + "header1: Header1Content\r\n" - + "HEADer2: Header2Content\r\n" - + "Subject: my test subject\r\n" - + "\r\n" - + "testmail").getBytes()), Date.from(dateTime.toInstant()), false, new Flags()); - - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": [\"" + username + "|inbox|1\"], \"properties\": [\"headers.from\", \"headers.heADER2\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messages")) - .body(ARGUMENTS + ".list", hasSize(1)) - .body(ARGUMENTS + ".list[0].id", equalTo(username + "|inbox|1")) - .body(ARGUMENTS + ".list[0].subject", nullValue()) - .body(ARGUMENTS + ".list[0].textBody", nullValue()) - .body(ARGUMENTS + ".list[0].isUnread", nullValue()) - .body(ARGUMENTS + ".list[0].preview", nullValue()) - .body(ARGUMENTS + ".list[0].headers", equalTo(ImmutableMap.of("from", "[email protected]", "header2", "Header2Content"))) - .body(ARGUMENTS + ".list[0].date", nullValue()); - } - - @Test - public void getMessagesShouldReturnNotFoundWhenIdDoesntMatch() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "inbox"); - - ZonedDateTime dateTime = ZonedDateTime.parse("2014-10-30T14:12:00Z"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "inbox"), - new ByteArrayInputStream("Subject: my test subject\r\n\r\ntestmail".getBytes()), Date.from(dateTime.toInstant()), false, new Flags()); - - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": [\"username|inbox|1\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messages")) - .body(ARGUMENTS + ".list", empty()) - .body(ARGUMENTS + ".notFound", hasSize(1)); - } - - @Test - public void getMessagesShouldReturnMandatoryPropertiesMessagesWhenNotAsked() throws Exception { - jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox"); - - ZonedDateTime dateTime = ZonedDateTime.parse("2014-10-30T14:12:00Z"); - jmapServer.serverProbe().appendMessage(username, new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"), - new ByteArrayInputStream("Subject: my test subject\r\n\r\ntestmail".getBytes()), Date.from(dateTime.toInstant()), false, new Flags()); - embeddedElasticSearch.awaitForElasticSearch(); - - given() - .accept(ContentType.JSON) - .contentType(ContentType.JSON) - .header("Authorization", accessToken.serialize()) - .body("[[\"getMessages\", {\"ids\": [\"" + username + "|mailbox|1\"], \"properties\": [\"subject\"]}, \"#0\"]]") - .when() - .post("/jmap") - .then() - .statusCode(200) - .body(NAME, equalTo("messages")) - .body(ARGUMENTS + ".list", hasSize(1)) - .body(ARGUMENTS + ".list[0].id", equalTo("[email protected]|mailbox|1")) - .body(ARGUMENTS + ".list[0].subject", equalTo("my test subject")); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
