AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / rosetta / src / test / java / org / onap / aaf / misc / rosetta / test / JU_Types.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.StringWriter;
25
26 import javax.xml.datatype.XMLGregorianCalendar;
27 import javax.xml.namespace.QName;
28
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;
48
49 import types.xsd.Multi;
50 import types.xsd.Multi.Single;
51
52 public class JU_Types {
53
54         @Test
55         public void single() throws Exception {
56                 Single single = setSData();
57                 SingleMarshal psingle = new SingleMarshal();
58                 
59                 OutRaw raw = new OutRaw();
60                 OutJson json = new OutJson();
61                 OutXML xml = new OutXML("Single","xmlns=urn:types:xsd");
62                 
63                 
64                 System.out.println("===== RAW =====");
65                 raw.extract(single, System.out, psingle);
66
67                 System.out.println("\n===== JSON =====");
68                 json.extract(single, System.out, psingle);
69                 
70                 System.out.println("\n\n===== Pretty JSON =====");
71                 json.extract(single, System.out, psingle, true);
72
73                 System.out.println("\n\n===== XML =====");
74                 xml.extract(single, System.out, psingle,false);
75
76                 System.out.println("\n\n===== Pretty XML =====");
77                 xml.extract(single, System.out, psingle, true);
78
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());
84                 jmar.pretty(true);
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());
95                 
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);
109
110                 System.out.println(news.getDatetime());
111
112         }
113         
114         @Test
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");
119
120                 Multi multi = new Multi();
121                 MultiMarshal pmulti = new MultiMarshal();
122         
123                 for(int i=0;i<10;++i) {
124                         System.out.println("===== Multi Iteration " + i + " =====");
125                         if(i>0) {
126                                 multi.getSingle().add(setSData());
127                         }
128                         System.out.println("  ===== RAW =====");
129                         raw.extract(multi, System.out, pmulti);
130                         
131                         System.out.println("\n  ===== JSON =====");
132                         json.extract(multi, System.out, pmulti);
133                         
134                         System.out.println("\n\n  ===== Pretty JSON =====");
135                         json.extract(multi, System.out, pmulti, true);
136         
137                         System.out.println("\n\n  ===== XML =====");
138                         xml.extract(multi, System.out, pmulti,false);
139         
140                         System.out.println("\n\n  ===== Pretty XML =====");
141                         xml.extract(multi, System.out, pmulti, true);
142                 }
143         }
144
145         @Test
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");
150
151                 Multi multi = new Multi();
152                 DocMarshal<Multi> doc = DocMarshal.root(new MultiMarshal());
153         
154                 for(int i=0;i<3;++i) {
155                         System.out.println("===== Multi Iteration " + i + " =====");
156                         if(i>0) {
157                                 multi.getSingle().add(setSData());
158                         }
159                         System.out.println("  ===== RAW =====");
160                         raw.extract(multi, System.out, doc);
161                         
162                         System.out.println("\n  ===== JSON =====");
163                         json.extract(multi, System.out, doc);
164                         
165                         System.out.println("\n\n  ===== Pretty JSON =====");
166                         json.extract(multi, System.out, doc, true);
167         
168                         System.out.println("\n\n  ===== XML =====");
169                         xml.extract(multi, System.out, doc,false);
170         
171                         System.out.println("\n\n  ===== Pretty XML =====");
172                         xml.extract(multi, System.out, doc, true);
173                 }
174         }
175
176
177 //      @Test
178 //      public void saved() throws Exception {
179 //              Saved saved = new Saved();
180 //              saved.extract(in, ignore, parser, options);
181 //      }
182         
183         @Test
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);
188                 
189                 Multi multi = new Multi();
190                 multi.getSingle().add(setSData());
191                 
192
193                 System.out.println("========== Original loading");
194                 Trans trans = env.newTrans();
195                 RosettaData<Multi> data = df.newData(trans);
196                 // Prime pump
197                 for(int i=0;i<100;++i) {
198                         data.load(multi);
199                 }
200                 trans = env.newTrans();
201                 data = df.newData(trans);
202                 
203                 int iters = 10000;
204                 for(int i=0;i<iters;++i) {
205                         data.load(multi);
206                 }
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");
210
211                 System.out.println("========== New loading");
212                 // With new
213                 df.rootMarshal(DocMarshal.root(new MultiMarshal()));
214                 trans = env.newTrans();
215                 data = df.newData(trans);
216
217                 // Prime pump
218                 for(int i=0;i<100;++i) {
219                         data.load(multi);
220                 }
221                 trans = env.newTrans();
222                 data = df.newData(trans);
223                 
224                 for(int i=0;i<iters;++i) {
225                         data.load(multi);
226                 }
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");
230                 
231                 // Assert.assertEquals(first, second);
232
233                 System.out.println("========== Direct Object to JSON String");
234                 trans = env.newTrans();
235                 data = df.newData(trans);
236                 StringBuilderWriter sbw = new StringBuilderWriter(256);
237                 // Prime pump
238                 for(int i=0;i<100;++i) {
239                         sbw.reset();
240                         data.direct(multi, sbw, true);
241                 }
242                 trans = env.newTrans();
243                 data = df.newData(trans);
244
245                 for(int i=0;i<iters;++i) {
246                         sbw.reset();
247                         data.direct(multi, sbw, true);
248                 }
249                 
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");
253                 
254         }
255         
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();
262                 s.setDate(ts);
263                 s.setDatetime(ts);
264                 byte[] bytes= new byte[] {-1,0,(byte)0XFF,0xE,0x8,0x25,0x7,Byte.MIN_VALUE,Byte.MAX_VALUE};
265                 s.setBinary(bytes);
266                 s.getArray().add("String 1");
267                 s.getArray().add("String 2");
268                 return s;
269         }
270
271 //      @Test
272 //      public void jsonInOut() throws IOException, ParseException {
273 //              Parse<?> jin = new InJson();
274 //              Out jout = new OutJson();
275 //
276 ////            go(jin, jout, "{\"id\":\"Me, Myself\",\"date\":1353094689100}");
277 //                      
278 //      }
279         
280         
281         /*
282         private void go(Parse<Reader,?> in, Out out, String str) throws IOException, ParseException {
283                 
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();
289                 
290                 if(!result.equals(str)) {
291                         sw.getBuffer().setLength(0);
292                         new OutRaw().extract(new StringReader(str), sw, in);
293                         System.out.println(sw);
294                 }
295
296                 Assert.assertEquals(str,result);
297                 System.out.println();
298
299         }
300         */
301 }