2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018 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=========================================================
27 package org.onap.appc.util;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
33 import java.text.ParseException;
34 import java.time.LocalDate;
35 import java.time.ZoneId;
36 import java.time.ZoneOffset;
37 import java.time.ZonedDateTime;
38 import java.util.Calendar;
39 import java.util.Date;
40 import java.util.GregorianCalendar;
41 import java.util.Locale;
42 import java.util.TimeZone;
44 import javax.xml.datatype.DatatypeConfigurationException;
45 import javax.xml.datatype.DatatypeFactory;
46 import javax.xml.datatype.XMLGregorianCalendar;
48 import org.junit.Test;
51 public class TimeTest {
53 private final Date dateForTests = Date.from(LocalDate.of(2004, 2, 9)
54 .atStartOfDay(ZoneId.systemDefault()).toInstant());
58 public void testAddTime() {
60 final Date dateNow = new Date();
61 long dateNowMSec = dateNow.getTime();
62 Date dateSecLater = Time.addTime(dateNow, 0, 0, 0, 0, 1);
63 long dateSecLaterMSec = dateSecLater.getTime();
64 assertEquals(dateNowMSec + 1000, dateSecLaterMSec);
69 public void testDateOnly() {
71 final Date dateNow = new Date();
72 Calendar cal = Calendar.getInstance();
77 long msecFromBegin = cal.get(Calendar.HOUR_OF_DAY)*60*60*1000 +
78 cal.get(Calendar.MINUTE)*60*1000 +
79 cal.get(Calendar.SECOND)*1000 +
80 cal.get(Calendar.MILLISECOND);
82 assertEquals( msecFromBegin, 0);
87 public void testGetCurrentUTCDate() {
89 ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
90 Date utcDate = Date.from(utc.toInstant());
91 long epochSecs = utc.toEpochSecond();
93 long utcSecs = utcDate.getTime() / 1000;
95 assertEquals(epochSecs, utcSecs);
99 public void testEndOfDayLocal() {
100 final Date dateNow = new Date();
101 assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
105 public void testGetDateByLocaleAndTimeZone() {
106 final Date dateNow = new Date();
107 Locale locale = new Locale("fr");
108 TimeZone timeZone = TimeZone.getTimeZone("Europe/France");
109 assertNotNull(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone));
110 assertTrue(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone) instanceof String);
114 public void testUtcFormat() {
115 final Date date = new Date();
116 assertNotNull(Time.utcFormat(date));
117 assertTrue(Time.utcFormat(date) instanceof String);
120 //this test succeeds if localTime() does not throw an exception
122 public void testLocalTime() {
123 Time.localTime(1532083631);
127 public void testSetDate() {
128 Calendar cal = Calendar.getInstance();
129 cal.set(Calendar.YEAR, 2018);
130 cal.set(Calendar.MONTH, 07);
131 cal.set(Calendar.DAY_OF_MONTH, 03);
132 Calendar cal1 = Time.setDate(cal, 2018, 07, 03);
133 assertEquals(cal, cal1);
137 public void testStartOfDayLocal() {
138 assertTrue(Time.startOfDayLocal() instanceof Date);
142 public void testTimeStamp() {
143 assertTrue(Time.timestamp() instanceof XMLGregorianCalendar);
147 public void testDateToStringConverterMillis() {
148 String dateString = Time.dateToStringConverterMillis(dateForTests);
149 String expected = "2004-02-09 00:00:00:000";
150 assertEquals(expected, dateString);
154 public void testStringToDateConverterMillis() throws ParseException{
155 Date date = Time.stringToDateConverterMillis("2004-02-09 00:00:00:000");
156 Date expected = dateForTests;
157 assertEquals(expected, date);
161 public void testTruncateDate() throws ParseException{
162 Date date = Time.truncDate(dateForTests);
163 Date expected = dateForTests;
164 assertEquals(expected, date);
168 public void testToDate() throws ParseException, DatatypeConfigurationException{
169 Date date = dateForTests;
170 GregorianCalendar c = new GregorianCalendar();
172 XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
173 Date actual = Time.toDate(calendar);
174 Date expected = dateForTests;
175 assertEquals(expected, actual);