From 5d365c14313a6179b00eb0cdca3d19b637e007eb Mon Sep 17 00:00:00 2001 From: Benoit Tellier Date: Mon, 24 Dec 2018 14:40:58 +0700 Subject: [PATCH] MAILBOX-359 Factorize tests for Flags ser-deserialization --- .../event/json/AddedSerializationTest.java | 372 +------------ .../event/json/ExpungedSerializationTest.java | 372 +------------ .../json/FlagsUpdatedSerializationTest.java | 488 +----------------- .../james/event/json/dtos/FlagsTest.java | 240 +++++++++ 4 files changed, 263 insertions(+), 1209 deletions(-) create mode 100644 mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/FlagsTest.java 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 d71b9e80f84..61ce957816d 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 @@ -20,7 +20,6 @@ package org.apache.james.event.json; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; -import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -139,157 +138,6 @@ void addedShouldBeWellDeSerializedWhenMapKeyIsEmpty() { } } - @Nested - class WithFlags { - - @Nested - class WithEmptyFlags { - private final Flags emptyFlags = new FlagsBuilder().build(); - private final MailboxListener.Added emptyFlagsAddedEvent = new MailboxListener.Added(SESSION_ID, USER, MAILBOX_PATH, MAILBOX_ID, - ImmutableSortedMap.of( - MESSAGE_UID, - new MessageMetaData(MESSAGE_UID, MOD_SEQ, emptyFlags, SIZE, Date.from(INSTANT), MESSAGE_ID))); - - private final String emptyFlagsAddedEventJson = - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[], " + - " \"userFlags\":[]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void addedShouldBeWellSerializedWhenEmptyFlags() { - assertThatJson(EVENT_SERIALIZER.toJson(emptyFlagsAddedEvent)) - .isEqualTo(emptyFlagsAddedEventJson); - } - - @Test - void addedShouldBeWellDeSerializedWhenEmptyFlags() { - assertThat(EVENT_SERIALIZER.fromJson(emptyFlagsAddedEventJson).get()) - .isEqualTo(emptyFlagsAddedEvent); - } - } - - @Nested - class WithOnlyUserFlags { - private final Flags onlyUserFlags = new FlagsBuilder() - .add("Custom 1", "Custom 2", "") - .build(); - private final MailboxListener.Added onlyUserFlagsAddedEvent = new MailboxListener.Added(SESSION_ID, USER, MAILBOX_PATH, MAILBOX_ID, - ImmutableSortedMap.of( - MESSAGE_UID, - new MessageMetaData(MESSAGE_UID, MOD_SEQ, onlyUserFlags, SIZE, Date.from(INSTANT), MESSAGE_ID))); - - private final String userOnlyFlagsAddedEventJson = - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[], " + - " \"userFlags\":[\"Custom 1\", \"Custom 2\", \"\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void addedShouldBeWellSerializedWhenOnlyUserFlags() { - assertThatJson(EVENT_SERIALIZER.toJson(onlyUserFlagsAddedEvent)) - .when(IGNORING_ARRAY_ORDER) - .isEqualTo(userOnlyFlagsAddedEventJson); - } - - @Test - void addedShouldBeWellDeSerializedWhenOnlyUserFlags() { - assertThat(EVENT_SERIALIZER.fromJson(userOnlyFlagsAddedEventJson).get()) - .isEqualTo(onlyUserFlagsAddedEvent); - } - } - - @Nested - class WithOnlySystemFlags { - private final Flags onlySystemFlags = new FlagsBuilder() - .add(Flags.Flag.SEEN, Flags.Flag.ANSWERED, Flags.Flag.DELETED) - .build(); - private final MailboxListener.Added onlySystemFlagsAddedEvent = new MailboxListener.Added(SESSION_ID, USER, MAILBOX_PATH, MAILBOX_ID, - ImmutableSortedMap.of( - MESSAGE_UID, - new MessageMetaData(MESSAGE_UID, MOD_SEQ, onlySystemFlags, SIZE, Date.from(INSTANT), MESSAGE_ID))); - - private final String systemOnlyFlagsAddedEventJson = - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Seen\",\"Answered\",\"Deleted\"], " + - " \"userFlags\":[]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void addedShouldBeWellSerializedWhenOnlySystemFlags() { - assertThatJson(EVENT_SERIALIZER.toJson(onlySystemFlagsAddedEvent)) - .when(IGNORING_ARRAY_ORDER) - .isEqualTo(systemOnlyFlagsAddedEventJson); - } - - @Test - void addedShouldBeWellDeSerializedWhenOnlySystemFlags() { - assertThat(EVENT_SERIALIZER.fromJson(systemOnlyFlagsAddedEventJson).get()) - .isEqualTo(onlySystemFlagsAddedEvent); - } - } - } - @Nested class WithInternalDate { @@ -1000,13 +848,10 @@ void addedShouldThrowWhenInternalDateIsNull() { } } - @Nested - class DeserializationErrorOnFlags { - - @Test - void addedShouldThrowWhenFlagsIsNull() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + + @Test + void addedShouldThrowWhenFlagsIsNull() { + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( + "{" + " \"Added\": {" + " \"path\": {" + " \"namespace\": \"#private\"," + @@ -1029,215 +874,6 @@ void addedShouldThrowWhenFlagsIsNull() { " }" + "}").get()) .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenSystemFlagsContainsNullElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[null, \"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenUserFlagsContainsNullElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\", null]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenSystemFlagsContainsNumberElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[42, \"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenUserFlagsContainsNumberElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\", 42]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenSystemFlagsDoNotHaveTheRightCase() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenNoSystemFlags() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenNoUserFlags() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Added\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"added\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Draft\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } } } } 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 40a70548d17..336aafa14dd 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 @@ -20,7 +20,6 @@ package org.apache.james.event.json; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; -import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -139,157 +138,6 @@ void expungedShouldBeWellDeSerializedWhenMapKeyIsEmpty() { } } - @Nested - class WithFlags { - - @Nested - class WithEmptyFlags { - private final Flags emptyFlags = new FlagsBuilder().build(); - private final MailboxListener.Expunged emptyFlagsExpungedEvent = new MailboxListener.Expunged(SESSION_ID, USER, MAILBOX_PATH, MAILBOX_ID, - ImmutableMap.of( - MESSAGE_UID, - new MessageMetaData(MESSAGE_UID, MOD_SEQ, emptyFlags, SIZE, Date.from(INSTANT), MESSAGE_ID))); - - private final String emptyFlagsExpungedEventJson = - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[], " + - " \"userFlags\":[]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void expungedShouldBeWellSerializedWhenEmptyFlags() { - assertThatJson(EVENT_SERIALIZER.toJson(emptyFlagsExpungedEvent)) - .isEqualTo(emptyFlagsExpungedEventJson); - } - - @Test - void expungedShouldBeWellDeSerializedWhenEmptyFlags() { - assertThat(EVENT_SERIALIZER.fromJson(emptyFlagsExpungedEventJson).get()) - .isEqualTo(emptyFlagsExpungedEvent); - } - } - - @Nested - class WithOnlyUserFlags { - private final Flags onlyUserFlags = new FlagsBuilder() - .add("Custom 1", "Custom 2", "") - .build(); - private final MailboxListener.Expunged onlyUserFlagsExpungedEvent = new MailboxListener.Expunged(SESSION_ID, USER, MAILBOX_PATH, MAILBOX_ID, - ImmutableMap.of( - MESSAGE_UID, - new MessageMetaData(MESSAGE_UID, MOD_SEQ, onlyUserFlags, SIZE, Date.from(INSTANT), MESSAGE_ID))); - - private final String userOnlyFlagsExpungedEventJson = - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[], " + - " \"userFlags\":[\"Custom 1\", \"Custom 2\", \"\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void expungedShouldBeWellSerializedWhenOnlyUserFlags() { - assertThatJson(EVENT_SERIALIZER.toJson(onlyUserFlagsExpungedEvent)) - .when(IGNORING_ARRAY_ORDER) - .isEqualTo(userOnlyFlagsExpungedEventJson); - } - - @Test - void expungedShouldBeWellDeSerializedWhenOnlyUserFlags() { - assertThat(EVENT_SERIALIZER.fromJson(userOnlyFlagsExpungedEventJson).get()) - .isEqualTo(onlyUserFlagsExpungedEvent); - } - } - - @Nested - class WithOnlySystemFlags { - private final Flags onlySystemFlags = new FlagsBuilder() - .add(Flags.Flag.SEEN, Flags.Flag.ANSWERED, Flags.Flag.DELETED) - .build(); - private final MailboxListener.Expunged onlySystemFlagsExpungedEvent = new MailboxListener.Expunged(SESSION_ID, USER, MAILBOX_PATH, MAILBOX_ID, - ImmutableMap.of( - MESSAGE_UID, - new MessageMetaData(MESSAGE_UID, MOD_SEQ, onlySystemFlags, SIZE, Date.from(INSTANT), MESSAGE_ID))); - - private final String systemOnlyFlagsExpungedEventJson = - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Seen\",\"Answered\",\"Deleted\"], " + - " \"userFlags\":[]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void expungedShouldBeWellSerializedWhenOnlySystemFlags() { - assertThatJson(EVENT_SERIALIZER.toJson(onlySystemFlagsExpungedEvent)) - .when(IGNORING_ARRAY_ORDER) - .isEqualTo(systemOnlyFlagsExpungedEventJson); - } - - @Test - void expungedShouldBeWellDeSerializedWhenOnlySystemFlags() { - assertThat(EVENT_SERIALIZER.fromJson(systemOnlyFlagsExpungedEventJson).get()) - .isEqualTo(onlySystemFlagsExpungedEvent); - } - } - } - @Nested class WithInternalDate { @@ -1001,13 +849,10 @@ void expungedShouldThrowWhenInternalDateIsNull() { } } - @Nested - class DeserializationErrorOnFlags { - - @Test - void addedShouldThrowWhenFlagsIsNull() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + + @Test + void addedShouldThrowWhenFlagsIsNull() { + assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( + "{" + " \"Expunged\": {" + " \"path\": {" + " \"namespace\": \"#private\"," + @@ -1029,216 +874,7 @@ void addedShouldThrowWhenFlagsIsNull() { " \"user\": \"user\"" + " }" + "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenSystemFlagsContainsNullElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[null, \"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenUserFlagsContainsNullElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\", null]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenSystemFlagsContainsNumberElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[42, \"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenUserFlagsContainsNumberElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Draft\"], " + - " \"userFlags\":[\"User Custom Flag\", 42]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenSystemFlagsDoNotHaveTheRightCase() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"draft\"], " + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenNoSystemFlags() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"userFlags\":[\"User Custom Flag\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void addedShouldThrowWhenNoUserFlags() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"Expunged\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"expunged\": {" + - " \"123456\": {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"flags\": {" + - " \"systemFlags\":[\"Draft\"]}," + - " \"size\": 45, " + - " \"internalDate\": \"2018-12-14T09:41:51.541Z\"," + - " \"messageId\": \"42\"" + - " }" + - " }," + - " \"sessionId\": 42," + - " \"user\": \"user\"" + - " }" + - "}").get()) .isInstanceOf(NoSuchElementException.class); - } } } } 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 90c0a014339..2c9bc1ee128 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 @@ -136,16 +136,13 @@ void flagsUpdatedShouldBeWellDeSerialized() { } @Nested - class WithUpdatedFlags { + class WithEmptyUpdatedFlags { + private final List emptyUpdatedFlags = ImmutableList.of(); + private final MailboxListener.FlagsUpdated emptyUpdatedFlagsEvent = new MailboxListener.FlagsUpdated(SESSION_ID, USER, MAILBOX_PATH, + MAILBOX_ID, emptyUpdatedFlags); - @Nested - class EmptyUpdatedFlags { - private final List emptyUpdatedFlags = ImmutableList.of(); - private final MailboxListener.FlagsUpdated emptyUpdatedFlagsEvent = new MailboxListener.FlagsUpdated(SESSION_ID, USER, MAILBOX_PATH, - MAILBOX_ID, emptyUpdatedFlags); - - private static final String EVENT_JSON_WITH_EMPTY_UPDATED_FLAGS = - "{" + + private static final String EVENT_JSON_WITH_EMPTY_UPDATED_FLAGS = + "{" + " \"FlagsUpdated\": {" + " \"path\": {" + " \"namespace\": \"#private\"," + @@ -159,159 +156,17 @@ class EmptyUpdatedFlags { " }" + "}"; - @Test - void flagsUpdatedShouldBeWellSerialized() { - assertThatJson(EVENT_SERIALIZER.toJson(emptyUpdatedFlagsEvent)) - .when(Option.IGNORING_ARRAY_ORDER) - .isEqualTo(EVENT_JSON_WITH_EMPTY_UPDATED_FLAGS); - } - - @Test - void flagsUpdatedShouldBeWellDeSerialized() { - assertThat(EVENT_SERIALIZER.fromJson(EVENT_JSON_WITH_EMPTY_UPDATED_FLAGS).get()) - .isEqualTo(emptyUpdatedFlagsEvent); - } - } - - @Nested - class EmptyOldFlags { - private final UpdatedFlags emptyOldFlags = UpdatedFlags.builder() - .uid(MESSAGE_UID_1) - .modSeq(MOD_SEQ_1) - .oldFlags(FlagsBuilder.builder().build()) - .newFlags(NEW_FLAGS_1) - .build(); - private final MailboxListener.FlagsUpdated emptyOldFlagsUpdatedFlagsEvent = new MailboxListener.FlagsUpdated(SESSION_ID, USER, MAILBOX_PATH, - MAILBOX_ID, ImmutableList.of(emptyOldFlags)); - - private static final String EVENT_JSON_WITH_EMPTY_OLD_FLAGS = - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"oldFlags\": {\"systemFlags\":[],\"userFlags\":[]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void flagsUpdatedShouldBeWellSerialized() { - assertThatJson(EVENT_SERIALIZER.toJson(emptyOldFlagsUpdatedFlagsEvent)) - .when(Option.IGNORING_ARRAY_ORDER) - .isEqualTo(EVENT_JSON_WITH_EMPTY_OLD_FLAGS); - } - - @Test - void flagsUpdatedShouldBeWellDeSerialized() { - assertThat(EVENT_SERIALIZER.fromJson(EVENT_JSON_WITH_EMPTY_OLD_FLAGS).get()) - .isEqualTo(emptyOldFlagsUpdatedFlagsEvent); - } - } - - @Nested - class EmptyNewFlags { - private final UpdatedFlags emptyNewFlags = UpdatedFlags.builder() - .uid(MESSAGE_UID_1) - .modSeq(MOD_SEQ_1) - .oldFlags(OLD_FLAGS_1) - .newFlags(FlagsBuilder.builder().build()) - .build(); - private final MailboxListener.FlagsUpdated emptyNewFlagsUpdatedFlagsEvent = new MailboxListener.FlagsUpdated(SESSION_ID, USER, MAILBOX_PATH, - MAILBOX_ID, ImmutableList.of(emptyNewFlags)); - - private static final String EVENT_JSON_WITH_EMPTY_NEW_FLAGS = - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"oldFlags\": {\"systemFlags\":[\"Deleted\",\"Seen\"],\"userFlags\":[\"Old Flag 1\"]}," + - " \"newFlags\": {\"systemFlags\":[],\"userFlags\":[]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void flagsUpdatedShouldBeWellSerialized() { - assertThatJson(EVENT_SERIALIZER.toJson(emptyNewFlagsUpdatedFlagsEvent)) - .when(Option.IGNORING_ARRAY_ORDER) - .isEqualTo(EVENT_JSON_WITH_EMPTY_NEW_FLAGS); - } - - @Test - void flagsUpdatedShouldBeWellDeSerialized() { - assertThat(EVENT_SERIALIZER.fromJson(EVENT_JSON_WITH_EMPTY_NEW_FLAGS).get()) - .isEqualTo(emptyNewFlagsUpdatedFlagsEvent); - } + @Test + void flagsUpdatedShouldBeWellSerialized() { + assertThatJson(EVENT_SERIALIZER.toJson(emptyUpdatedFlagsEvent)) + .when(Option.IGNORING_ARRAY_ORDER) + .isEqualTo(EVENT_JSON_WITH_EMPTY_UPDATED_FLAGS); } - @Nested - class EmptyOldFlagsAndNewFlags { - private final UpdatedFlags emptyFlags = UpdatedFlags.builder() - .uid(MESSAGE_UID_1) - .modSeq(MOD_SEQ_1) - .oldFlags(FlagsBuilder.builder().build()) - .newFlags(FlagsBuilder.builder().build()) - .build(); - private final MailboxListener.FlagsUpdated emptyFlagsUpdatedFlagsEvent = new MailboxListener.FlagsUpdated(SESSION_ID, USER, MAILBOX_PATH, - MAILBOX_ID, ImmutableList.of(emptyFlags)); - - private static final String EVENT_JSON_WITH_EMPTY_OLD_AND_NEW_FLAGS = - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": \"#private\"," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": 35," + - " \"oldFlags\": {\"systemFlags\":[],\"userFlags\":[]}," + - " \"newFlags\": {\"systemFlags\":[],\"userFlags\":[]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}"; - - @Test - void flagsUpdatedShouldBeWellSerialized() { - assertThatJson(EVENT_SERIALIZER.toJson(emptyFlagsUpdatedFlagsEvent)) - .when(Option.IGNORING_ARRAY_ORDER) - .isEqualTo(EVENT_JSON_WITH_EMPTY_OLD_AND_NEW_FLAGS); - } - - @Test - void flagsUpdatedShouldBeWellDeSerialized() { - assertThat(EVENT_SERIALIZER.fromJson(EVENT_JSON_WITH_EMPTY_OLD_AND_NEW_FLAGS).get()) - .isEqualTo(emptyFlagsUpdatedFlagsEvent); - } + @Test + void flagsUpdatedShouldBeWellDeSerialized() { + assertThat(EVENT_SERIALIZER.fromJson(EVENT_JSON_WITH_EMPTY_UPDATED_FLAGS).get()) + .isEqualTo(emptyUpdatedFlagsEvent); } } @@ -611,7 +466,6 @@ void flagsUpdatedShouldThrowWhenMoqSeqIsNotALongNumber() { @Nested class DeserializationErrorOnOldFlags { - @Test void flagsUpdatedShouldThrowWhenOldFlagsIsNull() { assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( @@ -637,162 +491,6 @@ void flagsUpdatedShouldThrowWhenOldFlagsIsNull() { "}").get()) .isInstanceOf(NoSuchElementException.class); } - - @Test - void flagsUpdatedShouldThrowWhenSystemFlagsContainsNullElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"oldFlags\": {\"systemFlags\":[null,\"Draft\"],\"userFlags\":[\"New Flag 1\"]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenUserFlagsContainsNullElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"oldFlags\": {\"systemFlags\":[\"Draft\"],\"userFlags\":[\"New Flag 1\", null]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenSystemFlagsDoesNotContainString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"oldFlags\": {\"systemFlags\":[42,\"Draft\"],\"userFlags\":[\"New Flag 1\"]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenSystemFlagsContainsUnknownString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"oldFlags\": {\"systemFlags\":[\"unKnown\"],\"userFlags\":[\"New Flag 1\"]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenSystemFlagsContainsBadCaseString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"oldFlags\": {\"systemFlags\":[\"dRaFt\"],\"userFlags\":[\"New Flag 1\"]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenUserFlagsDoesNotContainString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"oldFlags\": {\"systemFlags\":[\"Draft\"],\"userFlags\":[\"New Flag 1\", 42]}," + - " \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } } @Nested @@ -822,162 +520,6 @@ void flagsUpdatedShouldThrowWhenNewFlagsIsNull() { "}").get()) .isInstanceOf(NoSuchElementException.class); } - - @Test - void flagsUpdatedShouldThrowWhenSystemFlagsContainsNullElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"newFlags\": {\"systemFlags\":[null,\"Draft\"],\"userFlags\":[\"New Flag 1\"]}," + - " \"oldFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenUserFlagsContainsNullElements() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"newFlags\": {\"systemFlags\":[\"Draft\"],\"userFlags\":[\"New Flag 1\", null]}," + - " \"oldFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenSystemFlagsDoesNotContainString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"newFlags\": {\"systemFlags\":[42,\"Draft\"],\"userFlags\":[\"New Flag 1\"]}," + - " \"oldFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenSystemFlagsContainsUnknownString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"newFlags\": {\"systemFlags\":[\"unKnown\"],\"userFlags\":[\"New Flag 1\"]}," + - " \"oldFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenSystemFlagsContainsBadCaseString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"newFlags\": {\"systemFlags\":[\"dRaFt\"],\"userFlags\":[\"New Flag 1\"]}," + - " \"oldFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } - - @Test - void flagsUpdatedShouldThrowWhenUserFlagsDoesNotContainString() { - assertThatThrownBy(() -> EVENT_SERIALIZER.fromJson( - "{" + - " \"FlagsUpdated\": {" + - " \"path\": {" + - " \"namespace\": 482," + - " \"user\": \"user\"," + - " \"name\": \"mailboxName\"" + - " }," + - " \"mailboxId\": \"18\"," + - " \"sessionId\": 42," + - " \"updatedFlags\": [" + - " {" + - " \"uid\": 123456," + - " \"modSeq\": \"35\"," + - " \"newFlags\": {\"systemFlags\":[\"Draft\"],\"userFlags\":[\"New Flag 1\", 42]}," + - " \"oldFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" + - " }" + - " ]," + - " \"user\": \"user\"" + - " }" + - "}").get()) - .isInstanceOf(NoSuchElementException.class); - } } } } diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/FlagsTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/FlagsTest.java new file mode 100644 index 00000000000..a41c245324d --- /dev/null +++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/FlagsTest.java @@ -0,0 +1,240 @@ +/**************************************************************** + * 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 net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.NoSuchElementException; + +import javax.mail.Flags; + +import org.apache.james.event.json.DTOs; +import org.apache.james.event.json.JsonSerialize; +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.Json; + +class FlagsTest { + private static final JsonSerialize JSON_SERIALIZE = new JsonSerialize(new TestId.Factory(), new TestMessageId.Factory()); + + @Test + void emptyFlagsShouldBeWellSerialized() { + assertThatJson(JSON_SERIALIZE.flagWrites().writes(DTOs.Flags$.MODULE$.fromJavaFlags( + new Flags())) + .toString()) + .isEqualTo("{\"systemFlags\":[],\"userFlags\":[]}"); + } + + @Test + void answeredShouldBeWellSerialized() { + assertThatJson(JSON_SERIALIZE.flagWrites().writes(DTOs.Flags$.MODULE$.fromJavaFlags( + new Flags(Flags.Flag.ANSWERED))) + .toString()) + .isEqualTo("{\"systemFlags\":[\"Answered\"],\"userFlags\":[]}"); + } + + @Test + void deletedShouldBeWellSerialized() { + assertThatJson(JSON_SERIALIZE.flagWrites().writes(DTOs.Flags$.MODULE$.fromJavaFlags( + new Flags(Flags.Flag.DELETED))) + .toString()) + .isEqualTo("{\"systemFlags\":[\"Deleted\"],\"userFlags\":[]}"); + } + + @Test + void draftShouldBeWellSerialized() { + assertThatJson(JSON_SERIALIZE.flagWrites().writes(DTOs.Flags$.MODULE$.fromJavaFlags( + new Flags(Flags.Flag.DRAFT))) + .toString()) + .isEqualTo("{\"systemFlags\":[\"Draft\"],\"userFlags\":[]}"); + } + + @Test + void flaggedShouldBeWellSerialized() { + assertThatJson(JSON_SERIALIZE.flagWrites().writes(DTOs.Flags$.MODULE$.fromJavaFlags( + new Flags(Flags.Flag.FLAGGED))) + .toString()) + .isEqualTo("{\"systemFlags\":[\"Flagged\"],\"userFlags\":[]}"); + } + + @Test + void recentShouldBeWellSerialized() { + assertThatJson(JSON_SERIALIZE.flagWrites().writes(DTOs.Flags$.MODULE$.fromJavaFlags( + new Flags(Flags.Flag.RECENT))) + .toString()) + .isEqualTo("{\"systemFlags\":[\"Recent\"],\"userFlags\":[]}"); + } + + @Test + void seenShouldBeWellSerialized() { + assertThatJson(JSON_SERIALIZE.flagWrites().writes(DTOs.Flags$.MODULE$.fromJavaFlags( + new Flags(Flags.Flag.SEEN))) + .toString()) + .isEqualTo("{\"systemFlags\":[\"Seen\"],\"userFlags\":[]}"); + } + + @Test + void userFlagShouldBeWellSerialized() { + assertThatJson(JSON_SERIALIZE.flagWrites().writes(DTOs.Flags$.MODULE$.fromJavaFlags( + new Flags("user flag"))) + .toString()) + .isEqualTo("{\"systemFlags\":[],\"userFlags\":[\"user flag\"]}"); + } + + @Test + void emptyFlagsShouldBeWellDeSerialized() { + assertThat(DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[],\"userFlags\":[]}")) + .get())) + .isEqualTo(new Flags()); + } + + @Test + void answeredShouldBeWellDeSerialized() { + assertThat(DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[\"Answered\"],\"userFlags\":[]}")) + .get())) + .isEqualTo(new Flags(Flags.Flag.ANSWERED)); + } + + @Test + void deletedShouldBeWellDeSerialized() { + assertThat(DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[\"Deleted\"],\"userFlags\":[]}")) + .get())) + .isEqualTo(new Flags(Flags.Flag.DELETED)); + } + + @Test + void draftShouldBeWellDeSerialized() { + assertThat(DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[\"Draft\"],\"userFlags\":[]}")) + .get())) + .isEqualTo(new Flags(Flags.Flag.DRAFT)); + } + + @Test + void flaggedShouldBeWellDeSerialized() { + assertThat(DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[\"Flagged\"],\"userFlags\":[]}")) + .get())) + .isEqualTo(new Flags(Flags.Flag.FLAGGED)); + } + + @Test + void recentShouldBeWellDeSerialized() { + assertThat(DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[\"Recent\"],\"userFlags\":[]}")) + .get())) + .isEqualTo(new Flags(Flags.Flag.RECENT)); + } + + @Test + void seenShouldBeWellDeSerialized() { + assertThat(DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[\"Seen\"],\"userFlags\":[]}")) + .get())) + .isEqualTo(new Flags(Flags.Flag.SEEN)); + } + + @Test + void userFlagShouldBeWellDeSerialized() { + assertThat(DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[],\"userFlags\":[\"user flag\"]}")) + .get())) + .isEqualTo(new Flags("user flag")); + } + + @Test + void deserializeShouldThrowWhenUnknownSystemFlag() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[\"unknown\"],\"userFlags\":[]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void deserializeShouldThrowWhenBadCaseSystemFlag() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[\"AnSwErEd\"],\"userFlags\":[]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void deserializeShouldThrowWhenNullSystemFlag() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":null,\"userFlags\":[]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void deserializeShouldThrowWhenSystemFlagContainsNullElements() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[null,\"Draft\"],\"userFlags\":[]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void deserializeShouldThrowWhenSystemFlagContainsNotStringElements() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[42,\"Draft\"],\"userFlags\":[]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void deserializeShouldThrowWhenUserFlagsContainsNullElements() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[],\"userFlags\":[null, \"a\"]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void deserializeShouldThrowWhenUserFlagsContainsNonStringElements() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[],\"userFlags\":[42, \"a\"]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void deserializeShouldThrowWhenNoUserFlags() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"systemFlags\":[]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } + + @Test + void deserializeShouldThrowWhenNoSystemFlags() { + assertThatThrownBy(() -> DTOs.Flags$.MODULE$.toJavaFlags( + JSON_SERIALIZE.flagsReads().reads(Json.parse("{\"userFlags\":[]}")) + .get())) + .isInstanceOf(NoSuchElementException.class); + } +}