This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 7ff2ac7e114ee18da66c9fd3c58c4af0b82011c3 Author: Rene Cordier <[email protected]> AuthorDate: Thu Aug 13 15:56:25 2020 +0700 [Refactoring] Migrate AbstractNotificationRegistryTest to NotificationRegistryContract JUnit5 --- .../CassandraNotificationRegistryTest.java | 49 +++++---- .../vacation/AbstractNotificationRegistryTest.java | 114 --------------------- .../api/vacation/NotificationRegistryContract.java | 103 +++++++++++++++++++ .../vacation/MemoryNotificationRegistryTest.java | 25 ++++- 4 files changed, 146 insertions(+), 145 deletions(-) diff --git a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/vacation/CassandraNotificationRegistryTest.java b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/vacation/CassandraNotificationRegistryTest.java index d22d048..27f32f7 100644 --- a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/vacation/CassandraNotificationRegistryTest.java +++ b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/vacation/CassandraNotificationRegistryTest.java @@ -20,36 +20,33 @@ package org.apache.james.jmap.cassandra.vacation; import org.apache.james.backends.cassandra.CassandraCluster; -import org.apache.james.backends.cassandra.DockerCassandraRule; -import org.apache.james.jmap.api.vacation.AbstractNotificationRegistryTest; +import org.apache.james.backends.cassandra.CassandraClusterExtension; +import org.apache.james.core.MailAddress; import org.apache.james.jmap.api.vacation.NotificationRegistry; -import org.apache.james.util.date.ZonedDateTimeProvider; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; - -public class CassandraNotificationRegistryTest extends AbstractNotificationRegistryTest { - - @Rule - public DockerCassandraRule cassandraServer = new DockerCassandraRule(); - - private CassandraCluster cassandra; - - @Override - @Before - public void setUp() throws Exception { - cassandra = CassandraCluster.create(CassandraNotificationRegistryModule.MODULE, cassandraServer.getHost()); - super.setUp(); +import org.apache.james.jmap.api.vacation.NotificationRegistryContract; +import org.apache.james.jmap.api.vacation.RecipientId; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.RegisterExtension; + +class CassandraNotificationRegistryTest implements NotificationRegistryContract { + @RegisterExtension + static CassandraClusterExtension cassandraCluster = new CassandraClusterExtension(CassandraNotificationRegistryModule.MODULE); + + NotificationRegistry notificationRegistry; + RecipientId recipientId; + + @BeforeEach + public void setUp(CassandraCluster cassandra) throws Exception { + notificationRegistry = new CassandraNotificationRegistry(zonedDateTimeProvider, new CassandraNotificationRegistryDAO(cassandra.getConf()));; + recipientId = RecipientId.fromMailAddress(new MailAddress("[email protected]")); } - - @After - public void tearDown() { - cassandra.close(); + @Override + public NotificationRegistry notificationRegistry() { + return notificationRegistry; } @Override - protected NotificationRegistry createNotificationRegistry(ZonedDateTimeProvider zonedDateTimeProvider) { - return new CassandraNotificationRegistry(zonedDateTimeProvider, new CassandraNotificationRegistryDAO(cassandra.getConf())); + public RecipientId recipientId() { + return recipientId; } - } 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 deleted file mode 100644 index c1c27bb..0000000 --- a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractNotificationRegistryTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/**************************************************************** - * 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 static org.awaitility.Awaitility.await; -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.core.MailAddress; -import org.apache.james.util.date.ZonedDateTimeProvider; -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_4_SECONDS = ZonedDateTime.parse("2016-04-03T02:01:05+07:00[Asia/Vientiane]"); - public static final ZonedDateTime ZONED_DATE_TIME_PLUS_8_SECONDS = ZonedDateTime.parse("2016-04-03T02:01:09+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("[email protected]")); - } - - @Test - public void isRegisterShouldReturnFalseByDefault() { - assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).block()).isFalse(); - } - - @Test - public void registerShouldWork() { - notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.empty()).block(); - - assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).block()).isTrue(); - } - - @Test - public void registerShouldWorkWithExpiracyDate() { - when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); - notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.of(ZONED_DATE_TIME_PLUS_4_SECONDS)).block(); - - assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).block()).isTrue(); - } - - @Test - public void registerShouldExpireAfterExpiracyDate() { - when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); - - notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.of(ZONED_DATE_TIME_PLUS_4_SECONDS)).block(); - - when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_PLUS_8_SECONDS); - - await().atMost(20, TimeUnit.SECONDS).until(() -> !notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).block()); - } - - @Test - public void flushShouldWork() { - when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); - notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.empty()).block(); - - notificationRegistry.flush(ACCOUNT_ID).block(); - - assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).block()).isFalse(); - } - - @Test - public void registerShouldNotPersistWhenExpiryDateIsPast() { - when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_PLUS_4_SECONDS); - - notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.of(ZONED_DATE_TIME)).block(); - - assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).block()).isFalse(); - } - - @Test - public void registerShouldNotPersistWhenExpiryDateIsPresent() { - when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); - - notificationRegistry.register(ACCOUNT_ID, recipientId, Optional.of(ZONED_DATE_TIME)).block(); - - assertThat(notificationRegistry.isRegistered(ACCOUNT_ID, recipientId).block()).isTrue(); - } -} diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/NotificationRegistryContract.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/NotificationRegistryContract.java new file mode 100644 index 0000000..afd2c64 --- /dev/null +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/NotificationRegistryContract.java @@ -0,0 +1,103 @@ +/**************************************************************** + * 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 static org.awaitility.Awaitility.await; +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.junit.jupiter.api.Test; + +public interface NotificationRegistryContract { + ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.parse("2016-04-03T02:01:01+07:00[Asia/Vientiane]"); + ZonedDateTime ZONED_DATE_TIME_PLUS_4_SECONDS = ZonedDateTime.parse("2016-04-03T02:01:05+07:00[Asia/Vientiane]"); + ZonedDateTime ZONED_DATE_TIME_PLUS_8_SECONDS = ZonedDateTime.parse("2016-04-03T02:01:09+07:00[Asia/Vientiane]"); + AccountId ACCOUNT_ID = AccountId.fromString("id"); + + ZonedDateTimeProvider zonedDateTimeProvider = mock(ZonedDateTimeProvider.class); + + NotificationRegistry notificationRegistry(); + RecipientId recipientId(); + + @Test + default void isRegisterShouldReturnFalseByDefault() { + assertThat(notificationRegistry().isRegistered(ACCOUNT_ID, recipientId()).block()).isFalse(); + } + + @Test + default void registerShouldWork() { + notificationRegistry().register(ACCOUNT_ID, recipientId(), Optional.empty()).block(); + + assertThat(notificationRegistry().isRegistered(ACCOUNT_ID, recipientId()).block()).isTrue(); + } + + @Test + default void registerShouldWorkWithExpiracyDate() { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + notificationRegistry().register(ACCOUNT_ID, recipientId(), Optional.of(ZONED_DATE_TIME_PLUS_4_SECONDS)).block(); + + assertThat(notificationRegistry().isRegistered(ACCOUNT_ID, recipientId()).block()).isTrue(); + } + + @Test + default void registerShouldExpireAfterExpiracyDate() { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + + notificationRegistry().register(ACCOUNT_ID, recipientId(), Optional.of(ZONED_DATE_TIME_PLUS_4_SECONDS)).block(); + + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_PLUS_8_SECONDS); + + await().atMost(20, TimeUnit.SECONDS).until(() -> !notificationRegistry().isRegistered(ACCOUNT_ID, recipientId()).block()); + } + + @Test + default void flushShouldWork() { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + notificationRegistry().register(ACCOUNT_ID, recipientId(), Optional.empty()).block(); + + notificationRegistry().flush(ACCOUNT_ID).block(); + + assertThat(notificationRegistry().isRegistered(ACCOUNT_ID, recipientId()).block()).isFalse(); + } + + @Test + default void registerShouldNotPersistWhenExpiryDateIsPast() { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME_PLUS_4_SECONDS); + + notificationRegistry().register(ACCOUNT_ID, recipientId(), Optional.of(ZONED_DATE_TIME)).block(); + + assertThat(notificationRegistry().isRegistered(ACCOUNT_ID, recipientId()).block()).isFalse(); + } + + @Test + default void registerShouldNotPersistWhenExpiryDateIsPresent() { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + + notificationRegistry().register(ACCOUNT_ID, recipientId(), Optional.of(ZONED_DATE_TIME)).block(); + + assertThat(notificationRegistry().isRegistered(ACCOUNT_ID, recipientId()).block()).isTrue(); + } +} diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistryTest.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistryTest.java index 1d6bbc6..ade6ff7 100644 --- a/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistryTest.java +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryNotificationRegistryTest.java @@ -19,14 +19,29 @@ package org.apache.james.jmap.memory.vacation; -import org.apache.james.jmap.api.vacation.AbstractNotificationRegistryTest; +import org.apache.james.core.MailAddress; import org.apache.james.jmap.api.vacation.NotificationRegistry; -import org.apache.james.util.date.ZonedDateTimeProvider; +import org.apache.james.jmap.api.vacation.NotificationRegistryContract; +import org.apache.james.jmap.api.vacation.RecipientId; +import org.junit.jupiter.api.BeforeEach; -public class MemoryNotificationRegistryTest extends AbstractNotificationRegistryTest { +class MemoryNotificationRegistryTest implements NotificationRegistryContract { + NotificationRegistry notificationRegistry; + RecipientId recipientId; + + @BeforeEach + void setup() throws Exception { + notificationRegistry = new MemoryNotificationRegistry(zonedDateTimeProvider); + recipientId = RecipientId.fromMailAddress(new MailAddress("[email protected]")); + } + + @Override + public NotificationRegistry notificationRegistry() { + return notificationRegistry; + } @Override - protected NotificationRegistry createNotificationRegistry(ZonedDateTimeProvider zonedDateTimeProvider) { - return new MemoryNotificationRegistry(zonedDateTimeProvider); + public RecipientId recipientId() { + return recipientId; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
