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 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
25 package org.onap.appc.util;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import java.text.ParseException;
32 import java.time.ZoneOffset;
33 import java.time.ZonedDateTime;
34 import java.util.Calendar;
35 import java.util.Date;
36 import java.util.Locale;
37 import java.util.TimeZone;
39 import javax.xml.datatype.XMLGregorianCalendar;
41 import org.junit.Test;
44 public class TimeTest {
47 public void testAddTime() {
49 final Date dateNow = new Date();
50 long dateNowMSec = dateNow.getTime();
51 Date dateSecLater = Time.addTime(dateNow, 0, 0, 0, 0, 1);
52 long dateSecLaterMSec = dateSecLater.getTime();
53 assertEquals(dateNowMSec + 1000, dateSecLaterMSec);
58 public void testDateOnly() {
60 final Date dateNow = new Date();
61 Calendar cal = Calendar.getInstance();
66 long msecFromBegin = cal.get(Calendar.HOUR_OF_DAY)*60*60*1000 +
67 cal.get(Calendar.MINUTE)*60*1000 +
68 cal.get(Calendar.SECOND)*1000 +
69 cal.get(Calendar.MILLISECOND);
71 assertEquals( msecFromBegin, 0);
76 public void testGetCurrentUTCDate() {
78 Date utcDate = Time.getCurrentUTCDate();
80 ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
82 long epochSecs = utc.toEpochSecond();
84 long utcSecs = utcDate.getTime() / 1000;
86 assertEquals(epochSecs, utcSecs);
90 public void testEndOfDayLocal() {
91 final Date dateNow = new Date();
92 assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
96 public void testGetDateByLocaleAndTimeZone() {
97 final Date dateNow = new Date("19-Jul-2018");
98 Locale locale = new Locale("fr");
99 TimeZone timeZone = TimeZone.getTimeZone("Europe/France");
100 assertNotNull(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone));
101 assertTrue(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone) instanceof String);
105 public void testUtcFormat() {
106 final Date date = new Date("19-Jul-2018");
107 assertNotNull(Time.utcFormat(date));
108 assertTrue(Time.utcFormat(date) instanceof String);
111 //this test succeeds if localTime() does not throw an exception
113 public void testLocalTime() {
114 Time.localTime(1532083631);
118 public void testSetDate() {
119 Calendar cal = Calendar.getInstance();
120 cal.set(Calendar.YEAR, 2018);
121 cal.set(Calendar.MONTH, 07);
122 cal.set(Calendar.DAY_OF_MONTH, 03);
123 Calendar cal1= Time.setDate(cal, 2018, 07, 03);
124 assertEquals(cal, cal1);
128 public void testStartOfDayLocal() {
129 assertTrue(Time.startOfDayLocal() instanceof Date);
133 public void testTimeStamp() {
134 assertTrue(Time.timestamp() instanceof XMLGregorianCalendar);
138 public void testDateToStringConverterMillis() {
139 String dateString=Time.dateToStringConverterMillis(new Date("02/09/2004"));
140 String expected="2004-02-09 00:00:00:000";
141 assertEquals(expected, dateString);
145 public void testStringToDateConverterMillis() throws ParseException{
146 Date date=Time.stringToDateConverterMillis("2004-02-09 00:00:00:000");
147 Date expected=new Date("02/09/2004");
148 assertEquals(expected, date);