Skip to content

Commit

Permalink
Add unit tests for ProtobufTimeUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
chronos-tachyon committed Dec 13, 2023
1 parent bb43d37 commit 81179a0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,33 @@
import com.google.protobuf.util.Timestamps;
import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class ProtobufTimeUtils {

/**
* Converts a Protobuf Duration to a Java Duration with millisecond precision. Null inputs are
* treated as zero.
*/
@Nonnull
public static Duration toJavaDuration(com.google.protobuf.Duration d) {
// TODO we should refactor an implicit conversion of empty values into ZERO and rename the
// current method into toJavaDurationSafe, toJavaDurationOrDefault or something like that
if (d == null) {
public static Duration toJavaDuration(@Nullable com.google.protobuf.Duration d) {
if (Objects.isNull(d)) {
return Duration.ZERO;
}

return Duration.ofMillis(Durations.toMillis(d));
}

public static com.google.protobuf.Duration toProtoDuration(Duration d) {
// TODO we should refactor an implicit conversion of empty values into ZERO and rename the
// current method into toJavaDurationSafe, toJavaDurationOrDefault or something like that
if (d == null) {
/**
* Converts a Java Duration to a Protobuf Duration with millisecond precision. Null inputs are
* treated as zero.
*/
@Nonnull
public static com.google.protobuf.Duration toProtoDuration(@Nullable Duration d) {
if (Objects.isNull(d)) {
return Durations.ZERO;
}

return Durations.fromMillis(d.toMillis());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.temporal.internal.common;

import static org.junit.Assert.assertEquals;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ProtobufTimeUtilsTest {

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
// Values with integral milliseconds
new Object[] {0L, 0, 0L, 0},
new Object[] {0L, 1_000_000, 0L, 1_000_000},
new Object[] {0L, 500_000_000, 0L, 500_000_000},
new Object[] {0L, 999_000_000, 0L, 999_000_000},

// Values with fractional milliseconds
new Object[] {0L, 123_000_001, 0L, 123_000_000},
new Object[] {0L, 123_100_000, 0L, 123_000_000},
new Object[] {0L, 123_499_999, 0L, 123_000_000},
new Object[] {0L, 123_500_000, 0L, 123_000_000},
new Object[] {0L, 123_999_999, 0L, 123_000_000});
}

private final long inputSeconds;
private final int inputNanos;
private final long outputSeconds;
private final int outputNanos;

public ProtobufTimeUtilsTest(
long inputSeconds, int inputNanos, long outputSeconds, int outputNanos) {
this.inputSeconds = inputSeconds;
this.inputNanos = inputNanos;
this.outputSeconds = outputSeconds;
this.outputNanos = outputNanos;
}

@Test
public void toJavaDuration() {
final Duration output =
ProtobufTimeUtils.toJavaDuration(
com.google.protobuf.Duration.newBuilder()
.setSeconds(inputSeconds)
.setNanos(inputNanos)
.build());
assertEquals(outputSeconds, output.getSeconds());
assertEquals(outputNanos, output.getNano());
}

@Test
public void toProtoDuration() {
final com.google.protobuf.Duration output =
ProtobufTimeUtils.toProtoDuration(Duration.ofSeconds(inputSeconds, inputNanos));
assertEquals(outputSeconds, output.getSeconds());
assertEquals(outputNanos, output.getNanos());
}
}

0 comments on commit 81179a0

Please sign in to comment.