Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit 2fa48f9

Browse files
committed
chore: Add tests for static helper methods
1 parent 340ced6 commit 2fa48f9

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ static Instant fromEpochMicros(long micros) {
10481048

10491049
/** Best effort to try and convert a timestamp to an ISO8601 string */
10501050
@VisibleForTesting
1051-
String getTimestampAsString(Object val) {
1051+
static String getTimestampAsString(Object val) {
10521052
if (val instanceof String) {
10531053
// Validate the ISO8601 values before sending it to the server
10541054
String value = (String) val;

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.cloud.bigquery.storage.v1;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertThrows;
1920
import static org.junit.Assert.assertTrue;
2021

2122
import com.google.cloud.bigquery.storage.test.JsonTest.*;
@@ -28,6 +29,7 @@
2829
import com.google.protobuf.DynamicMessage;
2930
import com.google.protobuf.Message;
3031
import java.math.BigDecimal;
32+
import java.time.Instant;
3133
import java.time.LocalTime;
3234
import java.util.ArrayList;
3335
import java.util.Collection;
@@ -36,6 +38,8 @@
3638
import java.util.List;
3739
import java.util.Map;
3840
import java.util.logging.Logger;
41+
42+
import com.google.protobuf.Timestamp;
3943
import org.json.JSONArray;
4044
import org.json.JSONObject;
4145
import org.junit.Assert;
@@ -1834,4 +1838,33 @@ public void testDoubleAndFloatToRepeatedBigNumericConversion() {
18341838
JsonToProtoMessage.INSTANCE.convertToProtoMessage(TestBignumeric.getDescriptor(), ts, json);
18351839
assertEquals(expectedProto, protoMsg);
18361840
}
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+
}
18371870
}

0 commit comments

Comments
 (0)