961a62c0a94becc6aadb1c4808c28d2abe988246
[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.Locale;
37 import java.util.TimeZone;
38
39 import javax.xml.datatype.XMLGregorianCalendar;
40
41 import org.junit.Test;
42
43
44 public class TimeTest {
45
46     @Test
47     public void testAddTime() {
48
49         final Date dateNow = new Date();
50         long dateNowMSec = dateNow.getTime();
51         Date dateSecLater = Time.addTime(dateNow, 0, 0, 0, 0, 1);
52         long dateSecLaterMSec = dateSecLater.getTime();
53         assertEquals(dateNowMSec + 1000, dateSecLaterMSec);
54
55     }
56
57     @Test
58     public void testDateOnly() {
59
60         final Date dateNow = new Date();
61         Calendar cal = Calendar.getInstance();
62         cal.setTime(dateNow);
63
64         Time.dateOnly(cal);
65
66         long msecFromBegin = cal.get(Calendar.HOUR_OF_DAY)*60*60*1000 +
67             cal.get(Calendar.MINUTE)*60*1000 +
68             cal.get(Calendar.SECOND)*1000 +
69             cal.get(Calendar.MILLISECOND);
70
71         assertEquals( msecFromBegin, 0);
72
73     }
74
75     @Test
76     public void testGetCurrentUTCDate() {
77
78         Date utcDate  = Time.getCurrentUTCDate();
79
80         ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
81
82         long epochSecs = utc.toEpochSecond();
83
84         long utcSecs = utcDate.getTime() / 1000;
85
86         assertEquals(epochSecs, utcSecs);
87     }
88
89     @Test
90     public void testEndOfDayLocal() {
91         final Date dateNow = new Date();
92         assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
93     }
94     
95     @Test
96     public void testGetDateByLocaleAndTimeZone() {
97        final Date dateNow = new Date("19-Jul-2018");
98        Locale locale = new Locale("fr"); 
99        TimeZone timeZone = TimeZone.getTimeZone("Europe/France");
100        assertNotNull(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone));
101        assertTrue(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone) instanceof String);
102     }
103     
104     @Test
105     public void testUtcFormat() {
106        final Date date = new Date("19-Jul-2018");
107        assertNotNull(Time.utcFormat(date));
108        assertTrue(Time.utcFormat(date) instanceof String);
109     }
110     
111     //this test succeeds if localTime() does not throw an exception  
112     @Test
113     public void testLocalTime() {
114        Time.localTime(1532083631);
115     }
116     
117     @Test
118     public void testSetDate() {
119         Calendar cal = Calendar.getInstance();
120         cal.set(Calendar.YEAR, 2018);
121         cal.set(Calendar.MONTH, 07);
122         cal.set(Calendar.DAY_OF_MONTH, 03);
123         Calendar cal1= Time.setDate(cal, 2018, 07, 03);
124         assertEquals(cal, cal1);
125     }
126     
127     @Test
128     public void testStartOfDayLocal() {
129         assertTrue(Time.startOfDayLocal() instanceof Date);
130     }
131     
132     @Test
133     public void testTimeStamp() {
134         assertTrue(Time.timestamp() instanceof XMLGregorianCalendar);
135     }
136     
137     @Test
138     public void testDateToStringConverterMillis() {
139         String dateString=Time.dateToStringConverterMillis(new Date("02/09/2004"));
140         String expected="2004-02-09 00:00:00:000";
141         assertEquals(expected, dateString);
142     }
143     
144     @Test
145     public void testStringToDateConverterMillis() throws ParseException{
146         Date date=Time.stringToDateConverterMillis("2004-02-09 00:00:00:000");
147         Date expected=new Date("02/09/2004");
148         assertEquals(expected, date);
149     }
150 }