JAMES-2346 JSON de-serialization for MDN disposition types
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/857074e4 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/857074e4 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/857074e4 Branch: refs/heads/master Commit: 857074e42956c7075eb047beede625964243d425 Parents: 0f11476 Author: benwa <btell...@linagora.com> Authored: Thu Mar 8 10:55:24 2018 +0700 Committer: benwa <btell...@linagora.com> Committed: Tue Mar 13 15:11:54 2018 +0700 ---------------------------------------------------------------------- server/protocols/jmap/pom.xml | 4 + .../james/jmap/json/ObjectMapperFactory.java | 63 +++++++++- .../jmap/json/ObjectMapperFactoryTest.java | 116 ++++++++++++++++++- 3 files changed, 176 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/857074e4/server/protocols/jmap/pom.xml ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/pom.xml b/server/protocols/jmap/pom.xml index 546afd7..e58ce38 100644 --- a/server/protocols/jmap/pom.xml +++ b/server/protocols/jmap/pom.xml @@ -82,6 +82,10 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>james-mdn</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>james-server-core</artifactId> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/857074e4/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/ObjectMapperFactory.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/ObjectMapperFactory.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/ObjectMapperFactory.java index 36c76fa..42fbc0b 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/ObjectMapperFactory.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/ObjectMapperFactory.java @@ -20,6 +20,7 @@ package org.apache.james.jmap.json; import java.io.IOException; +import java.util.Arrays; import java.util.Set; import javax.inject.Inject; @@ -28,6 +29,9 @@ import org.apache.james.jmap.model.mailbox.Rights; import org.apache.james.mailbox.Role; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MessageId; +import org.apache.james.mdn.action.mode.DispositionActionMode; +import org.apache.james.mdn.sending.mode.DispositionSendingMode; +import org.apache.james.mdn.type.DispositionType; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; @@ -45,7 +49,9 @@ import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.guava.GuavaModule; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.github.steveash.guavate.Guavate; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; public class ObjectMapperFactory { @@ -72,9 +78,16 @@ public class ObjectMapperFactory { mailboxIdModule.addKeyDeserializer(Rights.Username.class, new UsernameKeyDeserializer()); mailboxIdModule.addDeserializer(Rights.Right.class, new RightDeserializer()); + SimpleModule mdnModule = new SimpleModule(); + mailboxIdModule.addDeserializer(DispositionActionMode.class, new MDNActionModeDeserializer()); + mailboxIdModule.addDeserializer(DispositionSendingMode.class, new MDNSendingModeDeserializer()); + mailboxIdModule.addDeserializer(DispositionType.class, new MDNTypeDeserializer()); + mailboxIdModule.setMixInAnnotation(Role.class, RoleMixIn.class); - jacksonModules = JACKSON_BASE_MODULES.add(mailboxIdModule).build(); + jacksonModules = JACKSON_BASE_MODULES.add(mailboxIdModule) + .add(mdnModule) + .build(); } public ObjectMapper forParsing() { @@ -89,6 +102,48 @@ public class ObjectMapperFactory { .registerModules(jacksonModules); } + public static class MDNActionModeDeserializer extends JsonDeserializer<DispositionActionMode> { + private static final ImmutableList<String> ALLOWED_VALUES = Arrays.stream(DispositionActionMode.values()) + .map(DispositionActionMode::getValue) + .collect(Guavate.toImmutableList()); + + @Override + public DispositionActionMode deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + String value = jsonParser.getValueAsString(); + return DispositionActionMode.fromString(value) + .orElseThrow(() -> new IllegalStateException( + String.format("Unrecognized MDN Disposition action mode %s. Should be one of %s", value, ALLOWED_VALUES))); + } + } + + public static class MDNSendingModeDeserializer extends JsonDeserializer<DispositionSendingMode> { + private static final ImmutableList<String> ALLOWED_VALUES = Arrays.stream(DispositionSendingMode.values()) + .map(DispositionSendingMode::getValue) + .collect(Guavate.toImmutableList()); + + @Override + public DispositionSendingMode deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + String value = jsonParser.getValueAsString(); + return DispositionSendingMode.fromString(value) + .orElseThrow(() -> new IllegalStateException( + String.format("Unrecognized MDN Disposition sending mode %s. Should be one of %s", value, ALLOWED_VALUES))); + } + } + + public static class MDNTypeDeserializer extends JsonDeserializer<DispositionType> { + private static final ImmutableList<String> ALLOWED_VALUES = Arrays.stream(DispositionType.values()) + .map(DispositionType::getValue) + .collect(Guavate.toImmutableList()); + + @Override + public DispositionType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + String value = jsonParser.getValueAsString(); + return DispositionType.fromString(value) + .orElseThrow(() -> new IllegalStateException( + String.format("Unrecognized MDN Disposition type %s. Should be one of %s", value, ALLOWED_VALUES))); + } + } + public static class MailboxIdDeserializer extends JsonDeserializer<MailboxId> { private MailboxId.Factory factory; @@ -112,7 +167,7 @@ public class ObjectMapperFactory { public static class UsernameKeyDeserializer extends KeyDeserializer { @Override - public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException { + public Object deserializeKey(String key, DeserializationContext ctxt) { return new Rights.Username(key); } } @@ -136,7 +191,7 @@ public class ObjectMapperFactory { } @Override - public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException { + public Object deserializeKey(String key, DeserializationContext ctxt) { return factory.fromString(key); } } @@ -170,7 +225,7 @@ public class ObjectMapperFactory { } @Override - public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException { + public Object deserializeKey(String key, DeserializationContext ctxt) { return factory.fromString(key); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/857074e4/server/protocols/jmap/src/test/java/org/apache/james/jmap/json/ObjectMapperFactoryTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/json/ObjectMapperFactoryTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/json/ObjectMapperFactoryTest.java index 8a181d7..f71d38d 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/json/ObjectMapperFactoryTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/json/ObjectMapperFactoryTest.java @@ -28,6 +28,9 @@ import org.apache.james.jmap.model.mailbox.Rights; import org.apache.james.mailbox.inmemory.InMemoryId; import org.apache.james.mailbox.inmemory.InMemoryMessageId; import org.apache.james.mailbox.model.MailboxId; +import org.apache.james.mdn.action.mode.DispositionActionMode; +import org.apache.james.mdn.sending.mode.DispositionSendingMode; +import org.apache.james.mdn.type.DispositionType; import org.junit.Before; import org.junit.Test; @@ -95,6 +98,113 @@ public class ObjectMapperFactoryTest { } @Test + public void readValueShouldParseActionModeWhenAutomatic() throws Exception { + DispositionActionMode actual = testee.forParsing() + .readValue("\"" + DispositionActionMode.Automatic.getValue() + "\"", + DispositionActionMode.class); + + assertThat(actual) + .isEqualTo(DispositionActionMode.Automatic); + } + + @Test + public void readValueShouldParseActionModeWhenManual() throws Exception { + DispositionActionMode actual = testee.forParsing() + .readValue("\"" + DispositionActionMode.Manual.getValue() + "\"", + DispositionActionMode.class); + + assertThat(actual) + .isEqualTo(DispositionActionMode.Manual); + } + + @Test + public void readValueShouldFailOnInvalidActionMode() { + assertThatThrownBy(() -> testee.forParsing() + .readValue("\"illegal\"", + DispositionActionMode.class)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Unrecognized MDN Disposition action mode illegal. Should be one of [manual-action, automatic-action]"); + } + + @Test + public void readValueShouldParseSendingModeWhenAutomatic() throws Exception { + DispositionSendingMode actual = testee.forParsing() + .readValue("\"" + DispositionSendingMode.Automatic.getValue() + "\"", + DispositionSendingMode.class); + + assertThat(actual) + .isEqualTo(DispositionSendingMode.Automatic); + } + + @Test + public void readValueShouldParseSendingModeWhenManual() throws Exception { + DispositionSendingMode actual = testee.forParsing() + .readValue("\"" + DispositionSendingMode.Manual.getValue() + "\"", + DispositionSendingMode.class); + + assertThat(actual) + .isEqualTo(DispositionSendingMode.Manual); + } + + @Test + public void readValueShouldFailOnInvalidSendingMode() { + assertThatThrownBy(() -> testee.forParsing() + .readValue("\"illegal\"", + DispositionSendingMode.class)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Unrecognized MDN Disposition sending mode illegal. Should be one of [MDN-sent-manually, MDN-sent-automatically]"); + } + + @Test + public void readValueShouldParseSendingModeWhenDeleted() throws Exception { + DispositionType actual = testee.forParsing() + .readValue("\"" + DispositionType.Deleted.getValue() + "\"", + DispositionType.class); + + assertThat(actual) + .isEqualTo(DispositionType.Deleted); + } + + @Test + public void readValueShouldParseSendingModeWhenDispatched() throws Exception { + DispositionType actual = testee.forParsing() + .readValue("\"" + DispositionType.Dispatched.getValue() + "\"", + DispositionType.class); + + assertThat(actual) + .isEqualTo(DispositionType.Dispatched); + } + + @Test + public void readValueShouldParseSendingModeWhenDisplayed() throws Exception { + DispositionType actual = testee.forParsing() + .readValue("\"" + DispositionType.Displayed.getValue() + "\"", + DispositionType.class); + + assertThat(actual) + .isEqualTo(DispositionType.Displayed); + } + + @Test + public void readValueShouldParseSendingModeWhenProcessed() throws Exception { + DispositionType actual = testee.forParsing() + .readValue("\"" + DispositionType.Processed.getValue() + "\"", + DispositionType.class); + + assertThat(actual) + .isEqualTo(DispositionType.Processed); + } + + @Test + public void readValueShouldFailOnInvalidDispositionType() { + assertThatThrownBy(() -> testee.forParsing() + .readValue("\"illegal\"", + DispositionType.class)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Unrecognized MDN Disposition type illegal. Should be one of [deleted, dispatched, displayed, processed]"); + } + + @Test public void readValueShouldParseRightsObject() throws Exception { String username = "username"; Rights actual = testee.forParsing() @@ -107,7 +217,7 @@ public class ObjectMapperFactoryTest { } @Test - public void readValueShouldRejectMultiCharacterRights() throws Exception { + public void readValueShouldRejectMultiCharacterRights() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"ae\"", Rights.Right.class)) @@ -115,7 +225,7 @@ public class ObjectMapperFactoryTest { } @Test - public void readValueShouldRejectUnsupportedRights() throws Exception { + public void readValueShouldRejectUnsupportedRights() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"p\"", Rights.Right.class)) @@ -123,7 +233,7 @@ public class ObjectMapperFactoryTest { } @Test - public void readValueShouldRejectUnExistingRights() throws Exception { + public void readValueShouldRejectUnExistingRights() { assertThatThrownBy(() -> testee.forParsing() .readValue("\"z\"", Rights.Right.class)) --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org