2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.policy.appc.util;
23 import static org.junit.Assert.*;
25 import java.time.Instant;
26 import java.time.ZoneId;
27 import java.time.ZonedDateTime;
29 import org.junit.Test;
31 public class TestSerialization {
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);
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);
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());
53 assertNull(Serialization.gsonPretty.fromJson("oz time is weird", ZonedDateTime.class));