JAMES-1717 Jackson should correctly handle named timezone dates
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1f3d5c8f Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1f3d5c8f Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1f3d5c8f Branch: refs/heads/master Commit: 1f3d5c8f2128f50e698a6d5ddb2cb20e69dfb187 Parents: d3b330f Author: Benoit Tellier <[email protected]> Authored: Wed Apr 20 09:26:31 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Fri Apr 22 15:29:29 2016 +0700 ---------------------------------------------------------------------- .../integration/GetVacationResponseTest.java | 4 +- .../integration/SetVacationResponseTest.java | 37 +++++++++++++++++++ .../json/OptionalZonedDateTimeDeserializer.java | 37 +++++++++++++++++++ .../json/OptionalZonedDateTimeSerializer.java | 39 ++++++++++++++++++++ .../james/jmap/model/VacationResponse.java | 7 ++++ 5 files changed, 122 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/1f3d5c8f/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java index b0a5f97..de29aed 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/GetVacationResponseTest.java @@ -140,7 +140,7 @@ public abstract class GetVacationResponseTest { Vacation.builder() .enabled(true) .fromDate(Optional.of(ZonedDateTime.parse("2014-09-30T14:10:00+02:00"))) - .toDate(Optional.of(ZonedDateTime.parse("2016-04-15T11:56:32.224+07:00"))) + .toDate(Optional.of(ZonedDateTime.parse("2016-04-15T11:56:32.224+07:00[Asia/Vientiane]"))) .textBody("Test explaining my vacations") .build()); @@ -162,7 +162,7 @@ public abstract class GetVacationResponseTest { .body(ARGUMENTS + ".list", hasSize(1)) .body(ARGUMENTS + ".list[0].id", equalTo("singleton")) .body(ARGUMENTS + ".list[0].fromDate", equalTo("2014-09-30T14:10:00+02:00")) - .body(ARGUMENTS + ".list[0].toDate", equalTo("2016-04-15T11:56:32.224+07:00")) + .body(ARGUMENTS + ".list[0].toDate", equalTo("2016-04-15T11:56:32.224+07:00[Asia/Vientiane]")) .body(ARGUMENTS + ".list[0].isEnabled", equalTo(true)) .body(ARGUMENTS + ".list[0].textBody", equalTo("Test explaining my vacations")); } http://git-wip-us.apache.org/repos/asf/james-project/blob/1f3d5c8f/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java index 0b7e935..6ad7afb 100644 --- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java +++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetVacationResponseTest.java @@ -173,6 +173,43 @@ public abstract class SetVacationResponseTest { } @Test + public void setVacationResponseShouldHandleNamedTimeZone() { + String bodyRequest = "[[" + + "\"setVacationResponse\", " + + "{" + + "\"update\":{" + + "\"singleton\" : {" + + "\"id\": \"singleton\"," + + "\"isEnabled\": \"true\"," + + "\"textBody\": \"Message explaining my wonderful vacations\"," + + "\"fromDate\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\"," + + "\"toDate\":\"2016-04-07T02:01+07:00[Asia/Vientiane]\"" + + "}" + + "}" + + "}, " + + "\"#0\"" + + "]]"; + + given() + .accept(ContentType.JSON) + .contentType(ContentType.JSON) + .header("Authorization", accessToken.serialize()) + .body(bodyRequest) + .when() + .post("/jmap") + .then() + .statusCode(200) + .body(NAME, equalTo("vacationResponseSet")) + .body(ARGUMENTS + ".updated[0]", equalTo("singleton")); + + Vacation vacation = jmapServer.serverProbe().retrieveVacation(AccountId.fromString(USER)); + assertThat(vacation.getTextBody()).isEqualTo("Message explaining my wonderful vacations"); + assertThat(vacation.isEnabled()).isTrue(); + assertThat(vacation.getFromDate()).contains(ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]")); + assertThat(vacation.getToDate()).contains(ZonedDateTime.parse("2016-04-07T02:01+07:00[Asia/Vientiane]")); + } + + @Test public void nullTextBodyShouldBeRejected() { String bodyRequest = "[[" + "\"setVacationResponse\", " + http://git-wip-us.apache.org/repos/asf/james-project/blob/1f3d5c8f/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/OptionalZonedDateTimeDeserializer.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/OptionalZonedDateTimeDeserializer.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/OptionalZonedDateTimeDeserializer.java new file mode 100644 index 0000000..f4e2b46 --- /dev/null +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/OptionalZonedDateTimeDeserializer.java @@ -0,0 +1,37 @@ +/**************************************************************** + * 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.jmap.json; + +import java.io.IOException; +import java.time.ZonedDateTime; +import java.util.Optional; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +public class OptionalZonedDateTimeDeserializer extends JsonDeserializer<Optional<ZonedDateTime>> { + + @Override + public Optional<ZonedDateTime> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { + return Optional.ofNullable(jsonParser.getValueAsString()) + .map(ZonedDateTime::parse); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/1f3d5c8f/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/OptionalZonedDateTimeSerializer.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/OptionalZonedDateTimeSerializer.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/OptionalZonedDateTimeSerializer.java new file mode 100644 index 0000000..9f37649 --- /dev/null +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/json/OptionalZonedDateTimeSerializer.java @@ -0,0 +1,39 @@ +/**************************************************************** + * 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.jmap.json; + +import java.io.IOException; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Optional; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +public class OptionalZonedDateTimeSerializer extends JsonSerializer<Optional<ZonedDateTime>> { + + @Override + public void serialize(Optional<ZonedDateTime> optionalZonedDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + jsonGenerator.writeString( + optionalZonedDateTime.map(zonedDateTime -> zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME)) + .orElse(null)); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/1f3d5c8f/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java index 593b29a..6d88e0c 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/VacationResponse.java @@ -24,11 +24,14 @@ import java.util.Objects; import java.util.Optional; import org.apache.james.jmap.api.vacation.Vacation; +import org.apache.james.jmap.json.OptionalZonedDateTimeDeserializer; +import org.apache.james.jmap.json.OptionalZonedDateTimeSerializer; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; 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.base.Preconditions; @JsonDeserialize(builder = VacationResponse.Builder.class) @@ -57,11 +60,13 @@ public class VacationResponse { return this; } + @JsonDeserialize(using = OptionalZonedDateTimeDeserializer.class) public Builder fromDate(Optional<ZonedDateTime> fromDate) { this.fromDate = fromDate; return this; } + @JsonDeserialize(using = OptionalZonedDateTimeDeserializer.class) public Builder toDate(Optional<ZonedDateTime> toDate) { this.toDate = toDate; return this; @@ -110,10 +115,12 @@ public class VacationResponse { return isEnabled; } + @JsonSerialize(using = OptionalZonedDateTimeSerializer.class) public Optional<ZonedDateTime> getFromDate() { return fromDate; } + @JsonSerialize(using = OptionalZonedDateTimeSerializer.class) public Optional<ZonedDateTime> getToDate() { return toDate; } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
