|
| 1 | +package com.microsoft.graph.serializer; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | + |
| 6 | +import java.util.Calendar; |
| 7 | +import java.util.TimeZone; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test cases for calendar serialization and deserialization |
| 13 | + * |
| 14 | + * @author mobilal |
| 15 | + * |
| 16 | + */ |
| 17 | +public class CalendarSerializerTests { |
| 18 | + |
| 19 | + /** |
| 20 | + * Validate if a calendar date instance could be serialized |
| 21 | + * @throws Exception if calendar couldn't be serialized |
| 22 | + */ |
| 23 | + @Test |
| 24 | + public void testDateSerialization() throws Exception { |
| 25 | + Calendar calendar = Calendar.getInstance(); |
| 26 | + calendar.setTimeInMillis(1561162355000L); |
| 27 | + String expected = "2019-06-22T00:12:35.000Z"; |
| 28 | + String actual = CalendarSerializer.serialize(calendar); |
| 29 | + assertNotNull(actual); |
| 30 | + assertEquals(expected, actual); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Validate if a date without milliseconds and offset could be deserialized |
| 35 | + * @throws Exception if date is not parsable |
| 36 | + */ |
| 37 | + @Test |
| 38 | + public void testSimpleDateTimeDeserialization() throws Exception { |
| 39 | + |
| 40 | + String datetime = "2019-06-21T17:12:35"; |
| 41 | + TimeZone defaultTimeZone = TimeZone.getDefault(); |
| 42 | + |
| 43 | + // Set timezone to UTC for testing |
| 44 | + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); |
| 45 | + Calendar expected = java.util.Calendar.getInstance(); |
| 46 | + expected.setTimeInMillis(1561137155000L); |
| 47 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 48 | + assertNotNull(actual); |
| 49 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 50 | + |
| 51 | + // Reset timezone after testing |
| 52 | + TimeZone.setDefault(defaultTimeZone); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Validate if date with UTC timezone (z) could be deserialized |
| 57 | + * @throws Exception if date is not parsable |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void testUTCSimpleDateTimeDeserialization() throws Exception { |
| 61 | + String datetime = "2019-06-21T17:12:35Z"; |
| 62 | + Calendar expected = java.util.Calendar.getInstance(); |
| 63 | + expected.setTimeInMillis(1561137155000L); |
| 64 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 65 | + assertNotNull(actual); |
| 66 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Validate if date with UTC timezone (z) and milliseconds could be deserialized |
| 71 | + * @throws Exception if date is not parsable |
| 72 | + */ |
| 73 | + @Test |
| 74 | + public void testUTCDateTimeWithMillisDeserialization() throws Exception { |
| 75 | + String datetime = "2019-06-21T17:12:35.1385912Z"; |
| 76 | + Calendar expected = java.util.Calendar.getInstance(); |
| 77 | + expected.setTimeInMillis(1561137155138L); |
| 78 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 79 | + assertNotNull(actual); |
| 80 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Validate if date with UTC offset (+00:00) could be deserialized |
| 85 | + * @throws Exception if date is not parsable |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void testUTCDateTimeInOffsetFormatDeserialization() throws Exception { |
| 89 | + String datetime = "2019-06-21T17:12:35+00:00"; |
| 90 | + Calendar expected = java.util.Calendar.getInstance(); |
| 91 | + expected.setTimeInMillis(1561137155000L); |
| 92 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 93 | + assertNotNull(actual); |
| 94 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Validate if date with UTC offset (+00:00) and milliseconds could be deserialized |
| 99 | + * @throws Exception if date is not parsable |
| 100 | + */ |
| 101 | + @Test |
| 102 | + public void testUTCDateTimeWithMillisAndOffsetDeserialization() throws Exception { |
| 103 | + String datetime = "2019-06-21T17:12:35.1385912+00:00"; |
| 104 | + Calendar expected = java.util.Calendar.getInstance(); |
| 105 | + expected.setTimeInMillis(1561137155138L); |
| 106 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 107 | + assertNotNull(actual); |
| 108 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Validate if date with different UTC offset (+0000) could be deserialized |
| 113 | + * @throws Exception if date is not parsable |
| 114 | + */ |
| 115 | + @Test |
| 116 | + public void testUTCDateTimeInNonstandardOffsetFormatDeserialization() throws Exception { |
| 117 | + String datetime = "2019-06-21T17:12:35+0000"; |
| 118 | + Calendar expected = java.util.Calendar.getInstance(); |
| 119 | + expected.setTimeInMillis(1561137155000L); |
| 120 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 121 | + assertNotNull(actual); |
| 122 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Validate if date with positive timezone offset (+07:00) could be deserialized |
| 127 | + * @throws Exception if date is not parsable |
| 128 | + */ |
| 129 | + @Test |
| 130 | + public void testDateTimePositiveOffsetFormatDeserialization() throws Exception { |
| 131 | + String datetime = "2019-06-21T17:12:35+07:00"; |
| 132 | + Calendar expected = java.util.Calendar.getInstance(); |
| 133 | + expected.setTimeInMillis(1561111955000L); |
| 134 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 135 | + assertNotNull(actual); |
| 136 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Validate if date with positive timezone offset (+07:00) and milliseconds could be deserialized |
| 141 | + * @throws Exception if date is not parsable |
| 142 | + */ |
| 143 | + @Test |
| 144 | + public void testDateTimeWithMillisAndPositiveOffsetDeserialization() throws Exception { |
| 145 | + String datetime = "2019-06-21T17:12:35.1385912+07:00"; |
| 146 | + Calendar expected = java.util.Calendar.getInstance(); |
| 147 | + expected.setTimeInMillis(1561111955138L); |
| 148 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 149 | + assertNotNull(actual); |
| 150 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Validate if date with positive timezone offset in different format (+0700) could be deserialized |
| 155 | + * @throws Exception if date is not parsable |
| 156 | + */ |
| 157 | + @Test |
| 158 | + public void testUTCDateTimeInNonstandardPositiveOffsetFormatDeserialization() throws Exception { |
| 159 | + String datetime = "2019-06-21T17:12:35+0700"; |
| 160 | + Calendar expected = java.util.Calendar.getInstance(); |
| 161 | + expected.setTimeInMillis(1561111955000L); |
| 162 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 163 | + assertNotNull(actual); |
| 164 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Validate if date with negative timezone offset (-07:00) could be deserialized |
| 169 | + * @throws Exception if date is not parsable |
| 170 | + */ |
| 171 | + @Test |
| 172 | + public void testDateTimeNegativeOffsetFormatDeserialization() throws Exception { |
| 173 | + String datetime = "2019-06-21T17:12:35-07:00"; |
| 174 | + Calendar expected = java.util.Calendar.getInstance(); |
| 175 | + expected.setTimeInMillis(1561162355000L); |
| 176 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 177 | + assertNotNull(actual); |
| 178 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Validate if date with negative timezone offset (-07:00) and milliseconds could be deserialized |
| 183 | + * @throws Exception if date is not parsable |
| 184 | + */ |
| 185 | + @Test |
| 186 | + public void testDateTimeWithMillisAndNegativeOffsetDeserialization() throws Exception { |
| 187 | + String datetime = "2019-06-21T17:12:35.1385912-07:00"; |
| 188 | + Calendar expected = java.util.Calendar.getInstance(); |
| 189 | + expected.setTimeInMillis(1561162355138L); |
| 190 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 191 | + assertNotNull(actual); |
| 192 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Validate if date with negative timezone offset in different format (-0700) could be deserialized |
| 197 | + * @throws Exception if date is not parsable |
| 198 | + */ |
| 199 | + @Test |
| 200 | + public void testUTCDateTimeInNonstandardNegativeOffsetFormatDeserialization() throws Exception { |
| 201 | + String datetime = "2019-06-21T17:12:35-0700"; |
| 202 | + Calendar expected = java.util.Calendar.getInstance(); |
| 203 | + expected.setTimeInMillis(1561162355000L); |
| 204 | + Calendar actual = CalendarSerializer.deserialize(datetime); |
| 205 | + assertNotNull(actual); |
| 206 | + assertEquals(expected.getTimeInMillis(), actual.getTimeInMillis()); |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments