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 565682230c0e4b96db376c3a0e37e28ff198dd0e Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Wed Dec 18 12:56:28 2019 +0700 JAMES-2993 Use JsonSerializationVerifier for Quota thresholds event DTO tests --- json/pom.xml | 4 ++ .../apache/james/JsonSerializationVerifier.java | 45 ++++++++----- mailbox/plugin/quota-mailing-cassandra/pom.xml | 6 ++ .../james/mailbox/quota/cassandra/dto/DTOTest.java | 76 +++------------------- 4 files changed, 46 insertions(+), 85 deletions(-) diff --git a/json/pom.xml b/json/pom.xml index c9d29b7..dcdd913 100644 --- a/json/pom.xml +++ b/json/pom.xml @@ -53,6 +53,10 @@ <artifactId>jackson-datatype-jsr310</artifactId> </dependency> <dependency> + <groupId>com.github.fge</groupId> + <artifactId>throwing-lambdas</artifactId> + </dependency> + <dependency> <groupId>com.github.steveash.guavate</groupId> <artifactId>guavate</artifactId> </dependency> diff --git a/json/src/test/java/org/apache/james/JsonSerializationVerifier.java b/json/src/test/java/org/apache/james/JsonSerializationVerifier.java index 53d423d..2d717e2 100644 --- a/json/src/test/java/org/apache/james/JsonSerializationVerifier.java +++ b/json/src/test/java/org/apache/james/JsonSerializationVerifier.java @@ -23,47 +23,58 @@ import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; +import java.util.List; +import org.apache.commons.lang3.tuple.Pair; import org.apache.james.json.DTO; import org.apache.james.json.DTOModule; import org.apache.james.json.JsonGenericSerializer; -public class JsonSerializationVerifier<T, U extends DTO> { - @FunctionalInterface - public interface RequireBean<T, U extends DTO> { - RequireJson<T, U> bean(T bean); - } +import com.github.fge.lambdas.Throwing; +import com.google.common.collect.ImmutableList; +public class JsonSerializationVerifier<T, U extends DTO> { @FunctionalInterface public interface RequireJson<T, U extends DTO> { JsonSerializationVerifier<T, U> json(String json); } - public static <T, U extends DTO> RequireBean<T, U> dtoModule(DTOModule<T, U> dtoModule) { - return bean -> json -> new JsonSerializationVerifier<>(dtoModule, json, bean); + public static <T, U extends DTO> JsonSerializationVerifier<T, U> dtoModule(DTOModule<T, U> dtoModule) { + return new JsonSerializationVerifier<>(dtoModule, ImmutableList.of()); } private final DTOModule<T, U> dtoModule; - private final String json; - private final T bean; + private final List<Pair<String, T>> testValues; - private JsonSerializationVerifier(DTOModule<T, U> dtoModule, String json, T bean) { + private JsonSerializationVerifier(DTOModule<T, U> dtoModule, List<Pair<String, T>> testValues) { this.dtoModule = dtoModule; - this.json = json; - this.bean = bean; + this.testValues = testValues; + } + + public RequireJson<T, U> bean(T bean) { + return json -> new JsonSerializationVerifier<>( + dtoModule, + ImmutableList.<Pair<String, T>>builder() + .addAll(testValues) + .add(Pair.of(json, bean)) + .build()); } public void verify() throws IOException { + testValues.forEach(Throwing.<Pair<String, T>>consumer(this::verify).sneakyThrow()); + } + + private void verify(Pair<String, T> testValue) throws IOException { JsonGenericSerializer<T, U> seriliazer = JsonGenericSerializer .forModules(dtoModule) .withoutNestedType(); - assertThatJson(seriliazer.serialize(bean)) + assertThatJson(seriliazer.serialize(testValue.getRight())) .describedAs("Serialization test") - .isEqualTo(json); + .isEqualTo(testValue.getLeft()); - assertThat(seriliazer.deserialize(json)) - .describedAs("Deserialization test") - .isEqualToComparingFieldByFieldRecursively(bean); + assertThat(seriliazer.deserialize(testValue.getLeft())) + .describedAs("Deserialization test [" + testValue.getRight() + "]") + .isEqualToComparingFieldByFieldRecursively(testValue.getRight()); } } diff --git a/mailbox/plugin/quota-mailing-cassandra/pom.xml b/mailbox/plugin/quota-mailing-cassandra/pom.xml index 656f066..d29f17c 100644 --- a/mailbox/plugin/quota-mailing-cassandra/pom.xml +++ b/mailbox/plugin/quota-mailing-cassandra/pom.xml @@ -91,6 +91,12 @@ </dependency> <dependency> <groupId>${james.groupId}</groupId> + <artifactId>james-json</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> <artifactId>james-server-core</artifactId> <scope>test</scope> </dependency> diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java index 3e3d824..98b4292 100644 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java +++ b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java @@ -20,19 +20,20 @@ package org.apache.james.mailbox.quota.cassandra.dto; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; +import static org.apache.james.mailbox.quota.cassandra.dto.QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE; import static org.apache.james.mailbox.quota.model.QuotaThresholdFixture._75; import static org.apache.james.mailbox.quota.model.QuotaThresholdFixture._80; import static org.assertj.core.api.Assertions.assertThat; import java.time.Instant; +import org.apache.james.JsonSerializationVerifier; import org.apache.james.core.Username; import org.apache.james.core.quota.QuotaCountLimit; import org.apache.james.core.quota.QuotaCountUsage; import org.apache.james.core.quota.QuotaSizeLimit; import org.apache.james.core.quota.QuotaSizeUsage; import org.apache.james.eventsourcing.EventId; -import org.apache.james.eventsourcing.eventstore.cassandra.JsonEventSerializer; import org.apache.james.mailbox.model.Quota; import org.apache.james.mailbox.quota.mailing.aggregates.UserQuotaThresholds; import org.apache.james.mailbox.quota.mailing.events.QuotaThresholdChangedEvent; @@ -135,73 +136,12 @@ class DTOTest { } @Test - void shouldSerializeQuotaThresholdChangedEventDTO() throws Exception { - assertThatJson(objectMapper.writeValueAsString( - QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE.toDTO(EVENT))) - .isEqualTo(EVENT_JSON); - } - - @Test - void shouldDeserializeQuotaThresholdChangedEventDTO() throws Exception { - assertThat(objectMapper.readValue(EVENT_JSON, QuotaThresholdChangedEventDTO.class) - .toEvent()) - .isEqualTo(EVENT); - } - - @Test void shouldSerializeQuotaThresholdChangedEvent() throws Exception { - assertThatJson(JsonEventSerializer.forModules(QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE).withoutNestedType() - .serialize(EVENT)) - .isEqualTo(EVENT_JSON); - } - - @Test - void shouldDeserializeQuotaThresholdChangedEvent() throws Exception { - assertThat(JsonEventSerializer.forModules(QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE).withoutNestedType() - .deserialize(EVENT_JSON)) - .isEqualTo(EVENT); - } - - @Test - void shouldSerializeEvent2() throws Exception { - assertThatJson(JsonEventSerializer.forModules(QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE).withoutNestedType() - .serialize(EVENT_2)) - .isEqualTo(EVENT_JSON_2); - } - - @Test - void shouldDeserializeEvent2() throws Exception { - assertThat(JsonEventSerializer.forModules(QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE).withoutNestedType() - .deserialize(EVENT_JSON_2)) - .isEqualTo(EVENT_2); - } - - @Test - void shouldSerializeEvent3() throws Exception { - assertThatJson(JsonEventSerializer.forModules(QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE).withoutNestedType() - .serialize(EVENT_3)) - .isEqualTo(EVENT_JSON_3); - } - - @Test - void shouldDeserializeEvent3() throws Exception { - assertThat(JsonEventSerializer.forModules(QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE).withoutNestedType() - .deserialize(EVENT_JSON_3)) - .isEqualTo(EVENT_3); + JsonSerializationVerifier.dtoModule(QUOTA_THRESHOLD_CHANGE) + .bean(EVENT).json(EVENT_JSON) + .bean(EVENT_2).json(EVENT_JSON_2) + .bean(EVENT_3).json(EVENT_JSON_3) + .bean(EVENT_4).json(EVENT_JSON_4) + .verify(); } - - @Test - void shouldSerializeEvent4() throws Exception { - assertThatJson(JsonEventSerializer.forModules(QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE).withoutNestedType() - .serialize(EVENT_4)) - .isEqualTo(EVENT_JSON_4); - } - - @Test - void shouldDeserializeEvent4() throws Exception { - assertThat(JsonEventSerializer.forModules(QuotaEventDTOModules.QUOTA_THRESHOLD_CHANGE).withoutNestedType() - .deserialize(EVENT_JSON_4)) - .isEqualTo(EVENT_4); - } - } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org