|
| 1 | +package com.dropbox.core.json; |
| 2 | + |
| 3 | +import static org.testng.Assert.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | + |
| 8 | +import org.testng.annotations.Test; |
| 9 | + |
| 10 | +import java.text.SimpleDateFormat; |
| 11 | +import java.util.Date; |
| 12 | +import java.util.TimeZone; |
| 13 | + |
| 14 | +public class JsonUtilTest { |
| 15 | + private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); |
| 16 | + private static final String LONG_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; |
| 17 | + private static final String SHORT_DATE_TIME_FORMAT = "yyyy-MM-dd"; |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testV2Timestamps() throws Exception { |
| 21 | + // v2 allows 2 different formats for Babel Timestamp fields: |
| 22 | + // |
| 23 | + // DateTime format: Timestamp("%Y-%m-%dT%H:%M:%SZ") |
| 24 | + // Date format: Timestamp("%Y-%m-%d") |
| 25 | + // |
| 26 | + // The SDKs should be able to handle both formats. |
| 27 | + ObjectMapper mapper = JsonUtil.getMapper(); |
| 28 | + |
| 29 | + // LONG FORMAT DESERIALIZATION |
| 30 | + String expectedTimestamp = "2016-03-08T21:38:04Z"; |
| 31 | + Date expected = fromTimestampString(expectedTimestamp); |
| 32 | + Date actual = mapper.readValue(quoted(expectedTimestamp).getBytes("UTF-8"), Date.class); |
| 33 | + |
| 34 | + assertEquals(actual, expected); |
| 35 | + |
| 36 | + // LONG FORMAT SERIALIZATION |
| 37 | + String actualTimestamp = mapper.writeValueAsString(expected); |
| 38 | + |
| 39 | + assertEquals(actualTimestamp, quoted(expectedTimestamp)); |
| 40 | + |
| 41 | + // SHORT FORMAT DESERIALIZATION |
| 42 | + String shortTimestamp = "2011-02-03"; |
| 43 | + expected = fromTimestampString(shortTimestamp); |
| 44 | + actual = mapper.readValue(quoted(shortTimestamp).getBytes("UTF-8"), Date.class); |
| 45 | + |
| 46 | + assertEquals(actual, expected); |
| 47 | + |
| 48 | + // SHORT FORMAT SERIALIZATION |
| 49 | + actualTimestamp = mapper.writeValueAsString(expected); |
| 50 | + |
| 51 | + // we always format to long-form |
| 52 | + expectedTimestamp = toTimestampString(expected); |
| 53 | + assertEquals(actualTimestamp, quoted(expectedTimestamp)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test(expectedExceptions = JsonProcessingException.class) |
| 57 | + public void testV2BadLongTimestamp() throws Exception { |
| 58 | + ObjectMapper mapper = JsonUtil.getMapper(); |
| 59 | + // we don't support milliseconds |
| 60 | + mapper.readValue(quoted("2016-03-08T21:38:04.352+0500"), Date.class); |
| 61 | + } |
| 62 | + |
| 63 | + @Test(expectedExceptions = JsonProcessingException.class) |
| 64 | + public void testV2BadShortTimestamp() throws Exception { |
| 65 | + ObjectMapper mapper = JsonUtil.getMapper(); |
| 66 | + mapper.readValue(quoted("2016/03/08"), Date.class); |
| 67 | + } |
| 68 | + |
| 69 | + private static String quoted(String value) { |
| 70 | + return "\"" + value + "\""; |
| 71 | + } |
| 72 | + |
| 73 | + private static String toTimestampString(Date date) { |
| 74 | + SimpleDateFormat df = new SimpleDateFormat(LONG_DATE_TIME_FORMAT); |
| 75 | + df.setTimeZone(UTC); |
| 76 | + return df.format(date); |
| 77 | + } |
| 78 | + |
| 79 | + private static Date fromTimestampString(String timestamp) { |
| 80 | + SimpleDateFormat df; |
| 81 | + if (timestamp.length() > SHORT_DATE_TIME_FORMAT.length()) { |
| 82 | + df = new SimpleDateFormat(LONG_DATE_TIME_FORMAT); |
| 83 | + } else { |
| 84 | + df = new SimpleDateFormat(SHORT_DATE_TIME_FORMAT); |
| 85 | + } |
| 86 | + |
| 87 | + df.setTimeZone(UTC); |
| 88 | + try { |
| 89 | + return df.parse(timestamp); |
| 90 | + } catch (Exception ex) { |
| 91 | + fail("invalid timestamp", ex); |
| 92 | + return null; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments