Repository: james-project Updated Branches: refs/heads/master 71153ddc1 -> a15752fd1
JAMES-1778 VacationResponse should be marked as unactivated if outside of time range Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/50e19442 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/50e19442 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/50e19442 Branch: refs/heads/master Commit: 50e19442a8b3e7c16c2675b1486ee68ae1f03d0a Parents: ef4f8d9 Author: Benoit Tellier <[email protected]> Authored: Thu Jun 23 18:33:36 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Thu Jun 23 18:42:42 2016 +0700 ---------------------------------------------------------------------- .../CassandraGetVacationResponseTest.java | 6 +- .../jmap/FixedDateZonedDateTimeProvider.java | 7 ++ .../integration/GetVacationResponseTest.java | 7 +- .../MemoryGetVacationResponseMethodTest.java | 6 +- .../jmap/methods/GetVacationResponseMethod.java | 7 +- .../james/jmap/model/VacationResponse.java | 10 ++- .../methods/GetVacationResponseMethodTest.java | 89 +++++++++++++++++++- .../james/jmap/model/VacationResponseTest.java | 52 ++++++++++++ 8 files changed, 172 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/50e19442/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetVacationResponseTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetVacationResponseTest.java b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetVacationResponseTest.java index 1421a2e..66f1687 100644 --- a/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetVacationResponseTest.java +++ b/server/protocols/jmap-integration-testing/cassandra-jmap-integration-testing/src/test/java/org/apache/james/jmap/cassandra/CassandraGetVacationResponseTest.java @@ -25,6 +25,7 @@ import org.apache.james.backends.cassandra.EmbeddedCassandra; import org.apache.james.jmap.methods.integration.GetVacationResponseTest; import org.apache.james.mailbox.elasticsearch.EmbeddedElasticSearch; import org.apache.james.modules.CassandraJmapServerModule; +import org.apache.james.util.date.ZonedDateTimeProvider; import org.junit.Rule; import org.junit.rules.RuleChain; import org.junit.rules.TemporaryFolder; @@ -41,10 +42,11 @@ public class CassandraGetVacationResponseTest extends GetVacationResponseTest { .around(embeddedElasticSearch); @Override - protected GuiceJamesServer createJmapServer() { + protected GuiceJamesServer createJmapServer(ZonedDateTimeProvider zonedDateTimeProvider) { return new GuiceJamesServer() .combineWith(CassandraJamesServerMain.cassandraServerModule) - .overrideWith(new CassandraJmapServerModule(temporaryFolder, embeddedElasticSearch, cassandra)); + .overrideWith(new CassandraJmapServerModule(temporaryFolder, embeddedElasticSearch, cassandra), + binder -> binder.bind(ZonedDateTimeProvider.class).toInstance(zonedDateTimeProvider)); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/50e19442/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/FixedDateZonedDateTimeProvider.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/FixedDateZonedDateTimeProvider.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/FixedDateZonedDateTimeProvider.java index fd0c24c..b7dc00c 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/FixedDateZonedDateTimeProvider.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/FixedDateZonedDateTimeProvider.java @@ -27,6 +27,13 @@ public class FixedDateZonedDateTimeProvider implements ZonedDateTimeProvider { private ZonedDateTime zonedDateTime; + public FixedDateZonedDateTimeProvider() { + } + + public FixedDateZonedDateTimeProvider(ZonedDateTime zonedDateTime) { + this.zonedDateTime = zonedDateTime; + } + public void setFixedDateTime(ZonedDateTime zonedDateTime) { this.zonedDateTime = zonedDateTime; } http://git-wip-us.apache.org/repos/asf/james-project/blob/50e19442/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 4dc59e0..fe6023c 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 @@ -30,10 +30,12 @@ import java.time.ZonedDateTime; import java.util.Optional; import org.apache.james.GuiceJamesServer; +import org.apache.james.jmap.FixedDateZonedDateTimeProvider; import org.apache.james.jmap.JmapAuthentication; import org.apache.james.jmap.api.access.AccessToken; import org.apache.james.jmap.api.vacation.AccountId; import org.apache.james.jmap.api.vacation.Vacation; +import org.apache.james.util.date.ZonedDateTimeProvider; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -44,6 +46,7 @@ import com.jayway.restassured.http.ContentType; public abstract class GetVacationResponseTest { + private static final ZonedDateTime ZONED_DATE_TIME_2014 = ZonedDateTime.parse("2014-10-15T14:10:00Z"); private static final String NAME = "[0][0]"; private static final String ARGUMENTS = "[0][1]"; private static final String USERS_DOMAIN = "domain.tld"; @@ -51,7 +54,7 @@ public abstract class GetVacationResponseTest { public static final String PASSWORD = "password"; public static final String SUBJECT = "subject"; - protected abstract GuiceJamesServer createJmapServer(); + protected abstract GuiceJamesServer createJmapServer(ZonedDateTimeProvider zonedDateTimeProvider); protected abstract void await(); @@ -60,7 +63,7 @@ public abstract class GetVacationResponseTest { @Before public void setup() throws Throwable { - jmapServer = createJmapServer(); + jmapServer = createJmapServer(new FixedDateZonedDateTimeProvider(ZONED_DATE_TIME_2014)); jmapServer.start(); RestAssured.port = jmapServer.getJmapPort(); RestAssured.config = newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)); http://git-wip-us.apache.org/repos/asf/james-project/blob/50e19442/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryGetVacationResponseMethodTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryGetVacationResponseMethodTest.java b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryGetVacationResponseMethodTest.java index 69e0d2b..c2c3a92 100644 --- a/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryGetVacationResponseMethodTest.java +++ b/server/protocols/jmap-integration-testing/memory-jmap-integration-testing/src/test/java/org/apache/james/jmap/memory/MemoryGetVacationResponseMethodTest.java @@ -23,6 +23,7 @@ import org.apache.james.GuiceJamesServer; import org.apache.james.MemoryJamesServerMain; import org.apache.james.jmap.methods.integration.GetVacationResponseTest; import org.apache.james.jmap.servers.MemoryJmapServerModule; +import org.apache.james.util.date.ZonedDateTimeProvider; import org.junit.Rule; import org.junit.rules.TemporaryFolder; @@ -32,10 +33,11 @@ public class MemoryGetVacationResponseMethodTest extends GetVacationResponseTest public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Override - protected GuiceJamesServer createJmapServer() { + protected GuiceJamesServer createJmapServer(ZonedDateTimeProvider zonedDateTimeProvider) { return new GuiceJamesServer() .combineWith(MemoryJamesServerMain.inMemoryServerModule) - .overrideWith(new MemoryJmapServerModule(temporaryFolder)); + .overrideWith(new MemoryJmapServerModule(temporaryFolder), + binder -> binder.bind(ZonedDateTimeProvider.class).toInstance(zonedDateTimeProvider)); } @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/50e19442/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 f5f8752..e50bc31 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 @@ -31,6 +31,7 @@ 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 org.apache.james.util.date.ZonedDateTimeProvider; import com.google.common.base.Preconditions; @@ -40,10 +41,12 @@ public class GetVacationResponseMethod implements Method { public static final Response.Name RESPONSE_NAME = Response.name("vacationResponse"); private final VacationRepository vacationRepository; + private final ZonedDateTimeProvider zonedDateTimeProvider; @Inject - public GetVacationResponseMethod(VacationRepository vacationRepository) { + public GetVacationResponseMethod(VacationRepository vacationRepository, ZonedDateTimeProvider zonedDateTimeProvider) { this.vacationRepository = vacationRepository; + this.zonedDateTimeProvider = zonedDateTimeProvider; } @Override @@ -75,7 +78,7 @@ public class GetVacationResponseMethod implements Method { return GetVacationResponse.builder() .accountId(mailboxSession.getUser().getUserName()) .vacationResponse(VacationResponse.builder() - .fromVacation(vacation) + .fromVacation(vacation, zonedDateTimeProvider.get()) .build()) .build(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/50e19442/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java index 751dfa9..2e9a4cf 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java @@ -26,6 +26,7 @@ import java.util.Optional; import org.apache.james.jmap.api.vacation.Vacation; import org.apache.james.jmap.json.OptionalZonedDateTimeDeserializer; import org.apache.james.jmap.json.OptionalZonedDateTimeSerializer; +import org.apache.james.util.date.ZonedDateTimeProvider; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -96,9 +97,9 @@ public class VacationResponse { return this; } - public Builder fromVacation(Vacation vacation) { + public Builder fromVacation(Vacation vacation, ZonedDateTime zonedDateTime) { this.id = Vacation.ID; - this.isEnabled = Optional.of(vacation.isEnabled()); + this.isEnabled = computeEnabledState(vacation, zonedDateTime); this.fromDate = vacation.getFromDate(); this.toDate = vacation.getToDate(); this.textBody = vacation.getTextBody(); @@ -107,6 +108,11 @@ public class VacationResponse { return this; } + private Optional<Boolean> computeEnabledState(Vacation vacation, ZonedDateTime zonedDateTime) { + return Optional.of(vacation.isEnabled()) + .map(enabled -> enabled && vacation.isActiveAtDate(zonedDateTime)); + } + public VacationResponse build() { boolean enabled = isEnabled.orElse(DEFAULT_DISABLED); if (enabled) { http://git-wip-us.apache.org/repos/asf/james-project/blob/50e19442/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 2bb3902..10f19e8 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 @@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.time.ZonedDateTime; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; @@ -37,23 +38,29 @@ 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.apache.james.util.date.ZonedDateTimeProvider; import org.junit.Before; import org.junit.Test; public class GetVacationResponseMethodTest { public static final String USERNAME = "username"; + public static final ZonedDateTime ZONED_DATE_TIME_2014 = ZonedDateTime.parse("2014-09-30T14:10:00Z"); + public static final ZonedDateTime ZONED_DATE_TIME_2015 = ZonedDateTime.parse("2015-09-30T14:10:00Z"); + public static final ZonedDateTime ZONED_DATE_TIME_2016 = ZonedDateTime.parse("2016-09-30T14:10:00Z"); private GetVacationResponseMethod testee; private VacationRepository vacationRepository; private MailboxSession mailboxSession; private MailboxSession.User user; + private ZonedDateTimeProvider zonedDateTimeProvider; @Before public void setUp() { vacationRepository = mock(VacationRepository.class); mailboxSession = mock(MailboxSession.class); user = mock(MailboxSession.User.class); - testee = new GetVacationResponseMethod(vacationRepository); + zonedDateTimeProvider = mock(ZonedDateTimeProvider.class); + testee = new GetVacationResponseMethod(vacationRepository, zonedDateTimeProvider); } @Test(expected = NullPointerException.class) @@ -87,6 +94,7 @@ public class GetVacationResponseMethodTest { when(vacationRepository.retrieveVacation(AccountId.fromString(USERNAME))).thenReturn(CompletableFuture.completedFuture(vacation)); when(mailboxSession.getUser()).thenReturn(user); when(user.getUserName()).thenReturn(USERNAME); + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_2015); GetVacationRequest getVacationRequest = GetVacationRequest.builder().build(); @@ -98,7 +106,84 @@ public class GetVacationResponseMethodTest { .response(GetVacationResponse.builder() .accountId(USERNAME) .vacationResponse(VacationResponse.builder() - .fromVacation(vacation) + .id(Vacation.ID) + .enabled(true) + .textBody(Optional.of("I am in vacation")) + .subject(Optional.of("subject")) + .build()) + .build()) + .build(); + assertThat(result).containsExactly(expected); + } + + @Test + public void processShouldMarkOutDatedVacationAsUnactivated() { + ClientId clientId = mock(ClientId.class); + Vacation vacation = Vacation.builder() + .enabled(true) + .textBody("I am in vacation") + .subject(Optional.of("subject")) + .fromDate(Optional.of(ZONED_DATE_TIME_2014)) + .toDate(Optional.of(ZONED_DATE_TIME_2015)) + .build(); + when(vacationRepository.retrieveVacation(AccountId.fromString(USERNAME))).thenReturn(CompletableFuture.completedFuture(vacation)); + when(mailboxSession.getUser()).thenReturn(user); + when(user.getUserName()).thenReturn(USERNAME); + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_2016); + + 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() + .enabled(false) + .id(Vacation.ID) + .textBody(Optional.of("I am in vacation")) + .subject(Optional.of("subject")) + .fromDate(Optional.of(ZONED_DATE_TIME_2014)) + .toDate(Optional.of(ZONED_DATE_TIME_2015)) + .build()) + .build()) + .build(); + assertThat(result).containsExactly(expected); + } + + @Test + public void processShouldMarkTooEarlyVacationAsUnactivated() { + ClientId clientId = mock(ClientId.class); + Vacation vacation = Vacation.builder() + .enabled(true) + .textBody("I am in vacation") + .subject(Optional.of("subject")) + .fromDate(Optional.of(ZONED_DATE_TIME_2015)) + .toDate(Optional.of(ZONED_DATE_TIME_2016)) + .build(); + when(vacationRepository.retrieveVacation(AccountId.fromString(USERNAME))).thenReturn(CompletableFuture.completedFuture(vacation)); + when(mailboxSession.getUser()).thenReturn(user); + when(user.getUserName()).thenReturn(USERNAME); + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_2014); + + 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() + .enabled(false) + .id(Vacation.ID) + .textBody(Optional.of("I am in vacation")) + .subject(Optional.of("subject")) + .fromDate(Optional.of(ZONED_DATE_TIME_2015)) + .toDate(Optional.of(ZONED_DATE_TIME_2016)) .build()) .build()) .build(); http://git-wip-us.apache.org/repos/asf/james-project/blob/50e19442/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/VacationResponseTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/VacationResponseTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/VacationResponseTest.java index 503acdf..d3aaeb9 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/VacationResponseTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/VacationResponseTest.java @@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.time.ZonedDateTime; import java.util.Optional; +import org.apache.james.jmap.api.vacation.Vacation; import org.junit.Test; public class VacationResponseTest { @@ -35,6 +36,9 @@ public class VacationResponseTest { public static final ZonedDateTime FROM_DATE = ZonedDateTime.parse("2016-04-15T11:56:32.224+07:00[Asia/Vientiane]"); public static final ZonedDateTime TO_DATE = ZonedDateTime.parse("2016-04-16T11:56:32.224+07:00[Asia/Vientiane]"); public static final String SUBJECT = "subject"; + public static final ZonedDateTime ZONED_DATE_TIME_2014 = ZonedDateTime.parse("2014-09-30T14:10:00Z"); + public static final ZonedDateTime ZONED_DATE_TIME_2015 = ZonedDateTime.parse("2015-09-30T14:10:00Z"); + public static final ZonedDateTime ZONED_DATE_TIME_2016 = ZonedDateTime.parse("2016-09-30T14:10:00Z"); @Test public void vacationResponseBuilderShouldBeConstructedWithTheRightInformation() { @@ -108,4 +112,52 @@ public class VacationResponseTest { assertThat(vacationResponse.getHtmlBody()).contains(MESSAGE); } + @Test + public void fromVacationShouldMarkOutDatedVacationAsDisabled() { + VacationResponse vacationResponse = VacationResponse.builder() + .fromVacation( + Vacation.builder() + .enabled(true) + .textBody("Any text") + .fromDate(Optional.of(ZONED_DATE_TIME_2014)) + .toDate(Optional.of(ZONED_DATE_TIME_2015)) + .build(), + ZONED_DATE_TIME_2016) + .build(); + + assertThat(vacationResponse.isEnabled()).isFalse(); + } + + @Test + public void fromVacationShouldMarkTooEarlyVacationAsDisabled() { + VacationResponse vacationResponse = VacationResponse.builder() + .fromVacation( + Vacation.builder() + .enabled(true) + .textBody("Any text") + .fromDate(Optional.of(ZONED_DATE_TIME_2015)) + .toDate(Optional.of(ZONED_DATE_TIME_2016)) + .build(), + ZONED_DATE_TIME_2014) + .build(); + + assertThat(vacationResponse.isEnabled()).isFalse(); + } + + @Test + public void fromVacationShouldMarkInRangeVacationAsEnabled() { + VacationResponse vacationResponse = VacationResponse.builder() + .fromVacation( + Vacation.builder() + .enabled(true) + .textBody("Any text") + .fromDate(Optional.of(ZONED_DATE_TIME_2014)) + .toDate(Optional.of(ZONED_DATE_TIME_2016)) + .build(), + ZONED_DATE_TIME_2015) + .build(); + + assertThat(vacationResponse.isEnabled()).isTrue(); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
