c7167f509e0fc45aaf4315dfbb51be98f3cfeff7
[policy/drools-applications.git] /
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.applcm.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 import org.onap.policy.appclcm.util.Serialization;
31
32 public class TestSerialization {
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 instant0 = Instant.ofEpochMilli(1516127215000L);
43                 String instantString0 = Serialization.gsonPretty.toJson(instant0, Instant.class);
44                 assertEquals("\"2018-01-16T18:26:55Z\"", instantString0);
45                 Instant outInstant0 = Serialization.gsonPretty.fromJson(instantString0, Instant.class);
46                 assertEquals(instant0, outInstant0);
47                 
48                 Instant instant1 = Instant.ofEpochMilli(1516127215000L);
49                 String instantString1 = Serialization.gsonJunit.toJson(instant1, Instant.class);
50                 assertEquals("1516127215000", instantString1);
51                 Instant outInstant1 = Serialization.gsonJunit.fromJson(instantString1, Instant.class);
52                 assertEquals(instant1, outInstant1);
53                 
54                 ZonedDateTime zdt = ZonedDateTime.ofInstant(instant0, ZoneId.of("UTC"));
55                 String zdtString = Serialization.gsonPretty.toJson(zdt, ZonedDateTime.class);
56                 assertEquals("{\n  \"dateTime\": {\n    \"date\":", zdtString.substring(0, 29));
57         }
58 }