Add Notice of aaf/inno source moving to aaf/authz
[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 import org.onap.aaf.inno.env.APIException;\r
32 import org.onap.aaf.inno.env.Data;\r
33 import org.onap.aaf.inno.env.DataFactory;\r
34 import org.onap.aaf.inno.env.EnvJAXB;\r
35 import org.onap.aaf.inno.env.impl.BasicEnv;\r
36 import org.onap.aaf.rosetta.InJson;\r
37 import org.onap.aaf.rosetta.InXML;\r
38 import org.onap.aaf.rosetta.Out;\r
39 import org.onap.aaf.rosetta.OutJson;\r
40 import org.onap.aaf.rosetta.OutRaw;\r
41 import org.onap.aaf.rosetta.OutXML;\r
42 import org.onap.aaf.rosetta.Parse;\r
43 import org.onap.aaf.rosetta.ParseException;\r
44 \r
45 import inherit.DerivedA;\r
46 import inherit.Root;\r
47 \r
48 public class JU_Stream2Obj {\r
49 \r
50         /*\r
51         <?xml version="1.0" encoding=Config.UTF-8 standalone="yes"?>\r
52         <root xmlns="urn:inherit">\r
53           <base xsi:type="derivedA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\r
54             <name>myDerivedA_1</name>\r
55             <num>1432</num>\r
56             <shortName>mda_1</shortName>\r
57             <value>value_1</value>\r
58             <value>value_2</value>\r
59           </base>\r
60         </root>\r
61         \r
62         {"base":[{"__extension":"derivedA","name":"myDerivedA_1","num":1432,"shortName":"mda_1","value":["value_1","value_2"]}]}\r
63         */\r
64 \r
65         @Test\r
66         public void json2Obj() throws APIException, SecurityException, NoSuchFieldException, ClassNotFoundException, ParseException, IOException {\r
67                 DerivedA da = new DerivedA();\r
68                 da.setName("myDerivedA_1");\r
69                 da.setNum((short)1432);\r
70                 da.setShortName("mda_1");\r
71                 da.getValue().add("value_1");\r
72                 da.getValue().add("value_2");\r
73                 \r
74                 Root root = new Root();\r
75                 root.getBase().add(da);\r
76 \r
77                 da = new DerivedA();\r
78                 da.setName("myDerivedA_2");\r
79                 da.setNum((short)1432);\r
80                 da.setShortName("mda_2");\r
81                 da.getValue().add("value_2.1");\r
82                 da.getValue().add("value_2.2");\r
83                 root.getBase().add(da);\r
84                 \r
85                 EnvJAXB env = new BasicEnv();\r
86                 DataFactory<Root> rootDF = env.newDataFactory(Root.class);\r
87                 \r
88                 String xml = rootDF.newData(env).out(Data.TYPE.XML).load(root).option(Data.PRETTY).asString();\r
89                 System.out.println(xml);\r
90 \r
91                 InXML inXML;\r
92                 Parse<Reader,?> in = inXML = new InXML(Root.class);\r
93                 Out out = new OutRaw();\r
94 \r
95                 StringWriter sw = new StringWriter();\r
96                 out.extract(new StringReader(xml), sw, in);\r
97                 System.out.println(sw.toString());\r
98 \r
99                 \r
100                 out = new OutJson();\r
101 \r
102                 sw = new StringWriter();\r
103                 out.extract(new StringReader(xml), sw, in);\r
104                 String json;\r
105                 System.out.println(json = sw.toString());\r
106                 \r
107                 in = new InJson();\r
108                 out = new OutRaw();\r
109 \r
110                 sw = new StringWriter();\r
111                 out.extract(new StringReader(json), sw, in);\r
112                 System.out.println(sw.toString());\r
113                 \r
114                 out = new OutXML(inXML);\r
115 \r
116                 sw = new StringWriter();\r
117                 out.extract(new StringReader(json), sw, in, true);\r
118                 System.out.println(sw.toString());\r
119 \r
120                 System.out.flush();\r
121 \r
122         }\r
123 \r
124 }\r