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.GregorianCalendar;
37 import java.util.Locale;
38 import java.util.TimeZone;
40 import javax.xml.datatype.DatatypeConfigurationException;
41 import javax.xml.datatype.DatatypeFactory;
42 import javax.xml.datatype.XMLGregorianCalendar;
44 import org.junit.Test;
47 public class TimeTest {
50 public void testAddTime() {
52 final Date dateNow = new Date();
53 long dateNowMSec = dateNow.getTime();
54 Date dateSecLater = Time.addTime(dateNow, 0, 0, 0, 0, 1);
55 long dateSecLaterMSec = dateSecLater.getTime();
56 assertEquals(dateNowMSec + 1000, dateSecLaterMSec);
61 public void testDateOnly() {
63 final Date dateNow = new Date();
64 Calendar cal = Calendar.getInstance();
69 long msecFromBegin = cal.get(Calendar.HOUR_OF_DAY)*60*60*1000 +
70 cal.get(Calendar.MINUTE)*60*1000 +
71 cal.get(Calendar.SECOND)*1000 +
72 cal.get(Calendar.MILLISECOND);
74 assertEquals( msecFromBegin, 0);
79 public void testGetCurrentUTCDate() {
81 Date utcDate = Time.getCurrentUTCDate();
83 ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
85 long epochSecs = utc.toEpochSecond();
87 long utcSecs = utcDate.getTime() / 1000;
89 assertEquals(epochSecs, utcSecs);
93 public void testEndOfDayLocal() {
94 final Date dateNow = new Date();
95 assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
99 public void testGetDateByLocaleAndTimeZone() {
100 final Date dateNow = new Date("19-Jul-2018");
101 Locale locale = new Locale("fr");
102 TimeZone timeZone = TimeZone.getTimeZone("Europe/France");
103 assertNotNull(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone));
104 assertTrue(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone) instanceof String);
108 public void testUtcFormat() {
109 final Date date = new Date("19-Jul-2018");
110 assertNotNull(Time.utcFormat(date));
111 assertTrue(Time.utcFormat(date) instanceof String);
114 //this test succeeds if localTime() does not throw an exception
116 public void testLocalTime() {
117 Time.localTime(1532083631);
121 public void testSetDate() {
122 Calendar cal = Calendar.getInstance();
123 cal.set(Calendar.YEAR, 2018);
124 cal.set(Calendar.MONTH, 07);
125 cal.set(Calendar.DAY_OF_MONTH, 03);
126 Calendar cal1= Time.setDate(cal, 2018, 07, 03);
127 assertEquals(cal, cal1);
131 public void testStartOfDayLocal() {
132 assertTrue(Time.startOfDayLocal() instanceof Date);
136 public void testTimeStamp() {
137 assertTrue(Time.timestamp() instanceof XMLGregorianCalendar);
141 public void testDateToStringConverterMillis() {
142 String dateString=Time.dateToStringConverterMillis(new Date("02/09/2004"));
143 String expected="2004-02-09 00:00:00:000";
144 assertEquals(expected, dateString);
148 public void testStringToDateConverterMillis() throws ParseException{
149 Date date=Time.stringToDateConverterMillis("2004-02-09 00:00:00:000");
150 Date expected=new Date("02/09/2004");
151 assertEquals(expected, date);
155 public void testTruncateDate() throws ParseException{
156 Date date=Time.truncDate(new Date("02/09/2004"));
157 Date expected=new Date("02/09/2004");
158 assertEquals(expected, date);
162 public void testToDate() throws ParseException, DatatypeConfigurationException{
163 Date date=new Date("02/09/2004");
164 GregorianCalendar c = new GregorianCalendar();
166 XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
167 Date actual= Time.toDate(calendar);
168 Date expected=new Date("02/09/2004");
169 assertEquals(expected, actual);