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