af58ea57e4d3ccccfa1c141401a94b373a3f012a
[aaf/authz.git] / misc / rosetta / src / test / java / org / onap / aaf / misc / rosetta / test / JU_JSON.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.IOException;
25 import java.io.Reader;
26 import java.io.StringReader;
27 import java.io.StringWriter;
28
29 import junit.framework.Assert;
30
31 import org.junit.Test;
32 import org.onap.aaf.misc.rosetta.InJson;
33 import org.onap.aaf.misc.rosetta.Out;
34 import org.onap.aaf.misc.rosetta.OutJson;
35 import org.onap.aaf.misc.rosetta.OutRaw;
36 import org.onap.aaf.misc.rosetta.Parse;
37 import org.onap.aaf.misc.rosetta.ParseException;
38
39 public class JU_JSON {
40
41     @Test
42     public void test() throws IOException, ParseException {
43         InJson jin = new InJson();
44         Out jout = new OutJson();
45
46         go(jin, jout, "{\"id\":\"Me, Myself\",\"date\":1353094689100}");
47         
48         go(jin, jout, "{\"id\":\"My ID 1\",\"desc\":\"My Description 1\",\"comment\":[\"My Comment 1\"],\"utc\":1360418381310}");
49         go(jin, jout, "{\"id\":\"My ID 1\",\"desc\":\"My Description 1\",\"comment\":[\"My Comment 1\",\"My Comment 2\"],\"utc\":1360418381310}");
50
51         go(jin, jout, "{\"SampleData\":[" +
52                    "{\"id\":\"sd object \\\"1\\\"\",\"date\":1316084944213,\"item\":[\"Item 1.1\",\"Item 1.2\"]}," +
53                    "{\"id\":\"sd object \\\"2\\\"\",\"date\":1316084945343,\"item\":[\"Item 2.1\",\"Item 2.2\"]}],\"fluff\":\"MyFluff\"}"
54                    );
55         
56         go(jin, jout, "{\"SampleData\":[{\"date\":1316084945343}],\"fluff\":\"MyFluff\"}");
57         
58         go(jin, jout, "{\"id\":\"Me,[}[eg[)(:x,\\\" Myself\",\"date\":1353094689100}");
59         
60         // TODO: Clean out AT&T specific data
61         go(jin,jout, "{\"userid\":\"xk3233\",\"timestamp\":1353097388531,\"item\":[{\"tag\":\"color\",\"value\":\"Mauve\"},{\"tag\":\"shirtsize\",\"value\":\"Xtra Large\"}]}");
62         //go()
63         //"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><vote xmlns=\"urn:poll.att.com\"><userid>xk3233</userid><timestamp>1353082669667</timestamp></vote>");
64         
65         // 3/11/2015 Jonathan found a case with missing comma
66         go(jin,jout, "{\"start\":\"2015-03-11T18:18:05.580-05:00\",\"end\":\"2015-09-11-05:00\",\"force\":\"false\",\"perm\":{\"type\":\"org.osaaf.myns.mytype\",\"instance\":\"myInstance\",\"action\":\"myAction\"}"
67                 + ",\"role\":\"org.osaaf.myns.myrole\"}");
68
69         // 3/12/2015 Jonathan Kurt Schurenberg noticed an issue of object names in an array.  This is valid code.
70         go(jin,jout, "{\"role\":[{\"name\":\"org.osaaf.myns.myrole\",\"perms\":[{\"type\":\"org.osaaf.myns.mytype\",\"instance\":\"myAction\"},{\"type\":\"org.osaaf.myns.mytype\",\"instance\":\"myOtherAction\"}]}"
71                 + ",{\"name\":\"org.osaaf.myns.myOtherRole\",\"perms\":[{\"type\":\"org.osaaf.myns.myOtherType\",\"instance\":\"myAction\"},{\"type\":\"org.osaaf.myns.myOthertype\",\"instance\":\"myOtherAction\"}]}]}");
72
73         // 3/13/2015 - discovered with complex Response
74         go(jin,jout, "{\"meth\":\"GET\",\"path\":\"/authz/perms/:type\",\"desc\":\"GetPermsByType\",\"comments\":[\"List All Permissions that match :type listed\"],"
75                 + "\"contentType\":[\"application/Permissions+json;q=1.0;charset=utf-8;version=1.1,application/json;q=1.0;version=1.1\""
76                 + ",\"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\""
77                 + ",\"application/Permissions+xml;q=1.0;charset=utf-8;version=1.1,text/xml;q=1.0;version=1.1\"]}"); 
78         
79
80         // Test a Windoze "Pretty Print", validate skipping of Windoze characters as well as other odd control characters listed
81         // in json.org
82         StringWriter sw = new StringWriter();
83         jout.extract(new StringReader(
84                 "{\b\f\n\r\t \"id\""
85                 + ":\"Me, \b\f\n\r\tMyself\",\"date\":1353094689100"
86                 + "\b\f\n\r\t }"
87                 ),sw,jin);
88         Assert.assertEquals("{\"id\":\"Me, \b\f\n\r\tMyself\",\"date\":1353094689100}",sw.toString());
89         System.out.println(sw.toString());
90         
91         // 10/01/2015 Jonathan AAF-703 Ron Gallagher, this response is ok    
92         go(jin,jout, "{\"perm\":[{\"type\":\"org.osaaf.myns.myPerm\",\"action\":\"myAction\",\"description\":\"something\"}]}");
93         // but when description:"" causes extra comma at end
94         go(jin,jout, "{\"perm\":[{\"type\":\"org.osaaf.myns.myPerm\",\"action\":\"myAction\",\"description\":\"\"}]}","{\"perm\":[{\"type\":\"org.osaaf.myns.myPerm\",\"action\":\"myAction\"}]}");
95         // Test other empty string scenarios
96          go(jin,jout, "{\"perm\":[{\"type\":\"\",\"action\":\"\",\"description\":\"\"}]}","{\"perm\":[{}]}");
97          go(jin,jout, "{\"perm\":[{\"type\":\"\",\"action\":\"\",\"description\":\"hi\"}]}","{\"perm\":[{\"description\":\"hi\"}]}");
98         go(jin,jout, "{\"perm\":[{\"type\":\"\",\"action\":\"myAction\",\"description\":\"\"}]}","{\"perm\":[{\"action\":\"myAction\"}]}");
99         
100         
101         go(jin,jout, "{\"perm\":[{\"type\":\"org.osaaf.myns.myPerm\",\"action\":,\"description\":\"something\"}]}","{\"perm\":[{\"type\":\"org.osaaf.myns.myPerm\",\"description\":\"something\"}]}");
102         
103         go(jin, jout, "{\"name\":\"\\\"hello\\\"\"}");
104         
105         go(jin, jout, "{\"name\":\"\\\\\"}");
106
107         go(jin, jout, "{\"role\":\"org.osaaf.scamper.UserStory0152 7_IT-00323-a-admin\",\"perm\":{\"type\":\"org.osaaf.scamper.application\",\"instance\":\"_()`!@#\\\\$%^=+][{}<>/.-valid.app.name-is_good\",\"action\":\"Administrator\"}}");
108         
109     
110     }
111     
112     
113     private void go(Parse<Reader,?> in, Out out, String str) throws IOException, ParseException {
114         go(in,out,str,str);
115     }
116
117
118     private void go(Parse<Reader, ?> in, Out out, String str, String cmp) throws IOException, ParseException {
119         
120         System.out.println(str);
121         StringWriter sw = new StringWriter(1024);
122         out.extract(new StringReader(str), sw, in);
123         System.out.println(sw);
124         String result = sw.toString();
125         
126         if (!result.equals(cmp)) {
127             sw.getBuffer().setLength(0);
128             new OutRaw().extract(new StringReader(str), sw, in);
129             System.out.println(sw);
130         }
131
132         Assert.assertEquals(cmp,result);
133         System.out.println();
134
135     }
136 }