Author: matthieu
Date: Fri Dec 11 12:32:07 2015
New Revision: 1719379
URL: http://svn.apache.org/viewvc?rev=1719379&view=rev
Log:
JAMES-1644 Convert GetMailboxesMethodTest to unit test
Added:
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BeansTest.java
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
Modified:
james/project/trunk/server/protocols/jmap/pom.xml
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
Modified: james/project/trunk/server/protocols/jmap/pom.xml
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/pom.xml?rev=1719379&r1=1719378&r2=1719379&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/pom.xml (original)
+++ james/project/trunk/server/protocols/jmap/pom.xml Fri Dec 11 12:32:07 2015
@@ -155,6 +155,11 @@
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
+ <artifactId>apache-james-mailbox-memory</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.james</groupId>
<artifactId>apache-james-mailbox-store</artifactId>
</dependency>
<dependency>
@@ -244,6 +249,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>nl.jqno.equalsverifier</groupId>
+ <artifactId>equalsverifier</artifactId>
+ <version>1.7.5</version>
+ </dependency>
+ <dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj3}</version>
Modified:
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java?rev=1719379&r1=1719378&r2=1719379&view=diff
==============================================================================
---
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java
(original)
+++
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java
Fri Dec 11 12:32:07 2015
@@ -19,6 +19,7 @@
package org.apache.james.jmap.model;
+import java.util.Objects;
import java.util.Optional;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@@ -254,4 +255,34 @@ public class Mailbox {
public long getUnreadThreads() {
return unreadThreads;
}
+
+ @Override
+ public final boolean equals(Object obj) {
+ if (obj instanceof Mailbox) {
+ Mailbox other = (Mailbox) obj;
+ return Objects.equals(this.id, other.id)
+ && Objects.equals(this.name, other.name)
+ && Objects.equals(this.parentId, other.parentId)
+ && Objects.equals(this.role, other.role)
+ && Objects.equals(this.sortOrder, other.sortOrder)
+ && Objects.equals(this.mustBeOnlyMailbox,
other.mustBeOnlyMailbox)
+ && Objects.equals(this.mayReadItems, other.mayReadItems)
+ && Objects.equals(this.mayAddItems, other.mayAddItems)
+ && Objects.equals(this.mayRemoveItems, other.mayRemoveItems)
+ && Objects.equals(this.mayCreateChild, other.mayCreateChild)
+ && Objects.equals(this.mayRename, other.mayRename)
+ && Objects.equals(this.mayDelete, other.mayDelete)
+ && Objects.equals(this.totalMessages, other.totalMessages)
+ && Objects.equals(this.unreadMessages, other.unreadMessages)
+ && Objects.equals(this.totalThreads, other.totalThreads)
+ && Objects.equals(this.unreadThreads, other.unreadThreads);
+ }
+ return false;
+ }
+
+ @Override
+ public final int hashCode() {
+ return Objects.hash(id, name, parentId, role, sortOrder,
mustBeOnlyMailbox, mayReadItems, mayAddItems,
+ mayRemoveItems, mayCreateChild, mayRename, mayDelete,
totalMessages, unreadMessages, totalThreads, unreadThreads);
+ }
}
Added:
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BeansTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BeansTest.java?rev=1719379&view=auto
==============================================================================
---
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BeansTest.java
(added)
+++
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BeansTest.java
Fri Dec 11 12:32:07 2015
@@ -0,0 +1,34 @@
+/****************************************************************
+ * 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;
+
+import org.apache.james.jmap.model.Mailbox;
+import org.junit.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+public class BeansTest {
+
+ @Test
+ public void beanShouldRespectBeanContract() {
+ EqualsVerifier.forClass(Mailbox.class)
+ .verify();
+ }
+}
Modified:
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java?rev=1719379&r1=1719378&r2=1719379&view=diff
==============================================================================
---
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
(original)
+++
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMailboxesMethodTest.java
Fri Dec 11 12:32:07 2015
@@ -18,211 +18,80 @@
****************************************************************/
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.equalTo;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.util.UUID;
-
-import org.apache.james.http.jetty.Configuration;
-import org.apache.james.http.jetty.JettyHttpServer;
-import org.apache.james.jmap.AuthenticationFilter;
-import org.apache.james.jmap.JMAPServlet;
-import org.apache.james.jmap.api.AccessTokenManager;
-import org.apache.james.jmap.api.access.AccessToken;
-import org.apache.james.jmap.methods.GetMailboxesMethod;
-import org.apache.james.jmap.methods.JmapRequestParser;
-import org.apache.james.jmap.methods.JmapRequestParserImpl;
-import org.apache.james.jmap.methods.JmapResponseWriter;
-import org.apache.james.jmap.methods.JmapResponseWriterImpl;
-import org.apache.james.jmap.methods.RequestHandler;
-import org.apache.james.mailbox.MailboxManager;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.ByteArrayInputStream;
+import java.util.Date;
+
+import javax.mail.Flags;
+
+import org.apache.james.jmap.model.GetMailboxesRequest;
+import org.apache.james.jmap.model.GetMailboxesResponse;
+import org.apache.james.jmap.model.Mailbox;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.MessageManager;
-import org.apache.james.mailbox.MessageManager.MetaData.FetchGroup;
+import org.apache.james.mailbox.acl.GroupMembershipResolver;
+import org.apache.james.mailbox.acl.MailboxACLResolver;
+import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
+import org.apache.james.mailbox.acl.UnionMailboxACLResolver;
+import org.apache.james.mailbox.inmemory.InMemoryId;
+import org.apache.james.mailbox.inmemory.InMemoryMailboxSessionMapperFactory;
import org.apache.james.mailbox.model.MailboxPath;
-import org.apache.james.mailbox.store.MailboxMetaData;
-import org.apache.james.mailbox.store.TestId;
-import org.apache.james.mailbox.store.mail.MailboxMapper;
-import org.apache.james.mailbox.store.mail.MailboxMapperFactory;
-import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
-import org.junit.After;
+import org.apache.james.mailbox.store.MockAuthenticator;
+import org.apache.james.mailbox.store.StoreMailboxManager;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
-
-import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
-import com.google.common.base.Charsets;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import com.jayway.restassured.RestAssured;
-import com.jayway.restassured.http.ContentType;
+import org.slf4j.LoggerFactory;
public class GetMailboxesMethodTest {
- private RequestHandler requestHandler;
- private JettyHttpServer server;
- private UUID accessToken;
- private MailboxManager mockedMailboxManager;
- private MailboxMapperFactory<TestId> mockedMailboxMapperFactory;
- private MailboxSession mockedMailboxSession;
+ private static final Logger LOGGER =
LoggerFactory.getLogger(GetMailboxesMethodTest.class);
+ private static final String USERNAME = "[email protected]";
+
+ private StoreMailboxManager<InMemoryId> mailboxManager;
+ private GetMailboxesMethod<InMemoryId> getMailboxesMethod;
- @SuppressWarnings("unchecked")
@Before
public void setup() throws Exception {
- String username = "[email protected]";
-
- AccessTokenManager mockedAccessTokenManager =
mock(AccessTokenManager.class);
- mockedMailboxMapperFactory = mock(MailboxMapperFactory.class);
- mockedMailboxManager = mock(MailboxManager.class);
- mockedMailboxSession = mock(MailboxSession.class);
-
- JmapRequestParser jmapRequestParser = new
JmapRequestParserImpl(ImmutableSet.of(new Jdk8Module()));
- JmapResponseWriter jmapResponseWriter = new
JmapResponseWriterImpl(ImmutableSet.of(new Jdk8Module()));
-
- GetMailboxesMethod<TestId> getMailboxMethod = new
GetMailboxesMethod<>(mockedMailboxManager, mockedMailboxMapperFactory);
- requestHandler = new RequestHandler(ImmutableSet.of(getMailboxMethod),
jmapRequestParser, jmapResponseWriter);
- JMAPServlet jmapServlet = new JMAPServlet(requestHandler);
-
- AuthenticationFilter authenticationFilter = new
AuthenticationFilter(mockedAccessTokenManager, mockedMailboxManager);
-
- when(mockedMailboxManager.createSystemSession(eq(username),
any(Logger.class))).thenReturn(mockedMailboxSession);
-
when(mockedAccessTokenManager.isValid(any(AccessToken.class))).thenReturn(true);
-
when(mockedAccessTokenManager.getUsernameFromToken(any(AccessToken.class))).thenReturn(username);
-
- accessToken = UUID.randomUUID();
-
- server = JettyHttpServer.create(
- Configuration.builder()
- .filter("/jmap")
- .with(authenticationFilter)
- .serve("/jmap")
- .with(jmapServlet)
- .randomPort()
- .build());
-
- server.start();
+ InMemoryMailboxSessionMapperFactory mailboxMapperFactory = new
InMemoryMailboxSessionMapperFactory();
+ MailboxACLResolver aclResolver = new UnionMailboxACLResolver();
+ GroupMembershipResolver groupMembershipResolver = new
SimpleGroupMembershipResolver();
+ mailboxManager = new
StoreMailboxManager<InMemoryId>(mailboxMapperFactory, new MockAuthenticator(),
aclResolver, groupMembershipResolver);
+ mailboxManager.init();
- RestAssured.port = server.getPort();
- RestAssured.config =
newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8));
- }
-
- @After
- public void teardown() throws Exception {
- server.stop();
- }
-
- @Test
- public void
getMailboxesShouldErrorNotSupportedWhenRequestContainsNonNullAccountId() throws
Exception {
- given()
- .accept(ContentType.JSON)
- .contentType(ContentType.JSON)
- .header("Authorization", accessToken)
- .body("[[\"getMailboxes\", {\"accountId\": \"1\"}, \"#0\"]]")
- .when()
- .post("/jmap")
- .then()
- .statusCode(200)
- .content(equalTo("[[\"error\",{\"type\":\"Not yet
implemented\"},\"#0\"]]"));
- }
-
-
- @Test
- public void
getMailboxesShouldErrorNotSupportedWhenRequestContainsNonNullIds() throws
Exception {
- given()
- .accept(ContentType.JSON)
- .contentType(ContentType.JSON)
- .header("Authorization", accessToken)
- .body("[[\"getMailboxes\", {\"ids\": []}, \"#0\"]]")
- .when()
- .post("/jmap")
- .then()
- .statusCode(200)
- .content(equalTo("[[\"error\",{\"type\":\"Not yet
implemented\"},\"#0\"]]"));
- }
-
- @Test
- public void
getMailboxesShouldErrorNotSupportedWhenRequestContainsNonNullProperties()
throws Exception {
- given()
- .accept(ContentType.JSON)
- .contentType(ContentType.JSON)
- .header("Authorization", accessToken)
- .body("[[\"getMailboxes\", {\"properties\": []}, \"#0\"]]")
- .when()
- .post("/jmap")
- .then()
- .statusCode(200)
- .content(equalTo("[[\"error\",{\"type\":\"Not yet
implemented\"},\"#0\"]]"));
- }
-
- @Test
- public void getMailboxesShouldErrorInvalidArgumentsWhenRequestIsInvalid()
throws Exception {
- given()
- .accept(ContentType.JSON)
- .contentType(ContentType.JSON)
- .header("Authorization", accessToken)
- .body("[[\"getMailboxes\", {\"ids\": true}, \"#0\"]]")
- .when()
- .post("/jmap")
- .then()
- .statusCode(200)
-
.content(equalTo("[[\"error\",{\"type\":\"invalidArguments\"},\"#0\"]]"));
+ getMailboxesMethod = new GetMailboxesMethod<>(mailboxManager,
mailboxMapperFactory);
}
@Test
public void getMailboxesShouldReturnEmptyListWhenNoMailboxes() throws
Exception {
- when(mockedMailboxManager.list(any()))
- .thenReturn(ImmutableList.<MailboxPath>of());
- given()
- .accept(ContentType.JSON)
- .contentType(ContentType.JSON)
- .header("Authorization", accessToken)
- .body("[[\"getMailboxes\", {}, \"#0\"]]")
- .when()
- .post("/jmap")
- .then()
- .statusCode(200)
-
.content(equalTo("[[\"getMailboxes\",{\"accountId\":null,\"state\":null,\"list\":[],\"notFound\":null},\"#0\"]]"));
+ GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder()
+ .build();
+
+ MailboxSession mailboxSession =
mailboxManager.createSystemSession(USERNAME, LOGGER);
+ GetMailboxesResponse getMailboxesResponse =
getMailboxesMethod.process(getMailboxesRequest, mailboxSession);
+ assertThat(getMailboxesResponse.getList()).isEmpty();
}
@Test
- @SuppressWarnings("unchecked")
public void getMailboxesShouldReturnMailboxesWhenAvailable() throws
Exception {
- MailboxPath mailboxPath = new MailboxPath("namespace", "user", "name");
- when(mockedMailboxManager.list(eq(mockedMailboxSession)))
- .thenReturn(ImmutableList.<MailboxPath>of(mailboxPath));
-
- MessageManager mockedMessageManager = mock(MessageManager.class);
- when(mockedMailboxManager.getMailbox(eq(mailboxPath),
eq(mockedMailboxSession)))
- .thenReturn(mockedMessageManager);
-
- MailboxMetaData mailboxMetaData = new
MailboxMetaData(ImmutableList.of(), null, 123L, 5L, 10L, 3L, 2L, 1L, false,
false, null);
- when(mockedMessageManager.getMetaData(eq(false),
eq(mockedMailboxSession), eq(FetchGroup.UNSEEN_COUNT)))
- .thenReturn(mailboxMetaData);
-
- MailboxMapper<TestId> mockedMailboxMapper = mock(MailboxMapper.class);
-
when(mockedMailboxMapperFactory.getMailboxMapper(eq(mockedMailboxSession)))
- .thenReturn(mockedMailboxMapper);
-
- SimpleMailbox<TestId> simpleMailbox = new
SimpleMailbox<TestId>(mailboxPath, 5L);
- simpleMailbox.setMailboxId(TestId.of(23432L));
- when(mockedMailboxMapper.findMailboxByPath(mailboxPath))
- .thenReturn(simpleMailbox);
-
- given()
- .accept(ContentType.JSON)
- .contentType(ContentType.JSON)
- .header("Authorization", accessToken)
- .body("[[\"getMailboxes\", {}, \"#0\"]]")
- .when()
- .post("/jmap")
- .then()
- .statusCode(200)
-
.content(equalTo("[[\"getMailboxes\",{\"accountId\":null,\"state\":null,\"list\":[{\"id\":\"23432\",\"name\":\"name\",\"parentId\":null,\"role\":null,\"sortOrder\":0,\"mustBeOnlyMailbox\":false,\"mayReadItems\":false,\"mayAddItems\":false,\"mayRemoveItems\":false,\"mayCreateChild\":false,\"mayRename\":false,\"mayDelete\":false,\"totalMessages\":0,\"unreadMessages\":2,\"totalThreads\":0,\"unreadThreads\":0}],\"notFound\":null},\"#0\"]]"));
+ MailboxPath mailboxPath = new MailboxPath("#private", USERNAME,
"name");
+ MailboxSession mailboxSession =
mailboxManager.createSystemSession(USERNAME, LOGGER);
+ mailboxManager.createMailbox(mailboxPath, mailboxSession);
+ MessageManager messageManager = mailboxManager.getMailbox(mailboxPath,
mailboxSession);
+ messageManager.appendMessage(new ByteArrayInputStream("Subject:
test\r\n\r\ntestmail".getBytes()), new Date(), mailboxSession, false, new
Flags());
+ messageManager.appendMessage(new ByteArrayInputStream("Subject:
test2\r\n\r\ntestmail".getBytes()), new Date(), mailboxSession, false, new
Flags());
+
+ GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder()
+ .build();
+
+ Mailbox expectedMailbox = Mailbox.builder()
+ .id("1")
+ .name(mailboxPath.getName())
+ .unreadMessages(2)
+ .build();
+
+ GetMailboxesResponse getMailboxesResponse =
getMailboxesMethod.process(getMailboxesRequest, mailboxSession);
+
assertThat(getMailboxesResponse.getList()).containsOnly(expectedMailbox);
}
}
Added:
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
URL:
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java?rev=1719379&view=auto
==============================================================================
---
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
(added)
+++
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMailboxesRequestTest.java
Fri Dec 11 12:32:07 2015
@@ -0,0 +1,43 @@
+/****************************************************************
+ * 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.model;
+
+import org.apache.commons.lang.NotImplementedException;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+public class GetMailboxesRequestTest {
+
+ @Test(expected=NotImplementedException.class)
+ public void builderShouldThrowWhenAccountId() {
+ GetMailboxesRequest.builder().accountId("1");
+ }
+
+ @Test(expected=NotImplementedException.class)
+ public void builderShouldThrowWhenIds() {
+ GetMailboxesRequest.builder().ids(ImmutableList.of());
+ }
+
+ @Test(expected=NotImplementedException.class)
+ public void builderShouldThrowWhenProperties() {
+ GetMailboxesRequest.builder().properties(ImmutableList.of());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]