2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.utils;
23 import org.apache.log4j.LogManager;
24 import org.apache.log4j.Logger;
25 import org.testng.Assert;
26 import org.testng.annotations.DataProvider;
27 import org.testng.annotations.Test;
29 import java.time.Instant;
30 import java.time.ZoneId;
31 import java.time.ZonedDateTime;
32 import java.time.format.DateTimeParseException;
34 public class TimeUtilsTest {
35 private static final Logger logger = LogManager.getLogger(TimeUtilsTest.class);
38 public static Object[][] goodData() {
39 return new Object[][]{
40 {"Wed, 15 Oct 2014 13:01:52 GMT", 1413378112, "Timestamp as described in the documentation"},
41 {"Wed, 15 Oct 2014 14:01:52 +0100", 1413378112, "GMT +1"},
42 {"Wed, 15 Oct 2014 11:01:52 -0200", 1413378112, "GMT -2"}
47 public static Object[][] goodDataToString() {
48 return new Object[][]{
49 {"Wed, 15 Oct 2014 13:01:52 GMT", 1413378112, "UTC", "Timestamp as described in the documentation"},
50 {"Wed, 15 Oct 2014 14:01:52 +0100", 1413378112, "+1", "GMT +1"},
51 {"Wed, 15 Oct 2014 11:01:52 -0200", 1413378112, "-2", "GMT -2"}
56 public static Object[][] badData() {
57 return new Object[][]{
58 {"Wed, 15 Oct 2014 13:01:52", "No offset"},
59 {"Wed, 15 Oct 2014 13:01:52 UTC", "UTC"},
60 {"Wed, 15 Oct 2014 13:01:52 UT", "UT"},
61 {"Wed, 15 Oct 2014 13:01:52Z", "Zulu time"},
62 {"Wed, 15 Oct 2014 13:01:52 EST", "EST time"}
66 @Test(dataProvider = "goodData")
67 public void parseSuccessTest(String timestamp, long expectedResult, String description) {
68 logger.info(description);
69 ZonedDateTime parsedTime = TimeUtils.parseZonedDateTime(timestamp);
70 Assert.assertEquals(parsedTime.toEpochSecond(), expectedResult);
73 @Test(expectedExceptions = DateTimeParseException.class, dataProvider = "badData")
74 public void parseFailedTest(String timestamp, String description) {
75 logger.info(description);
76 TimeUtils.parseZonedDateTime(timestamp);
79 @Test(dataProvider = "goodDataToString")
80 public void toStringSuccessTest(String expectedResult, long epochTime, String zoneId, String description) {
81 logger.info(description);
82 Instant instant = Instant.ofEpochSecond(epochTime);
83 ZonedDateTime time = ZonedDateTime.ofInstant(instant, ZoneId.of(zoneId));
84 String timeStamp = TimeUtils.zonedDateTimeToString(time);
85 Assert.assertEquals(timeStamp, expectedResult);