MAILBOX-359 Factorize tests for QuotaRoot ser-deserialization
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d7910fcb Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d7910fcb Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d7910fcb Branch: refs/heads/master Commit: d7910fcb001416800b401e8b6ba477652a36464c Parents: 70a4fd5 Author: Benoit Tellier <btell...@linagora.com> Authored: Mon Dec 24 14:10:17 2018 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Tue Jan 8 14:37:57 2019 +0700 ---------------------------------------------------------------------- .../json/MailboxDeletionSerializationTest.java | 93 +------------------- ...QuotaUsageUpdatedEventSerializationTest.java | 88 ------------------ .../james/event/json/dtos/QuotaRootTest.java | 92 +++++++++++++++++++ 3 files changed, 96 insertions(+), 177 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/d7910fcb/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxDeletionSerializationTest.java ---------------------------------------------------------------------- diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxDeletionSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxDeletionSerializationTest.java index f0f9f66..1f76c5b 100644 --- a/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxDeletionSerializationTest.java +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxDeletionSerializationTest.java @@ -90,46 +90,6 @@ class MailboxDeletionSerializationTest { } @Nested - class EmptyQuotaRoot { - private final MailboxListener.MailboxDeletion emptyQuotaRootEvent = new MailboxListener.MailboxDeletion( - SESSION_ID, - USER, - MAILBOX_PATH, - QuotaRoot.quotaRoot("", Optional.empty()), - DELETED_MESSAGE_COUNT, - TOTAL_DELETED_SIZE, - MAILBOX_ID); - private final String nullUserMailboxEventJson = - "{" + - " \"MailboxDeletion\":{" + - " \"sessionId\":3652," + - " \"user\":\"user\"," + - " \"path\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"user\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"quotaRoot\":\"\"," + - " \"deletedMessageCount\":60," + - " \"totalDeletedSize\":100," + - " \"mailboxId\":\"789\"" + - " }" + - "}"; - - @Test - void mailboxAddedShouldBeWellSerializedWhenEmptyQuotaRoot() { - assertThatJson(EVENT_SERIALIZER.toJson(emptyQuotaRootEvent)) - .isEqualTo(nullUserMailboxEventJson); - } - - @Test - void mailboxAddedShouldBeWellDeSerializedWhenEmptyQuotaRoot() { - assertThat(EVENT_SERIALIZER.fromJson(nullUserMailboxEventJson).get()) - .isEqualTo(emptyQuotaRootEvent); - } - } - - @Nested class NullQuotaCountInDeletedMessageCount { private final MailboxListener.MailboxDeletion unlimitedQuotaCountDeletedMessageEvent = new MailboxListener.MailboxDeletion( SESSION_ID, @@ -251,53 +211,10 @@ class MailboxDeletionSerializationTest { .isInstanceOf(NoSuchElementException.class); } - @Nested - class DeserializationErrorOnQuotaRoot { - @Test - void mailboxAddedShouldThrowWhenMissingQuotaRoot() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"MailboxDeletion\":{" + - " \"sessionId\":3652," + - " \"user\":\"user\"," + - " \"path\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"user\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"deletedMessageCount\":60," + - " \"totalDeletedSize\":100," + - " \"mailboxId\":\"789\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void mailboxAddedShouldThrowWhenNullQuotaRoot() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"MailboxDeletion\":{" + - " \"sessionId\":3652," + - " \"user\":\"user\"," + - " \"path\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"user\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"quotaRoot\":null," + - " \"deletedMessageCount\":60," + - " \"totalDeletedSize\":100," + - " \"mailboxId\":\"789\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void mailboxAddedShouldThrowWhenQuotaRootIsNotAString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + + @Test + void mailboxAddedShouldThrowWhenMissingQuotaRoot() { + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( + "{" + " \"MailboxDeletion\":{" + " \"sessionId\":3652," + " \"user\":\"user\"," + @@ -306,14 +223,12 @@ class MailboxDeletionSerializationTest { " \"user\":\"user\"," + " \"name\":\"mailboxName\"" + " }," + - " \"quotaRoot\":123456," + " \"deletedMessageCount\":60," + " \"totalDeletedSize\":100," + " \"mailboxId\":\"789\"" + " }" + "}").get()) .isInstanceOf(NoSuchElementException.class); - } } @Nested http://git-wip-us.apache.org/repos/asf/james-project/blob/d7910fcb/mailbox/event/json/src/test/java/org/apache/james/event/json/QuotaUsageUpdatedEventSerializationTest.java ---------------------------------------------------------------------- diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/QuotaUsageUpdatedEventSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/QuotaUsageUpdatedEventSerializationTest.java index b76b53b..af95172 100644 --- a/mailbox/event/json/src/test/java/org/apache/james/event/json/QuotaUsageUpdatedEventSerializationTest.java +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/QuotaUsageUpdatedEventSerializationTest.java @@ -111,94 +111,6 @@ class QuotaUsageUpdatedEventSerializationTest { assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson(quotaUsageUpdatedEvent).get()) .isInstanceOf(NoSuchElementException.class); } - - @Nested - class WitQuotaRoot { - - @Nested - class WithNormalQuotaRoot { - - @Test - void toJsonShouldReturnSerializedJsonQuotaRoot() { - assertThatJson(EVENT_SERIALIZER.toJson(DEFAULT_QUOTA_EVENT)) - .isEqualTo(DEFAULT_QUOTA_EVENT_JSON); - } - - @Test - void fromJsonShouldDeserializeQuotaRootJson() { - assertThat(EVENT_SERIALIZER.fromJson(DEFAULT_QUOTA_EVENT_JSON).get()) - .isEqualTo(DEFAULT_QUOTA_EVENT); - } - } - - @Nested - class WithEmptyQuotaRoot { - private final QuotaRoot emptyQuotaRoot = QuotaRoot.quotaRoot("", Optional.empty()); - private final MailboxListener.QuotaUsageUpdatedEvent eventWithEmptyQuotaRoot = - new MailboxListener.QuotaUsageUpdatedEvent( - USER, - emptyQuotaRoot, - QUOTA_COUNT, - QUOTA_SIZE, - INSTANT); - private final String quotaUsageUpdatedEvent = - "{" + - "\"QuotaUsageUpdatedEvent\":{" + - "\"quotaRoot\":\"\"," + - "\"countQuota\":{\"used\":12,\"limit\":100,\"limits\":{}}," + - "\"time\":\"2018-11-13T12:00:55Z\"," + - "\"sizeQuota\":{\"used\":1234,\"limit\":10000,\"limits\":{}}," + - "\"user\":\"user\"" + - "}" + - "}"; - - @Test - void toJsonShouldSerializeWithEmptyQuotaRoot() { - assertThatJson(EVENT_SERIALIZER.toJson(eventWithEmptyQuotaRoot)) - .isEqualTo(quotaUsageUpdatedEvent); - } - - @Test - void fromJsonShouldDeserializeWithEmptyQuotaRoot() { - assertThat(EVENT_SERIALIZER.fromJson(quotaUsageUpdatedEvent).get()) - .isEqualTo(eventWithEmptyQuotaRoot); - } - } - - @Nested - class WithNullQuotaRoot { - private final MailboxListener.QuotaUsageUpdatedEvent eventWithNullQuotaRoot = - new MailboxListener.QuotaUsageUpdatedEvent( - USER, - null, - QUOTA_COUNT, - QUOTA_SIZE, - INSTANT); - - private final String quotaUsageUpdatedEvent = - "{" + - "\"QuotaUsageUpdatedEvent\":{" + - "\"countQuota\":{\"used\":12,\"limit\":100,\"limits\":{}}," + - "\"time\":\"2018-11-13T12:00:55Z\"," + - "\"sizeQuota\":{\"used\":1234,\"limit\":10000,\"limits\":{}}," + - "\"user\":\"user\"" + - "}" + - "}"; - - @Test - void toJsonShouldThrowWithNullQuotaRoot() { - assertThatThrownBy(() -> EVENT_SERIALIZER.toJson(eventWithNullQuotaRoot)) - .isInstanceOf(NullPointerException.class); - } - - @Test - void fromJsonShouldThrowWithNullQuotaRoot() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson(quotaUsageUpdatedEvent).get()) - .isInstanceOf(NoSuchElementException.class); - } - } - } - @Nested class WithQuotaCount { http://git-wip-us.apache.org/repos/asf/james-project/blob/d7910fcb/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaRootTest.java ---------------------------------------------------------------------- diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaRootTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaRootTest.java new file mode 100644 index 0000000..53076a2 --- /dev/null +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/QuotaRootTest.java @@ -0,0 +1,92 @@ +/**************************************************************** + * 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.event.json.dtos; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Optional; + +import org.apache.james.core.Domain; +import org.apache.james.event.json.JsonSerialize; +import org.apache.james.mailbox.model.QuotaRoot; +import org.apache.james.mailbox.model.TestId; +import org.apache.james.mailbox.model.TestMessageId; +import org.junit.jupiter.api.Test; + +import play.api.libs.json.JsError; +import play.api.libs.json.JsNull$; +import play.api.libs.json.JsNumber; +import play.api.libs.json.JsPath; +import play.api.libs.json.JsString; +import play.api.libs.json.JsSuccess; +import scala.collection.immutable.List; +import scala.math.BigDecimal; + +class QuotaRootTest { + private static final JsonSerialize JSON_SERIALIZE = new JsonSerialize(new TestId.Factory(), new TestMessageId.Factory()); + + @Test + void quotaRootWithDomainShouldBeWellSerialized() { + assertThat(JSON_SERIALIZE.quotaRootWrites().writes(QuotaRoot.quotaRoot("b...@domain.tld", Optional.of(Domain.of("domain.tld"))))) + .isEqualTo(new JsString("b...@domain.tld")); + } + + @Test + void quotaRootWithDomainShouldBeWellDeSerialized() { + assertThat(JSON_SERIALIZE.quotaRootReads().reads(new JsString("b...@domain.tld"))) + .isEqualTo(new JsSuccess<>(QuotaRoot.quotaRoot("b...@domain.tld", Optional.of(Domain.of("domain.tld"))), new JsPath(List.empty()))); + } + + @Test + void quotaRootShouldBeWellSerialized() { + assertThat(JSON_SERIALIZE.quotaRootWrites().writes(QuotaRoot.quotaRoot("bob", Optional.empty()))) + .isEqualTo(new JsString("bob")); + } + + @Test + void quotaRootShouldBeWellDeSerialized() { + assertThat(JSON_SERIALIZE.quotaRootReads().reads(new JsString("bob"))) + .isEqualTo(new JsSuccess<>(QuotaRoot.quotaRoot("bob", Optional.empty()), new JsPath(List.empty()))); + } + + @Test + void emptyQuotaRootShouldBeWellSerialized() { + assertThat(JSON_SERIALIZE.quotaRootWrites().writes(QuotaRoot.quotaRoot("", Optional.empty()))) + .isEqualTo(new JsString("")); + } + + @Test + void emptyQuotaRootShouldBeWellDeSerialized() { + assertThat(JSON_SERIALIZE.quotaRootReads().reads(new JsString(""))) + .isEqualTo(new JsSuccess<>(QuotaRoot.quotaRoot("", Optional.empty()), new JsPath(List.empty()))); + } + + @Test + void emptyQuotaRootShouldReturnErrorWhenNull() { + assertThat(JSON_SERIALIZE.quotaRootReads().reads(JsNull$.MODULE$)) + .isInstanceOf(JsError.class); + } + + @Test + void emptyQuotaRootShouldReturnErrorWhenNotString() { + assertThat(JSON_SERIALIZE.quotaRootReads().reads(new JsNumber(BigDecimal.valueOf(18)))) + .isInstanceOf(JsError.class); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org