added test cases to TimeTest.java
[appc.git] / appc-core / appc-common-bundle / 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 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;
39
40 import javax.xml.datatype.DatatypeConfigurationException;
41 import javax.xml.datatype.DatatypeFactory;
42 import javax.xml.datatype.XMLGregorianCalendar;
43
44 import org.junit.Test;
45
46
47 public class TimeTest {
48
49     @Test
50     public void testAddTime() {
51
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);
57
58     }
59
60     @Test
61     public void testDateOnly() {
62
63         final Date dateNow = new Date();
64         Calendar cal = Calendar.getInstance();
65         cal.setTime(dateNow);
66
67         Time.dateOnly(cal);
68
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);
73
74         assertEquals( msecFromBegin, 0);
75
76     }
77
78     @Test
79     public void testGetCurrentUTCDate() {
80
81         Date utcDate  = Time.getCurrentUTCDate();
82
83         ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
84
85         long epochSecs = utc.toEpochSecond();
86
87         long utcSecs = utcDate.getTime() / 1000;
88
89         assertEquals(epochSecs, utcSecs);
90     }
91
92     @Test
93     public void testEndOfDayLocal() {
94         final Date dateNow = new Date();
95         assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
96     }
97     
98     @Test
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);
105     }
106     
107     @Test
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);
112     }
113     
114     //this test succeeds if localTime() does not throw an exception  
115     @Test
116     public void testLocalTime() {
117        Time.localTime(1532083631);
118     }
119     
120     @Test
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);
128     }
129     
130     @Test
131     public void testStartOfDayLocal() {
132         assertTrue(Time.startOfDayLocal() instanceof Date);
133     }
134     
135     @Test
136     public void testTimeStamp() {
137         assertTrue(Time.timestamp() instanceof XMLGregorianCalendar);
138     }
139     
140     @Test
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);
145     }
146     
147     @Test
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);
152     }
153     
154     @Test
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);
159     }
160     
161     @Test
162     public void testToDate() throws ParseException, DatatypeConfigurationException{
163         Date date=new Date("02/09/2004");
164         GregorianCalendar c = new GregorianCalendar();
165         c.setTime(date);
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);
170     }
171 }