2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018-2019 IBM
8 * ================================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.util;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
32 import java.text.ParseException;
33 import java.time.LocalDate;
34 import java.time.ZoneId;
35 import java.time.ZoneOffset;
36 import java.time.ZonedDateTime;
37 import java.util.Calendar;
38 import java.util.Date;
39 import java.util.GregorianCalendar;
40 import java.util.Locale;
41 import java.util.TimeZone;
43 import javax.xml.datatype.DatatypeConfigurationException;
44 import javax.xml.datatype.DatatypeFactory;
45 import javax.xml.datatype.XMLGregorianCalendar;
47 import org.junit.Test;
49 public class TimeTest {
51 private final Date dateForTests = Date
52 .from(LocalDate.of(2004, 2, 9).atStartOfDay(ZoneId.systemDefault()).toInstant());
55 public void testAddTime() {
57 final Date dateNow = new Date();
58 long dateNowMSec = dateNow.getTime();
59 Date dateSecLater = Time.addTime(dateNow, 0, 0, 0, 0, 1);
60 long dateSecLaterMSec = dateSecLater.getTime();
61 assertEquals(dateNowMSec + 1000, dateSecLaterMSec);
66 public void testDateOnly() {
68 final Date dateNow = new Date();
69 Calendar cal = Calendar.getInstance();
74 long msecFromBegin = cal.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 + cal.get(Calendar.MINUTE) * 60 * 1000
75 + cal.get(Calendar.SECOND) * 1000 + cal.get(Calendar.MILLISECOND);
77 assertEquals(msecFromBegin, 0);
82 public void testGetCurrentUTCDate() {
84 ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
85 Date utcDate = Date.from(utc.toInstant());
86 long epochSecs = utc.toEpochSecond();
88 long utcSecs = utcDate.getTime() / 1000;
90 assertEquals(epochSecs, utcSecs);
94 public void testEndOfDayLocal() {
95 final Date dateNow = new Date();
96 assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
100 public void testGetDateByLocaleAndTimeZone() {
101 final Date dateNow = new Date();
102 Locale locale = new Locale("fr");
103 TimeZone timeZone = TimeZone.getTimeZone("Europe/France");
104 assertNotNull(Time.getDateByLocaleAndTimeZone(dateNow, locale, timeZone));
105 assertTrue(Time.getDateByLocaleAndTimeZone(dateNow, locale, timeZone) instanceof String);
109 public void testUtcFormat() {
110 final Date date = new Date();
111 assertNotNull(Time.utcFormat(date));
112 assertTrue(Time.utcFormat(date) instanceof String);
115 // this test succeeds if localTime() does not throw an exception
117 public void testLocalTime() {
118 Time.localTime(1532083631);
122 public void testSetDate() {
123 Calendar cal = Calendar.getInstance();
124 cal.set(Calendar.YEAR, 2018);
125 cal.set(Calendar.MONTH, 07);
126 cal.set(Calendar.DAY_OF_MONTH, 03);
127 Calendar cal1 = Time.setDate(cal, 2018, 07, 03);
128 assertEquals(cal, cal1);
132 public void testStartOfDayLocal() {
133 assertTrue(Time.startOfDayLocal() instanceof Date);
137 public void testTimeStamp() {
138 assertTrue(Time.timestamp() instanceof XMLGregorianCalendar);
142 public void testDateToStringConverterMillis() {
143 String dateString = Time.dateToStringConverterMillis(dateForTests);
144 String expected = "2004-02-09 00:00:00:000";
145 assertEquals(expected, dateString);
149 public void testStringToDateConverterMillis() throws ParseException {
150 Date date = Time.stringToDateConverterMillis("2004-02-09 00:00:00:000");
151 Date expected = dateForTests;
152 assertEquals(expected, date);
156 public void testTruncateDate() throws ParseException {
157 Date date = Time.truncDate(dateForTests);
158 Date expected = dateForTests;
159 assertEquals(expected, date);
163 public void testToDate() throws ParseException, DatatypeConfigurationException {
164 Date date = dateForTests;
165 GregorianCalendar c = new GregorianCalendar();
167 XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
168 Date actual = Time.toDate(calendar);
169 Date expected = dateForTests;
170 assertEquals(expected, actual);
174 public void testUtcTime() {
175 assertTrue(Time.utcTime()!=0);
179 public void testCurrentUTCDate() {
180 assertTrue(Time.getCurrentUTCDate() instanceof Date);