AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / rosetta / src / test / java / org / onap / aaf / misc / rosetta / test / JU_FromJSON.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
22 package org.onap.aaf.misc.rosetta.test;
23
24 import java.io.Reader;
25 import java.io.StringReader;
26
27 import org.junit.Test;
28 import org.onap.aaf.misc.env.Env;
29 import org.onap.aaf.misc.env.LogTarget;
30 import org.onap.aaf.misc.env.TimeTaken;
31 import org.onap.aaf.misc.env.Trans;
32 import org.onap.aaf.misc.env.impl.EnvFactory;
33 import org.onap.aaf.misc.env.jaxb.JAXBmar;
34 import org.onap.aaf.misc.env.util.IndentPrintWriter;
35 import org.onap.aaf.misc.env.util.StringBuilderWriter;
36 import org.onap.aaf.misc.rosetta.InJson;
37 import org.onap.aaf.misc.rosetta.Out;
38 import org.onap.aaf.misc.rosetta.OutJson;
39 import org.onap.aaf.misc.rosetta.OutRaw;
40 import org.onap.aaf.misc.rosetta.OutXML;
41
42 import junit.framework.Assert;
43 import s.xsd.LargerData;
44 import s.xsd.SampleData;
45
46 public class JU_FromJSON {
47         private static int ITERATIONS = 10000;
48         static String str = "{\"SampleData\":[" +
49                                                    "{\"id\":\"sd object \\\"1\\\"\",\"date\":1316084944213,\"item\":[\"Item 1.1\",\"Item 1.2\"]}," +
50                                                    "{\"id\":\"sd object \\\"2\\\"\",\"date\":1316084945343,\"item\":[\"Item 2.1\",\"Item 2.2\"]}],\"fluff\":\"MyFluff\"}";
51         InJson inJSON = new InJson();
52
53         @Test
54         public void rawParse() throws Exception {
55                 System.out.println("*** PARSE JSON -> RAW Dump ***");
56                 System.out.println(str);
57                 StringBuilderWriter sbw = new StringBuilderWriter();
58                 new OutRaw().extract(new StringReader(str),sbw,inJSON);
59                 System.out.println(sbw.getBuffer());
60         }
61         
62         @Test
63         public void parseJSON2Dump() throws Exception {
64                 System.out.println("*** PARSE JSON -> Dump ***");
65                 System.out.println(str);
66                 StringBuilderWriter sbw = new StringBuilderWriter(1024);
67                 
68                 new OutDump().extract(new StringReader(str), sbw, inJSON);
69                 
70                 System.out.println(sbw.getBuffer());
71         }
72         
73         @Test
74         public void nonprettyJSON() throws Exception {
75                 System.out.println("*** JSON -> (Intermediate Stream) -> Non-pretty JSON ***");
76                 System.out.println(str);
77                 StringBuilderWriter sbw = new StringBuilderWriter(1024);
78
79                 Out jout = new OutJson();
80                 Trans trans;
81                 Report report = new Report(ITERATIONS,"JSON");
82                 do {
83                         sbw.reset();
84                         trans = EnvFactory.newTrans();
85                         Reader sr = new StringReader(str);
86                         TimeTaken tt = trans.start("Parse JSON", Env.JSON);
87                         try {
88                                 jout.extract(sr, sbw, inJSON);
89                         } finally {
90                                 tt.done();
91                         }
92                         report.glean(trans,Env.JSON);
93                 } while(report.go());
94                 
95                 String result = sbw.toString();
96                 System.out.println(result);
97                 Assert.assertEquals(result, str);
98                 report.report(sbw);
99                 System.out.println(sbw.toString());
100         }
101         
102         @Test
103         public void parseJSON2JSON() throws Exception {
104                 System.out.println("*** JSON -> (Intermediate Stream) -> Pretty JSON ***");
105                 System.out.println(str);
106
107                 StringBuilderWriter sbw = new StringBuilderWriter(1024);
108                 
109                 Out jout = new OutJson();
110                 Trans trans;
111                 Report report = new Report(ITERATIONS,"JSON");
112                 do {
113                         sbw.reset();
114                         trans = EnvFactory.newTrans();
115                         Reader sr = new StringReader(str);
116                         TimeTaken tt = trans.start("Parse JSON", Env.JSON);
117                         try {
118                                 jout.extract(sr, sbw, inJSON,true);
119                         } finally {
120                                 tt.done();
121                         }
122                         report.glean(trans,Env.JSON);
123                 } while(report.go());
124                 
125                 report.report(sbw);
126                 System.out.println(sbw.toString());
127         }
128
129         @Test
130         public void parseJSON2XML() throws Exception {
131                 System.out.println("*** PARSE JSON -> XML ***");
132                 System.out.println(str);
133
134                 StringBuilderWriter sbw = new StringBuilderWriter(1024);
135                 
136                 Out xout = new OutXML("LargerData","xmlns=urn:s:xsd");
137                 Trans trans;
138                 Report report = new Report(ITERATIONS,"JSON");
139                 do {
140                         sbw.reset();
141                         trans = EnvFactory.newTrans();
142                         Reader sr = new StringReader(str);
143                         TimeTaken tt = trans.start("Parse JSON", Env.JSON);
144                         try {
145                                 xout.extract(sr, sbw, inJSON);
146                         } finally {
147                                 tt.done();
148                         }
149                         report.glean(trans,Env.JSON);
150                 } while(report.go());
151                 
152                 report.report(sbw);
153                 System.out.println(sbw.toString());
154         }
155
156         @Test
157         public void parseJSON2PrettyXML() throws Exception {
158                 System.out.println("*** PARSE JSON -> Pretty XML ***");
159                 System.out.println(str);
160
161                 StringBuilderWriter sbw = new StringBuilderWriter(1024);
162                 IndentPrintWriter ipw = new IndentPrintWriter(sbw);
163                 
164                 Out xout = new OutXML("LargerData","xmlns=urn:s:xsd");
165                 Trans trans;
166                 Report report = new Report(ITERATIONS,"JSON");
167                 do {
168                         sbw.reset();
169                         trans = EnvFactory.newTrans();
170                         Reader sr = new StringReader(str);
171                         TimeTaken tt = trans.start("Parse JSON", Env.JSON);
172                         try {
173                                 xout.extract(sr, ipw, inJSON);
174                         } finally {
175                                 tt.done();
176                         }
177                         report.glean(trans,Env.JSON);
178                 } while(report.go());
179                 
180                 report.report(sbw);
181                 System.out.println(sbw.toString());
182         }
183         
184                 
185         @Test
186         public void jaxbObj2XML() throws Exception {
187                 System.out.println("*** JAXB Object -> XML ***");
188
189                 LargerData ld = new LargerData();
190                 SampleData sd = new SampleData();
191                 sd.setDate(System.currentTimeMillis());
192                 sd.setId("sd object \"1\"");
193                 sd.getItem().add("Item 1.1");
194                 sd.getItem().add("Item 1.2");
195                 ld.getSampleData().add(sd);
196                 sd = new SampleData();
197                 sd.setDate(System.currentTimeMillis());
198                 sd.setId("sd object \"2\"");
199                 sd.getItem().add("Item 2.1");
200                 sd.getItem().add("Item 2.2");
201                 ld.getSampleData().add(sd);
202                 ld.setFluff("MyFluff");
203                 
204                 JAXBmar jaxBmar = new JAXBmar(LargerData.class);
205                 //jaxBmar.asFragment(true);
206                 //jaxBmar.pretty(true);
207                 StringBuilderWriter sbw = new StringBuilderWriter(1024);
208
209                 Trans trans;
210                 Report report = new Report(ITERATIONS,"XML");
211                 do {
212                         sbw.reset();
213                         trans = EnvFactory.newTrans();
214                         TimeTaken tt = trans.start("JAXB", Env.XML);
215                         try {
216                                 jaxBmar.marshal(LogTarget.NULL, ld, sbw);
217                         } finally {
218                                 tt.done();
219                         }
220                         report.glean(trans,Env.XML);
221                 } while(report.go());
222                 
223                 report.report(sbw);
224                 System.out.println(sbw.toString());
225         }
226
227         @Test
228         public void jaxbObj2PrettyXML() throws Exception {
229                 System.out.println("*** JAXB Object -> Pretty XML ***");
230
231                 LargerData ld = new LargerData();
232                 SampleData sd = new SampleData();
233                 sd.setDate(System.currentTimeMillis());
234                 sd.setId("sd object \"1\"");
235                 sd.getItem().add("Item 1.1");
236                 sd.getItem().add("Item 1.2");
237                 ld.getSampleData().add(sd);
238                 sd = new SampleData();
239                 sd.setDate(System.currentTimeMillis());
240                 sd.setId("sd object \"2\"");
241                 sd.getItem().add("Item 2.1");
242                 sd.getItem().add("Item 2.2");
243                 ld.getSampleData().add(sd);
244                 ld.setFluff("MyFluff");
245                 
246                 JAXBmar jaxBmar = new JAXBmar(LargerData.class);
247                 //jaxBmar.asFragment(true);
248                 jaxBmar.pretty(true);
249                 StringBuilderWriter sbw = new StringBuilderWriter(1024);
250
251                 Trans trans;
252                 Report report = new Report(ITERATIONS,"XML");
253                 do {
254                         sbw.reset();
255                         trans = EnvFactory.newTrans();
256                         TimeTaken tt = trans.start("JAXB", Env.XML);
257                         try {
258                                 jaxBmar.marshal(LogTarget.NULL, ld, sbw);
259                         } finally {
260                                 tt.done();
261                         }
262                         report.glean(trans,Env.XML);
263                 } while(report.go());
264
265                 report.report(sbw);
266                 System.out.println(sbw.toString());
267         }
268 }