AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / util / AAIUtilsTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.util;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.text.DateFormat;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Date;
34 import java.util.List;
35 import java.util.TimeZone;
36 import java.util.concurrent.TimeUnit;
37
38 import org.junit.Test;
39
40 public class AAIUtilsTest {
41
42     @Test
43     public void testNullCheckWithNull() {
44         List<String> newList = null;
45         Iterable<String> res = AAIUtils.nullCheck(newList);
46         assertNotNull("nullCheck() should return empty list", res);
47         assertEquals(Collections.<String>emptyList(), res);
48     }
49
50     @Test
51     public void testNullCheckWithList() {
52         List<String> newList = new ArrayList<String>();
53         newList.add("testString");
54
55         Iterable<String> res = AAIUtils.nullCheck(newList);
56
57         assertNotNull("nullCheck() should return back list", res);
58         assertEquals(newList, res);
59     }
60
61     @Test
62     public void testGenDate() {
63
64         Date d1 = new Date(0);
65
66         DateFormat formatter = new SimpleDateFormat("YYMMdd-HH:mm:ss:SSS");
67         formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
68         formatter.setLenient(false);
69
70         Date d2 = null;
71
72         try {
73             d2 = formatter.parse(AAIUtils.genDate());
74         } catch (ParseException e) {
75             fail("Date parsing exception");
76             e.printStackTrace();
77         }
78
79         try {
80             TimeUnit.SECONDS.sleep(1);
81         } catch (InterruptedException e1) {
82         }
83
84         Date d3 = new Date();
85
86         assertTrue("Generated date is not after a past date", d2.after(d1));
87         assertTrue("Generated date is not before a future date", d2.before(d3));
88     }
89
90 }