Sonar Fixes, Formatting
[aaf/authz.git] / misc / rosetta / src / test / java / org / onap / aaf / misc / rosetta / test / JU_Stream2Obj.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 inherit.DerivedA;
25 import inherit.Root;
26
27 import java.io.IOException;
28 import java.io.Reader;
29 import java.io.StringReader;
30 import java.io.StringWriter;
31
32 import org.junit.Test;
33 import org.onap.aaf.misc.env.APIException;
34 import org.onap.aaf.misc.env.Data;
35 import org.onap.aaf.misc.env.DataFactory;
36 import org.onap.aaf.misc.env.EnvJAXB;
37 import org.onap.aaf.misc.env.impl.BasicEnv;
38 import org.onap.aaf.misc.rosetta.InJson;
39 import org.onap.aaf.misc.rosetta.InXML;
40 import org.onap.aaf.misc.rosetta.Out;
41 import org.onap.aaf.misc.rosetta.OutJson;
42 import org.onap.aaf.misc.rosetta.OutRaw;
43 import org.onap.aaf.misc.rosetta.OutXML;
44 import org.onap.aaf.misc.rosetta.Parse;
45 import org.onap.aaf.misc.rosetta.ParseException;
46
47 public class JU_Stream2Obj {
48
49     /*
50     <?xml version="1.0" encoding=Config.UTF-8 standalone="yes"?>
51     <root xmlns="urn:inherit">
52       <base xsi:type="derivedA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
53         <name>myDerivedA_1</name>
54         <num>1432</num>
55         <shortName>mda_1</shortName>
56         <value>value_1</value>
57         <value>value_2</value>
58       </base>
59     </root>
60
61     {"base":[{"__extension":"derivedA","name":"myDerivedA_1","num":1432,"shortName":"mda_1","value":["value_1","value_2"]}]}
62     */
63
64     @Test
65     public void json2Obj() throws APIException, SecurityException, NoSuchFieldException, ClassNotFoundException, ParseException, IOException {
66         DerivedA da = new DerivedA();
67         da.setName("myDerivedA_1");
68         da.setNum((short)1432);
69         da.setShortName("mda_1");
70         da.getValue().add("value_1");
71         da.getValue().add("value_2");
72
73         Root root = new Root();
74         root.getBase().add(da);
75
76         da = new DerivedA();
77         da.setName("myDerivedA_2");
78         da.setNum((short)1432);
79         da.setShortName("mda_2");
80         da.getValue().add("value_2.1");
81         da.getValue().add("value_2.2");
82         root.getBase().add(da);
83
84         EnvJAXB env = new BasicEnv();
85         DataFactory<Root> rootDF = env.newDataFactory(Root.class);
86
87         String xml = rootDF.newData(env).out(Data.TYPE.XML).load(root).option(Data.PRETTY).asString();
88         System.out.println(xml);
89
90         InXML inXML;
91         Parse<Reader,?> in = inXML = new InXML(Root.class);
92         Out out = new OutRaw();
93
94         StringWriter sw = new StringWriter();
95         out.extract(new StringReader(xml), sw, in);
96         System.out.println(sw.toString());
97
98
99         out = new OutJson();
100
101         sw = new StringWriter();
102         out.extract(new StringReader(xml), sw, in);
103         String json;
104         System.out.println(json = sw.toString());
105
106         in = new InJson();
107         out = new OutRaw();
108
109         sw = new StringWriter();
110         out.extract(new StringReader(json), sw, in);
111         System.out.println(sw.toString());
112
113         out = new OutXML(inXML);
114
115         sw = new StringWriter();
116         out.extract(new StringReader(json), sw, in, true);
117         System.out.println(sw.toString());
118
119         System.out.flush();
120
121     }
122
123 }