Fix intermittent time based test failures
[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  * 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
27 package org.onap.appc.util;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
32
33 import java.text.ParseException;
34 import java.time.LocalDate;
35 import java.time.ZoneId;
36 import java.time.ZoneOffset;
37 import java.time.ZonedDateTime;
38 import java.util.Calendar;
39 import java.util.Date;
40 import java.util.GregorianCalendar;
41 import java.util.Locale;
42 import java.util.TimeZone;
43
44 import javax.xml.datatype.DatatypeConfigurationException;
45 import javax.xml.datatype.DatatypeFactory;
46 import javax.xml.datatype.XMLGregorianCalendar;
47
48 import org.junit.Test;
49
50
51 public class TimeTest {
52
53     private final Date dateForTests = Date.from(LocalDate.of(2004, 2, 9)
54             .atStartOfDay(ZoneId.systemDefault()).toInstant());
55
56
57     @Test
58     public void testAddTime() {
59
60         final Date dateNow = new Date();
61         long dateNowMSec = dateNow.getTime();
62         Date dateSecLater = Time.addTime(dateNow, 0, 0, 0, 0, 1);
63         long dateSecLaterMSec = dateSecLater.getTime();
64         assertEquals(dateNowMSec + 1000, dateSecLaterMSec);
65
66     }
67
68     @Test
69     public void testDateOnly() {
70
71         final Date dateNow = new Date();
72         Calendar cal = Calendar.getInstance();
73         cal.setTime(dateNow);
74
75         Time.dateOnly(cal);
76
77         long msecFromBegin = cal.get(Calendar.HOUR_OF_DAY)*60*60*1000 +
78             cal.get(Calendar.MINUTE)*60*1000 +
79             cal.get(Calendar.SECOND)*1000 +
80             cal.get(Calendar.MILLISECOND);
81
82         assertEquals( msecFromBegin, 0);
83
84     }
85
86     @Test
87     public void testGetCurrentUTCDate() {
88
89         ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
90         Date utcDate = Date.from(utc.toInstant());
91         long epochSecs = utc.toEpochSecond();
92
93         long utcSecs = utcDate.getTime() / 1000;
94
95         assertEquals(epochSecs, utcSecs);
96     }
97
98     @Test
99     public void testEndOfDayLocal() {
100         final Date dateNow = new Date();
101         assertTrue(Time.endOfDayLocal(dateNow) instanceof Date);
102     }
103
104     @Test
105     public void testGetDateByLocaleAndTimeZone() {
106        final Date dateNow = new Date();
107        Locale locale = new Locale("fr"); 
108        TimeZone timeZone = TimeZone.getTimeZone("Europe/France");
109        assertNotNull(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone));
110        assertTrue(Time.getDateByLocaleAndTimeZone(dateNow,locale,timeZone) instanceof String);
111     }
112
113     @Test
114     public void testUtcFormat() {
115        final Date date = new Date();
116        assertNotNull(Time.utcFormat(date));
117        assertTrue(Time.utcFormat(date) instanceof String);
118     }
119
120     //this test succeeds if localTime() does not throw an exception  
121     @Test
122     public void testLocalTime() {
123        Time.localTime(1532083631);
124     }
125
126     @Test
127     public void testSetDate() {
128         Calendar cal = Calendar.getInstance();
129         cal.set(Calendar.YEAR, 2018);
130         cal.set(Calendar.MONTH, 07);
131         cal.set(Calendar.DAY_OF_MONTH, 03);
132         Calendar cal1 = Time.setDate(cal, 2018, 07, 03);
133         assertEquals(cal, cal1);
134     }
135
136     @Test
137     public void testStartOfDayLocal() {
138         assertTrue(Time.startOfDayLocal() instanceof Date);
139     }
140
141     @Test
142     public void testTimeStamp() {
143         assertTrue(Time.timestamp() instanceof XMLGregorianCalendar);
144     }
145
146     @Test
147     public void testDateToStringConverterMillis() {
148         String dateString = Time.dateToStringConverterMillis(dateForTests);
149         String expected = "2004-02-09 00:00:00:000";
150         assertEquals(expected, dateString);
151     }
152
153     @Test
154     public void testStringToDateConverterMillis() throws ParseException{
155         Date date = Time.stringToDateConverterMillis("2004-02-09 00:00:00:000");
156         Date expected = dateForTests;
157         assertEquals(expected, date);
158     }
159
160     @Test
161     public void testTruncateDate() throws ParseException{
162         Date date = Time.truncDate(dateForTests);
163         Date expected = dateForTests;
164         assertEquals(expected, date);
165     }
166
167     @Test
168     public void testToDate() throws ParseException, DatatypeConfigurationException{
169         Date date = dateForTests;
170         GregorianCalendar c = new GregorianCalendar();
171         c.setTime(date);
172         XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
173         Date actual = Time.toDate(calendar);
174         Date expected = dateForTests;
175         assertEquals(expected, actual);
176     }
177 }