JAMES-1717 Implement GetVacationResponseMethod
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d690b553 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d690b553 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d690b553 Branch: refs/heads/master Commit: d690b553d4ae60dd36b811b6cfcf5a50031362e4 Parents: 44e9983 Author: Benoit Tellier <[email protected]> Authored: Thu Apr 7 16:08:22 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Fri Apr 22 15:29:29 2016 +0700 ---------------------------------------------------------------------- .../apache/james/utils/ExtendedServerProbe.java | 4 + .../apache/james/utils/GuiceServerProbe.java | 12 +- .../integration/GetVacationResponseTest.java | 115 ++++++++++++++++++- .../jmap/methods/GetVacationResponseMethod.java | 39 ++++++- .../methods/GetVacationResponseMethodTest.java | 82 ++++++++++++- 5 files changed, 242 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/d690b553/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java index d7cf48c..d380a2b 100644 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/ExtendedServerProbe.java @@ -25,6 +25,8 @@ import java.util.Date; import javax.mail.Flags; import org.apache.james.cli.probe.ServerProbe; +import org.apache.james.jmap.api.vacation.AccountId; +import org.apache.james.jmap.api.vacation.Vacation; import org.apache.james.mailbox.exception.BadCredentialsException; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.model.MailboxPath; @@ -37,4 +39,6 @@ public interface ExtendedServerProbe<Id extends MailboxId> extends ServerProbe { throws BadCredentialsException, MailboxException; Mailbox<Id> getMailbox(String namespace, String user, String name); + + void modifyVacation(AccountId accountId, Vacation vacation); } http://git-wip-us.apache.org/repos/asf/james-project/blob/d690b553/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java index 11a6369..fa5e48a 100644 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/utils/GuiceServerProbe.java @@ -33,6 +33,9 @@ import javax.mail.Flags; import org.apache.commons.lang.NotImplementedException; import org.apache.james.adapter.mailbox.SerializableQuota; import org.apache.james.domainlist.api.DomainList; +import org.apache.james.jmap.api.vacation.AccountId; +import org.apache.james.jmap.api.vacation.Vacation; +import org.apache.james.jmap.api.vacation.VacationRepository; import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageManager; @@ -66,17 +69,19 @@ public class GuiceServerProbe<Id extends MailboxId> implements ExtendedServerPro private final UsersRepository usersRepository; private final SieveRepository sieveRepository; private final RecipientRewriteTable recipientRewriteTable; + private final VacationRepository vacationRepository; @Inject private GuiceServerProbe(MailboxManager mailboxManager, MailboxMapperFactory<Id> mailboxMapperFactory, DomainList domainList, UsersRepository usersRepository, SieveRepository sieveRepository, - RecipientRewriteTable recipientRewriteTable) { + RecipientRewriteTable recipientRewriteTable, VacationRepository vacationRepository) { this.mailboxManager = mailboxManager; this.mailboxMapperFactory = mailboxMapperFactory; this.domainList = domainList; this.usersRepository = usersRepository; this.sieveRepository = sieveRepository; this.recipientRewriteTable = recipientRewriteTable; + this.vacationRepository = vacationRepository; } @Override @@ -345,4 +350,9 @@ public class GuiceServerProbe<Id extends MailboxId> implements ExtendedServerPro public void removeSieveQuota(String user) throws Exception { sieveRepository.removeQuota(user); } + + @Override + public void modifyVacation(AccountId accountId, Vacation vacation) { + vacationRepository.modifyVacation(accountId, vacation).join(); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/d690b553/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java index 32190db..b0a5f97 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java @@ -22,12 +22,18 @@ package org.apache.james.jmap.methods.integration; 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.core.IsNull.nullValue; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; + +import java.time.ZonedDateTime; +import java.util.Optional; import org.apache.james.GuiceJamesServer; import org.apache.james.jmap.JmapAuthentication; import org.apache.james.jmap.api.access.AccessToken; -import org.apache.james.mailbox.model.MailboxConstants; +import org.apache.james.jmap.api.vacation.AccountId; +import org.apache.james.jmap.api.vacation.Vacation; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -71,12 +77,115 @@ public abstract class GetVacationResponseTest { } @Test - public void getVacationResponseIsNotImplementedYet() { + public void getVacationResponseShouldReturnDefaultValue() { + given() + .accept(ContentType.JSON) + .contentType(ContentType.JSON) + .header("Authorization", accessToken.serialize()) + .body("[[" + + "\"getVacationResponse\", " + + "{}, " + + "\"#0\"" + + "]]") + .when() + .post("/jmap") + .then() + .statusCode(200) + .body(NAME, equalTo("vacationResponse")) + .body(ARGUMENTS + ".accountId", equalTo(USER)) + .body(ARGUMENTS + ".list", hasSize(1)) + .body(ARGUMENTS + ".list[0].id", equalTo("singleton")) + .body(ARGUMENTS + ".list[0].fromDate", nullValue()) + .body(ARGUMENTS + ".list[0].toDate", nullValue()) + .body(ARGUMENTS + ".list[0].isEnabled", equalTo(false)) + .body(ARGUMENTS + ".list[0].textBody", equalTo("")); + } + + @Test + public void getVacationResponseShouldReturnStoredValue() { + jmapServer.serverProbe().modifyVacation(AccountId.fromString(USER), + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(ZonedDateTime.parse("2014-09-30T14:10:00Z"))) + .toDate(Optional.of(ZonedDateTime.parse("2014-10-30T14:10:00Z"))) + .textBody("Test explaining my vacations") + .build()); + + given() + .accept(ContentType.JSON) + .contentType(ContentType.JSON) + .header("Authorization", accessToken.serialize()) + .body("[[" + + "\"getVacationResponse\", " + + "{}, " + + "\"#0\"" + + "]]") + .when() + .post("/jmap") + .then() + .statusCode(200) + .body(NAME, equalTo("vacationResponse")) + .body(ARGUMENTS + ".accountId", equalTo(USER)) + .body(ARGUMENTS + ".list", hasSize(1)) + .body(ARGUMENTS + ".list[0].id", equalTo("singleton")) + .body(ARGUMENTS + ".list[0].fromDate", equalTo("2014-09-30T14:10:00Z")) + .body(ARGUMENTS + ".list[0].toDate", equalTo("2014-10-30T14:10:00Z")) + .body(ARGUMENTS + ".list[0].isEnabled", equalTo(true)) + .body(ARGUMENTS + ".list[0].textBody", equalTo("Test explaining my vacations")); + } + + @Test + public void getVacationResponseShouldReturnStoredValueWithNonDefaultTimezone() { + jmapServer.serverProbe().modifyVacation(AccountId.fromString(USER), + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(ZonedDateTime.parse("2014-09-30T14:10:00+02:00"))) + .toDate(Optional.of(ZonedDateTime.parse("2016-04-15T11:56:32.224+07:00"))) + .textBody("Test explaining my vacations") + .build()); + + given() + .accept(ContentType.JSON) + .contentType(ContentType.JSON) + .header("Authorization", accessToken.serialize()) + .body("[[" + + "\"getVacationResponse\", " + + "{}, " + + "\"#0\"" + + "]]") + .when() + .post("/jmap") + .then() + .statusCode(200) + .body(NAME, equalTo("vacationResponse")) + .body(ARGUMENTS + ".accountId", equalTo(USER)) + .body(ARGUMENTS + ".list", hasSize(1)) + .body(ARGUMENTS + ".list[0].id", equalTo("singleton")) + .body(ARGUMENTS + ".list[0].fromDate", equalTo("2014-09-30T14:10:00+02:00")) + .body(ARGUMENTS + ".list[0].toDate", equalTo("2016-04-15T11:56:32.224+07:00")) + .body(ARGUMENTS + ".list[0].isEnabled", equalTo(true)) + .body(ARGUMENTS + ".list[0].textBody", equalTo("Test explaining my vacations")); + } + + @Test + public void accountIdIsNotSupported() { + jmapServer.serverProbe().modifyVacation(AccountId.fromString(USER), + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(ZonedDateTime.parse("2014-09-30T14:10:00+02:00"))) + .toDate(Optional.of(ZonedDateTime.parse("2014-10-30T14:10:00+02:00"))) + .textBody("Test explaining my vacations") + .build()); + given() .accept(ContentType.JSON) .contentType(ContentType.JSON) .header("Authorization", accessToken.serialize()) - .body("[[\"getVacationResponse\", {\"accountId\": \"1\"}, \"#0\"]]") + .body("[[" + + "\"getVacationResponse\", " + + "{\"accountId\":\"1\"}, " + + "\"#0\"" + + "]]") .when() .post("/jmap") .then() http://git-wip-us.apache.org/repos/asf/james-project/blob/d690b553/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java index 964af0e..f5f8752 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetVacationResponseMethod.java @@ -21,14 +21,30 @@ package org.apache.james.jmap.methods; import java.util.stream.Stream; -import org.apache.commons.lang.NotImplementedException; +import javax.inject.Inject; + +import org.apache.james.jmap.api.vacation.AccountId; +import org.apache.james.jmap.api.vacation.Vacation; +import org.apache.james.jmap.api.vacation.VacationRepository; import org.apache.james.jmap.model.ClientId; import org.apache.james.jmap.model.GetVacationRequest; +import org.apache.james.jmap.model.GetVacationResponse; +import org.apache.james.jmap.model.VacationResponse; import org.apache.james.mailbox.MailboxSession; +import com.google.common.base.Preconditions; + public class GetVacationResponseMethod implements Method { public static final Request.Name METHOD_NAME = Request.name("getVacationResponse"); + public static final Response.Name RESPONSE_NAME = Response.name("vacationResponse"); + + private final VacationRepository vacationRepository; + + @Inject + public GetVacationResponseMethod(VacationRepository vacationRepository) { + this.vacationRepository = vacationRepository; + } @Override public Request.Name requestHandled() { @@ -42,6 +58,25 @@ public class GetVacationResponseMethod implements Method { @Override public Stream<JmapResponse> process(JmapRequest request, ClientId clientId, MailboxSession mailboxSession) { - throw new NotImplementedException(); + Preconditions.checkNotNull(request); + Preconditions.checkNotNull(clientId); + Preconditions.checkNotNull(mailboxSession); + Preconditions.checkArgument(request instanceof GetVacationRequest); + + return Stream.of(JmapResponse.builder() + .clientId(clientId) + .responseName(RESPONSE_NAME) + .response(process(mailboxSession)) + .build()); + } + + private GetVacationResponse process(MailboxSession mailboxSession) { + Vacation vacation = vacationRepository.retrieveVacation(AccountId.fromString(mailboxSession.getUser().getUserName())).join(); + return GetVacationResponse.builder() + .accountId(mailboxSession.getUser().getUserName()) + .vacationResponse(VacationResponse.builder() + .fromVacation(vacation) + .build()) + .build(); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/d690b553/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetVacationResponseMethodTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetVacationResponseMethodTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetVacationResponseMethodTest.java index d8ec531..a03a844 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetVacationResponseMethodTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetVacationResponseMethodTest.java @@ -19,14 +19,88 @@ package org.apache.james.jmap.methods; -import org.apache.commons.lang.NotImplementedException; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + +import org.apache.james.jmap.api.vacation.AccountId; +import org.apache.james.jmap.api.vacation.Vacation; +import org.apache.james.jmap.api.vacation.VacationRepository; +import org.apache.james.jmap.model.ClientId; +import org.apache.james.jmap.model.GetMailboxesRequest; +import org.apache.james.jmap.model.GetVacationRequest; +import org.apache.james.jmap.model.GetVacationResponse; +import org.apache.james.jmap.model.SetMailboxesRequest; +import org.apache.james.jmap.model.VacationResponse; +import org.apache.james.mailbox.MailboxSession; +import org.junit.Before; import org.junit.Test; public class GetVacationResponseMethodTest { - @Test(expected = NotImplementedException.class) - public void setVacationResponseMethodIsNotImplemented() { - new GetVacationResponseMethod().process(null, null, null); + public static final String USERNAME = "username"; + private GetVacationResponseMethod testee; + private VacationRepository vacationRepository; + private MailboxSession mailboxSession; + private MailboxSession.User user; + + @Before + public void setUp() { + vacationRepository = mock(VacationRepository.class); + mailboxSession = mock(MailboxSession.class); + user = mock(MailboxSession.User.class); + testee = new GetVacationResponseMethod(vacationRepository); + } + + @Test(expected = NullPointerException.class) + public void processShouldThrowOnNullRequest() { + testee.process(null, mock(ClientId.class), mock(MailboxSession.class)); + } + + @Test(expected = NullPointerException.class) + public void processShouldThrowOnNullClientId() { + testee.process(mock(GetMailboxesRequest.class), null, mock(MailboxSession.class)); + } + + @Test(expected = NullPointerException.class) + public void processShouldThrowOnNullMailboxSession() { + testee.process(mock(GetMailboxesRequest.class), mock(ClientId.class), null); + } + + @Test(expected = IllegalArgumentException.class) + public void processShouldThrowOnWrongRequestType() { + testee.process(mock(SetMailboxesRequest.class), mock(ClientId.class), mock(MailboxSession.class)); + } + + @Test + public void processShouldReturnTheAppropriateVacationResponse() { + ClientId clientId = mock(ClientId.class); + Vacation vacation = Vacation.builder() + .enabled(true) + .textBody("I am in vacation") + .build(); + when(vacationRepository.retrieveVacation(AccountId.fromString(USERNAME))).thenReturn(CompletableFuture.completedFuture(vacation)); + when(mailboxSession.getUser()).thenReturn(user); + when(user.getUserName()).thenReturn(USERNAME); + + GetVacationRequest getVacationRequest = GetVacationRequest.builder().build(); + + Stream<JmapResponse> result = testee.process(getVacationRequest, clientId, mailboxSession); + + JmapResponse expected = JmapResponse.builder() + .clientId(clientId) + .responseName(GetVacationResponseMethod.RESPONSE_NAME) + .response(GetVacationResponse.builder() + .accountId(USERNAME) + .vacationResponse(VacationResponse.builder() + .fromVacation(vacation) + .build()) + .build()) + .build(); + assertThat(result).containsExactly(expected); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
