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.StringWriter;
26 import javax.xml.datatype.XMLGregorianCalendar;
27 import javax.xml.namespace.QName;
29 import org.junit.Test;
30 import org.onap.aaf.misc.env.Data;
31 import org.onap.aaf.misc.env.Env;
32 import org.onap.aaf.misc.env.Trans;
33 import org.onap.aaf.misc.env.Data.TYPE;
34 import org.onap.aaf.misc.env.Trans.Metric;
35 import org.onap.aaf.misc.env.jaxb.JAXBmar;
36 import org.onap.aaf.misc.env.jaxb.JAXBumar;
37 import org.onap.aaf.misc.env.util.Chrono;
38 import org.onap.aaf.misc.env.util.StringBuilderWriter;
39 import org.onap.aaf.misc.rosetta.OutJson;
40 import org.onap.aaf.misc.rosetta.OutRaw;
41 import org.onap.aaf.misc.rosetta.OutXML;
42 import org.onap.aaf.misc.rosetta.env.RosettaDF;
43 import org.onap.aaf.misc.rosetta.env.RosettaData;
44 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
45 import org.onap.aaf.misc.rosetta.marshal.DocMarshal;
46 import org.onap.aaf.misc.rosetta.test.obj.MultiMarshal;
47 import org.onap.aaf.misc.rosetta.test.obj.SingleMarshal;
49 import types.xsd.Multi;
50 import types.xsd.Multi.Single;
52 public class JU_Types {
55 public void single() throws Exception {
56 Single single = setSData();
57 SingleMarshal psingle = new SingleMarshal();
59 OutRaw raw = new OutRaw();
60 OutJson json = new OutJson();
61 OutXML xml = new OutXML("Single","xmlns=urn:types:xsd");
64 System.out.println("===== RAW =====");
65 raw.extract(single, System.out, psingle);
67 System.out.println("\n===== JSON =====");
68 json.extract(single, System.out, psingle);
70 System.out.println("\n\n===== Pretty JSON =====");
71 json.extract(single, System.out, psingle, true);
73 System.out.println("\n\n===== XML =====");
74 xml.extract(single, System.out, psingle,false);
76 System.out.println("\n\n===== Pretty XML =====");
77 xml.extract(single, System.out, psingle, true);
79 RosettaEnv env = new RosettaEnv();
80 StringWriter sw = new StringWriter();
81 xml.extract(single, sw, psingle, true);
82 JAXBumar jumar = new JAXBumar(single.getClass());
83 JAXBmar jmar = new JAXBmar(new QName("Single","urn.types.xsd"),single.getClass());
85 sw = new StringWriter();
86 jmar.marshal(env.info(), single, sw);
87 System.out.println(sw);
88 Single news = jumar.unmarshal(env.info(), sw.toString());
89 // System.out.println(news.getDatetime());
90 // sw = new StringWriter();
91 // news.setDatetime(Chrono.timeStamp());
92 // xml.extract(single, sw, psingle, true);
93 news = jumar.unmarshal(env.info(), sw.toString());
94 System.out.println(sw.toString());
96 String sample = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
97 + "\n<ns2:urn.types.xsd xmlns:ns2=\"Single\" xmlns=\"urn:types:xsd\">"
98 + "\n<str>MyString</str>"
99 + "\n<int>2147483647</int>"
100 + "\n<long>9223372036854775807</long>"
101 + "\n<date>2015-05-27-05:00</date>"
102 + "\n<datetime>2015-05-27T07:05:04.234-05:00</datetime>"
103 + "\n<binary>FF00FF0E082507807F</binary>"
104 + "\n<array>String 1</array>"
105 + "\n<array>String 2</array>"
106 + "\n</ns2:urn.types.xsd>";
107 System.out.println(sample);
108 news = jumar.unmarshal(env.info(), sample);
110 System.out.println(news.getDatetime());
115 public void multi() throws Exception {
116 OutRaw raw = new OutRaw();
117 OutJson json = new OutJson();
118 OutXML xml = new OutXML("Multi","xmlns=urn:types:xsd");
120 Multi multi = new Multi();
121 MultiMarshal pmulti = new MultiMarshal();
123 for (int i=0;i<10;++i) {
124 System.out.println("===== Multi Iteration " + i + " =====");
126 multi.getSingle().add(setSData());
128 System.out.println(" ===== RAW =====");
129 raw.extract(multi, System.out, pmulti);
131 System.out.println("\n ===== JSON =====");
132 json.extract(multi, System.out, pmulti);
134 System.out.println("\n\n ===== Pretty JSON =====");
135 json.extract(multi, System.out, pmulti, true);
137 System.out.println("\n\n ===== XML =====");
138 xml.extract(multi, System.out, pmulti,false);
140 System.out.println("\n\n ===== Pretty XML =====");
141 xml.extract(multi, System.out, pmulti, true);
146 public void doc() throws Exception {
147 OutRaw raw = new OutRaw();
148 OutJson json = new OutJson();
149 OutXML xml = new OutXML("Multi","xmlns=urn:types:xsd");
151 Multi multi = new Multi();
152 DocMarshal<Multi> doc = DocMarshal.root(new MultiMarshal());
154 for (int i=0;i<3;++i) {
155 System.out.println("===== Multi Iteration " + i + " =====");
157 multi.getSingle().add(setSData());
159 System.out.println(" ===== RAW =====");
160 raw.extract(multi, System.out, doc);
162 System.out.println("\n ===== JSON =====");
163 json.extract(multi, System.out, doc);
165 System.out.println("\n\n ===== Pretty JSON =====");
166 json.extract(multi, System.out, doc, true);
168 System.out.println("\n\n ===== XML =====");
169 xml.extract(multi, System.out, doc,false);
171 System.out.println("\n\n ===== Pretty XML =====");
172 xml.extract(multi, System.out, doc, true);
178 // public void saved() throws Exception {
179 // Saved saved = new Saved();
180 // saved.extract(in, ignore, parser, options);
184 public void df() throws Exception {
185 RosettaEnv env = new RosettaEnv();
186 RosettaDF<Multi> df = env.newDataFactory(Multi.class);
187 df.out(TYPE.JSON).option(Data.PRETTY);
189 Multi multi = new Multi();
190 multi.getSingle().add(setSData());
193 System.out.println("========== Original loading");
194 Trans trans = env.newTrans();
195 RosettaData<Multi> data = df.newData(trans);
197 for (int i=0;i<100;++i) {
200 trans = env.newTrans();
201 data = df.newData(trans);
204 for (int i=0;i<iters;++i) {
207 Metric metrics = trans.auditTrail(0, null,Env.JSON,Env.XML);
208 System.out.println(data.asString());
209 System.out.println(metrics.total/iters + "ms avg");
211 System.out.println("========== New loading");
213 df.rootMarshal(DocMarshal.root(new MultiMarshal()));
214 trans = env.newTrans();
215 data = df.newData(trans);
218 for (int i=0;i<100;++i) {
221 trans = env.newTrans();
222 data = df.newData(trans);
224 for (int i=0;i<iters;++i) {
227 metrics = trans.auditTrail(0, null,Env.JSON,Env.XML);
228 System.out.println(data.asString());
229 System.out.println(metrics.total/iters + "ms avg");
231 // Assert.assertEquals(first, second);
233 System.out.println("========== Direct Object to JSON String");
234 trans = env.newTrans();
235 data = df.newData(trans);
236 StringBuilderWriter sbw = new StringBuilderWriter(256);
238 for (int i=0;i<100;++i) {
240 data.direct(multi, sbw, true);
242 trans = env.newTrans();
243 data = df.newData(trans);
245 for (int i=0;i<iters;++i) {
247 data.direct(multi, sbw, true);
250 metrics = trans.auditTrail(0, null,Env.JSON,Env.XML);
251 System.out.println(sbw.toString());
252 System.out.println(metrics.total/iters + "ms avg");
256 private Single setSData() {
257 Single s = new Single();
258 s.setStr("MyString");
259 s.setInt(Integer.MAX_VALUE);
260 s.setLong(Long.MAX_VALUE);
261 XMLGregorianCalendar ts = Chrono.timeStamp();
264 byte[] bytes= new byte[] {-1,0,(byte)0XFF,0xE,0x8,0x25,0x7,Byte.MIN_VALUE,Byte.MAX_VALUE};
266 s.getArray().add("String 1");
267 s.getArray().add("String 2");
272 // public void jsonInOut() throws IOException, ParseException {
273 // Parse<?> jin = new InJson();
274 // Out jout = new OutJson();
276 //// go(jin, jout, "{\"id\":\"Me, Myself\",\"date\":1353094689100}");
282 private void go(Parse<Reader,?> in, Out out, String str) throws IOException, ParseException {
284 System.out.println(str);
285 StringWriter sw = new StringWriter(1024);
286 out.extract(new StringReader(str), sw, in);
287 System.out.println(sw);
288 String result = sw.toString();
290 if (!result.equals(str)) {
291 sw.getBuffer().setLength(0);
292 new OutRaw().extract(new StringReader(str), sw, in);
293 System.out.println(sw);
296 Assert.assertEquals(str,result);
297 System.out.println();