View Javadoc
1   package emissary.util;
2   
3   import emissary.test.core.junit5.UnitTest;
4   
5   import org.junit.jupiter.api.Test;
6   
7   import java.time.Instant;
8   import java.time.LocalDateTime;
9   import java.time.Month;
10  import java.time.ZoneId;
11  import java.time.ZoneOffset;
12  import java.time.ZonedDateTime;
13  import java.time.zone.ZoneRulesException;
14  
15  import static org.junit.jupiter.api.Assertions.assertEquals;
16  import static org.junit.jupiter.api.Assertions.assertNull;
17  import static org.junit.jupiter.api.Assertions.assertThrows;
18  import static org.junit.jupiter.api.Assertions.assertTrue;
19  
20  class TimeUtilTest extends UnitTest {
21  
22      private static final ZoneId GMT = ZoneId.of("GMT");
23      private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
24      private static final LocalDateTime testLocalDate = LocalDateTime.of(2016, Month.DECEMBER, 25, 15, 30, 25);
25      private static final Instant testUtilDate = testLocalDate.toInstant(ZoneOffset.UTC);
26      private static final ZonedDateTime testZoneDate = ZonedDateTime.of(testLocalDate, GMT);
27  
28      @Test
29      void testGetDate() {
30          assertEquals("2016 Dec 25 15 25 30", TimeUtil.getDate(testZoneDate, "yyyy MMM dd HH ss mm", GMT), "getDate did not match");
31          assertEquals("2016-12-25T15:30:25 GMT", TimeUtil.getDate(testZoneDate, "yyyy-MM-dd'T'HH:mm:ss z", null), "getDate did not match");
32          assertEquals("2016-12-25T14:30:25 GMT",
33                  TimeUtil.getDate(ZonedDateTime.of(testLocalDate, ZoneId.of("Europe/Paris")), "yyyy-MM-dd'T'HH:mm:ss z", GMT),
34                  "getDate did not match");
35          assertEquals("2016-12-25T15:30:25 CET",
36                  TimeUtil.getDate(ZonedDateTime.of(testLocalDate, ZoneId.of("Europe/Paris")), "yyyy-MM-dd'T'HH:mm:ss z", PARIS),
37                  "getDate did not match");
38          assertNull(TimeUtil.getDate(null, null, null));
39      }
40  
41      @Test
42      void testGetDateExceptionBadFormat() {
43          assertThrows(IllegalArgumentException.class, () -> TimeUtil.getDate(testZoneDate, null, null));
44      }
45  
46      @Test
47      void testGetDateExceptionBadFormat2() {
48          assertThrows(IllegalArgumentException.class, () -> TimeUtil.getDate(testZoneDate, "ThisReallyShouldntWork", null));
49      }
50  
51  
52      @Test
53      void testGetDateExceptionBadZone() {
54          assertThrows(ZoneRulesException.class, () -> TimeUtil.getDate("yyyy", "BAD"));
55      }
56  
57      @Test
58      void testGetDateAsISO8601Long() {
59          assertEquals("2016-12-25 15:30:25", TimeUtil.getDateAsISO8601(testUtilDate), "GetDateAsISO8601Long");
60      }
61  
62      @Deprecated
63      @Test
64      void testGetZonedDateFromISO8601() {
65          ZonedDateTime zdt = TimeUtil.getZonedDateFromISO8601("2016-12-25 15:30:25");
66          assertEquals(15, zdt.getHour());
67          assertEquals(30, zdt.getMinute());
68          assertEquals(25, zdt.getSecond());
69          assertEquals(2016, zdt.getYear());
70          assertEquals(12, zdt.getMonthValue());
71          assertEquals(25, zdt.getDayOfMonth());
72          assertEquals(360, zdt.getDayOfYear());
73          assertEquals("GMT", zdt.getZone().getId());
74      }
75  
76      @Test
77      void testOrdinal() {
78          assertEquals(7, TimeUtil.getCurrentDateOrdinal().length(), "Date with ordinal must be 7 long");
79      }
80  
81      @Test
82      void testCurrentTimeAsPath() {
83          final String path = TimeUtil.getCurrentDate();
84          assertTrue(path.contains("/"), "Path with time should have slashes");
85      }
86  
87      @Test
88      void testTimeZoneAddedOnFormat() {
89          String dt = TimeUtil.getDate("yyyy-MM-dd'T'HH:mm:ss z", null);
90          assertTrue(dt.contains("GMT"), "GMT must be added by default - " + dt);
91  
92          // timezone changes due to daylight savings
93          dt = TimeUtil.getDate("yyyy-MM-dd'T'HH:mm:ss z", "Europe/Paris");
94          assertTrue(dt.contains("CET") || dt.contains("CEST"), "Specified tz must be added - " + dt);
95      }
96  
97      @Test
98      void testDateAsFullIOS8601() {
99          final String currentTime = TimeUtil.getCurrentDateFullISO8601();
100         assertTrue(currentTime.contains("T"), "Full ISO8601 must have a 'T'");
101         assertTrue(currentTime.contains("Z"), "Full ISO8601 must have a 'Z'");
102 
103         final String time = TimeUtil.getDateAsFullISO8601(ZonedDateTime.now(ZoneId.systemDefault()));
104         assertTrue(time.contains("T"), "Full ISO8601 must have a 'T'");
105         assertTrue(time.contains("Z"), "Full ISO8601 must have a 'Z'");
106     }
107 
108     @Test
109     void testConvertHexDate() {
110         String hexDate1 = "0x00009FF700F77536";
111         String hexDate2 = "0x0000A07800E93033";
112         assertEquals("2012-02-14 15:00:57.993", TimeUtil.convertHexDate(hexDate1), "convertHexDate conversion did not match");
113         assertEquals("2012-06-22 14:09:00.757", TimeUtil.convertHexDate(hexDate2), "convertHexDate conversion did not match");
114 
115         String invalidHexDate1 = "0x00009FF700F7753"; // too few characters
116         String invalidHexDate2 = "0x00009FF_00F77536"; // format doesn't support any non-hex characters
117         assertThrows(IllegalArgumentException.class, () -> TimeUtil.convertHexDate(invalidHexDate1));
118         assertThrows(IllegalArgumentException.class, () -> TimeUtil.convertHexDate(invalidHexDate2));
119     }
120 
121     @Test
122     void testGetDateOrdinalWithTime() {
123         assertEquals("2016360153025", TimeUtil.getDateOrdinalWithTime(testUtilDate), "Date Ordinal with Time did not match");
124     }
125 }