added few more test cases to TimeTest.java
[appc.git] / appc-common / src / test / java / org / onap / appc / util / TimeTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24
25 package org.onap.appc.util;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import org.junit.Test;
32
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;
39
40
41 public class TimeTest {
42
43     @Test
44     public void testAddTime() {
45
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);
51
52     }
53
54     @Test
55     public void testDateOnly() {
56
57         final Date dateNow = new Date();
58         Calendar cal = Calendar.getInstance();
59         cal.setTime(dateNow);
60
61         Time.dateOnly(cal);
62
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);
67
68         assertEquals( msecFromBegin, 0);
69
70     }
71
72     @Test
73     public void testGetCurrentUTCDate() {
74
75         Date utcDate  = Time.getCurrentUTCDate();
76
77         ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
78
79         long epochSecs = utc.toEpochSecond();
80
81         long utcSecs = utcDate.getTime() / 1000;
82
83         assertEquals(epochSecs, utcSecs);
84     }
85
86     @Test
87     public void testEndOfDayLocal() {
88         final Date dateNow = new Date();
89         assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
90     }
91     
92     @Test
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));
99     }
100     
101     @Test
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));
106     }
107     
108     @Test
109     public void testLocalTime() {
110        long expected=1532083631; 
111        assertEquals(expected,Time.localTime(1532083631));
112     }
113     
114     @Test
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);
122     }
123     
124     @Test
125     public void testStartOfDayLocal() {
126         assertTrue(Time.startOfDayLocal() instanceof Date);
127     }
128 }