7121a82b02bd48ef7ca6ecd4455d92f11ca59632
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / util / PojoUtilsTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.util;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.time.LocalDateTime;
29 import java.time.Month;
30
31 import org.junit.Test;
32 import org.mockito.Mockito;
33
34 public class PojoUtilsTest {
35
36         @Test
37         public void testGetJsonFromObject_Clazz_MockTest() throws Exception {
38
39                 String obj = "helloWorld";
40                 PojoUtils pojoUtils = Mockito.mock(PojoUtils.class);
41                 Mockito.doCallRealMethod().when(pojoUtils).getJsonFromObject(Mockito.anyString());
42
43                 pojoUtils.getJsonFromObject(obj);
44
45                 Mockito.verify(pojoUtils, Mockito.times(1)).getJsonFromObject(Mockito.anyString(), Mockito.eq(false), Mockito.eq(true));
46         }
47
48         @Test
49         public void testGetJsonFromObject_Clazz() throws Exception {
50                 
51                 PojoUtils pojoUtils = PojoUtilsTest.getInstance();
52                 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
53
54                 String res = pojoUtils.getJsonFromObject(date);
55
56                 assertNotNull(res);
57                 assertTrue(res.contains("\"dayOfMonth\" : 18"));
58                 assertTrue(res.contains("\"dayOfWeek\" : \"MONDAY\""));
59                 assertTrue(res.contains("\"dayOfYear\" : 261"));
60                 assertTrue(res.contains("\"hour\" : 10"));
61                 assertTrue(res.contains("\"minute\" : 55"));
62                 assertTrue(res.contains("\"month\" : \"SEPTEMBER\""));
63                 assertTrue(res.contains("\"monthValue\" : 9"));
64                 assertTrue(res.contains("\"nano\" : 300"));
65                 assertTrue(res.contains("\"second\" : 0"));
66                 assertTrue(res.contains("\"year\" : 2017"));
67         }
68         
69         @Test
70         public void testGetJsonFromObject_Clazz_null() throws Exception {
71                 PojoUtils pojoUtils = PojoUtilsTest.getInstance();
72                 
73                 String res = pojoUtils.getJsonFromObject(null);
74
75                 assertNotNull(res);
76                 assertEquals("null", res);
77         }
78         
79         @Test
80         public void testGetJsonFromObject_null() throws Exception {
81                 PojoUtils pojoUtils = PojoUtilsTest.getInstance();
82                 
83                 String res = pojoUtils.getJsonFromObject(null, false, true);
84
85                 assertNotNull(res);
86                 assertEquals("null", res);
87         }
88
89         @Test
90         public void testGetJsonFromObject() throws Exception {
91                 PojoUtils pojoUtils = PojoUtilsTest.getInstance();
92                 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
93                 
94                 String res = pojoUtils.getJsonFromObject(date, false, false);
95                 assertNotNull(res);
96                 
97                 res = pojoUtils.getJsonFromObject(date, true, false);
98                 assertNotNull(res);
99                 
100                 res = pojoUtils.getJsonFromObject(date, true, true);
101                 assertNotNull(res);
102         }
103
104         static PojoUtils getInstance() {
105                 return new PojoUtils(); 
106         }
107 }