[AAF-23] Updated Copyright Headers for AAF Inno
[aaf/inno.git] / rosetta / src / test / java / com / data / test / JU_Stream2Obj.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.data.test;\r
24 \r
25 import java.io.IOException;\r
26 import java.io.Reader;\r
27 import java.io.StringReader;\r
28 import java.io.StringWriter;\r
29 \r
30 import org.junit.Test;\r
31 \r
32 import com.att.inno.env.APIException;\r
33 import com.att.inno.env.Data;\r
34 import com.att.inno.env.DataFactory;\r
35 import com.att.inno.env.EnvJAXB;\r
36 import com.att.inno.env.impl.BasicEnv;\r
37 import com.att.rosetta.InJson;\r
38 import com.att.rosetta.InXML;\r
39 import com.att.rosetta.Out;\r
40 import com.att.rosetta.OutJson;\r
41 import com.att.rosetta.OutRaw;\r
42 import com.att.rosetta.OutXML;\r
43 import com.att.rosetta.Parse;\r
44 import com.att.rosetta.ParseException;\r
45 \r
46 import inherit.DerivedA;\r
47 import inherit.Root;\r
48 \r
49 public class JU_Stream2Obj {\r
50 \r
51         /*\r
52         <?xml version="1.0" encoding=Config.UTF-8 standalone="yes"?>\r
53         <root xmlns="urn:inherit">\r
54           <base xsi:type="derivedA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
55             <name>myDerivedA_1</name>\r
56             <num>1432</num>\r
57             <shortName>mda_1</shortName>\r
58             <value>value_1</value>\r
59             <value>value_2</value>\r
60           </base>\r
61         </root>\r
62         \r
63         {"base":[{"__extension":"derivedA","name":"myDerivedA_1","num":1432,"shortName":"mda_1","value":["value_1","value_2"]}]}\r
64         */\r
65 \r
66         @Test\r
67         public void json2Obj() throws APIException, SecurityException, NoSuchFieldException, ClassNotFoundException, ParseException, IOException {\r
68                 DerivedA da = new DerivedA();\r
69                 da.setName("myDerivedA_1");\r
70                 da.setNum((short)1432);\r
71                 da.setShortName("mda_1");\r
72                 da.getValue().add("value_1");\r
73                 da.getValue().add("value_2");\r
74                 \r
75                 Root root = new Root();\r
76                 root.getBase().add(da);\r
77 \r
78                 da = new DerivedA();\r
79                 da.setName("myDerivedA_2");\r
80                 da.setNum((short)1432);\r
81                 da.setShortName("mda_2");\r
82                 da.getValue().add("value_2.1");\r
83                 da.getValue().add("value_2.2");\r
84                 root.getBase().add(da);\r
85                 \r
86                 EnvJAXB env = new BasicEnv();\r
87                 DataFactory<Root> rootDF = env.newDataFactory(Root.class);\r
88                 \r
89                 String xml = rootDF.newData(env).out(Data.TYPE.XML).load(root).option(Data.PRETTY).asString();\r
90                 System.out.println(xml);\r
91 \r
92                 InXML inXML;\r
93                 Parse<Reader,?> in = inXML = new InXML(Root.class);\r
94                 Out out = new OutRaw();\r
95 \r
96                 StringWriter sw = new StringWriter();\r
97                 out.extract(new StringReader(xml), sw, in);\r
98                 System.out.println(sw.toString());\r
99 \r
100                 \r
101                 out = new OutJson();\r
102 \r
103                 sw = new StringWriter();\r
104                 out.extract(new StringReader(xml), sw, in);\r
105                 String json;\r
106                 System.out.println(json = sw.toString());\r
107                 \r
108                 in = new InJson();\r
109                 out = new OutRaw();\r
110 \r
111                 sw = new StringWriter();\r
112                 out.extract(new StringReader(json), sw, in);\r
113                 System.out.println(sw.toString());\r
114                 \r
115                 out = new OutXML(inXML);\r
116 \r
117                 sw = new StringWriter();\r
118                 out.extract(new StringReader(json), sw, in, true);\r
119                 System.out.println(sw.toString());\r
120 \r
121                 System.out.flush();\r
122 \r
123         }\r
124 \r
125 }\r