You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/05/10 02:31:18 UTC

[11/15] james-project git commit: MAILBOX-332 Enforce event deserialization stability

MAILBOX-332 Enforce event deserialization stability

Store JSON in files to prevent them to be changed by IDE refactorings


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/409792c2
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/409792c2
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/409792c2

Branch: refs/heads/master
Commit: 409792c2499fea1a792f4e23eb421e4e0e69cbb1
Parents: 0a393f6
Author: benwa <bt...@linagora.com>
Authored: Wed May 9 16:48:15 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu May 10 09:15:41 2018 +0700

----------------------------------------------------------------------
 .../mailbox/quota/cassandra/dto/DTOTest.java    | 111 ++++++++++++++-----
 .../src/test/resources/json/event.json          |  19 ++++
 .../src/test/resources/json/event2.json         |  20 ++++
 .../src/test/resources/json/event3.json         |  23 ++++
 .../src/test/resources/json/event4.json         |  23 ++++
 .../org/apache/james/util/ClassLoaderUtils.java |  16 ++-
 6 files changed, 180 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/409792c2/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java
----------------------------------------------------------------------
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 a42725a..2f48a55 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,8 +20,12 @@
 package org.apache.james.mailbox.quota.cassandra.dto;
 
 import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;
+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.core.User;
 import org.apache.james.eventsourcing.EventId;
 import org.apache.james.eventsourcing.cassandra.JsonEventSerializer;
@@ -31,6 +35,8 @@ import org.apache.james.mailbox.quota.QuotaSize;
 import org.apache.james.mailbox.quota.mailing.aggregates.UserQuotaThresholds;
 import org.apache.james.mailbox.quota.mailing.events.QuotaThresholdChangedEvent;
 import org.apache.james.mailbox.quota.model.HistoryEvolution;
+import org.apache.james.mailbox.quota.model.QuotaThresholdChange;
+import org.apache.james.util.ClassLoaderUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -42,6 +48,7 @@ class DTOTest {
 
     public static final Quota<QuotaSize> SIZE_QUOTA = Quota.<QuotaSize>builder().used(QuotaSize.size(23)).computedLimit(QuotaSize.size(33)).build();
     public static final Quota<QuotaCount> COUNT_QUOTA = Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.count(45)).build();
+    public static final Instant INSTANT = Instant.ofEpochMilli(45554);
     public static final QuotaThresholdChangedEvent EVENT = new QuotaThresholdChangedEvent(
         EventId.first(),
         HistoryEvolution.noChanges(),
@@ -49,32 +56,34 @@ class DTOTest {
         SIZE_QUOTA,
         COUNT_QUOTA,
         UserQuotaThresholds.Id.from(User.fromUsername("foo@bar.com")));
+    public static final QuotaThresholdChangedEvent EVENT_2 = new QuotaThresholdChangedEvent(
+        EventId.first(),
+        HistoryEvolution.lowerThresholdReached(new QuotaThresholdChange(_75, INSTANT)),
+        HistoryEvolution.noChanges(),
+        SIZE_QUOTA,
+        Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.unlimited()).build(),
+        UserQuotaThresholds.Id.from(User.fromUsername("foo@bar.com")));
+    public static final QuotaThresholdChangedEvent EVENT_3 = new QuotaThresholdChangedEvent(
+        EventId.first(),
+        HistoryEvolution.lowerThresholdReached(new QuotaThresholdChange(_75, INSTANT)),
+        HistoryEvolution.higherThresholdReached(new QuotaThresholdChange(_80, INSTANT),
+            HistoryEvolution.HighestThresholdRecentness.NotAlreadyReachedDuringGracePeriod),
+        SIZE_QUOTA,
+        Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.unlimited()).build(),
+        UserQuotaThresholds.Id.from(User.fromUsername("foo@bar.com")));
+    public static final QuotaThresholdChangedEvent EVENT_4 = new QuotaThresholdChangedEvent(
+        EventId.first(),
+        HistoryEvolution.lowerThresholdReached(new QuotaThresholdChange(_75, INSTANT)),
+        HistoryEvolution.higherThresholdReached(new QuotaThresholdChange(_80, INSTANT),
+            HistoryEvolution.HighestThresholdRecentness.AlreadyReachedDuringGracePeriod),
+        SIZE_QUOTA,
+        Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.unlimited()).build(),
+        UserQuotaThresholds.Id.from(User.fromUsername("foo@bar.com")));
 
-    public static final String EVENT_JSON = "{" +
-        " \"type\": \"quota-threshold-change\"," +
-        " \"eventId\": 0," +
-        " \"user\": \"foo@bar.com\"," +
-        " \"sizeQuota\": {" +
-        "   \"used\": 23," +
-        "   \"limit\": 33" +
-        " }," +
-        " \"countQuota\": {" +
-        "   \"used\": 12," +
-        "   \"limit\": 45" +
-        " }," +
-        " \"sizeEvolution\": {" +
-        "   \"change\": \"NoChange\"," +
-        "   \"threshold\": null," +
-        "   \"instant\": null," +
-        "   \"recentness\": null" +
-        " }," +
-        " \"countEvolution\": {" +
-        "   \"change\": \"NoChange\"," +
-        "   \"threshold\": null," +
-        "   \"instant\": null," +
-        "   \"recentness\": null" +
-        " }" +
-        "}";
+    public static final String EVENT_JSON = ClassLoaderUtils.getSystemResourceAsString("json/event.json");
+    public static final String EVENT_JSON_2 = ClassLoaderUtils.getSystemResourceAsString("json/event2.json");
+    public static final String EVENT_JSON_3 = ClassLoaderUtils.getSystemResourceAsString("json/event3.json");
+    public static final String EVENT_JSON_4 = ClassLoaderUtils.getSystemResourceAsString("json/event4.json");
 
     public static final String COUNT_QUOTA_JSON = "{" +
         "   \"used\": 12," +
@@ -129,9 +138,6 @@ class DTOTest {
 
     @Test
     void shouldDeserializeQuotaThresholdChangedEventDTO() throws Exception {
-        ObjectMapper objectMapper = new ObjectMapper();
-        objectMapper.registerModule(new Jdk8Module());
-
         assertThat(objectMapper.readValue(EVENT_JSON, QuotaThresholdChangedEventDTO.class)
             .toEvent())
             .isEqualTo(EVENT);
@@ -144,4 +150,53 @@ class DTOTest {
             .isEqualTo(EVENT_JSON);
     }
 
+    @Test
+    void shouldDeserializeQuotaThresholdChangedEvent() throws Exception {
+        assertThatJson(new JsonEventSerializer(new QuotaThresholdChangedEventDTOModule())
+            .deserialize(EVENT_JSON))
+            .isEqualTo(EVENT);
+    }
+
+    @Test
+    void shouldSerializeEvent2() throws Exception {
+        assertThatJson(new JsonEventSerializer(new QuotaThresholdChangedEventDTOModule())
+            .serialize(EVENT_2))
+            .isEqualTo(EVENT_JSON_2);
+    }
+
+    @Test
+    void shouldDeserializeEvent2() throws Exception {
+        assertThatJson(new JsonEventSerializer(new QuotaThresholdChangedEventDTOModule())
+            .deserialize(EVENT_JSON_2))
+            .isEqualTo(EVENT_2);
+    }
+
+    @Test
+    void shouldSerializeEvent3() throws Exception {
+        assertThatJson(new JsonEventSerializer(new QuotaThresholdChangedEventDTOModule())
+            .serialize(EVENT_3))
+            .isEqualTo(EVENT_JSON_3);
+    }
+
+    @Test
+    void shouldDeserializeEvent3() throws Exception {
+        assertThatJson(new JsonEventSerializer(new QuotaThresholdChangedEventDTOModule())
+            .deserialize(EVENT_JSON_3))
+            .isEqualTo(EVENT_3);
+    }
+
+    @Test
+    void shouldSerializeEvent4() throws Exception {
+        assertThatJson(new JsonEventSerializer(new QuotaThresholdChangedEventDTOModule())
+            .serialize(EVENT_4))
+            .isEqualTo(EVENT_JSON_4);
+    }
+
+    @Test
+    void shouldDeserializeEvent4() throws Exception {
+        assertThatJson(new JsonEventSerializer(new QuotaThresholdChangedEventDTOModule())
+            .deserialize(EVENT_JSON_4))
+            .isEqualTo(EVENT_4);
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/409792c2/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event.json
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event.json b/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event.json
new file mode 100644
index 0000000..4449047
--- /dev/null
+++ b/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event.json
@@ -0,0 +1,19 @@
+{
+ "type": "quota-threshold-change",
+ "eventId": 0,
+ "user": "foo@bar.com",
+ "sizeQuota": {
+   "used": 23,
+   "limit": 33
+ },
+ "countQuota": {
+   "used": 12,
+   "limit": 45
+ },
+ "sizeEvolution": {
+   "change": "NoChange"
+ },
+ "countEvolution": {
+   "change": "NoChange"
+ }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/409792c2/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event2.json
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event2.json b/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event2.json
new file mode 100644
index 0000000..b20cfab
--- /dev/null
+++ b/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event2.json
@@ -0,0 +1,20 @@
+{
+  "type":"quota-threshold-change",
+  "eventId":0,
+  "user":"foo@bar.com",
+  "sizeQuota":{
+    "used":23,
+    "limit":33
+  },
+  "countQuota":{
+    "used":12
+  },
+  "sizeEvolution":{
+    "threshold":0.75,
+    "instant":45554,
+    "change":"LowerThresholdReached"
+  },
+  "countEvolution":{
+    "change":"NoChange"
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/409792c2/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event3.json
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event3.json b/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event3.json
new file mode 100644
index 0000000..4bda126
--- /dev/null
+++ b/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event3.json
@@ -0,0 +1,23 @@
+{
+  "type":"quota-threshold-change",
+  "eventId":0,
+  "user":"foo@bar.com",
+  "sizeQuota":{
+    "used":23,
+    "limit":33
+  },
+  "countQuota":{
+    "used":12
+  },
+  "sizeEvolution":{
+    "threshold":0.75,
+    "instant":45554,
+    "change":"LowerThresholdReached"
+  },
+  "countEvolution":{
+    "recentness":"NotAlreadyReachedDuringGracePeriod",
+    "threshold":0.8,
+    "instant":45554,
+    "change":"HigherThresholdReached"
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/409792c2/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event4.json
----------------------------------------------------------------------
diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event4.json b/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event4.json
new file mode 100644
index 0000000..0852dc5
--- /dev/null
+++ b/mailbox/plugin/quota-mailing-cassandra/src/test/resources/json/event4.json
@@ -0,0 +1,23 @@
+{
+  "type":"quota-threshold-change",
+  "eventId":0,
+  "user":"foo@bar.com",
+  "sizeQuota":{
+    "used":23,
+    "limit":33
+  },
+  "countQuota":{
+    "used":12
+  },
+  "sizeEvolution":{
+    "threshold":0.75,
+    "instant":45554,
+    "change":"LowerThresholdReached"
+  },
+  "countEvolution":{
+    "recentness":"AlreadyReachedDuringGracePeriod",
+    "threshold":0.8,
+    "instant":45554,
+    "change":"HigherThresholdReached"
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/409792c2/server/container/util-java8/src/main/java/org/apache/james/util/ClassLoaderUtils.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/ClassLoaderUtils.java b/server/container/util-java8/src/main/java/org/apache/james/util/ClassLoaderUtils.java
index d7df10a..89574b6 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/ClassLoaderUtils.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/ClassLoaderUtils.java
@@ -25,11 +25,19 @@ import java.nio.charset.StandardCharsets;
 import org.apache.commons.io.IOUtils;
 
 public class ClassLoaderUtils {
-    public static String getSystemResourceAsString(String filename) throws IOException {
-        return IOUtils.toString(ClassLoader.getSystemResourceAsStream(filename), StandardCharsets.US_ASCII);
+    public static String getSystemResourceAsString(String filename) {
+        try {
+            return IOUtils.toString(ClassLoader.getSystemResourceAsStream(filename), StandardCharsets.US_ASCII);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 
-    public static byte[] getSystemResourceAsByteArray(String filename) throws IOException {
-        return IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream(filename));
+    public static byte[] getSystemResourceAsByteArray(String filename) {
+        try {
+            return IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream(filename));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org