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 0b85634d2f0e1092bdacecc8bc36900ac9e145c7 Author: Rene Cordier <[email protected]> AuthorDate: Thu Aug 13 16:13:45 2020 +0700 [Refactoring] Migrate AbstractVacationRepositoryTest to VacationRepositoryContract JUnit5 --- .../vacation/CassandraVacationRepositoryTest.java | 44 +++--- ...ryTest.java => VacationRepositoryContract.java} | 148 ++++++++++----------- .../vacation/MemoryVacationRepositoryTest.java | 15 ++- 3 files changed, 100 insertions(+), 107 deletions(-) diff --git a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationRepositoryTest.java b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationRepositoryTest.java index c45e956..e0c9e40 100644 --- a/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationRepositoryTest.java +++ b/server/data/data-jmap-cassandra/src/test/java/org/apache/james/jmap/cassandra/vacation/CassandraVacationRepositoryTest.java @@ -20,43 +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.backends.cassandra.CassandraClusterExtension; import org.apache.james.backends.cassandra.components.CassandraModule; import org.apache.james.backends.cassandra.init.CassandraZonedDateTimeModule; import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionModule; -import org.apache.james.jmap.api.vacation.AbstractVacationRepositoryTest; import org.apache.james.jmap.api.vacation.VacationRepository; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; +import org.apache.james.jmap.api.vacation.VacationRepositoryContract; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.RegisterExtension; -public class CassandraVacationRepositoryTest extends AbstractVacationRepositoryTest { +class CassandraVacationRepositoryTest implements VacationRepositoryContract { + static final CassandraModule MODULE = CassandraModule.aggregateModules( + CassandraSchemaVersionModule.MODULE, + CassandraVacationModule.MODULE, + CassandraZonedDateTimeModule.MODULE); - @Rule - public DockerCassandraRule cassandraServer = new DockerCassandraRule(); - - private CassandraCluster cassandra; + @RegisterExtension + static CassandraClusterExtension cassandraCluster = new CassandraClusterExtension(MODULE); + VacationRepository vacationRepository; - @Override - @Before - public void setUp() throws Exception { - CassandraModule module = CassandraModule.aggregateModules( - CassandraSchemaVersionModule.MODULE, - CassandraVacationModule.MODULE, - CassandraZonedDateTimeModule.MODULE); - cassandra = CassandraCluster.create(module, cassandraServer.getHost()); - super.setUp(); - } - - @After - public void tearDown() { - cassandra.close(); + @BeforeEach + public void setUp(CassandraCluster cassandra) throws Exception { + vacationRepository = new CassandraVacationRepository(new CassandraVacationDAO(cassandra.getConf(), cassandra.getTypesProvider())); } @Override - protected VacationRepository createVacationRepository() { - return new CassandraVacationRepository(new CassandraVacationDAO(cassandra.getConf(), cassandra.getTypesProvider())); + public VacationRepository vacationRepository() { + return vacationRepository; } - } diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractVacationRepositoryTest.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/VacationRepositoryContract.java similarity index 59% rename from server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractVacationRepositoryTest.java rename to server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/VacationRepositoryContract.java index 133143e..436e13a 100644 --- a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/AbstractVacationRepositoryTest.java +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/vacation/VacationRepositoryContract.java @@ -20,22 +20,22 @@ package org.apache.james.jmap.api.vacation; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.time.ZonedDateTime; import java.util.Optional; import org.apache.james.util.ValuePatch; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public abstract class AbstractVacationRepositoryTest { +public interface VacationRepositoryContract { - public static final AccountId ACCOUNT_ID = AccountId.fromString("identifier"); - public static final ZonedDateTime DATE_2014 = ZonedDateTime.parse("2014-04-03T02:01+07:00[Asia/Vientiane]"); - public static final ZonedDateTime DATE_2015 = ZonedDateTime.parse("2015-04-03T02:01+07:00[Asia/Vientiane]"); - public static final ZonedDateTime DATE_2016 = ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]"); - public static final ZonedDateTime DATE_2017 = ZonedDateTime.parse("2017-04-03T02:01+07:00[Asia/Vientiane]"); - public static final Vacation VACATION = Vacation.builder() + AccountId ACCOUNT_ID = AccountId.fromString("identifier"); + ZonedDateTime DATE_2014 = ZonedDateTime.parse("2014-04-03T02:01+07:00[Asia/Vientiane]"); + ZonedDateTime DATE_2015 = ZonedDateTime.parse("2015-04-03T02:01+07:00[Asia/Vientiane]"); + ZonedDateTime DATE_2016 = ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]"); + ZonedDateTime DATE_2017 = ZonedDateTime.parse("2017-04-03T02:01+07:00[Asia/Vientiane]"); + Vacation VACATION = Vacation.builder() .fromDate(Optional.of(DATE_2015)) .toDate(Optional.of(DATE_2016)) .enabled(true) @@ -45,43 +45,36 @@ public abstract class AbstractVacationRepositoryTest { .build(); - private VacationRepository vacationRepository; - - protected abstract VacationRepository createVacationRepository(); - - @Before - public void setUp() throws Exception { - vacationRepository = createVacationRepository(); - } + VacationRepository vacationRepository(); @Test - public void retrieveVacationShouldReturnDefaultValueByDefault() { - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()).isEqualTo(VacationRepository.DEFAULT_VACATION); + default void retrieveVacationShouldReturnDefaultValueByDefault() { + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()).isEqualTo(VacationRepository.DEFAULT_VACATION); } @Test - public void modifyVacationShouldUpdateEnabled() { + default void modifyVacationShouldUpdateEnabled() { VacationPatch vacationPatch = VacationPatch.builder() .isEnabled(true) .build(); - vacationRepository.modifyVacation(ACCOUNT_ID, vacationPatch).block(); + vacationRepository().modifyVacation(ACCOUNT_ID, vacationPatch).block(); - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(Vacation.builder() .enabled(true) .build()); } @Test - public void modifyVacationShouldUpdateFromDate() { + default void modifyVacationShouldUpdateFromDate() { VacationPatch vacationPatch = VacationPatch.builder() .fromDate(DATE_2014) .build(); - vacationRepository.modifyVacation(ACCOUNT_ID, vacationPatch).block(); + vacationRepository().modifyVacation(ACCOUNT_ID, vacationPatch).block(); - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(Vacation.builder() .fromDate(Optional.of(DATE_2014)) .enabled(false) @@ -89,14 +82,14 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void modifyVacationShouldUpdateToDate() { + default void modifyVacationShouldUpdateToDate() { VacationPatch vacationPatch = VacationPatch.builder() .toDate(DATE_2017) .build(); - vacationRepository.modifyVacation(ACCOUNT_ID, vacationPatch).block(); + vacationRepository().modifyVacation(ACCOUNT_ID, vacationPatch).block(); - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(Vacation.builder() .toDate(Optional.of(DATE_2017)) .enabled(false) @@ -104,15 +97,15 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void modifyVacationShouldUpdateSubject() { + default void modifyVacationShouldUpdateSubject() { String newSubject = "new subject"; VacationPatch vacationPatch = VacationPatch.builder() .subject(newSubject) .build(); - vacationRepository.modifyVacation(ACCOUNT_ID, vacationPatch).block(); + vacationRepository().modifyVacation(ACCOUNT_ID, vacationPatch).block(); - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(Vacation.builder() .subject(Optional.of(newSubject)) .enabled(false) @@ -120,15 +113,15 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void modifyVacationShouldUpdateTextBody() { + default void modifyVacationShouldUpdateTextBody() { String newTextBody = "new text body"; VacationPatch vacationPatch = VacationPatch.builder() .textBody(newTextBody) .build(); - vacationRepository.modifyVacation(ACCOUNT_ID, vacationPatch).block(); + vacationRepository().modifyVacation(ACCOUNT_ID, vacationPatch).block(); - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(Vacation.builder() .textBody(newTextBody) .enabled(false) @@ -136,15 +129,15 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void modifyVacationShouldUpdateHtmlBody() { + default void modifyVacationShouldUpdateHtmlBody() { String newHtmlBody = "new <b>html</b> body"; VacationPatch vacationPatch = VacationPatch.builder() .htmlBody(newHtmlBody) .build(); - vacationRepository.modifyVacation(ACCOUNT_ID, vacationPatch).block(); + vacationRepository().modifyVacation(ACCOUNT_ID, vacationPatch).block(); - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(Vacation.builder() .enabled(false) .htmlBody(newHtmlBody) @@ -152,61 +145,61 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void modifyVacationShouldAllowToUpdateAllFieldsAtOnce() { + default void modifyVacationShouldAllowToUpdateAllFieldsAtOnce() { VacationPatch vacationPatch = VacationPatch.builderFrom(VACATION) .build(); - vacationRepository.modifyVacation(ACCOUNT_ID, vacationPatch).block(); + vacationRepository().modifyVacation(ACCOUNT_ID, vacationPatch).block(); - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(VACATION); } @Test - public void modifyVacationShouldAllowEmptyUpdates() { + default void modifyVacationShouldAllowEmptyUpdates() { VacationPatch vacationPatch = VacationPatch.builder() .build(); - vacationRepository.modifyVacation(ACCOUNT_ID, vacationPatch).block(); + vacationRepository().modifyVacation(ACCOUNT_ID, vacationPatch).block(); - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(VacationRepository.DEFAULT_VACATION); } @Test - public void emptyUpdatesShouldNotChangeExistingVacations() { + default void emptyUpdatesShouldNotChangeExistingVacations() { // Given - vacationRepository.modifyVacation(ACCOUNT_ID, + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builderFrom(VACATION) .build()) .block(); // When - vacationRepository.modifyVacation(ACCOUNT_ID, VacationPatch.builder() + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builder() .build()) .block(); // Then - assertThat(vacationRepository.retrieveVacation(ACCOUNT_ID).block()) + assertThat(vacationRepository().retrieveVacation(ACCOUNT_ID).block()) .isEqualTo(VACATION); } @Test - public void nullUpdateShouldResetSubject() { + default void nullUpdateShouldResetSubject() { // Given - vacationRepository.modifyVacation(ACCOUNT_ID, + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builderFrom(VACATION) .build()) .block(); // When - vacationRepository.modifyVacation(ACCOUNT_ID, VacationPatch.builder() + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builder() .subject(ValuePatch.remove()) .build()) .block(); // Then - Vacation vacation = vacationRepository.retrieveVacation(ACCOUNT_ID).block(); + Vacation vacation = vacationRepository().retrieveVacation(ACCOUNT_ID).block(); assertThat(vacation.getSubject()).isEmpty(); assertThat(vacation) .isEqualTo(Vacation.builder() @@ -219,21 +212,21 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void nullUpdateShouldResetText() { + default void nullUpdateShouldResetText() { // Given - vacationRepository.modifyVacation(ACCOUNT_ID, + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builderFrom(VACATION) .build()) .block(); // When - vacationRepository.modifyVacation(ACCOUNT_ID, VacationPatch.builder() + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builder() .textBody(ValuePatch.remove()) .build()) .block(); // Then - Vacation vacation = vacationRepository.retrieveVacation(ACCOUNT_ID).block(); + Vacation vacation = vacationRepository().retrieveVacation(ACCOUNT_ID).block(); assertThat(vacation.getTextBody()).isEmpty(); assertThat(vacation) .isEqualTo(Vacation.builder() @@ -246,21 +239,21 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void nullUpdateShouldResetHtml() { + default void nullUpdateShouldResetHtml() { // Given - vacationRepository.modifyVacation(ACCOUNT_ID, + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builderFrom(VACATION) .build()) .block(); // When - vacationRepository.modifyVacation(ACCOUNT_ID, VacationPatch.builder() + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builder() .htmlBody(ValuePatch.remove()) .build()) .block(); // Then - Vacation vacation = vacationRepository.retrieveVacation(ACCOUNT_ID).block(); + Vacation vacation = vacationRepository().retrieveVacation(ACCOUNT_ID).block(); assertThat(vacation.getHtmlBody()).isEmpty(); assertThat(vacation) .isEqualTo(Vacation.builder() @@ -273,21 +266,21 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void nullUpdateShouldResetToDate() { + default void nullUpdateShouldResetToDate() { // Given - vacationRepository.modifyVacation(ACCOUNT_ID, + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builderFrom(VACATION) .build()) .block(); // When - vacationRepository.modifyVacation(ACCOUNT_ID, VacationPatch.builder() + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builder() .toDate(ValuePatch.remove()) .build()) .block(); // Then - Vacation vacation = vacationRepository.retrieveVacation(ACCOUNT_ID).block(); + Vacation vacation = vacationRepository().retrieveVacation(ACCOUNT_ID).block(); assertThat(vacation.getToDate()).isEmpty(); assertThat(vacation) .isEqualTo(Vacation.builder() @@ -300,21 +293,21 @@ public abstract class AbstractVacationRepositoryTest { } @Test - public void nullUpdateShouldResetFromDate() { + default void nullUpdateShouldResetFromDate() { // Given - vacationRepository.modifyVacation(ACCOUNT_ID, + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builderFrom(VACATION) .build()) .block(); // When - vacationRepository.modifyVacation(ACCOUNT_ID, VacationPatch.builder() + vacationRepository().modifyVacation(ACCOUNT_ID, VacationPatch.builder() .fromDate(ValuePatch.remove()) .build()) .block(); // Then - Vacation vacation = vacationRepository.retrieveVacation(ACCOUNT_ID).block(); + Vacation vacation = vacationRepository().retrieveVacation(ACCOUNT_ID).block(); assertThat(vacation.getFromDate()).isEmpty(); assertThat(vacation) .isEqualTo(Vacation.builder() @@ -326,19 +319,22 @@ public abstract class AbstractVacationRepositoryTest { .build()); } - @Test(expected = NullPointerException.class) - public void retrieveVacationShouldThrowOnNullAccountId() { - vacationRepository.retrieveVacation(null); + @Test + default void retrieveVacationShouldThrowOnNullAccountId() { + assertThatThrownBy(() -> vacationRepository().retrieveVacation(null)) + .isInstanceOf(NullPointerException.class); } - @Test(expected = NullPointerException.class) - public void modifyVacationShouldThrowOnNullAccountId() { - vacationRepository.modifyVacation(null, VacationPatch.builder().build()); + @Test + default void modifyVacationShouldThrowOnNullAccountId() { + assertThatThrownBy(() -> vacationRepository().modifyVacation(null, VacationPatch.builder().build())) + .isInstanceOf(NullPointerException.class); } - @Test(expected = NullPointerException.class) - public void modifyVacationShouldThrowOnNullVacation() { - vacationRepository.modifyVacation(ACCOUNT_ID, null); + @Test + default void modifyVacationShouldThrowOnNullVacation() { + assertThatThrownBy(() -> vacationRepository().modifyVacation(ACCOUNT_ID, null)) + .isInstanceOf(NullPointerException.class); } } diff --git a/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryVacationRepositoryTest.java b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryVacationRepositoryTest.java index 0ec2d99..4fbb273 100644 --- a/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryVacationRepositoryTest.java +++ b/server/data/data-jmap/src/test/java/org/apache/james/jmap/memory/vacation/MemoryVacationRepositoryTest.java @@ -19,13 +19,20 @@ package org.apache.james.jmap.memory.vacation; -import org.apache.james.jmap.api.vacation.AbstractVacationRepositoryTest; import org.apache.james.jmap.api.vacation.VacationRepository; +import org.apache.james.jmap.api.vacation.VacationRepositoryContract; +import org.junit.jupiter.api.BeforeEach; -public class MemoryVacationRepositoryTest extends AbstractVacationRepositoryTest { +class MemoryVacationRepositoryTest implements VacationRepositoryContract { + VacationRepository vacationRepository; + + @BeforeEach + void setup() { + vacationRepository = new MemoryVacationRepository(); + } @Override - protected VacationRepository createVacationRepository() { - return new MemoryVacationRepository(); + public VacationRepository vacationRepository() { + return vacationRepository; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
