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-2019 IBM
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.util;
27  
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.text.ParseException;
33 import java.time.LocalDate;
34 import java.time.ZoneId;
35 import java.time.ZoneOffset;
36 import java.time.ZonedDateTime;
37 import java.util.Calendar;
38 import java.util.Date;
39 import java.util.GregorianCalendar;
40 import java.util.Locale;
41 import java.util.TimeZone;
42
43 import javax.xml.datatype.DatatypeConfigurationException;
44 import javax.xml.datatype.DatatypeFactory;
45 import javax.xml.datatype.XMLGregorianCalendar;
46
47 import org.junit.Test;
48
49 public class TimeTest {
50
51     private final Date dateForTests = Date
52             .from(LocalDate.of(2004, 2, 9).atStartOfDay(ZoneId.systemDefault()).toInstant());
53
54     @Test
55     public void testAddTime() {
56
57         final Date dateNow = new Date();
58         long dateNowMSec = dateNow.getTime();
59         Date dateSecLater = Time.addTime(dateNow, 0, 0, 0, 0, 1);
60         long dateSecLaterMSec = dateSecLater.getTime();
61         assertEquals(dateNowMSec + 1000, dateSecLaterMSec);
62
63     }
64
65     @Test
66     public void testDateOnly() {
67
68         final Date dateNow = new Date();
69         Calendar cal = Calendar.getInstance();
70         cal.setTime(dateNow);
71
72         Time.dateOnly(cal);
73
74         long msecFromBegin = cal.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 + cal.get(Calendar.MINUTE) * 60 * 1000
75                 + cal.get(Calendar.SECOND) * 1000 + cal.get(Calendar.MILLISECOND);
76
77         assertEquals(msecFromBegin, 0);
78
79     }
80
81     @Test
82     public void testGetCurrentUTCDate() {
83
84         ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
85         Date utcDate = Date.from(utc.toInstant());
86         long epochSecs = utc.toEpochSecond();
87
88         long utcSecs = utcDate.getTime() / 1000;
89
90         assertEquals(epochSecs, utcSecs);
91     }
92
93     @Test
94     public void testEndOfDayLocal() {
95         final Date dateNow = new Date();
96         assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
97     }
98
99     @Test
100     public void testGetDateByLocaleAndTimeZone() {
101         final Date dateNow = new Date();
102         Locale locale = new Locale("fr");
103         TimeZone timeZone = TimeZone.getTimeZone("Europe/France");
104         assertNotNull(Time.getDateByLocaleAndTimeZone(dateNow, locale, timeZone));
105         assertTrue(Time.getDateByLocaleAndTimeZone(dateNow, locale, timeZone) instanceof String);
106     }
107
108     @Test
109     public void testUtcFormat() {
110         final Date date = new Date();
111         assertNotNull(Time.utcFormat(date));
112         assertTrue(Time.utcFormat(date) instanceof String);
113     }
114
115     // this test succeeds if localTime() does not throw an exception
116     @Test
117     public void testLocalTime() {
118         Time.localTime(1532083631);
119     }
120
121     @Test
122     public void testSetDate() {
123         Calendar cal = Calendar.getInstance();
124         cal.set(Calendar.YEAR, 2018);
125         cal.set(Calendar.MONTH, 07);
126         cal.set(Calendar.DAY_OF_MONTH, 03);
127         Calendar cal1 = Time.setDate(cal, 2018, 07, 03);
128         assertEquals(cal, cal1);
129     }
130
131     @Test
132     public void testStartOfDayLocal() {
133         assertTrue(Time.startOfDayLocal() instanceof Date);
134     }
135
136     @Test
137     public void testTimeStamp() {
138         assertTrue(Time.timestamp() instanceof XMLGregorianCalendar);
139     }
140
141     @Test
142     public void testDateToStringConverterMillis() {
143         String dateString = Time.dateToStringConverterMillis(dateForTests);
144         String expected = "2004-02-09 00:00:00:000";
145         assertEquals(expected, dateString);
146     }
147
148     @Test
149     public void testStringToDateConverterMillis() throws ParseException {
150         Date date = Time.stringToDateConverterMillis("2004-02-09 00:00:00:000");
151         Date expected = dateForTests;
152         assertEquals(expected, date);
153     }
154
155     @Test
156     public void testTruncateDate() throws ParseException {
157         Date date = Time.truncDate(dateForTests);
158         Date expected = dateForTests;
159         assertEquals(expected, date);
160     }
161
162     @Test
163     public void testToDate() throws ParseException, DatatypeConfigurationException {
164         Date date = dateForTests;
165         GregorianCalendar c = new GregorianCalendar();
166         c.setTime(date);
167         XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
168         Date actual = Time.toDate(calendar);
169         Date expected = dateForTests;
170         assertEquals(expected, actual);
171     }
172
173     @Test
174     public void testUtcTime() {
175         assertTrue(Time.utcTime()!=0);
176     }
177
178     @Test
179     public void testCurrentUTCDate() {
180         assertTrue(Time.getCurrentUTCDate() instanceof Date);
181     }
182 }