Fix Sonar Technical Debt, Unit Test for APPC POJOs
[policy/drools-applications.git] / controlloop / common / model-impl / appc / src / test / java / org / onap / policy / appc / util / TestSerialization.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * appc
4  * ================================================================================
5  * Copyright (C) 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
21 package org.onap.policy.appc.util;
22
23 import static org.junit.Assert.*;
24
25 import java.time.Instant;
26 import java.time.ZoneId;
27 import java.time.ZonedDateTime;
28
29 import org.junit.Test;
30
31 public class TestSerialization {
32
33         @Test
34         public void test() {
35                 String nameString = "Dorothy";
36                 String jsonName = Serialization.gsonPretty.toJson(nameString, String.class);
37                 assertEquals("\"Dorothy\"", jsonName);
38                 String jsonInOutName = Serialization.gsonPretty.fromJson(jsonName, String.class);
39                 assertEquals("Dorothy", jsonInOutName);
40                 
41                 Instant instant = Instant.ofEpochMilli(1516127215000L);
42                 String instantString = Serialization.gsonPretty.toJson(instant, Instant.class);
43                 assertEquals("1516127215000", instantString);
44                 Instant outInstant = Serialization.gsonPretty.fromJson(instantString, Instant.class);
45                 assertEquals(instant, outInstant);
46                 
47                 ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
48                 String zdtString = Serialization.gsonPretty.toJson(zdt, ZonedDateTime.class);
49                 assertEquals("\"2018-01-16 18:26:55.000000+00:00\"", zdtString);
50                 ZonedDateTime outZdt = Serialization.gsonPretty.fromJson(zdtString, ZonedDateTime.class);
51                 assertEquals(zdt.getDayOfWeek(), outZdt.getDayOfWeek());
52                 
53                 assertNull(Serialization.gsonPretty.fromJson("oz time is weird", ZonedDateTime.class));
54         }
55 }