Changes for Checkstyle 8.32
[policy/models.git] / models-interactions / model-impl / appc / src / test / java / org / onap / policy / appc / util / SerializationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * appc
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.appc.util;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26
27 import java.time.Instant;
28 import java.time.ZoneId;
29 import java.time.ZonedDateTime;
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 }