|
16 | 16 | package com.google.cloud.bigquery.storage.v1; |
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertThrows; |
19 | 20 | import static org.junit.Assert.assertTrue; |
20 | 21 |
|
21 | 22 | import com.google.cloud.bigquery.storage.test.JsonTest.*; |
|
28 | 29 | import com.google.protobuf.DynamicMessage; |
29 | 30 | import com.google.protobuf.Message; |
30 | 31 | import java.math.BigDecimal; |
| 32 | +import java.time.Instant; |
31 | 33 | import java.time.LocalTime; |
32 | 34 | import java.util.ArrayList; |
33 | 35 | import java.util.Collection; |
|
36 | 38 | import java.util.List; |
37 | 39 | import java.util.Map; |
38 | 40 | import java.util.logging.Logger; |
| 41 | + |
| 42 | +import com.google.protobuf.Timestamp; |
39 | 43 | import org.json.JSONArray; |
40 | 44 | import org.json.JSONObject; |
41 | 45 | import org.junit.Assert; |
@@ -1834,4 +1838,33 @@ public void testDoubleAndFloatToRepeatedBigNumericConversion() { |
1834 | 1838 | JsonToProtoMessage.INSTANCE.convertToProtoMessage(TestBignumeric.getDescriptor(), ts, json); |
1835 | 1839 | assertEquals(expectedProto, protoMsg); |
1836 | 1840 | } |
| 1841 | + |
| 1842 | + @Test |
| 1843 | + public void testGetTimestampAsString() { |
| 1844 | + assertEquals("2025-10-01 12:34:56.123456+00:00", JsonToProtoMessage.getTimestampAsString("2025-10-01 12:34:56.123456+00:00")); |
| 1845 | + assertEquals("2025-10-01 12:34:56.123456789123+00:00", JsonToProtoMessage.getTimestampAsString("2025-10-01 12:34:56.123456789123+00:00")); |
| 1846 | + |
| 1847 | + assertEquals("1970-01-01T00:00:00.000001Z", JsonToProtoMessage.getTimestampAsString(1L)); |
| 1848 | + assertEquals("1969-12-31T23:59:59.999999Z", JsonToProtoMessage.getTimestampAsString(-1L)); |
| 1849 | + |
| 1850 | + assertEquals("1970-01-02T10:17:36.000123456Z", JsonToProtoMessage.getTimestampAsString(Timestamp.newBuilder().setSeconds(123456).setNanos(123456).build())); |
| 1851 | + assertEquals("1969-12-30T13:42:23.999876544Z", JsonToProtoMessage.getTimestampAsString(Timestamp.newBuilder().setSeconds(-123456).setNanos(-123456).build())); |
| 1852 | + |
| 1853 | + assertThrows(IllegalArgumentException.class, () -> JsonToProtoMessage.getTimestampAsString("2025-10-01")); |
| 1854 | + assertThrows(IllegalArgumentException.class, () -> JsonToProtoMessage.getTimestampAsString("1234")); |
| 1855 | + assertThrows(IllegalArgumentException.class, () -> JsonToProtoMessage.getTimestampAsString("abc")); |
| 1856 | + assertThrows(IllegalArgumentException.class, () -> JsonToProtoMessage.getTimestampAsString(10.4)); |
| 1857 | + assertThrows(IllegalArgumentException.class, () -> JsonToProtoMessage.getTimestampAsString(-1000.4)); |
| 1858 | + assertThrows(IllegalArgumentException.class, () -> JsonToProtoMessage.getTimestampAsString(Timestamp.newBuilder())); |
| 1859 | + assertThrows(IllegalArgumentException.class, () -> JsonToProtoMessage.getTimestampAsString(new Object())); |
| 1860 | + assertThrows(IllegalArgumentException.class, () -> JsonToProtoMessage.getTimestampAsString(null)); |
| 1861 | + } |
| 1862 | + |
| 1863 | + @Test |
| 1864 | + public void testFromEpochMicros() { |
| 1865 | + // The `+` is added if there are more than 4 digits for years |
| 1866 | + assertEquals("+294247-01-10T04:00:54.775807Z", JsonToProtoMessage.fromEpochMicros(Long.MAX_VALUE).toString()); |
| 1867 | + assertEquals("-290308-12-21T19:59:05.224192Z", JsonToProtoMessage.fromEpochMicros(Long.MIN_VALUE).toString()); |
| 1868 | + assertEquals(Instant.EPOCH.toString(), JsonToProtoMessage.fromEpochMicros(0L).toString()); |
| 1869 | + } |
1837 | 1870 | } |
0 commit comments