summaryrefslogtreecommitdiffstats
path: root/rosetta/src/test/java/com/data/test/JU_JSON.java
blob: ffaf429a128f72643b619fe458ca90a122d799cf (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
/*******************************************************************************
 * ============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.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;

import org.junit.Test;
import org.onap.aaf.rosetta.InJson;
import org.onap.aaf.rosetta.Out;
import org.onap.aaf.rosetta.OutJson;
import org.onap.aaf.rosetta.OutRaw;
import org.onap.aaf.rosetta.Parse;
import org.onap.aaf.rosetta.ParseException;

import junit.framework.Assert;

public class JU_JSON {

	@Test
	public void test() throws IOException, ParseException {
		InJson jin = new InJson();
		Out jout = new OutJson();

		go(jin, jout, "{\"id\":\"Me, Myself\",\"date\":1353094689100}");
		
		go(jin, jout, "{\"id\":\"My ID 1\",\"desc\":\"My Description 1\",\"comment\":[\"My Comment 1\"],\"utc\":1360418381310}");
		go(jin, jout, "{\"id\":\"My ID 1\",\"desc\":\"My Description 1\",\"comment\":[\"My Comment 1\",\"My Comment 2\"],\"utc\":1360418381310}");

		go(jin, jout, "{\"SampleData\":[" +
				   "{\"id\":\"sd object \\\"1\\\"\",\"date\":1316084944213,\"item\":[\"Item 1.1\",\"Item 1.2\"]}," +
				   "{\"id\":\"sd object \\\"2\\\"\",\"date\":1316084945343,\"item\":[\"Item 2.1\",\"Item 2.2\"]}],\"fluff\":\"MyFluff\"}"
				   );
		
		go(jin, jout, "{\"SampleData\":[{\"date\":1316084945343}],\"fluff\":\"MyFluff\"}");
		
		go(jin, jout, "{\"id\":\"Me,[}[eg[)(:x,\\\" Myself\",\"date\":1353094689100}");
		
		go(jin,jout, "{\"userid\":\"ab1234\",\"timestamp\":1353097388531,\"item\":[{\"tag\":\"color\",\"value\":\"Mauve\"},{\"tag\":\"shirtsize\",\"value\":\"Xtra Large\"}]}");
		//go()
		//"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><vote xmlns=\"urn:poll.att.com\"><userid>ab1234</userid><timestamp>1353082669667</timestamp></vote>");
		
		// 3/11/2015 jcg found a case with missing comma
		go(jin,jout, "{\"start\":\"2015-03-11T18:18:05.580-05:00\",\"end\":\"2015-09-11-05:00\",\"force\":\"false\",\"perm\":{\"type\":\"com.att.myns.mytype\",\"instance\":\"myInstance\",\"action\":\"myAction\"}"
				+ ",\"role\":\"com.att.myns.myrole\"}");

		// 3/12/2015 jcg Kurt Schurenberg noticed an issue of object names in an array.  This is valid code.
		go(jin,jout, "{\"role\":[{\"name\":\"com.att.myns.myrole\",\"perms\":[{\"type\":\"com.att.myns.mytype\",\"instance\":\"myAction\"},{\"type\":\"com.att.myns.mytype\",\"instance\":\"myOtherAction\"}]}"
				+ ",{\"name\":\"com.att.myns.myOtherRole\",\"perms\":[{\"type\":\"com.att.myns.myOtherType\",\"instance\":\"myAction\"},{\"type\":\"com.att.myns.myOthertype\",\"instance\":\"myOtherAction\"}]}]}");

		// 3/13/2015 - discovered with complex Response
		go(jin,jout, "{\"meth\":\"GET\",\"path\":\"/authz/perms/:type\",\"desc\":\"GetPermsByType\",\"comments\":[\"List All Permissions that match :type listed\"],"
				+ "\"contentType\":[\"application/Permissions+json;q=1.0;charset=utf-8;version=1.1,application/json;q=1.0;version=1.1\""
				+ ",\"application/Perms+xml;q=1.0;charset=utf-8;version=2.0,text/xml;q=1.0;version=2.0\",\"application/Perms+json;q=1.0;charset=utf-8;version=2.0,application/json;q=1.0;version=2.0,*/*;q=1.0\""
				+ ",\"application/Permissions+xml;q=1.0;charset=utf-8;version=1.1,text/xml;q=1.0;version=1.1\"]}"); 
		

		// Test a Windoze "Pretty Print", validate skipping of Windoze characters as well as other odd control characters listed
		// in json.org
		StringWriter sw = new StringWriter();
		jout.extract(new StringReader(
				"{\b\f\n\r\t \"id\""
				+ ":\"Me, \b\f\n\r\tMyself\",\"date\":1353094689100"
				+ "\b\f\n\r\t }"
				),sw,jin);
		Assert.assertEquals("{\"id\":\"Me, \b\f\n\r\tMyself\",\"date\":1353094689100}",sw.toString());
		System.out.println(sw.toString());
		
		// 10/01/2015 jcg AAF-703 Ron Gallagher, this response is ok	
		go(jin,jout, "{\"perm\":[{\"type\":\"com.att.myns.myPerm\",\"action\":\"myAction\",\"description\":\"something\"}]}");
		// but when description:"" causes extra comma at end
		go(jin,jout, "{\"perm\":[{\"type\":\"com.att.myns.myPerm\",\"action\":\"myAction\",\"description\":\"\"}]}","{\"perm\":[{\"type\":\"com.att.myns.myPerm\",\"action\":\"myAction\"}]}");
		// Test other empty string scenarios
 		go(jin,jout, "{\"perm\":[{\"type\":\"\",\"action\":\"\",\"description\":\"\"}]}","{\"perm\":[{}]}");
 		go(jin,jout, "{\"perm\":[{\"type\":\"\",\"action\":\"\",\"description\":\"hi\"}]}","{\"perm\":[{\"description\":\"hi\"}]}");
		go(jin,jout, "{\"perm\":[{\"type\":\"\",\"action\":\"myAction\",\"description\":\"\"}]}","{\"perm\":[{\"action\":\"myAction\"}]}");
		
		
		go(jin,jout, "{\"perm\":[{\"type\":\"com.att.myns.myPerm\",\"action\":,\"description\":\"something\"}]}","{\"perm\":[{\"type\":\"com.att.myns.myPerm\",\"description\":\"something\"}]}");
		
		go(jin, jout, "{\"name\":\"\\\"hello\\\"\"}");
		
		go(jin, jout, "{\"name\":\"\\\\\"}");

		go(jin, jout, "{\"role\":\"com.att.scamper.UserStory0152 7_IT-00323-a-admin\",\"perm\":{\"type\":\"com.att.scamper.application\",\"instance\":\"_()`!@#\\\\$%^=+][{}<>/.-valid.app.name-is_good\",\"action\":\"Administrator\"}}");
		
	
	}
	
	
	private void go(Parse<Reader,?> in, Out out, String str) throws IOException, ParseException {
		go(in,out,str,str);
	}


	private void go(Parse<Reader, ?> in, Out out, String str, String cmp) throws IOException, ParseException {
		
		System.out.println(str);
		StringWriter sw = new StringWriter(1024);
		out.extract(new StringReader(str), sw, in);
		System.out.println(sw);
		String result = sw.toString();
		
		if(!result.equals(cmp)) {
			sw.getBuffer().setLength(0);
			new OutRaw().extract(new StringReader(str), sw, in);
			System.out.println(sw);
		}

		Assert.assertEquals(cmp,result);
		System.out.println();

	}
}