b7f71d2b43e546fa4b43de4d05371816a44d81b5
[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 package org.onap.aai.util;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.text.DateFormat;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.List;
34 import java.util.TimeZone;
35 import java.util.concurrent.TimeUnit;
36
37 import org.junit.Test;
38
39 public class AAIUtilsTest {
40
41         @Test
42         public void testNullCheckWithNull() {
43                 List<String> newList = null;
44                 Iterable<String> res = AAIUtils.nullCheck(newList);
45                 assertNotNull("nullCheck() should return empty list", res);
46                 assertEquals(Collections.<String>emptyList(), res);
47         }
48
49         @Test
50         public void testNullCheckWithList() {
51                 List<String> newList = new ArrayList<String>();
52                 newList.add("testString");
53
54                 Iterable<String> res = AAIUtils.nullCheck(newList);
55
56                 assertNotNull("nullCheck() should return back list", res);
57                 assertEquals(newList, res);
58         }
59
60         @Test
61         public void testGenDate() {
62
63                 Date d1 = new Date(0);
64
65                 DateFormat formatter = new SimpleDateFormat("YYMMdd-HH:mm:ss:SSS");
66                 formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
67                 formatter.setLenient(false);
68
69                 Date d2 = null;
70
71                 try {
72                         d2 = formatter.parse(AAIUtils.genDate());
73                 } catch (ParseException e) {
74                         fail("Date parsing exception");
75                         e.printStackTrace();
76                 }
77
78                 try {
79                         TimeUnit.SECONDS.sleep(1);
80                 } catch (InterruptedException e1) {}
81
82                 Date d3 = new Date();
83
84                 assertTrue("Generated date is not after a past date", d2.after(d1));
85                 assertTrue("Generated date is not before a future date", d2.before(d3));
86         }
87
88 }