Sonar Fixes, Formatting
[aaf/authz.git] / misc / rosetta / src / test / java / org / onap / aaf / misc / rosetta / test / JU_FromXML.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.Trans.Metric;
33 import org.onap.aaf.misc.env.impl.EnvFactory;
34 import org.onap.aaf.misc.env.jaxb.JAXBmar;
35 import org.onap.aaf.misc.env.jaxb.JAXBumar;
36 import org.onap.aaf.misc.env.util.StringBuilderWriter;
37 import org.onap.aaf.misc.rosetta.InXML;
38 import org.onap.aaf.misc.rosetta.Out;
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
43 import s.xsd.LargerData;
44
45 public class JU_FromXML {
46     private static int ITERATIONS = 1;
47         ;
48
49     private final static String xml =
50     "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
51     "<LargerData xmlns=\"urn:s:xsd\">\n" +
52     "   <SampleData>\n" +
53     "      <id>sd object 1</id>\n" +
54     "        <date>1346765355134</date>\n" +
55     "        <item>Item 1.1</item>\n" +
56     "        <item>Item 1.2</item>\n" +
57     "   </SampleData>\n" +
58     "   <SampleData>\n" +
59     "        <id>sd object 2</id>\n" +
60     "        <date>1346765355134</date>\n" +
61     "        <item>Item 2.1</item>\n" +
62     "        <item>Item 2.2</item>\n" +
63     "   </SampleData>\n" +
64     "   <fluff>MyFluff</fluff>\n" +
65     "</LargerData>\n";
66
67
68     @Test
69     public void test() throws Exception {
70         InXML inXML = new InXML(LargerData.class);
71
72         System.out.println(xml);
73         StringBuilderWriter sbw = new StringBuilderWriter(1024);
74
75         Reader rdr = new StringReader(xml);
76
77         new OutRaw().extract(rdr, sbw, inXML);
78         System.out.println(sbw.getBuffer());
79     }
80
81
82     @Test
83     public void xml2JSON() throws Exception {
84         System.out.println("*** XML -> JSON  (No Warm up) ***");
85         Out jout = new OutJson();
86         InXML inXML = new InXML(LargerData.class);
87
88         StringBuilderWriter sbw = new StringBuilderWriter(1024);
89
90         Trans trans;
91         Report report = new Report(ITERATIONS,"XML");
92         do {
93             sbw.reset();
94             trans = EnvFactory.newTrans();
95             Reader sr = new StringReader(xml);
96             TimeTaken tt = trans.start("Parse XML", Env.XML);
97             try {
98                 jout.extract(sr, sbw, inXML);
99             } finally {
100                 tt.done();
101             }
102             report.glean(trans,Env.XML);
103         } while (report.go());
104
105         report.report(sbw);
106         System.out.println(sbw.toString());
107     }
108
109     @Test
110     public void xml2XML() throws Exception {
111         System.out.println("*** XML -> (Event Queue) -> XML (No Warm up) ***");
112         Out xout = new OutXML("LargerData");
113         InXML inXML = new InXML(LargerData.class);
114
115         StringBuilderWriter sbw = new StringBuilderWriter(1024);
116
117         Trans trans;
118         Report report = new Report(ITERATIONS,"XML");
119         do {
120             sbw.reset();
121             trans = EnvFactory.newTrans();
122             Reader sr = new StringReader(xml);
123             TimeTaken tt = trans.start("Parse XML", Env.XML);
124             try {
125                 xout.extract(sr, sbw, inXML);
126             } finally {
127                 tt.done();
128             }
129             report.glean(trans,Env.XML);
130         } while (report.go());
131
132         report.report(sbw);
133         System.out.println(sbw.toString());
134     }
135
136
137     @Test
138     public void warmup() throws Exception {
139         if (ITERATIONS>20) {
140             System.out.println("*** Warmup JAXB ***");
141
142             JAXBumar jaxbUmar = new JAXBumar(LargerData.class);
143             JAXBmar jaxBmar = new JAXBmar(LargerData.class);
144             //jaxBmar.asFragment(true);
145             //jaxBmar.pretty(true);
146             StringBuilderWriter sbw = new StringBuilderWriter(1024);
147
148
149             LargerData ld;
150             Trans trans;
151             Report report = new Report(ITERATIONS,"XML");
152             do {
153                 sbw.reset();
154                 trans = EnvFactory.newTrans();
155                 TimeTaken all = trans.start("Combo", Env.SUB);
156                 try {
157                     TimeTaken tt = trans.start("JAXB Unmarshal", Env.XML);
158                     try {
159                         ld = jaxbUmar.unmarshal(LogTarget.NULL, xml);
160                     } finally {
161                         tt.done();
162                     }
163                     tt = trans.start("JAXB marshal", Env.XML);
164                     try {
165                         jaxBmar.marshal(LogTarget.NULL, ld, sbw);
166                     } finally {
167                         tt.done();
168                     }
169                 } finally {
170                     all.done();
171                 }
172                 report.glean(trans,Env.XML);
173             } while (report.go());
174
175             report.report(sbw);
176             System.out.println(sbw.toString());
177         }
178     }
179     @Test
180     public void xml2jaxb2xml() throws Exception {
181         System.out.println("*** XML -> JAXB Object -> XML ***");
182         JAXBumar jaxbUmar = new JAXBumar(LargerData.class);
183         JAXBmar jaxBmar = new JAXBmar(LargerData.class);
184         //jaxBmar.asFragment(true);
185         //jaxBmar.pretty(true);
186         StringBuilderWriter sbw = new StringBuilderWriter(1024);
187
188         LargerData ld;
189         Trans trans;
190         Report report = new Report(ITERATIONS,"XML");
191         do {
192             sbw.reset();
193             trans = EnvFactory.newTrans();
194             TimeTaken all = trans.start("Combo", Env.SUB);
195             try {
196                 TimeTaken tt = trans.start("JAXB Unmarshal", Env.XML);
197                 try {
198                     ld = jaxbUmar.unmarshal(LogTarget.NULL, xml);
199                 } finally {
200                     tt.done();
201                 }
202                 tt = trans.start("JAXB marshal", Env.XML);
203                 try {
204                     jaxBmar.marshal(LogTarget.NULL, ld, sbw);
205                 } finally {
206                     tt.done();
207                 }
208             } finally {
209                 all.done();
210             }
211             report.glean(trans,Env.XML);
212         } while (report.go());
213
214         report.report(sbw);
215         System.out.println(sbw.toString());    }
216
217     @Test
218     public void xml2jaxb2PrettyXml() throws Exception {
219         System.out.println("*** XML -> JAXB Object -> Pretty XML ***");
220         JAXBumar jaxbUmar = new JAXBumar(LargerData.class);
221         JAXBmar jaxBmar = new JAXBmar(LargerData.class);
222         //jaxBmar.asFragment(true);
223         jaxBmar.pretty(true);
224         StringBuilderWriter sbw = new StringBuilderWriter(1024);
225
226         Trans trans = EnvFactory.newTrans();
227         LargerData ld;
228         for (int i=0;i<ITERATIONS;++i) {
229             sbw.reset();
230             TimeTaken all = trans.start("Combo", Env.SUB);
231             try {
232                 TimeTaken tt = trans.start("JAXB Unmarshal", Env.XML);
233                 try {
234                     ld = jaxbUmar.unmarshal(LogTarget.NULL, xml);
235                 } finally {
236                     tt.done();
237                 }
238                 tt = trans.start("JAXB marshal", Env.XML);
239                 try {
240                     jaxBmar.marshal(LogTarget.NULL, ld, sbw);
241                 } finally {
242                     tt.done();
243                 }
244             } finally {
245                 all.done();
246             }
247         }
248         sbw.append('\n');
249         Metric m;
250         if (ITERATIONS>20) {
251             m = trans.auditTrail(0,null);
252         } else {
253             m = trans.auditTrail(0, sbw.getBuffer());
254             System.out.println(sbw.getBuffer());
255         }
256         System.out.println(ITERATIONS + " entries, Total Time: " + m.total + "ms, Avg Time: " + m.total/ITERATIONS + "ms");
257     }
258
259 }