summaryrefslogtreecommitdiffstats
path: root/rosetta/src/test/java/com/data/test/JU_RosettaDF.java
blob: 5c26afe5cdaad0213dd7dd4225a71ac24a204acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.data.test;

import java.io.StringReader;

import org.junit.Assert;
import org.junit.Test;
import org.onap.aaf.inno.env.Data;
import org.onap.aaf.inno.env.TimeTaken;
import org.onap.aaf.inno.env.Trans;
import org.onap.aaf.inno.env.Data.TYPE;
import org.onap.aaf.inno.env.impl.EnvFactory;
import org.onap.aaf.inno.env.jaxb.JAXBmar;
import org.onap.aaf.inno.env.util.StringBuilderWriter;
import org.onap.aaf.rosetta.env.RosettaDF;
import org.onap.aaf.rosetta.env.RosettaData;
import org.onap.aaf.rosetta.env.RosettaEnv;

import s.xsd.LargerData;
import s.xsd.Multi;
import s.xsd.SampleData;

public class JU_RosettaDF {
	public static int ITERATIONS = 1;

	@Test
	public void testCached() throws Exception {
		RosettaEnv env = new RosettaEnv();
		RosettaDF<LargerData> df = env.newDataFactory(LargerData.class);
		JAXBmar jmar = new JAXBmar(LargerData.class);

		StringBuilderWriter sbw = new StringBuilderWriter(1024);
		Trans trans = EnvFactory.newTrans();

		Report report = new Report(ITERATIONS,"Load JSON","Extract JAXB", "JAXB Marshal", "Cached to XML", "Cached to JSON");
		do {
			sbw.reset();
			trans = EnvFactory.newTrans();
			Data<LargerData> data;
			TimeTaken tt = trans.start("Load JSON", 1);
			try {
				data = df.newData(trans).out(Data.TYPE.JSON).in(Data.TYPE.JSON).load(JU_FromJSON.str);
			} finally {
				tt.done();
			}
			LargerData ld;
			tt = trans.start("Extract JAXB", 2);
			try {
				ld = data.asObject();
			} finally {
				tt.done();
			}

			tt = trans.start("JAXB marshal", 3);
			try {
				jmar.marshal(trans.debug(), ld, sbw);
			} finally {
				tt.done();
			}
			sbw.append('\n');
			
			tt = trans.start("To XML from Cache",4);
			try {
				data.out(Data.TYPE.XML).to(sbw);
			} finally {
				tt.done();
			}
			
			sbw.append('\n');
			
			tt = trans.start("To JSON from Cache",5);
			try {
				data.out(Data.TYPE.JSON).to(sbw);
			} finally {
				tt.done();
			}
			report.glean(trans, 1,2,3,4,5);
		} while(report.go());
		
		report.report(sbw);
		System.out.println(sbw);
		
	}

	@Test
	public void testDirect() throws Exception {
		RosettaEnv env = new RosettaEnv();
		RosettaDF<LargerData> df = env.newDataFactory(LargerData.class);

		StringBuilderWriter sbw = new StringBuilderWriter(1024);
		Trans trans = EnvFactory.newTrans();

		Report report = new Report(ITERATIONS);
		do {
			sbw.reset();
			trans = EnvFactory.newTrans();
			RosettaData<?> data = df.newData(trans).in(Data.TYPE.JSON).out(Data.TYPE.XML);
			data.direct(new StringReader(JU_FromJSON.str), sbw);
			report.glean(trans);
		} while(report.go());
		
		report.report(sbw);
		System.out.println(sbw);
		
	}
	
	@Test
	public void testMulti() throws Exception {
		RosettaEnv env = new RosettaEnv();
		RosettaDF<Multi> df = env.newDataFactory(Multi.class);

//		StringBuilderWriter sbw = new StringBuilderWriter(1024);
//		Trans trans = EnvFactory.newTrans();

		Multi m = new Multi();
		m.getF1().add("String1");
		m.getF2().add("String2");
		
		System.out.println(df.newData().load(m).out(TYPE.RAW).asString());
		System.out.println(df.newData().load(m).out(TYPE.JSON).asString());
		
	}

	@Test
	public void testQuotes() throws Exception {
		RosettaEnv env = new RosettaEnv();
		RosettaDF<SampleData> df = env.newDataFactory(SampleData.class);

		SampleData sd = new SampleData();
		sd.setId("\"AT&T Services, Inc.\"");
		System.out.println(sd.getId());
		String out =df.newData().load(sd).out(TYPE.JSON).asString();
		System.out.println(out);
		Assert.assertEquals(
				"{\"id\":\"\\\"AT&T Services, Inc.\\\"\",\"date\":0}",
				out);
		
		SampleData sd2 = df.newData().in(TYPE.JSON).load(out).asObject();
		System.out.println(sd2.getId());
		Assert.assertEquals(sd.getId(),sd2.getId());
	}
}