Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / utils / TimeUtilsTest.java
1 package org.onap.vid.utils;
2
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;
8
9 import java.time.Instant;
10 import java.time.ZoneId;
11 import java.time.ZonedDateTime;
12 import java.time.format.DateTimeParseException;
13
14 public class TimeUtilsTest {
15     private static final Logger logger = LogManager.getLogger(TimeUtilsTest.class);
16
17     @DataProvider
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"}
23         };
24     }
25
26     @DataProvider
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"}
32         };
33     }
34
35     @DataProvider
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"}
43         };
44     }
45
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);
51     }
52
53     @Test(expectedExceptions = DateTimeParseException.class, dataProvider = "badData")
54     public void parseFailedTest(String timestamp, String description) {
55         logger.info(description);
56         TimeUtils.parseZonedDateTime(timestamp);
57     }
58
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);
66     }
67 }