JAMES-1717 NotificationRegistry API
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/9ebad96c Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/9ebad96c Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/9ebad96c Branch: refs/heads/master Commit: 9ebad96ccbd12dbbed524cdc3736358c7ecaedca Parents: 9550a3b Author: Benoit Tellier <btell...@linagora.com> Authored: Tue Apr 19 13:59:54 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Fri May 27 18:02:46 2016 +0700 ---------------------------------------------------------------------- server/data/data-jmap/pom.xml | 9 ++ .../jmap/api/vacation/NotificationRegistry.java | 34 ++++++ .../james/jmap/api/vacation/RecipientId.java | 66 +++++++++++ .../AbstractNotificationRegistryTest.java | 113 +++++++++++++++++++ 4 files changed, 222 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/9ebad96c/server/data/data-jmap/pom.xml ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/pom.xml b/server/data/data-jmap/pom.xml index 48b55ff..55e569f 100644 --- a/server/data/data-jmap/pom.xml +++ b/server/data/data-jmap/pom.xml @@ -178,10 +178,19 @@ <artifactId>james-server-data-api</artifactId> </dependency> <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-util-java8</artifactId> + </dependency> + <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> <dependency> + <groupId>com.jayway.awaitility</groupId> + <artifactId>awaitility</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/9ebad96c/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/NotificationRegistry.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/NotificationRegistry.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/NotificationRegistry.java new file mode 100644 index 0000000..6d4889e --- /dev/null +++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/NotificationRegistry.java @@ -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.api.vacation; + +import java.time.ZonedDateTime; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; + +public interface NotificationRegistry { + + CompletableFuture<Void> register(AccountId accountId, RecipientId recipientId, Optional<ZonedDateTime> expiryDate); + + CompletableFuture<Boolean> isRegistered(AccountId accountId, RecipientId recipientId); + + CompletableFuture<Void> flush(AccountId accountId); + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/9ebad96c/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/RecipientId.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/RecipientId.java b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/RecipientId.java new file mode 100644 index 0000000..5ab63ee --- /dev/null +++ b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/vacation/RecipientId.java @@ -0,0 +1,66 @@ +/**************************************************************** + * 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 java.util.Objects; + +import javax.mail.internet.AddressException; + +import org.apache.mailet.MailAddress; + +import com.google.common.base.Preconditions; + +public class RecipientId { + + public static RecipientId fromMailAddress(MailAddress mailAddress) { + Preconditions.checkNotNull(mailAddress, "RecipientId mailAddress should not be null"); + return new RecipientId(mailAddress); + } + + private final MailAddress mailAddress; + + private RecipientId(MailAddress mailAddress) { + this.mailAddress = mailAddress; + } + + public MailAddress getMailAddress() { + return mailAddress; + } + + public String getAsString() { + return mailAddress.toString(); + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) { + return false; + } + + RecipientId accountId = (RecipientId) o; + + return Objects.equals(this.mailAddress, accountId.mailAddress); + } + + @Override + public int hashCode() { + return Objects.hash(mailAddress); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/9ebad96c/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractNotificationRegistryTest.java ---------------------------------------------------------------------- diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractNotificationRegistryTest.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractNotificationRegistryTest.java new file mode 100644 index 0000000..ba687d0 --- /dev/null +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractNotificationRegistryTest.java @@ -0,0 +1,113 @@ +/**************************************************************** + * 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 com.jayway.awaitility.Awaitility.await; +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.TimeUnit; + +import org.apache.james.util.date.ZonedDateTimeProvider; +import org.apache.mailet.MailAddress; +import org.junit.Before; +import org.junit.Test; + +public abstract class AbstractNotificationRegistryTest { + + public static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.parse("2016-04-03T02:01:01+07:00[Asia/Vientiane]"); + public static final ZonedDateTime ZONED_DATE_TIME_PLUS_1_SECONDS = ZonedDateTime.parse("2016-04-03T02:01:02+07:00[Asia/Vientiane]"); + public static final ZonedDateTime ZONED_DATE_TIME_PLUS_10_SECONDS = ZonedDateTime.parse("2016-04-03T02:01:11+07:00[Asia/Vientiane]"); + public static final AccountId ACCOUNT_ID = AccountId.fromString("id"); + private NotificationRegistry notificationRegistry; + private ZonedDateTimeProvider zonedDateTimeProvider; + private RecipientId recipientId; + + protected abstract NotificationRegistry createNotificationRegistry(ZonedDateTimeProvider zonedDateTimeProvider); + + @Before + public void setUp() throws Exception { + zonedDateTimeProvider = mock(ZonedDateTimeProvider.class); + notificationRegistry = createNotificationRegistry(zonedDateTimeProvider); + recipientId = RecipientId.fromMailAddress(new MailAddress("be...@apache.org")); + } + + @Test + public void isRegisterShouldReturnFalseByDefault() throws Exception { + assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).join()).isFalse(); + } + + @Test + public void registerShouldWork() throws Exception { + notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.empty()).join(); + + assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).join()).isTrue(); + } + + @Test + public void registerShouldWorkWithExpiracyDate() throws Exception { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.of(ZONED_DATE_TIME_PLUS_1_SECONDS)).join(); + + assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).join()).isTrue(); + } + + @Test + public void registerShouldExpireAfterExpiracyDate() throws Exception { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + + notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.of(ZONED_DATE_TIME_PLUS_1_SECONDS)).join(); + + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_PLUS_10_SECONDS); + + await().atMost(2, TimeUnit.SECONDS).until(() -> !notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).join()); + } + + @Test + public void flushShouldWork() throws Exception { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.empty()).join(); + + notificationRegistry.flush(ACCOUNT_ID).join(); + + assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).join()).isFalse(); + } + + @Test + public void registerShouldNotPersistWhenExpiryDateIsPast() throws Exception { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_PLUS_1_SECONDS); + + notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.of(ZONED_DATE_TIME)).join(); + + assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).join()).isFalse(); + } + + @Test + public void registerShouldNotPersistWhenExpiryDateIsPresent() throws Exception { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + + notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.of(ZONED_DATE_TIME)).join(); + + assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).join()).isTrue(); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org