2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.util;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
26 import java.time.LocalDateTime;
27 import java.time.Month;
29 import org.junit.Test;
30 import org.mockito.Mockito;
32 public class PojoUtilsTest {
35 public void testGetJsonFromObject_Clazz_MockTest() throws Exception {
37 String obj = "helloWorld";
38 PojoUtils pojoUtils = Mockito.mock(PojoUtils.class);
39 Mockito.doCallRealMethod().when(pojoUtils).getJsonFromObject(Mockito.anyString());
41 pojoUtils.getJsonFromObject(obj);
43 Mockito.verify(pojoUtils, Mockito.times(1)).getJsonFromObject(Mockito.anyString(), Mockito.eq(false), Mockito.eq(true));
47 public void testGetJsonFromObject_Clazz() throws Exception {
49 PojoUtils pojoUtils = PojoUtilsTest.getInstance();
50 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
52 String res = pojoUtils.getJsonFromObject(date);
55 assertTrue(res.contains("\"dayOfMonth\" : 18"));
56 assertTrue(res.contains("\"dayOfWeek\" : \"MONDAY\""));
57 assertTrue(res.contains("\"dayOfYear\" : 261"));
58 assertTrue(res.contains("\"hour\" : 10"));
59 assertTrue(res.contains("\"minute\" : 55"));
60 assertTrue(res.contains("\"month\" : \"SEPTEMBER\""));
61 assertTrue(res.contains("\"monthValue\" : 9"));
62 assertTrue(res.contains("\"nano\" : 300"));
63 assertTrue(res.contains("\"second\" : 0"));
64 assertTrue(res.contains("\"year\" : 2017"));
68 public void testGetJsonFromObject_Clazz_null() throws Exception {
69 PojoUtils pojoUtils = PojoUtilsTest.getInstance();
71 String res = pojoUtils.getJsonFromObject(null);
74 assertEquals("null", res);
78 public void testGetJsonFromObject_null() throws Exception {
79 PojoUtils pojoUtils = PojoUtilsTest.getInstance();
81 String res = pojoUtils.getJsonFromObject(null, false, true);
84 assertEquals("null", res);
88 public void testGetJsonFromObject() throws Exception {
89 PojoUtils pojoUtils = PojoUtilsTest.getInstance();
90 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
92 String res = pojoUtils.getJsonFromObject(date, false, false);
95 res = pojoUtils.getJsonFromObject(date, true, false);
98 res = pojoUtils.getJsonFromObject(date, true, true);
102 static PojoUtils getInstance() {
103 return new PojoUtils();