2 * ============LICENSE_START====================================================
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
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====================================================
22 package org.onap.aaf.misc.rosetta.test;
24 import java.io.Reader;
25 import java.io.StringReader;
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;
42 import junit.framework.Assert;
43 import s.xsd.LargerData;
44 import s.xsd.SampleData;
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();
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());
63 public void parseJSON2Dump() throws Exception {
64 System.out.println("*** PARSE JSON -> Dump ***");
65 System.out.println(str);
66 StringBuilderWriter sbw = new StringBuilderWriter(1024);
68 new OutDump().extract(new StringReader(str), sbw, inJSON);
70 System.out.println(sbw.getBuffer());
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);
79 Out jout = new OutJson();
81 Report report = new Report(ITERATIONS,"JSON");
84 trans = EnvFactory.newTrans();
85 Reader sr = new StringReader(str);
86 TimeTaken tt = trans.start("Parse JSON", Env.JSON);
88 jout.extract(sr, sbw, inJSON);
92 report.glean(trans,Env.JSON);
95 String result = sbw.toString();
96 System.out.println(result);
97 Assert.assertEquals(result, str);
99 System.out.println(sbw.toString());
103 public void parseJSON2JSON() throws Exception {
104 System.out.println("*** JSON -> (Intermediate Stream) -> Pretty JSON ***");
105 System.out.println(str);
107 StringBuilderWriter sbw = new StringBuilderWriter(1024);
109 Out jout = new OutJson();
111 Report report = new Report(ITERATIONS,"JSON");
114 trans = EnvFactory.newTrans();
115 Reader sr = new StringReader(str);
116 TimeTaken tt = trans.start("Parse JSON", Env.JSON);
118 jout.extract(sr, sbw, inJSON,true);
122 report.glean(trans,Env.JSON);
123 } while(report.go());
126 System.out.println(sbw.toString());
130 public void parseJSON2XML() throws Exception {
131 System.out.println("*** PARSE JSON -> XML ***");
132 System.out.println(str);
134 StringBuilderWriter sbw = new StringBuilderWriter(1024);
136 Out xout = new OutXML("LargerData","xmlns=urn:s:xsd");
138 Report report = new Report(ITERATIONS,"JSON");
141 trans = EnvFactory.newTrans();
142 Reader sr = new StringReader(str);
143 TimeTaken tt = trans.start("Parse JSON", Env.JSON);
145 xout.extract(sr, sbw, inJSON);
149 report.glean(trans,Env.JSON);
150 } while(report.go());
153 System.out.println(sbw.toString());
157 public void parseJSON2PrettyXML() throws Exception {
158 System.out.println("*** PARSE JSON -> Pretty XML ***");
159 System.out.println(str);
161 StringBuilderWriter sbw = new StringBuilderWriter(1024);
162 IndentPrintWriter ipw = new IndentPrintWriter(sbw);
164 Out xout = new OutXML("LargerData","xmlns=urn:s:xsd");
166 Report report = new Report(ITERATIONS,"JSON");
169 trans = EnvFactory.newTrans();
170 Reader sr = new StringReader(str);
171 TimeTaken tt = trans.start("Parse JSON", Env.JSON);
173 xout.extract(sr, ipw, inJSON);
177 report.glean(trans,Env.JSON);
178 } while(report.go());
181 System.out.println(sbw.toString());
186 public void jaxbObj2XML() throws Exception {
187 System.out.println("*** JAXB Object -> XML ***");
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");
204 JAXBmar jaxBmar = new JAXBmar(LargerData.class);
205 //jaxBmar.asFragment(true);
206 //jaxBmar.pretty(true);
207 StringBuilderWriter sbw = new StringBuilderWriter(1024);
210 Report report = new Report(ITERATIONS,"XML");
213 trans = EnvFactory.newTrans();
214 TimeTaken tt = trans.start("JAXB", Env.XML);
216 jaxBmar.marshal(LogTarget.NULL, ld, sbw);
220 report.glean(trans,Env.XML);
221 } while(report.go());
224 System.out.println(sbw.toString());
228 public void jaxbObj2PrettyXML() throws Exception {
229 System.out.println("*** JAXB Object -> Pretty XML ***");
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");
246 JAXBmar jaxBmar = new JAXBmar(LargerData.class);
247 //jaxBmar.asFragment(true);
248 jaxBmar.pretty(true);
249 StringBuilderWriter sbw = new StringBuilderWriter(1024);
252 Report report = new Report(ITERATIONS,"XML");
255 trans = EnvFactory.newTrans();
256 TimeTaken tt = trans.start("JAXB", Env.XML);
258 jaxBmar.marshal(LogTarget.NULL, ld, sbw);
262 report.glean(trans,Env.XML);
263 } while(report.go());
266 System.out.println(sbw.toString());