Repository: james-project Updated Branches: refs/heads/master f6ba5d502 -> 03a46df84
JAMES-1717 Vacation should indicate if it is active Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/be08b5a5 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/be08b5a5 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/be08b5a5 Branch: refs/heads/master Commit: be08b5a5beaf00e4aa65a127329ee898250742af Parents: bd314bf Author: Benoit Tellier <btell...@linagora.com> Authored: Fri Apr 8 11:58:43 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Thu May 19 17:10:39 2016 +0700 ---------------------------------------------------------------------- .../james/jmap/api/vacation/Vacation.java | 15 ++ .../james/jmap/api/vacation/VacationTest.java | 179 +++++++++++++++++++ 2 files changed, 194 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/be08b5a5/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java index 5957d37..cb0fbfe 100644 --- a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java +++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/Vacation.java @@ -105,6 +105,21 @@ public class Vacation { return textBody; } + public boolean isActiveAtDate(ZonedDateTime instant) { + Preconditions.checkNotNull(instant); + return isEnabled + && isAfterOrEqualToFromDate(instant) + && isBeforeOrEqualToToDate(instant); + } + + private Boolean isAfterOrEqualToFromDate(ZonedDateTime instant) { + return fromDate.map(date -> date.isBefore(instant) || date.equals(instant)).orElse(true); + } + + private Boolean isBeforeOrEqualToToDate(ZonedDateTime instant) { + return toDate.map(date -> date.isAfter(instant) || date.equals(instant)).orElse(true); + } + @Override public boolean equals(Object o) { if (this == o) return true; http://git-wip-us.apache.org/repos/asf/james-project/blob/be08b5a5/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/VacationTest.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/VacationTest.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/VacationTest.java new file mode 100644 index 0000000..9704ae9 --- /dev/null +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/VacationTest.java @@ -0,0 +1,179 @@ +/**************************************************************** + * 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.api.vacation; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.ZonedDateTime; +import java.util.Optional; + +import org.junit.Test; + +public class VacationTest { + + public static final ZonedDateTime DATE_TIME_2016 = ZonedDateTime.parse("2016-10-09T08:07:06+07:00[Asia/Vientiane]"); + public static final ZonedDateTime DATE_TIME_2017 = ZonedDateTime.parse("2017-10-09T08:07:06+07:00[Asia/Vientiane]"); + public static final ZonedDateTime DATE_TIME_2017_1MS = ZonedDateTime.parse("2017-10-09T08:07:06.001+07:00[Asia/Vientiane]"); + public static final ZonedDateTime DATE_TIME_2018 = ZonedDateTime.parse("2018-10-09T08:07:06+07:00[Asia/Vientiane]"); + + @Test + public void disabledVacationsAreNotActive() { + assertThat( + Vacation.builder() + .enabled(false) + .build() + .isActiveAtDate(DATE_TIME_2016)) + .isFalse(); + } + + @Test + public void enabledVacationWithoutDatesIsActive() { + assertThat( + Vacation.builder() + .enabled(true) + .build() + .isActiveAtDate(DATE_TIME_2016)) + .isTrue(); + } + + @Test + public void rangeShouldBeInclusiveOnFromDate() { + assertThat( + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(DATE_TIME_2016)) + .build() + .isActiveAtDate(DATE_TIME_2016)) + .isTrue(); + } + + @Test + public void rangeShouldBeInclusiveOnToDate() { + assertThat( + Vacation.builder() + .enabled(true) + .toDate(Optional.of(DATE_TIME_2016)) + .build() + .isActiveAtDate(DATE_TIME_2016)) + .isTrue(); + } + + @Test + public void vacationShouldBeActiveDuringRange() { + assertThat( + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(DATE_TIME_2016)) + .toDate(Optional.of(DATE_TIME_2018)) + .build() + .isActiveAtDate(DATE_TIME_2017)) + .isTrue(); + } + + @Test + public void vacationShouldNotBeActiveAfterRange() { + assertThat( + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(DATE_TIME_2016)) + .toDate(Optional.of(DATE_TIME_2017)) + .build() + .isActiveAtDate(DATE_TIME_2018)) + .isFalse(); + } + + @Test + public void vacationShouldNotBeActiveBeforeRange() { + assertThat( + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(DATE_TIME_2017)) + .toDate(Optional.of(DATE_TIME_2018)) + .build() + .isActiveAtDate(DATE_TIME_2016)) + .isFalse(); + } + + @Test(expected = NullPointerException.class) + public void isActiveAtDateShouldThrowOnNullValue() { + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(DATE_TIME_2016)) + .toDate(Optional.of(DATE_TIME_2016)) + .build() + .isActiveAtDate(null); + } + + @Test + public void vacationShouldBeActiveAfterFromDate() { + assertThat( + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(DATE_TIME_2016)) + .build() + .isActiveAtDate(DATE_TIME_2017)) + .isTrue(); + } + + @Test + public void vacationShouldNotBeActiveBeforeFromDate() { + assertThat( + Vacation.builder() + .enabled(true) + .fromDate(Optional.of(DATE_TIME_2017)) + .build() + .isActiveAtDate(DATE_TIME_2016)) + .isFalse(); + } + + @Test + public void vacationShouldNotBeActiveAfterToDate() { + assertThat( + Vacation.builder() + .enabled(true) + .toDate(Optional.of(DATE_TIME_2017)) + .build() + .isActiveAtDate(DATE_TIME_2018)) + .isFalse(); + } + + @Test + public void vacationShouldBeActiveBeforeToDate() { + assertThat( + Vacation.builder() + .enabled(true) + .toDate(Optional.of(DATE_TIME_2017)) + .build() + .isActiveAtDate(DATE_TIME_2016)) + .isTrue(); + } + + @Test + public void isActiveAtDateShouldHaveMillisecondPrecision() { + assertThat( + Vacation.builder() + .enabled(true) + .toDate(Optional.of(DATE_TIME_2017)) + .build() + .isActiveAtDate(DATE_TIME_2017_1MS)) + .isFalse(); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org