|
| 1 | +package com.databricks.sdk.core.utils; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import com.databricks.sdk.core.ApiClient; |
| 6 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 7 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.google.protobuf.Duration; |
| 10 | +import com.google.protobuf.FieldMask; |
| 11 | +import com.google.protobuf.Timestamp; |
| 12 | +import java.util.stream.Stream; |
| 13 | +import org.junit.jupiter.params.ParameterizedTest; |
| 14 | +import org.junit.jupiter.params.provider.*; |
| 15 | + |
| 16 | +public class ProtobufModuleTest { |
| 17 | + |
| 18 | + // Helper wrapper classes for individual protobuf types. |
| 19 | + public static class FieldMaskWrapper { |
| 20 | + @JsonProperty("mask") |
| 21 | + public FieldMask mask; |
| 22 | + } |
| 23 | + |
| 24 | + public static class DurationWrapper { |
| 25 | + @JsonProperty("duration") |
| 26 | + public Duration duration; |
| 27 | + } |
| 28 | + |
| 29 | + public static class TimestampWrapper { |
| 30 | + @JsonProperty("timestamp") |
| 31 | + public Timestamp timestamp; |
| 32 | + } |
| 33 | + |
| 34 | + // FieldMask Parameterized Tests. |
| 35 | + @ParameterizedTest |
| 36 | + @ValueSource( |
| 37 | + strings = { |
| 38 | + "", |
| 39 | + "user.name,user.email", |
| 40 | + "profile.snake_case,profile.avatar", |
| 41 | + "profile.displayName,profile.avatar,settings.theme", |
| 42 | + "nested.deep.field", |
| 43 | + "nested.deep.field.value", |
| 44 | + "complex.nested.deep.path.with.multiple.levels" |
| 45 | + }) |
| 46 | + public void testFieldMaskSerializationAndRoundtrip(String pathsString) |
| 47 | + throws JsonProcessingException { |
| 48 | + // Create original FieldMask. |
| 49 | + FieldMask.Builder builder = FieldMask.newBuilder(); |
| 50 | + if (!pathsString.isEmpty()) { |
| 51 | + for (String path : pathsString.split(",")) { |
| 52 | + builder.addPaths(path.trim()); |
| 53 | + } |
| 54 | + } |
| 55 | + FieldMask original = builder.build(); |
| 56 | + |
| 57 | + // Test serialization. |
| 58 | + FieldMaskWrapper wrapper = new FieldMaskWrapper(); |
| 59 | + wrapper.mask = original; |
| 60 | + |
| 61 | + String json = new ApiClient().serialize(wrapper); |
| 62 | + String expectedJson = "{\"mask\":\"" + pathsString + "\"}"; |
| 63 | + assertEquals(expectedJson, json); |
| 64 | + |
| 65 | + // Test roundtrip (deserialize and verify). |
| 66 | + ObjectMapper mapper = SerDeUtils.createMapper(); |
| 67 | + FieldMaskWrapper deserialized = mapper.readValue(json, FieldMaskWrapper.class); |
| 68 | + assertEquals(original.getPathsList(), deserialized.mask.getPathsList()); |
| 69 | + } |
| 70 | + |
| 71 | + // Duration Parameterized Tests. |
| 72 | + static Stream<Arguments> durationTestCases() { |
| 73 | + return Stream.of( |
| 74 | + Arguments.of(0L, 0, "0s"), |
| 75 | + Arguments.of(1L, 0, "1s"), |
| 76 | + Arguments.of(30L, 0, "30s"), |
| 77 | + Arguments.of(3661L, 0, "3661s"), // 1 hour 1 minute 1 second |
| 78 | + Arguments.of(0L, 500_000_000, "0.500s"), // 0.5 seconds |
| 79 | + Arguments.of(1L, 500_000_000, "1.500s"), // 1.5 seconds |
| 80 | + Arguments.of(30L, 3, "30.000000003s") // 30 seconds + 3 nanoseconds |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + @ParameterizedTest |
| 85 | + @MethodSource("durationTestCases") |
| 86 | + public void testDurationSerializationAndRoundtrip( |
| 87 | + long seconds, int nanos, String expectedDurationString) throws JsonProcessingException { |
| 88 | + Duration original = Duration.newBuilder().setSeconds(seconds).setNanos(nanos).build(); |
| 89 | + |
| 90 | + DurationWrapper wrapper = new DurationWrapper(); |
| 91 | + wrapper.duration = original; |
| 92 | + |
| 93 | + // Test serialization. |
| 94 | + String json = new ApiClient().serialize(wrapper); |
| 95 | + String expectedJson = "{\"duration\":\"" + expectedDurationString + "\"}"; |
| 96 | + assertEquals(expectedJson, json); |
| 97 | + |
| 98 | + // Test roundtrip (deserialize and verify). |
| 99 | + ObjectMapper mapper = SerDeUtils.createMapper(); |
| 100 | + DurationWrapper deserialized = mapper.readValue(json, DurationWrapper.class); |
| 101 | + assertEquals(original.getSeconds(), deserialized.duration.getSeconds()); |
| 102 | + assertEquals(original.getNanos(), deserialized.duration.getNanos()); |
| 103 | + } |
| 104 | + |
| 105 | + // Timestamp Parameterized Tests. |
| 106 | + static Stream<Arguments> timestampTestCases() { |
| 107 | + return Stream.of( |
| 108 | + Arguments.of(0L, 0, "1970-01-01T00:00:00Z"), // Unix epoch |
| 109 | + Arguments.of(1717756800L, 0, "2024-06-07T10:40:00Z"), // Test timestamp |
| 110 | + Arguments.of(1609459200L, 0, "2021-01-01T00:00:00Z"), // New Year 2021 |
| 111 | + Arguments.of(1577836800L, 0, "2020-01-01T00:00:00Z"), // New Year 2020 |
| 112 | + Arguments.of(1640995200L, 500_000_000, "2022-01-01T00:00:00.500Z"), // With nanoseconds |
| 113 | + Arguments.of(253402300799L, 999_999_999, "9999-12-31T23:59:59.999999999Z") // Far future |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + @ParameterizedTest |
| 118 | + @MethodSource("timestampTestCases") |
| 119 | + public void testTimestampSerializationAndRoundtrip( |
| 120 | + long seconds, int nanos, String expectedTimestampString) throws JsonProcessingException { |
| 121 | + Timestamp original = Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build(); |
| 122 | + |
| 123 | + TimestampWrapper wrapper = new TimestampWrapper(); |
| 124 | + wrapper.timestamp = original; |
| 125 | + |
| 126 | + // Test serialization. |
| 127 | + String json = new ApiClient().serialize(wrapper); |
| 128 | + String expectedJson = "{\"timestamp\":\"" + expectedTimestampString + "\"}"; |
| 129 | + assertEquals(expectedJson, json); |
| 130 | + |
| 131 | + // Test roundtrip (deserialize and verify). |
| 132 | + ObjectMapper mapper = SerDeUtils.createMapper(); |
| 133 | + TimestampWrapper deserialized = mapper.readValue(json, TimestampWrapper.class); |
| 134 | + assertEquals(original.getSeconds(), deserialized.timestamp.getSeconds()); |
| 135 | + assertEquals(original.getNanos(), deserialized.timestamp.getNanos()); |
| 136 | + } |
| 137 | +} |
0 commit comments