|  | 
| 5 | 5 | 
 | 
| 6 | 6 | package gov.nist.secauto.metaschema.core.datatype.adapter; | 
| 7 | 7 | 
 | 
| 8 |  | -import gov.nist.secauto.metaschema.core.util.ObjectUtils; | 
|  | 8 | +import static org.junit.jupiter.api.Assertions.assertAll; | 
|  | 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 
|  | 10 | + | 
|  | 11 | +import gov.nist.secauto.metaschema.core.datatype.object.AmbiguousDateTime; | 
| 9 | 12 | 
 | 
| 10 | 13 | import org.junit.jupiter.params.ParameterizedTest; | 
| 11 |  | -import org.junit.jupiter.params.provider.ValueSource; | 
|  | 14 | +import org.junit.jupiter.params.provider.Arguments; | 
|  | 15 | +import org.junit.jupiter.params.provider.MethodSource; | 
|  | 16 | + | 
|  | 17 | +import java.time.ZoneOffset; | 
|  | 18 | +import java.time.ZonedDateTime; | 
|  | 19 | +import java.util.concurrent.TimeUnit; | 
|  | 20 | +import java.util.stream.Stream; | 
|  | 21 | + | 
|  | 22 | +import edu.umd.cs.findbugs.annotations.NonNull; | 
| 12 | 23 | 
 | 
| 13 | 24 | class DateTimeAdapterTest { | 
|  | 25 | +  private static final DateTimeAdapter ADAPTER = new DateTimeAdapter(); | 
|  | 26 | +  private static final long NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1); | 
| 14 | 27 | 
 | 
| 15 |  | -  @ParameterizedTest | 
| 16 |  | -  @ValueSource(strings = { | 
| 17 |  | -      "2018-01-01T00:00:00", | 
| 18 |  | -      "2019-09-28T23:20:50.52Z", | 
| 19 |  | -      "2019-09-28T23:20:50.0Z", | 
| 20 |  | -      "2019-09-28T23:20:50.5200", | 
| 21 |  | -      "2019-12-02T16:39:57-08:00", | 
| 22 |  | -      "2019-12-02T16:39:57.100-08:00", | 
| 23 |  | -      "2019-12-02T16:39:57", | 
| 24 |  | -      "2019-12-31T23:59:59Z", | 
| 25 |  | -      "2019-12-31T23:59:59" | 
| 26 |  | -  }) | 
| 27 |  | -  void testSimpleDate(String date) { | 
| 28 |  | -    new DateTimeAdapter().parse(ObjectUtils.requireNonNull(date)); | 
|  | 28 | +  private static Stream<Arguments> provideValues() { | 
|  | 29 | +    return Stream.of( | 
|  | 30 | +        Arguments.of( | 
|  | 31 | +            "2018-01-01T00:00:00", | 
|  | 32 | +            true, | 
|  | 33 | +            ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)), | 
|  | 34 | +        Arguments.of( | 
|  | 35 | +            "2019-09-28T23:20:50.52Z", | 
|  | 36 | +            false, | 
|  | 37 | +            ZonedDateTime.of(2019, 9, 28, 23, 20, 50, toNanos(0.52), ZoneOffset.UTC)), | 
|  | 38 | +        Arguments.of( | 
|  | 39 | +            "2019-09-28T23:20:50.0Z", | 
|  | 40 | +            false, | 
|  | 41 | +            ZonedDateTime.of(2019, 9, 28, 23, 20, 50, 0, ZoneOffset.UTC)), | 
|  | 42 | +        Arguments.of( | 
|  | 43 | +            "2019-09-28T23:20:50.5200", | 
|  | 44 | +            true, | 
|  | 45 | +            ZonedDateTime.of(2019, 9, 28, 23, 20, 50, toNanos(0.5200), ZoneOffset.UTC)), | 
|  | 46 | +        Arguments.of( | 
|  | 47 | +            "2019-12-02T16:39:57-08:00", | 
|  | 48 | +            false, | 
|  | 49 | +            ZonedDateTime.of(2019, 12, 2, 16, 39, 57, 0, ZoneOffset.of("-08:00"))), | 
|  | 50 | +        Arguments.of( | 
|  | 51 | +            "2019-12-02T16:39:57.100-08:00", | 
|  | 52 | +            false, | 
|  | 53 | +            ZonedDateTime.of(2019, 12, 2, 16, 39, 57, toNanos(0.100), ZoneOffset.of("-08:00"))), | 
|  | 54 | +        Arguments.of( | 
|  | 55 | +            "2019-12-02T16:39:57", | 
|  | 56 | +            true, | 
|  | 57 | +            ZonedDateTime.of(2019, 12, 2, 16, 39, 57, 0, ZoneOffset.UTC)), | 
|  | 58 | +        Arguments.of( | 
|  | 59 | +            "2019-12-31T23:59:59Z", | 
|  | 60 | +            false, | 
|  | 61 | +            ZonedDateTime.of(2019, 12, 31, 23, 59, 59, 0, ZoneOffset.UTC)), | 
|  | 62 | +        Arguments.of( | 
|  | 63 | +            "2019-12-31T23:59:59", | 
|  | 64 | +            true, | 
|  | 65 | +            ZonedDateTime.of(2019, 12, 31, 23, 59, 59, 0, ZoneOffset.UTC))); | 
| 29 | 66 |   } | 
| 30 | 67 | 
 | 
|  | 68 | +  private static int toNanos(double fraction) { | 
|  | 69 | +    return (int) Math.round(TimeUnit.SECONDS.toNanos(1) * fraction); | 
|  | 70 | +  } | 
|  | 71 | + | 
|  | 72 | +  @ParameterizedTest | 
|  | 73 | +  @MethodSource("provideValues") | 
|  | 74 | +  void testSimpleDateTime(@NonNull String actual, boolean ambiguous, @NonNull ZonedDateTime expected) { | 
|  | 75 | +    AmbiguousDateTime date = ADAPTER.parse(actual); | 
|  | 76 | +    assertAll( | 
|  | 77 | +        () -> assertEquals(ambiguous, !date.hasTimeZone()), | 
|  | 78 | +        () -> assertEquals(expected, date.getValue())); | 
|  | 79 | +  } | 
| 31 | 80 | } | 
0 commit comments