JAMES-1676 add properties to SetError ...according to jmap specification. from: http://jmap.io/spec.html#updating-messages <<The Error object SHOULD contain a property called properties of type String[] that lists all the properties that were invalid.>>
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c4ee5576 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c4ee5576 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c4ee5576 Branch: refs/heads/master Commit: c4ee5576c507efef17baa30595f96d319f12d796 Parents: 946586f Author: Fabien Vignon <fvig...@linagora.com> Authored: Wed Feb 3 14:46:58 2016 +0100 Committer: Fabien Vignon <fvig...@linagora.com> Committed: Wed Feb 10 17:29:50 2016 +0100 ---------------------------------------------------------------------- .../jmap/methods/SetMessagesMethodTest.java | 9 ++- .../org/apache/james/jmap/model/SetError.java | 28 ++++++++- .../apache/james/jmap/model/SetErrorTest.java | 60 ++++++++++++++++++-- 3 files changed, 87 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/c4ee5576/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/SetMessagesMethodTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/SetMessagesMethodTest.java b/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/SetMessagesMethodTest.java index 7dbb2f6..8941367 100644 --- a/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/SetMessagesMethodTest.java +++ b/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/SetMessagesMethodTest.java @@ -27,6 +27,7 @@ import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.collection.IsMapWithSize.aMapWithSize; import static org.hamcrest.collection.IsMapWithSize.anEmptyMap; @@ -130,12 +131,14 @@ public abstract class SetMessagesMethodTest { .when() .post("/jmap") .then() + .log().ifValidationFails() .statusCode(200) .body(NAME, equalTo("messagesSet")) .body(ARGUMENTS + ".destroyed", empty()) .body(ARGUMENTS + ".notDestroyed", hasEntry(equalTo(unknownMailboxMessageId), Matchers.allOf( hasEntry("type", "anErrorOccurred"), - hasEntry("description", "An error occurred while deleting message " + unknownMailboxMessageId))) + hasEntry("description", "An error occurred while deleting message " + unknownMailboxMessageId), + hasEntry(equalTo("properties"), isEmptyOrNullString()))) ); } @@ -152,12 +155,14 @@ public abstract class SetMessagesMethodTest { .when() .post("/jmap") .then() + .log().ifValidationFails() .statusCode(200) .body(NAME, equalTo("messagesSet")) .body(ARGUMENTS + ".destroyed", empty()) .body(ARGUMENTS + ".notDestroyed", hasEntry(equalTo(messageId), Matchers.allOf( hasEntry("type", "notFound"), - hasEntry("description", "The message " + messageId + " can't be found"))) + hasEntry("description", "The message " + messageId + " can't be found"), + hasEntry(equalTo("properties"), isEmptyOrNullString()))) ); } http://git-wip-us.apache.org/repos/asf/james-project/blob/c4ee5576/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetError.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetError.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetError.java index da94af5..f52ad15 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetError.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/SetError.java @@ -19,14 +19,21 @@ package org.apache.james.jmap.model; +import static org.apache.james.jmap.model.MessageProperties.MessageProperty; + import java.util.Optional; +import java.util.Set; +import java.util.stream.Stream; +import org.apache.james.util.streams.Collectors; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Strings; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; @JsonDeserialize(builder = SetError.Builder.class) public class SetError { @@ -40,6 +47,7 @@ public class SetError { private String type; private String description; + private Optional<ImmutableSet<MessageProperty>> properties = Optional.empty(); private Builder() { } @@ -54,18 +62,29 @@ public class SetError { return this; } + public Builder properties(Set<MessageProperty> properties) { + this.properties = Optional.of(Sets.union( + this.properties.orElse(ImmutableSet.of()), + Optional.ofNullable(properties).orElse(ImmutableSet.of())) + .immutableCopy()); + return this; + } + public SetError build() { Preconditions.checkState(!Strings.isNullOrEmpty(type), "'type' is mandatory"); - return new SetError(type, Optional.ofNullable(description)); + return new SetError(type, Optional.ofNullable(description), properties); } } private final String type; private final Optional<String> description; + private final Optional<ImmutableSet<MessageProperty>> properties; - @VisibleForTesting SetError(String type, Optional<String> description) { + + @VisibleForTesting SetError(String type, Optional<String> description, Optional<ImmutableSet<MessageProperty>> properties) { this.type = type; this.description = description; + this.properties = properties; } @JsonSerialize @@ -77,4 +96,9 @@ public class SetError { public Optional<String> getDescription() { return description; } + + @JsonSerialize + public Optional<ImmutableSet<MessageProperty>> getProperties() { + return properties; + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/c4ee5576/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/SetErrorTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/SetErrorTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/SetErrorTest.java index 1d5ace1..db11c27 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/SetErrorTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/SetErrorTest.java @@ -21,10 +21,12 @@ package org.apache.james.jmap.model; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.apache.james.jmap.model.MessageProperties.MessageProperty; import java.util.Optional; import org.junit.Test; +import com.google.common.collect.ImmutableSet; public class SetErrorTest { @@ -36,7 +38,7 @@ public class SetErrorTest { @Test public void buildShouldWorkWhenAllMandatoryFieldsAreGiven() { - SetError expected = new SetError("type", Optional.empty()); + SetError expected = new SetError("type", Optional.empty(), Optional.empty()); SetError setError = SetError.builder() .type("type") @@ -47,13 +49,59 @@ public class SetErrorTest { @Test public void buildShouldWorkWhenAllFieldsAreGiven() { - SetError expected = new SetError("type", Optional.of("description")); + ImmutableSet<MessageProperty> props = ImmutableSet.of(MessageProperty.attachedMessages); + + SetError expected = new SetError("type", Optional.of("description"), Optional.of(props)); SetError setError = SetError.builder() - .type("type") - .description("description") - .build(); + .type("type") + .description("description") + .properties(ImmutableSet.of(MessageProperty.attachedMessages)) + .build(); assertThat(setError).isEqualToComparingFieldByField(expected); } -} + + @Test + public void buildShouldMergePassedProperties() { + SetError result = SetError.builder() + .type("a type").description("a description") + .properties(ImmutableSet.of(MessageProperty.bcc)) + .properties(ImmutableSet.of(MessageProperty.cc)) + .build(); + + assertThat(result.getProperties()).contains(ImmutableSet.of(MessageProperty.bcc, MessageProperty.cc)); + } + + @Test + public void buildShouldDefaultToEmptyWhenPropertiesOmitted() { + SetError result = SetError.builder() + .type("a type").description("a description") + .build(); + + assertThat(result.getProperties()).isEmpty(); + } + + @Test + public void buildShouldDefaultToEmptyWhenPropertiesNull() { + SetError result = SetError.builder() + .type("a type").description("a description").properties(null) + .build(); + + assertThat(result.getProperties()).isPresent(); + assertThat(result.getProperties().get()).isEmpty(); + } + + @Test + public void buildShouldBeIdempotentWhenNullPropertiesSet() { + ImmutableSet<MessageProperty> nonNullProperty = ImmutableSet.of(MessageProperty.from); + SetError result = SetError.builder() + .type("a type").description("a description") + .properties(nonNullProperty) + .properties(null) + .build(); + + assertThat(result.getProperties()).isPresent(); + assertThat(result.getProperties().get()).isEqualTo(nonNullProperty); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org