Policy 1707 commit to LF
[policy/engine.git] / ECOMP-XACML / src / test / java / org / openecomp / policy / xacml / test / json / RequestConformanceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-XACML
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.openecomp.policy.xacml.test.json;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.io.StringWriter;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Iterator;
31 import java.util.List;
32
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerFactory;
35 import javax.xml.transform.dom.DOMSource;
36 import javax.xml.transform.stream.StreamResult;
37
38 import org.junit.Test;
39
40 import com.att.research.xacml.api.Request;
41 import com.att.research.xacml.api.RequestAttributes;
42 import com.att.research.xacml.api.RequestReference;
43 import com.att.research.xacml.std.dom.DOMRequest;
44 import com.att.research.xacml.std.json.JSONRequest;
45 import com.att.research.xacml.std.json.JSONStructureException;
46 /**
47  * Test JSON Request convert to object - Conformance tests
48  * 
49  * TO RUN - use jUnit
50  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
51  * 
52  * NOTE:
53  * The "correct" way to verify that each JSON string gets translated into our internal Objects correctly is to look explicitly at each of the child objects
54  * and verify that they are correct.  This would involve a lot of coding to get child of child of child and individually verify each property of each element.
55  * To simplify testing we assume that request.toString() correctly includes a complete text representation of every sub-component of the Request object
56  * and we compare the resulting String to our expected String.
57  * This has two possible sources of error:
58  *      - toString might not include some sub-component, and
59  *      - the initial verification of the resulting string is done by hand and may have been incorrect.
60  * 
61  *
62  */
63 public class RequestConformanceTest {
64         
65         // where to find the conformance test XML files
66         private final String CONFORMANCE_DIRECTORY_PATH = "testsets/conformance/xacml3.0-ct-v.0.4";
67         
68         // The request object output from each test conversion from JSON string
69         Request request;
70
71
72         
73         
74         
75         // test just one of each top-level element.
76         // For simple elements also test for incorrect type
77         @Test
78         public void testConformanceRequests() {
79                 
80                 List<File> filesInDirectory = null;
81                 
82                 File conformanceDirectory = null;
83                 
84                 File currentFile = null;
85                 
86                 try {
87                         conformanceDirectory = new File(CONFORMANCE_DIRECTORY_PATH);
88                         filesInDirectory = getRequestsInDirectory(conformanceDirectory);
89                 } catch (Exception e) {
90                         fail("Unable to set up Conformance tests for dir '" + conformanceDirectory.getAbsolutePath()+"' e="+ e);
91                 }
92                 
93                 // run through each XML file
94                 //      - load the file from XML into an internal Request object
95                 //      - generate the JSON representation of that Request object
96                 //      - load that JSON representation into a new Request object
97                 //      - compare the 2 Request objects
98                 Request xmlRequest = null;
99                 Request jsonRequest = null;
100                 try {
101                         for (File f : filesInDirectory) {
102                                 currentFile = f;
103
104 //// This is a simple way to select just one file for debugging - comment out when not being used
105 //if ( ! f.getName().equals("IIA023Request.xml")) {   continue;  }
106
107 // during debugging it is helpful to know what file it is starting to work on
108 //                              System.out.println("starting file="+currentFile.getName());
109                                 
110                                 try {
111                                         // load XML into a Request object
112                                         xmlRequest = DOMRequest.load(f);
113                                         xmlRequest.getStatus();
114                                 } catch (Exception e) {
115                                         // if XML does not load, just note it and continue with next file
116                                         System.out.println("XML file did not load: '" + f.getName() + "  e=" + e);
117                                         continue;
118                                 }
119                                 
120 //System.out.println(JSONRequest.toString(xmlRequest, false));
121
122                                 // generate JSON from the Request
123                                 String jsonString = JSONRequest.toString(xmlRequest, false);
124                                 
125
126 // single-value elements
127 //jsonString = "{\"Request\":{\"Category\":[{\"CategoryId\":\"urn:oasis:names:tc:xacml:1.0:subject-category:access-subject\",\"Attribute\":["
128 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"Julius Hibbert\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#string\",\"IncludeInResult\":\"true\",\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-id\"},"
129 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":123,\"DataType\":\"http://www.w3.org/2001/XMLSchema#string\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-string\"},"
130 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"true\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#boolean\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-boolean\"},"
131 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"56\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#integer\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-integer\"},"
132 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"27.5\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#double\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-double\"},"
133 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"2002-03-22\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#date\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-date\"},"
134 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"2002-03-22T08:23:47-05:00\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#dateTime\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-dateTime\"},"
135 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"P50DT5H4M3S\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#dayTimeDuration\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-dayTimeDuration\"},"
136 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"-P5Y3M\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#yearMonthDuration\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-yearMonthDuration\"},"
137 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"0bf7a9876cde\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#hexBinary\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-hexBinary\"},"
138 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"c3VyZS4=\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#base64Binary\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-rfc822Name\"},"
139 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"j_hibbert@medico.com\",\"DataType\":\"urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-rfc822Name\"},"
140 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"CN=Julius Hibbert, O=Medi Corporation, C=US\",\"DataType\":\"urn:oasis:names:tc:xacml:1.0:data-type:x500Name\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-x500Name\"},"
141 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"122.45.38.245/255.255.255.64:8080-8080\",\"DataType\":\"urn:oasis:names:tc:xacml:2.0:data-type:ipAddress\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-ipAddress\"},"
142 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"some.host.name:147-874\",\"DataType\":\"urn:oasis:names:tc:xacml:2.0:data-type:dnsName\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-dnsName\"}"
143 //              + "]},"
144 //              + "{\"CategoryId\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\",\"Attribute\":["
145 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":{\"Namespaces\":[{\"Namespace\":\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\"},{\"Namespace\":\"http://www.medico.com/schemas/record\",\"Prefix\":\"md\"},{\"Namespace\":\"http://www.w3.org/2001/XMLSchema-instance\",\"Prefix\":\"xsi\"}],\"XPathCategory\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\",\"XPath\":\"//md:records/md:record\"},\"DataType\":\"urn:oasis:names:tc:xacml:3.0:data-type:xpathExpression\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:resource:xpathExpression\"},"
146 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"http://medico.com/record/patient/BartSimpson\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#anyURI\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:resource:resource-id\"}"
147 //              + "]},"
148 //              + "{\"CategoryId\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:action\",\"Attribute\":["
149 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"read\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#string\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:action:action-id\"}"
150 //              + "]},"
151 //              + "{\"CategoryId\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:environment\",\"Attribute\":["
152 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"08:23:47-05:00\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#time\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:environment:current-time\"}"
153 //              + "]}],\"ReturnPolicyIdList\":false,\"CombinedDecision\":false}}";
154
155
156                                 
157                                 
158                                 
159                                 
160 // array attributes WITH explicit data types
161                         
162                         
163                                 
164                                 
165 // String for testing Arrays of Attribute values
166 //jsonString = "{\"Request\":{\"Category\":[{\"CategoryId\":\"urn:oasis:names:tc:xacml:1.0:subject-category:access-subject\",\"Attribute\":["
167 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":[\"test string\",\"Julius Hibbert\",\"Julius Hibbert as string\"],\"DataType\":\"http://www.w3.org/2001/XMLSchema#string\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-id\"},"
168 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":[true, false],\"DataType\":\"http://www.w3.org/2001/XMLSchema#boolean\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-boolean\"},"
169 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":[56, 12],\"DataType\":\"http://www.w3.org/2001/XMLSchema#integer\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-integer\"},"
170 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":[27.12, 12112.344],\"DataType\":\"http://www.w3.org/2001/XMLSchema#double\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-double\"},"
171 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":[\"2002-03-22\",\"1256-11-11\"],\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-date\"},"
172 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":[\"2002-03-22T08:23:47-05:00\",\"1056-11-05T19:08:12-14:30\"],\"DataType\":\"http://www.w3.org/2001/XMLSchema#dateTime\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-dateTime\"},"
173 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":[\"P50DT5H4M3S\",\"P12DT148H18M21S\"],\"DataType\":\"http://www.w3.org/2001/XMLSchema#dayTimeDuration\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-dayTimeDuration\"},"
174 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"-P5Y3M\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#yearMonthDuration\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-yearMonthDuration\"},"
175 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"-P28Y7M\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#yearMonthDuration\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-yearMonthDuration\"},"
176 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"0bf7a9876cde\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#hexBinary\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-hexBinary\"},"
177 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"0fb8\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#hexBinary\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-hexBinary\"},"
178 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"c3VyZS4=\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#base64Binary\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-rfc822Name\"},"
179 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"YXN1cmUu\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#base64Binary\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-rfc822Name\"},"
180 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"j_hibbert@medico.com\",\"DataType\":\"urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-rfc822Name\"},"
181 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"c_clown@nose_medico.com\",\"DataType\":\"urn:oasis:names:tc:xacml:1.0:data-type:rfc822Name\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-rfc822Name\"},"
182 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"CN=Julius Hibbert, O=Medi Corporation, C=US\",\"DataType\":\"urn:oasis:names:tc:xacml:1.0:data-type:x500Name\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-x500Name\"},"
183 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"CN=Crusty Clown, O=Red Nose Corporation, C=US\",\"DataType\":\"urn:oasis:names:tc:xacml:1.0:data-type:x500Name\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-x500Name\"},"
184 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"122.45.38.245/255.255.255.64:8080-8080\",\"DataType\":\"urn:oasis:names:tc:xacml:2.0:data-type:ipAddress\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-ipAddress\"},"
185 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"35.123.111.56/255.64.32.255:9999-9999\",\"DataType\":\"urn:oasis:names:tc:xacml:2.0:data-type:ipAddress\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-ipAddress\"},"
186 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"some.host.name:147-874\",\"DataType\":\"urn:oasis:names:tc:xacml:2.0:data-type:dnsName\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-dnsName\"},"
187 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"a.different.host:-45\",\"DataType\":\"urn:oasis:names:tc:xacml:2.0:data-type:dnsName\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:subject:subject-dnsName\"}"
188 //              + "]},"
189 //              + "{\"CategoryId\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\",\"Attribute\":["
190 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":{\"Namespaces\":[{\"Namespace\":\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\"},{\"Namespace\":\"http://www.medico.com/schemas/record\",\"Prefix\":\"md\"},{\"Namespace\":\"http://www.w3.org/2001/XMLSchema-instance\",\"Prefix\":\"xsi\"}],\"XPathCategory\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\",\"XPath\":\"//md:records/md:record\"},\"DataType\":\"urn:oasis:names:tc:xacml:3.0:data-type:xpathExpression\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:resource:xpathExpression\"},"
191 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":{\"Namespaces\":[{\"Namespace\":\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\"},{\"Namespace\":\"http://www.medico.com/schemas/record\",\"Prefix\":\"md\"},{\"Namespace\":\"http://www.w3.org/2001/XMLSchema-instance\",\"Prefix\":\"xsi\"}],\"XPathCategory\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\",\"XPath\":\"//md:records/md:diagnosis_info\"},\"DataType\":\"urn:oasis:names:tc:xacml:3.0:data-type:xpathExpression\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:resource:xpathExpression\"},"
192 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"http://medico.com/record/patient/BartSimpson\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#anyURI\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:resource:resource-id\"},"
193 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"http://medico.com/record/patient/HomerSimpson\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#anyURI\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:resource:resource-id\"}"
194 //              + "],"
195 //              + "\"Content\":\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><md:records xmlns:md=\\\"http://www.medico.com/schemas/record\\\">\\r\\n            \\t<md:record>\\r\\n                \\t<md:patient_info>\\r\\n                    \\t<md:name>Bart Simpson</md:name>\\r\\n                    \\t<md:age>60</md:age>\\r\\n                    \\t<md:sex>male</md:sex>\\r\\n                    \\t<md:health_insurance>123456</md:health_insurance>\\r\\n                \\t</md:patient_info>\\r\\n                \\t<md:diagnosis_info>\\r\\n                    \\t<md:diagnosis>\\r\\n                        \\t<md:item type=\\\"primary\\\">Gastric Cancer</md:item>\\r\\n                        \\t<md:item type=\\\"secondary\\\">Hyper tension</md:item>\\r\\n                    \\t</md:diagnosis>\\r\\n                    \\t<md:pathological_diagnosis>\\r\\n                        \\t<md:diagnosis>\\r\\n                            \\t<md:item type=\\\"primary\\\">Well differentiated adeno carcinoma</md:item>\\r\\n                        \\t</md:diagnosis>\\r\\n                        \\t<md:date>2000-10-05</md:date>\\r\\n                        \\t<md:malignancy type=\\\"yes\\\"/>\\r\\n                    \\t</md:pathological_diagnosis>\\r\\n                \\t</md:diagnosis_info>                \\r\\n            \\t</md:record>\\r\\n            \\t<md:record>\\r\\n                \\t<md:patient_info>\\r\\n                    \\t<md:name>Homer Simpson</md:name>\\r\\n                    \\t<md:age>80</md:age>\\r\\n                    \\t<md:sex>male</md:sex>\\r\\n                    \\t<md:health_insurance>123456</md:health_insurance>\\r\\n                \\t</md:patient_info>\\r\\n                \\t<md:diagnosis_info>\\r\\n                    \\t<md:diagnosis>\\r\\n                        \\t<md:item type=\\\"primary\\\">Gastric Cancer</md:item>\\r\\n                        \\t<md:item type=\\\"secondary\\\">Hyper tension</md:item>\\r\\n                    \\t</md:diagnosis>\\r\\n                    \\t<md:pathological_diagnosis>\\r\\n                        \\t<md:diagnosis>\\r\\n                            \\t<md:item type=\\\"primary\\\">Well differentiated adeno carcinoma</md:item>\\r\\n                        \\t</md:diagnosis>\\r\\n                        \\t<md:date>2000-10-05</md:date>\\r\\n                        \\t<md:malignancy type=\\\"yes\\\"/>\\r\\n                    \\t</md:pathological_diagnosis>\\r\\n                \\t</md:diagnosis_info>                \\r\\n            \\t</md:record>\\r\\n\\t    </md:records>\"},"
196 //              + "{\"CategoryId\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:action\",\"Attribute\":["
197 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"read\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#string\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:action:action-id\"},"
198 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"write\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#string\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:action:action-id\"}"
199 //              + "]},"
200 //              + "{\"CategoryId\":\"urn:oasis:names:tc:xacml:3.0:attribute-category:environment\",\"Attribute\":["
201 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"08:23:47-05:00\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#time\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:environment:current-time\"},"
202 //              + "{\"Issuer\":\"ConformanceTester\",\"Value\":\"22:12:10Z\",\"DataType\":\"http://www.w3.org/2001/XMLSchema#time\",\"IncludeInResult\":true,\"AttributeId\":\"urn:oasis:names:tc:xacml:1.0:environment:current-time\"}]}]"
203 //              + ",\"ReturnPolicyIdList\":false,\"CombinedDecision\":false}}";
204
205         
206                                 
207                                 
208                                 // load JSON into a Request
209                                 jsonRequest = JSONRequest.load(jsonString);
210                                 
211                                 // compare the two Request objects
212                                 
213                                 // check simple things first
214                                 assertEquals("File '" + currentFile.getName() + "' CombinedDecision", xmlRequest.getCombinedDecision(), jsonRequest.getCombinedDecision());
215                                 assertEquals("File '" + currentFile.getName() + "' getReturnPolicyIdList", xmlRequest.getReturnPolicyIdList(), jsonRequest.getReturnPolicyIdList());
216                                 assertEquals("File '" + currentFile.getName() + "' requestDefaults", xmlRequest.getRequestDefaults(), jsonRequest.getRequestDefaults());
217
218                                 // multiRequests (guaranteed to not be null)
219                                 // We do NOT care about ordering, so compare the two collections inefficiently
220                                 Collection<RequestReference> xmlCollection = xmlRequest.getMultiRequests();
221                                 Collection<RequestReference> jsonCollection = jsonRequest.getMultiRequests();
222                                 String errorMessage = null;
223                                 if (jsonCollection.size() != xmlCollection.size()) {
224                                         errorMessage = "File '" + currentFile.getName() + "' MultiRequests not same size.  ";
225                                 } else if (! jsonCollection.containsAll(xmlCollection)) {
226                                         errorMessage = "File '" + currentFile.getName() + "' MultiRequests have different contents.  ";
227                                 }
228                                 if (errorMessage != null) {
229                                         String xmlContents = "";
230                                         String jsonContents = "";
231                                         Iterator<RequestReference> rrIt = xmlCollection.iterator();
232                                         while (rrIt.hasNext()) {
233                                                 xmlContents += "\n   " + rrIt.next().toString(); 
234                                         }
235                                         rrIt = jsonCollection.iterator();
236                                         while (rrIt.hasNext()) { 
237                                                 jsonContents += "\n  " + rrIt.next().toString(); 
238                                         }
239                                         fail(errorMessage + "\nXML(" + xmlCollection.size() + ")='" + xmlContents + 
240                                                         "'  \nJSON(" + jsonCollection.size() + ")='" + jsonContents +
241                                                         "'" +
242                                                         "\njson='" + jsonString + "'");
243                                 }
244                                 
245                                 // attributes (guaranteed to not be null)
246                                 // We do NOT care about ordering, so compare the two collections inefficiently
247                                 Collection<RequestAttributes> xmlAttrCollection = xmlRequest.getRequestAttributes();
248                                 Collection<RequestAttributes> jsonAttrCollection = jsonRequest.getRequestAttributes();
249                                 errorMessage = null;
250                                 if (jsonAttrCollection.size() != xmlAttrCollection.size()) {
251                                         errorMessage = "File '" + currentFile.getName() + "' RequestAttributes not same size.  ";
252                                 } else if (! jsonAttrCollection.containsAll(xmlAttrCollection)) {
253                                         String attrName = "";
254                                         Iterator<RequestAttributes> rait = xmlAttrCollection.iterator();
255                                         while (rait.hasNext()) {
256                                                 RequestAttributes ra = rait.next();
257                                                 if (jsonAttrCollection.contains(ra) == false) {
258                                                         attrName = ra.toString();
259                                                 }
260                                         }
261                                         errorMessage = "File '" + currentFile.getName() + "' RequestAttributes have different contents.  JSON is missing attr=" + attrName;
262                                 }
263                                 if (errorMessage != null) {
264                                         String xmlContents = "";
265                                         String jsonContents = "";
266                                         Iterator<RequestAttributes> rrIt = xmlAttrCollection.iterator();
267                                         while (rrIt.hasNext()) {
268                                                 RequestAttributes ras = rrIt.next();
269                                                 xmlContents += "\n   " + ras.toString();
270                                                 if (ras.getContentRoot() != null) {
271                                                         StringWriter writer = new StringWriter();
272                                                         Transformer transformer = null;
273                                                         try {
274                                                                 transformer = TransformerFactory.newInstance().newTransformer();
275                                                                 transformer.transform(new DOMSource(ras.getContentRoot()), new StreamResult(writer));
276                                                         } catch (Exception e) {
277                                                                 throw new JSONStructureException("Unable to Content node to string; e="+e);
278                                                         }
279
280                                                         xmlContents += "\n        Content: " + writer.toString();
281                                                 }
282                                         }
283                                         rrIt = jsonAttrCollection.iterator();
284                                         while (rrIt.hasNext()) { 
285                                                 RequestAttributes ras = rrIt.next();
286                                                 jsonContents += "\n   " + ras.toString();       
287                                                 if (ras.getContentRoot() != null) {
288                                                         StringWriter writer = new StringWriter();
289                                                         Transformer transformer = null;
290                                                         try {
291                                                                 transformer = TransformerFactory.newInstance().newTransformer();
292                                                                 transformer.transform(new DOMSource(ras.getContentRoot()), new StreamResult(writer));
293                                                         } catch (Exception e) {
294                                                                 throw new JSONStructureException("Unable to Content node to string; e="+e);
295                                                         }
296
297                                                         jsonContents += "\n        Content: " + writer.toString();
298                                                 }
299                                         }
300                                         fail(errorMessage + "\nXML(" + xmlAttrCollection.size() + ")='" + xmlContents + 
301                                                         "'  \nJSON(" + jsonAttrCollection.size() + ")='" + jsonContents +
302                                                         "\njson='" + jsonString + "'");
303                                 }
304                                 
305
306                         }                       
307
308                 } catch (Exception e) {
309                         fail ("Failed test with '" + currentFile.getName() + "', e=" + e);
310                 }
311
312                 
313         }
314         
315         //
316         // HELPER to get list of all Request files in the given directory
317         //
318         
319         private List<File> getRequestsInDirectory(File directory) {
320                 List<File> fileList = new ArrayList<File>();
321                 
322                 File[] fileArray = directory.listFiles();
323                 for (File f : fileArray) {
324                         if (f.isDirectory()) {
325                                 List<File> subDirList = getRequestsInDirectory(f);
326                                 fileList.addAll(subDirList);
327                         }
328                         if (f.getName().endsWith("Request.xml")) {
329                                 fileList.add(f);
330                         }
331                 }
332                 return fileList;
333                 
334         }
335         
336 }
337
338
339
340
341 /*
342 Place to dump very long trace/exception strings that need manual editing to understand
343
344
345
346
347
348
349  */
350
351
352
353
354
355
356
357
358