1 package org.onap.vid.utils;
3 import org.apache.log4j.LogManager;
4 import org.apache.log4j.Logger;
5 import org.testng.Assert;
6 import org.testng.annotations.DataProvider;
7 import org.testng.annotations.Test;
9 import java.time.Instant;
10 import java.time.ZoneId;
11 import java.time.ZonedDateTime;
12 import java.time.format.DateTimeParseException;
14 public class TimeUtilsTest {
15 private static final Logger logger = LogManager.getLogger(TimeUtilsTest.class);
18 public static Object[][] goodData() {
19 return new Object[][]{
20 {"Wed, 15 Oct 2014 13:01:52 GMT", 1413378112, "Timestamp as described in the documentation"},
21 {"Wed, 15 Oct 2014 14:01:52 +0100", 1413378112, "GMT +1"},
22 {"Wed, 15 Oct 2014 11:01:52 -0200", 1413378112, "GMT -2"}
27 public static Object[][] goodDataToString() {
28 return new Object[][]{
29 {"Wed, 15 Oct 2014 13:01:52 GMT", 1413378112, "UTC", "Timestamp as described in the documentation"},
30 {"Wed, 15 Oct 2014 14:01:52 +0100", 1413378112, "+1", "GMT +1"},
31 {"Wed, 15 Oct 2014 11:01:52 -0200", 1413378112, "-2", "GMT -2"}
36 public static Object[][] badData() {
37 return new Object[][]{
38 {"Wed, 15 Oct 2014 13:01:52", "No offset"},
39 {"Wed, 15 Oct 2014 13:01:52 UTC", "UTC"},
40 {"Wed, 15 Oct 2014 13:01:52 UT", "UT"},
41 {"Wed, 15 Oct 2014 13:01:52Z", "Zulu time"},
42 {"Wed, 15 Oct 2014 13:01:52 EST", "EST time"}
46 @Test(dataProvider = "goodData")
47 public void parseSuccessTest(String timestamp, long expectedResult, String description) {
48 logger.info(description);
49 ZonedDateTime parsedTime = TimeUtils.parseZonedDateTime(timestamp);
50 Assert.assertEquals(parsedTime.toEpochSecond(), expectedResult);
53 @Test(expectedExceptions = DateTimeParseException.class, dataProvider = "badData")
54 public void parseFailedTest(String timestamp, String description) {
55 logger.info(description);
56 TimeUtils.parseZonedDateTime(timestamp);
59 @Test(dataProvider = "goodDataToString")
60 public void toStringSuccessTest(String expectedResult, long epochTime, String zoneId, String description) {
61 logger.info(description);
62 Instant instant = Instant.ofEpochSecond(epochTime);
63 ZonedDateTime time = ZonedDateTime.ofInstant(instant, ZoneId.of(zoneId));
64 String timeStamp = TimeUtils.zonedDateTimeToString(time);
65 Assert.assertEquals(timeStamp, expectedResult);