From 8ca8a33073360917a2d0f3aa2ba2a440d05023fe Mon Sep 17 00:00:00 2001 From: Benoit Tellier Date: Mon, 24 Dec 2018 12:59:52 +0700 Subject: [PATCH] MAILBOX-359 Factorize tests for SessionId ser-deserialization --- .../james/event/json/EventSerializer.scala | 4 +- .../event/json/AddedSerializationTest.java | 72 +----------------- .../event/json/ExpungedSerializationTest.java | 72 +----------------- .../json/FlagsUpdatedSerializationTest.java | 76 +------------------ ...ilboxACLUpdatedEventSerializationTest.java | 56 ++------------ .../json/MailboxAddedSerializationTest.java | 34 --------- .../MailboxDeletionSerializationTest.java | 54 +------------ .../json/MailboxRenamedSerializationTest.java | 38 ++-------- .../james/event/json/dtos/SessionIdTest.java | 65 ++++++++++++++++ 9 files changed, 94 insertions(+), 377 deletions(-) create mode 100644 mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/SessionIdTest.java diff --git a/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala b/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala index af2785ec7ab..6418425dd10 100644 --- a/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala +++ b/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala @@ -35,7 +35,7 @@ import play.api.libs.json.{JsError, JsNull, JsNumber, JsObject, JsResult, JsStri import scala.collection.JavaConverters._ -private sealed trait Event { +sealed trait Event { def toJava: JavaEvent } @@ -190,7 +190,7 @@ private object ScalaConverter { } } -private class JsonSerialize(mailboxIdFactory: MailboxId.Factory, messageIdFactory: MessageId.Factory) { +class JsonSerialize(mailboxIdFactory: MailboxId.Factory, messageIdFactory: MessageId.Factory) { implicit val systemFlagsWrites: Writes[SystemFlag] = Writes.enumNameWrites implicit val userWriters: Writes[User] = (user: User) => JsString(user.asString) implicit val quotaRootWrites: Writes[QuotaRoot] = quotaRoot => JsString(quotaRoot.getValue) diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/AddedSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/AddedSerializationTest.java index a96de11bf2b..82afcd2093d 100644 --- a/mailbox/event/json/src/test/java/org/apache/james/event/json/AddedSerializationTest.java +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/AddedSerializationTest.java @@ -510,72 +510,10 @@ void addedShouldBeWellDeSerialized() { @Nested class DeserializationErrors { - - @Nested - class DeserializationErrorOnSessionId { - @Test - void addedShouldThrowWhenMissingSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Answered\",\"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenNullSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Answered\",\"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": null," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenStringSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + + @Test + void addedShouldThrowWhenMissingSessionId() { + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( + "{" + " \"Added\": {" + " \"path\": {" + " \"namespace\": \"#private\"," + @@ -595,12 +533,10 @@ void addedShouldThrowWhenStringSessionId() { " \"messageId\": \"42\"" + " }" + " }," + - " \"sessionId\": \"42\"," + " \"user\": \"user\"" + " }" + "}").get()) .isInstanceOf(NoSuchElementException.class); - } } @Nested diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/ExpungedSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/ExpungedSerializationTest.java index 7676b6f0b72..85987f51332 100644 --- a/mailbox/event/json/src/test/java/org/apache/james/event/json/ExpungedSerializationTest.java +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/ExpungedSerializationTest.java @@ -509,72 +509,10 @@ void expungedShouldBeWellDeSerialized() { @Nested class DeserializationErrors { - - @Nested - class DeserializationErrorOnSessionId { - @Test - void expungedShouldThrowWhenMissingSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Answered\",\"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void expungedShouldThrowWhenNullSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Answered\",\"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": null," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void expungedShouldThrowWhenStringSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + + @Test + void expungedShouldThrowWhenMissingSessionId() { + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( + "{" + " \"Expunged\": {" + " \"path\": {" + " \"namespace\": \"#private\"," + @@ -594,12 +532,10 @@ void expungedShouldThrowWhenStringSessionId() { " \"messageId\": \"42\"" + " }" + " }," + - " \"sessionId\": \"42\"," + " \"user\": \"user\"" + " }" + "}").get()) .isInstanceOf(NoSuchElementException.class); - } } @Nested diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/FlagsUpdatedSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/FlagsUpdatedSerializationTest.java index e6e12ba21e3..76fdba34135 100644 --- a/mailbox/event/json/src/test/java/org/apache/james/event/json/FlagsUpdatedSerializationTest.java +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/FlagsUpdatedSerializationTest.java @@ -433,76 +433,10 @@ void flagsUpdatedShouldBeWellDeSerialized() { @Nested class DeserializationError { - - @Nested - class DeserializationErrorOnSessionId { - @Test - void flagsUpdatedShouldThrowWhenMissingSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"oldFlags\": {\"systemFlags\":[\"Deleted\",\"Seen\"],\"userFlags\":[\"Old Flag 1\"]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }," + - " {" + - " \"uid\": 654321," + - " \"modSeq\": 36," + - " \"oldFlags\": {\"systemFlags\":[\"Flagged\",\"Recent\"],\"userFlags\":[\"Old Flag 2\"]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Seen\"],\"userFlags\":[\"New Flag 2\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenNullSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": null," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"oldFlags\": {\"systemFlags\":[\"Deleted\",\"Seen\"],\"userFlags\":[\"Old Flag 1\"]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }," + - " {" + - " \"uid\": 654321," + - " \"modSeq\": 36," + - " \"oldFlags\": {\"systemFlags\":[\"Flagged\",\"Recent\"],\"userFlags\":[\"Old Flag 2\"]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Seen\"],\"userFlags\":[\"New Flag 2\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenStringSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + + @Test + void flagsUpdatedShouldThrowWhenMissingSessionId() { + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( + "{" + " \"FlagsUpdated\": {" + " \"path\": {" + " \"namespace\": \"#private\"," + @@ -510,7 +444,6 @@ void flagsUpdatedShouldThrowWhenStringSessionId() { " \"name\": \"mailboxName\"" + " }," + " \"mailboxId\": \"18\"," + - " \"sessionId\": \"42\"," + " \"updatedFlags\": [" + " {" + " \"uid\": 123456," + @@ -529,7 +462,6 @@ void flagsUpdatedShouldThrowWhenStringSessionId() { " }" + "}").get()) .isInstanceOf(NoSuchElementException.class); - } } @Nested diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxACLUpdatedEventSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxACLUpdatedEventSerializationTest.java index 14d3bf162bb..da5b509111f 100644 --- a/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxACLUpdatedEventSerializationTest.java +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxACLUpdatedEventSerializationTest.java @@ -270,54 +270,10 @@ void mailboxACLUpdatedShouldBeWellDeSerializedWithNullUser() { @Nested class DeserializationErrors { - - @Nested - class DeserializationErrorOnSessionId { - @Test - void mailboxACLUpdatedShouldThrowWhenMissingSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"MailboxACLUpdated\":{" + - " \"mailboxPath\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"bob\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"aclDiff\":{" + - " \"oldACL\":{}," + - " \"newACL\":{\"$any\":\"ar\"}}," + - " \"mailboxId\":\"23\"," + - " \"user\":\"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void mailboxACLUpdatedShouldThrowWhenNullSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"MailboxACLUpdated\":{" + - " \"mailboxPath\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"bob\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"aclDiff\":{" + - " \"oldACL\":{}," + - " \"newACL\":{\"$any\":\"ar\"}}," + - " \"mailboxId\":\"23\"," + - " \"sessionId\":null," + - " \"user\":\"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void mailboxACLUpdatedShouldThrowWhenStringSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + + @Test + void mailboxACLUpdatedShouldThrowWhenMissingSessionId() { + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( + "{" + " \"MailboxACLUpdated\":{" + " \"mailboxPath\":{" + " \"namespace\":\"#private\"," + @@ -328,12 +284,10 @@ void mailboxACLUpdatedShouldThrowWhenStringSessionId() { " \"oldACL\":{}," + " \"newACL\":{\"$any\":\"ar\"}}," + " \"mailboxId\":\"23\"," + - " \"sessionId\":\"123\"," + " \"user\":\"user\"" + " }" + "}").get()) - .isInstanceOf(NoSuchElementException.class); - } + .isInstanceOf(NoSuchElementException.class); } @Nested diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxAddedSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxAddedSerializationTest.java index 339e4d69b8f..f880328fce9 100644 --- a/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxAddedSerializationTest.java +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxAddedSerializationTest.java @@ -148,40 +148,6 @@ void mailboxAddedShouldBeWellDeSerializedWhenNullNameSpace() { @Nested class DeserializationErrors { - @Test - void fromJsonShouldRejectNullSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson("{" + - " \"MailboxAdded\":{" + - " \"mailboxPath\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"bob\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"mailboxId\":\"18\"," + - " \"user\":\"user\"," + - " \"sessionId\":null" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void fromJsonShouldRejectStringSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson("{" + - " \"MailboxAdded\":{" + - " \"mailboxPath\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"bob\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"mailboxId\":\"18\"," + - " \"user\":\"user\"," + - " \"sessionId\":\"invalid\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - @Test void fromJsonShouldRejectMissingSessionId() { assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson("{" + 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 cc5da619f34..4c926304cf0 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 @@ -277,56 +277,11 @@ void mailboxAddedShouldBeWellDeSerializedWhenNullQuotaSize() { @Nested class DeserializationErrors { - - @Nested - class DeserializationErrorOnSessionId { - @Test - void mailboxAddedShouldThrowWhenMissingSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"MailboxDeletion\":{" + - " \"user\":\"user\"," + - " \"path\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"user\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"quotaRoot\":\"user@domain\"," + - " \"deletedMessageCount\":60," + - " \"totalDeletedSize\":100," + - " \"mailboxId\":\"789\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void mailboxAddedShouldThrowWhenNullSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"MailboxDeletion\":{" + - " \"sessionId\":null," + - " \"user\":\"user\"," + - " \"path\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"user\"," + - " \"name\":\"mailboxName\"" + - " }," + - " \"quotaRoot\":\"user@domain\"," + - " \"deletedMessageCount\":60," + - " \"totalDeletedSize\":100," + - " \"mailboxId\":\"789\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void mailboxAddedShouldThrowWhenStringSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + + @Test + void mailboxAddedShouldThrowWhenMissingSessionId() { + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( + "{" + " \"MailboxDeletion\":{" + - " \"sessionId\":\"3652\"," + " \"user\":\"user\"," + " \"path\":{" + " \"namespace\":\"#private\"," + @@ -340,7 +295,6 @@ void mailboxAddedShouldThrowWhenStringSessionId() { " }" + "}").get()) .isInstanceOf(NoSuchElementException.class); - } } @Nested diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxRenamedSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxRenamedSerializationTest.java index 557eac7ce17..e5ffc7eaa52 100644 --- a/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxRenamedSerializationTest.java +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/MailboxRenamedSerializationTest.java @@ -358,35 +358,10 @@ void mailboxRenamedDeSerializeShouldThrowWhenBadUser() { } } - @Nested - class DeserializationErrorOnSessionId { - @Test - void mailboxRenamedDeSerializeShouldThrowWhenStringSessionId() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"MailboxRenamed\":{" + - " \"sessionId\":\"123456789\"," + - " \"user\":\"user\"," + - " \"path\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"user\"," + - " \"name\":\"oldMailboxName\"" + - " }," + - " \"mailboxId\":\"123456\"," + - " \"newPath\":{" + - " \"namespace\":\"#private\"," + - " \"user\":\"user\"," + - " \"name\":\"newMailboxName\"" + - " }" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void mailboxRenamedDeSerializeShouldThrowWhenNullSessionId() { - String eventWithNullSessionId = - "{" + + @Test + void mailboxRenamedDeSerializeShouldThrowWhenMissingSessionId() { + String eventWithNullSessionId = + "{" + " \"MailboxRenamed\":{" + " \"user\":\"user\"," + " \"path\":{" + @@ -402,9 +377,8 @@ void mailboxRenamedDeSerializeShouldThrowWhenNullSessionId() { " }" + " }" + "}"; - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson(eventWithNullSessionId).get()) - .isInstanceOf(NoSuchElementException.class); - } + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson(eventWithNullSessionId).get()) + .isInstanceOf(NoSuchElementException.class); } @Nested diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/SessionIdTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/SessionIdTest.java new file mode 100644 index 00000000000..49ad7ebadc3 --- /dev/null +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/SessionIdTest.java @@ -0,0 +1,65 @@ +/**************************************************************** + * 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 org.apache.james.event.json.JsonSerialize; +import org.apache.james.mailbox.MailboxSession; +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 SessionIdTest { + private static final JsonSerialize JSON_SERIALIZE = new JsonSerialize(new TestId.Factory(), new TestMessageId.Factory()); + + @Test + void sessionIdShouldBeWellSerialized() { + assertThat(JSON_SERIALIZE.sessionIdWrites().writes(MailboxSession.SessionId.of(18)).toString()) + .isEqualTo("18"); + } + + @Test + void sessionIdShouldBeWellDeSerialized() { + assertThat(JSON_SERIALIZE.sessionIdReads().reads(new JsNumber(BigDecimal.valueOf(18)))) + .isEqualTo(new JsSuccess<>(MailboxSession.SessionId.of(18), new JsPath(List.empty()))); + } + + @Test + void sessionIdShouldReturnErrorWhenString() { + assertThat(JSON_SERIALIZE.sessionIdReads().reads(new JsString("18"))) + .isInstanceOf(JsError.class); + } + + @Test + void sessionIdShouldReturnErrorWhenNull() { + assertThat(JSON_SERIALIZE.sessionIdReads().reads(JsNull$.MODULE$)) + .isInstanceOf(JsError.class); + } +}