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 org.junit.Test;
33 import java.time.ZoneOffset;
34 import java.time.ZonedDateTime;
35 import java.util.Calendar;
36 import java.util.Date;
37 import java.util.Locale;
38 import java.util.TimeZone;
41 public class TimeTest {
44 public void testAddTime() {
46 final Date dateNow = new Date();
47 long dateNowMSec = dateNow.getTime();
48 Date dateSecLater = Time.addTime(dateNow, 0, 0, 0, 0, 1);
49 long dateSecLaterMSec = dateSecLater.getTime();
50 assertEquals(dateNowMSec + 1000, dateSecLaterMSec);
55 public void testDateOnly() {
57 final Date dateNow = new Date();
58 Calendar cal = Calendar.getInstance();
63 long msecFromBegin = cal.get(Calendar.HOUR_OF_DAY)*60*60*1000 +
64 cal.get(Calendar.MINUTE)*60*1000 +
65 cal.get(Calendar.SECOND)*1000 +
66 cal.get(Calendar.MILLISECOND);
68 assertEquals( msecFromBegin, 0);
73 public void testGetCurrentUTCDate() {
75 Date utcDate = Time.getCurrentUTCDate();
77 ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
79 long epochSecs = utc.toEpochSecond();
81 long utcSecs = utcDate.getTime() / 1000;
83 assertEquals(epochSecs, utcSecs);
87 public void testEndOfDayLocal() {
88 final Date dateNow = new Date();
89 assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
93 public void testGetDateByLocaleAndTimeZone() {
94 final Date dateNow = new Date("19-Jul-2018");
95 Locale locale = new Locale("fr");
96 TimeZone timeZone = TimeZone.getTimeZone("Europe/France");
97 String expected="19 juil. 2018 00:00:00";
98 assertEquals(expected,Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone));
102 public void testUtcFormat() {
103 final Date date = new Date("19-Jul-2018");
104 String expected="07/19/2018 00:00:00";
105 assertEquals(expected,Time.utcFormat(date));
109 public void testLocalTime() {
110 long expected=1532083631;
111 assertEquals(expected,Time.localTime(1532083631));
115 public void testSetDate() {
116 Calendar cal = Calendar.getInstance();
117 cal.set(Calendar.YEAR, 2018);
118 cal.set(Calendar.MONTH, 07);
119 cal.set(Calendar.DAY_OF_MONTH, 03);
120 Calendar cal1= Time.setDate(cal, 2018, 07, 03);
121 assertEquals(cal, cal1);
125 public void testStartOfDayLocal() {
126 assertTrue(Time.startOfDayLocal() instanceof Date);