1 package com.fasterxml.jackson.datatype.threetenbp;
2
3 import com.fasterxml.jackson.annotation.JsonFormat;
4 import com.fasterxml.jackson.databind.DeserializationFeature;
5 import com.fasterxml.jackson.databind.JsonMappingException;
6 import com.fasterxml.jackson.databind.ObjectReader;
7
8 import org.junit.Test;
9
10 import java.io.IOException;
11 import org.threeten.bp.OffsetDateTime;
12 import org.threeten.bp.ZoneOffset;
13 import org.threeten.bp.format.DateTimeParseException;
14 import java.util.TimeZone;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.fail;
19
20
21 public class TestOffsetDateTimeDeserialization extends ModuleTestBase
22 {
23 private final ObjectReader READER = newMapper().readerFor(OffsetDateTime.class);
24
25 @Test
26 public void testDeserializationAsString01() throws Exception
27 {
28 expectSuccess(OffsetDateTime.of(2000, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC), "'2000-01-01T12:00Z'");
29 }
30
31 @Test
32 public void testDeserializationAsString02() throws Exception
33 {
34 expectSuccess(OffsetDateTime.of(2000, 1, 1, 7, 0, 0, 0, ZoneOffset.UTC), "'2000-01-01T12:00+05:00'");
35 }
36
37 @Test
38 public void testDeserializationAsString03() throws Exception
39 {
40
41
42
43 ObjectReader reader2 = newMapper().disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).readerFor(OffsetDateTime.class);
44 OffsetDateTime parsed = reader2.readValue(aposToQuotes("'2000-01-01T12:00+05:00'"));
45 notNull(parsed);
46 expect(OffsetDateTime.of(2000, 1, 1, 12, 0, 0, 0, ZoneOffset.ofHours(5)), parsed) ;
47 }
48
49 public static class WithContextTimezoneDateFieldBean {
50 @JsonFormat(shape = JsonFormat.Shape.STRING,
51 pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX", with = JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
52 public OffsetDateTime date;
53 }
54
55 @Test
56 public void testDeserializationWithContextTimezoneFeatureOverride() throws Exception
57 {
58 String inputStr = "{\"date\":\"2016-05-13T17:24:40.545+03\"}";
59 WithContextTimezoneDateFieldBean result = newMapper().setTimeZone(TimeZone.getTimeZone("UTC")).
60 disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).readValue(inputStr, WithContextTimezoneDateFieldBean.class);
61 notNull(result);
62 expect(OffsetDateTime.of(2016, 5, 13, 14, 24, 40, 545000000, ZoneOffset.UTC), result.date);
63 }
64
65 public static class WithoutContextTimezoneDateFieldBean {
66 @JsonFormat(shape = JsonFormat.Shape.STRING,
67 pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX", without = JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
68 public OffsetDateTime date;
69 }
70
71 @Test
72 public void testDeserializationWithoutContextTimezoneFeatureOverride() throws Exception
73 {
74 String inputStr = "{\"date\":\"2016-05-13T17:24:40.545+03\"}";
75 WithoutContextTimezoneDateFieldBean result = newMapper().setTimeZone(TimeZone.getTimeZone("UTC")).
76 enable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).readValue(inputStr, WithoutContextTimezoneDateFieldBean.class);
77 notNull(result);
78 expect(OffsetDateTime.of(2016, 5, 13, 17, 24, 40, 545000000, ZoneOffset.ofHours(3)), result.date);
79 }
80
81 @Test
82 public void testBadDeserializationAsString01() throws Throwable
83 {
84 expectFailure("'notanoffsetdatetime'");
85 }
86
87 private void expectFailure(String json) throws Exception {
88 try {
89 read(json);
90 fail("expected JsonMappingException");
91 } catch (JsonMappingException e) {
92 Throwable t = e.getCause();
93 if (t == null) {
94 fail("Should have `cause` for exception: "+e);
95 }
96 if (!(t instanceof DateTimeParseException)) {
97 fail("Should have DateTimeParseException as root cause, had: "+t);
98 }
99 }
100 }
101
102 private void expectSuccess(Object exp, String json) throws IOException {
103 final OffsetDateTime value = read(json);
104 notNull(value);
105 expect(exp, value);
106 }
107
108 private OffsetDateTime read(final String json) throws IOException {
109 return READER.readValue(aposToQuotes(json));
110 }
111
112 private static void notNull(Object value) {
113 assertNotNull("The value should not be null.", value);
114 }
115
116 private static void expect(Object exp, Object value) {
117 assertEquals("The value is not correct.", exp, value);
118 }
119 }