2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.xacml.test.json;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
26 import org.junit.Test;
28 import com.att.research.xacml.api.Request;
29 import com.att.research.xacml.std.json.JSONRequest;
30 import com.att.research.xacml.std.json.JSONStructureException;
32 * Test JSON Request convert to object - High-level Request-as-a-whole tests including test that fills in all fields with multiple values (where appropriate)
35 * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
38 * 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
39 * 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.
40 * To simplify testing we assume that request.toString() correctly includes a complete text representation of every sub-component of the Request object
41 * and we compare the resulting String to our expected String.
42 * This has two possible sources of error:
43 * - toString might not include some sub-component, and
44 * - the initial verification of the resulting string is done by hand and may have been incorrect.
48 public class RequestMainTest {
50 // The request object output from each test conversion from JSON string
55 * Request that uses all fields with both single and multiple entries
57 String allFieldsRequest =
59 "\"ReturnPolicyIdList\" : true ," +
60 "\"CombinedDecision\" : true ," +
61 "\"XPathVersion\" : \"http://www.w3.org/TR/1999/REC-xpath-19991116\"," +
62 "\"MultiRequests\" : {" +
63 "\"RequestReference\": [" +
65 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
68 "\"ReferenceId\" : [\"foo2\",\"bar1\"]" +
74 "\"CategoryId\": \"custom-category\", " +
75 "\"Id\" : \"customId\", " +
78 "\"AttributeId\" : \"document-id\", " +
79 "\"DataType\" : \"integer\", " +
83 "\"AttributeId\" : \"document-url\", " +
84 "\"DataType\" : \"anyURI\", " +
85 "\"Value\" : \"http://somewhere.over.the.com/rainbow\" " +
88 "\"AttributeId\" : \"page-list\", " +
89 "\"Value\" : [1, 2, 3, 4.5, 3, 2, 1] " +
94 "\"CategoryId\": \"another-custom-cat\", " +
95 "\"Id\" : \"anotherXmlId\", " +
96 "\"Attribute\" : []" +
100 "\"AccessSubject\":{ " +
101 "\"Content\" : \"<?xml version=\\\"1.0\\\"?><catalog>" +
102 "<book id=\\\"bk101\\\"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre>" +
103 "<price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description>"+
104 "</book></catalog>\"," +
105 "\"Attribute\" : []" +
109 "\"Content\" : \"PD94bWwgdmVyc2lvbj0iMS4wIj8+PGNhdGFsb2c+PGJvb2sgaWQ9ImJrMTAxIj48YXV0aG9yPkdhbWJhcmRlbGxhLCBNYXR0aGV3PC9hdXRob3I+PHRpdGxlPlhNT" +
110 "CBEZXZlbG9wZXIncyBHdWlkZTwvdGl0bGU+PGdlbnJlPkNvbXB1dGVyPC9nZW5yZT48cHJpY2U+NDQuOTU8L3ByaWNlPjxwdWJsaXNoX2RhdGU+MjAwMC0xMC0wMTwvcHVibGlzaF"+
111 "9kYXRlPjxkZXNjcmlwdGlvbj5BbiBpbi1kZXB0aCBsb29rIGF0IGNyZWF0aW5nIGFwcGxpY2F0aW9ucyB3aXRoIFhNTC48L2Rlc2NyaXB0aW9uPjwvYm9vaz48L2NhdGFsb2c+\"" +
120 * The following example comes directly from the JSON Profile Spec
122 String exampleFromSpec = "{ " +
124 "\"AccessSubject\" : { " +
125 "\"Attribute\": [ " +
127 "\"Id\" : \"subject-id\", " +
128 "\"Value\" : \"Andreas\" " +
131 "\"Id\" : \"location\", " +
132 "\"Value\" : \"Gamla Stan\" " +
139 "\"Id\" : \"action-id\", " +
140 "\"Value\" : \"http://www.xacml.eu/buy\", " +
141 "\"DataType\" : \"anyURI\" " +
144 "\"Resource\" : { " +
145 "\"Attribute\": [ " +
147 "\"Id\" : \"book-title\", " +
148 "\"Value\" : \"Learn German in 90 days\" " +
151 "\"Id\" : \"currency\", " +
152 "\"Value\" : \"SEK\" " +
155 "\"Id\" : \"price\", " +
156 "\"Value\" : 123.34 " +
165 * The following example comes directly from the JSON Profile Spec (modified to include a "</Catalog>" missing from both examples).
166 * It shows the two ways of handling XPath content, as escaped XML and as Base64 encoding.
168 String xPathExampleFromSpec = "{ " +
170 "\"Resource\" : { " +
171 "\"Attribute\": [ " +
173 "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " +
174 "\"DataType\" : \"xpathExpression\", " +
176 "\"XPathCategory\" : \"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\", " +
177 "\"Namespaces\" : [{ " +
178 "\"Namespace\" : \"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" " +
181 "\"Prefix\" : \"md\", " +
182 "\"Namespace\" : \"urn:example:med:schemas:record\" " +
185 "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " +
195 // test various ways that request might be empty
197 public void testEmptyRequest() {
200 request = JSONRequest.load((String)null);
201 fail("Request should throw exception");
202 } catch (JSONStructureException e) {
204 } catch (Exception e) {
205 fail ("Failed convert from JSON to object: " + e);
210 request = JSONRequest.load((String)"");
211 fail("Request should throw exception");
212 } catch (JSONStructureException e) {
214 } catch (Exception e) {
215 fail ("Failed convert from JSON to object: " + e);
219 request = JSONRequest.load((String)" ");
220 fail("Request should throw exception");
221 } catch (JSONStructureException e) {
223 } catch (Exception e) {
224 fail ("Failed convert from JSON to object: " + e);
227 // empty JSON request
229 request = JSONRequest.load((String)"{}");
230 fail("Request should throw exception");
231 } catch (JSONStructureException e) {
233 } catch (Exception e) {
234 fail ("Failed convert from JSON to object: " + e);
238 request = JSONRequest.load((String)"{{}}");
239 fail("Request should throw exception");
240 } catch (JSONStructureException e) {
242 } catch (Exception e) {
243 fail ("Failed convert from JSON to object: " + e);
248 request = JSONRequest.load((String)"Some non-JSON string");
249 fail("Request should throw exception");
250 } catch (JSONStructureException e) {
252 } catch (Exception e) {
253 fail ("Failed convert from JSON to object: " + e);
257 request = JSONRequest.load((String)"{something non-JSON}");
258 fail("Request should throw exception");
259 } catch (JSONStructureException e) {
261 } catch (Exception e) {
262 fail ("Failed convert from JSON to object: " + e);
265 // bad syntax (Request with no content)
267 request = JSONRequest.load((String)"{\"Request\"}");
268 fail("Request should throw exception");
269 } catch (JSONStructureException e) {
271 } catch (Exception e) {
272 fail ("Failed convert from JSON to object: " + e);
275 // bad syntax (no :field after Request)
277 request = JSONRequest.load((String)"{\"Request\" : }");
278 fail("Request should throw exception");
279 } catch (JSONStructureException e) {
281 } catch (Exception e) {
282 fail ("Failed convert from JSON to object: " + e);
285 // bad syntax (no " around Request)
287 request = JSONRequest.load((String)"{Request}");
288 fail("Request should throw exception");
289 } catch (JSONStructureException e) {
291 } catch (Exception e) {
292 fail ("Failed convert from JSON to object: " + e);
295 // empty content in Request
297 request = JSONRequest.load((String)"{\"Request\" : \"\"}");
298 fail("Request should throw exception");
299 } catch (JSONStructureException e) {
301 } catch (Exception e) {
302 fail ("Failed convert from JSON to object: " + e);
305 // content is not an object
307 request = JSONRequest.load((String)"{\"Request\" : \"CombinedDecision\" : true }");
308 fail("Request should throw exception");
309 } catch (JSONStructureException e) {
311 } catch (Exception e) {
312 fail ("Failed convert from JSON to object: " + e);
316 // Jackson parser does not treat this as an error
318 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \"http://www.w3.org/TR/1999/REC-xpath-19991116\"}}}}}");
319 assertEquals("{requestDefaults={xpatherVersion=http://www.w3.org/TR/1999/REC-xpath-19991116},returnPolicyIdList=false,combinedDecision=false}", request.toString());
320 } catch (Exception e) {
321 fail ("Failed convert from JSON to object: " + e);
326 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \"http://www.w3.org/TR/1999/REC-xpath-19991116\" }");
327 fail("Request should throw exception");
328 } catch (JSONStructureException e) {
330 } catch (Exception e) {
331 fail ("Failed convert from JSON to object: " + e);
335 // misplaced } in middle
337 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : } \"http://www.w3.org/TR/1999/REC-xpath-19991116\"}}}}}");
338 fail("Request should throw exception");
339 } catch (JSONStructureException e) {
341 } catch (Exception e) {
342 fail ("Failed convert from JSON to object: " + e);
350 // Test double braces around request
352 public void testDoubleBraces() {
355 request = JSONRequest.load((String)"{{\"Request\" }");
356 fail("Request should throw exception");
357 } catch (JSONStructureException e) {
359 } catch (Exception e) {
360 fail ("Failed convert from JSON to object: " + e);
364 request = JSONRequest.load((String)"{{\"Request\" : }");
365 fail("Request should throw exception");
366 } catch (JSONStructureException e) {
368 } catch (Exception e) {
369 fail ("Failed convert from JSON to object: " + e);
373 request = JSONRequest.load((String)"{{\"Request\" : {\"CombinedDecision\" : true }}");
374 fail("Request should throw exception");
375 } catch (JSONStructureException e) {
377 } catch (Exception e) {
378 fail ("Failed convert from JSON to object: " + e);
384 // test elements missing from top-level Request and arrays where single elements should be
386 public void testMissingFields() {
388 // Request containing empty array
390 request = JSONRequest.load((String)"{\"Request\" : []}");
391 fail("Request should throw exception");
392 } catch (JSONStructureException e) {
394 } catch (Exception e) {
395 fail ("Failed convert from JSON to object: " + e);
398 // array of one element
400 request = JSONRequest.load((String)"{\"Request\" : [{\"CombinedDecision\" : true }]}");
401 fail("Request should throw exception");
402 } catch (JSONStructureException e) {
404 } catch (Exception e) {
405 fail ("Failed convert from JSON to object: " + e);
408 // correctly formatted empty request gives request with defaults set
410 request = JSONRequest.load((String)"{\"Request\" : { }}");
411 assertEquals("{returnPolicyIdList=false,combinedDecision=false}", request.toString());
412 } catch (Exception e) {
413 fail ("Failed convert from JSON to object: " + e);
417 // space in front of name (inside quotes)
419 request = JSONRequest.load((String)"{\" Request\" : {\"XPathVersion\" : \"http://some/other/default/uri\" }}");
420 fail("Request should throw exception");
421 } catch (JSONStructureException e) {
423 } catch (Exception e) {
424 fail ("Failed convert from JSON to object: " + e);
427 // space at end of name (inside quotes)
429 request = JSONRequest.load((String)"{\"Request \" : {\"XPathVersion\" : \"http://some/other/default/uri\" }}");
430 fail("Request should throw exception");
431 } catch (JSONStructureException e) {
433 } catch (Exception e) {
434 fail ("Failed convert from JSON to object: " + e);
437 // space in front of value (inside quotes) - valid String but not valid URI
439 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \" http://some/other/default/uri\" }}");
440 fail("Request should throw exception");
441 } catch (JSONStructureException e) {
443 } catch (Exception e) {
444 fail ("Failed convert from JSON to object: " + e);
447 // space at end of value (inside quotes) - valid String but not valid URI
449 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \"http://some/other/default/uri \" }}");
450 fail("Request should throw exception");
451 } catch (JSONStructureException e) {
453 } catch (Exception e) {
454 fail ("Failed convert from JSON to object: " + e);
461 // test just one of each top-level element.
462 // For simple elements also test for incorrect type
464 public void testTopLevelElements() {
468 request = JSONRequest.load((String)"{\"Request\" : {}}");
469 assertEquals("{returnPolicyIdList=false,combinedDecision=false}", request.toString());
470 } catch (Exception e) {
471 fail ("Failed convert from JSON to object: " + e);
476 // ReturnPolicyIdList
478 request = JSONRequest.load((String)"{\"Request\" : {\"ReturnPolicyIdList\" : true }}");
479 assertEquals("{returnPolicyIdList=true,combinedDecision=false}", request.toString());
480 } catch (Exception e) {
481 fail ("Failed convert from JSON to object: " + e);
485 request = JSONRequest.load((String)"{\"Request\" : {\"ReturnPolicyIdList\" : \"abc\" }}");
486 fail("Request should throw exception");
487 } catch (JSONStructureException e) {
489 } catch (Exception e) {
490 fail ("Failed convert from JSON to object: " + e);
493 request = JSONRequest.load((String)"{\"Request\" : {\"ReturnPolicyIdList\" : 123 }}");
494 fail("Request should throw exception");
495 } catch (JSONStructureException e) {
497 } catch (Exception e) {
498 fail ("Failed convert from JSON to object: " + e);
503 request = JSONRequest.load((String)"{\"Request\" : { \"CombinedDecision\" : true }}");
504 assertEquals("{returnPolicyIdList=false,combinedDecision=true}", request.toString());
505 } catch (Exception e) {
506 fail ("Failed convert from JSON to object: " + e);
510 request = JSONRequest.load((String)"{\"Request\" : {\"CombinedDecision\" : \"abc\" }}");
511 fail("Request should throw exception");
512 } catch (JSONStructureException e) {
514 } catch (Exception e) {
515 fail ("Failed convert from JSON to object: " + e);
518 request = JSONRequest.load((String)"{\"Request\" : {\"CombinedDecision\" : 123 }}");
519 fail("Request should throw exception");
520 } catch (JSONStructureException e) {
522 } catch (Exception e) {
523 fail ("Failed convert from JSON to object: " + e);
528 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \"http://some/other/default/uri\" }}");
529 assertEquals("{requestDefaults={xpatherVersion=http://some/other/default/uri},returnPolicyIdList=false,combinedDecision=false}", request.toString());
530 } catch (Exception e) {
531 fail ("Failed convert from JSON to object: " + e);
535 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : true }}");
536 fail("Request should throw exception");
537 } catch (JSONStructureException e) {
539 } catch (Exception e) {
540 fail ("Failed convert from JSON to object: " + e);
543 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : 123 }}");
544 fail("Request should throw exception");
545 } catch (JSONStructureException e) {
547 } catch (Exception e) {
548 fail ("Failed convert from JSON to object: " + e);
552 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \"not a uri\" }}");
553 fail("Request should throw exception");
554 } catch (JSONStructureException e) {
556 } catch (Exception e) {
557 fail ("Failed convert from JSON to object: " + e);
563 request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
564 "\"CategoryId\": \"another-custom-cat\", " +
565 "\"Id\" : \"anotherXmlId\", " +
566 "\"Attribute\" : []" +
569 assertEquals("{returnPolicyIdList=false,combinedDecision=false,requestAttributes=[{super={category=another-custom-cat},xmlId=anotherXmlId}]}", request.toString());
570 } catch (Exception e) {
571 fail ("Failed convert from JSON to object: " + e);
576 request = JSONRequest.load((String)"{\"Request\" : { \"AccessSubject\":{ }}}");
577 assertEquals("{returnPolicyIdList=false,combinedDecision=false,requestAttributes=[{super={category=urn:oasis:names:tc:xacml:1.0:subject-category:access-subject}}]}", request.toString());
578 } catch (Exception e) {
579 fail ("Failed convert from JSON to object: " + e);
584 request = JSONRequest.load((String)"{\"Request\" : { \"Action\":{ }}}");
585 assertEquals("{returnPolicyIdList=false,combinedDecision=false,requestAttributes=[{super={category=urn:oasis:names:tc:xacml:3.0:attribute-category:action}}]}", request.toString());
586 } catch (Exception e) {
587 fail ("Failed convert from JSON to object: " + e);
592 request = JSONRequest.load((String)"{\"Request\" : {\"Resource\":{ }}}");
593 assertEquals("{returnPolicyIdList=false,combinedDecision=false,requestAttributes=[{super={category=urn:oasis:names:tc:xacml:3.0:attribute-category:resource}}]}", request.toString());
594 } catch (Exception e) {
595 fail ("Failed convert from JSON to object: " + e);
600 request = JSONRequest.load((String)"{\"Request\" : {\"Environment\":{ } }}");
601 assertEquals("{returnPolicyIdList=false,combinedDecision=false,requestAttributes=[{super={category=urn:oasis:names:tc:xacml:3.0:attribute-category:environment}}]}", request.toString());
602 } catch (Exception e) {
603 fail ("Failed convert from JSON to object: " + e);
608 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
609 "\"RequestReference\": [" +
611 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
614 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
617 assertEquals("{returnPolicyIdList=false,combinedDecision=false,multiRequests=[{requestAttributesReferences=[{referenceId=foo1}{referenceId=bar1}]}{requestAttributesReferences=[{referenceId=foo2}{referenceId=bar2}]}]}", request.toString());
618 } catch (Exception e) {
619 fail ("Failed convert from JSON to object: " + e);
622 // MultiRequest with 1
624 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
625 "\"RequestReference\": [" +
627 "\"ReferenceId\" : [\"bar2\"]" +
630 assertEquals("{returnPolicyIdList=false,combinedDecision=false,multiRequests=[{requestAttributesReferences=[{referenceId=bar2}]}]}", request.toString());
631 } catch (Exception e) {
632 fail ("Failed convert from JSON to object: " + e);
635 // MultiRequest with RequestReferences with no ReferenceId
637 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
638 "\"RequestReference\": [" +
640 "\"ReferenceId\" : []" +
643 assertEquals("{returnPolicyIdList=false,combinedDecision=false}", request.toString());
644 } catch (Exception e) {
645 fail ("Failed convert from JSON to object: " + e);
649 // MultiRequests with no RequestReference
651 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
652 "\"RequestReference\": []" +
654 fail("Request should throw exception");
655 } catch (JSONStructureException e) {
657 } catch (Exception e) {
658 fail ("Failed convert from JSON to object: " + e);
661 // MultiRequests with something other than RequestReference
663 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
664 "\"SomeOtherAttribute\": 123" +
666 fail("Request should throw exception");
667 } catch (JSONStructureException e) {
669 } catch (Exception e) {
670 fail ("Failed convert from JSON to object: " + e);
673 // MultiRequests with single RequestReference rather than array
675 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
676 "\"RequestReference\": " +
678 "\"ReferenceId\" : []" +
681 fail("Request should throw exception");
682 } catch (JSONStructureException e) {
684 } catch (Exception e) {
685 fail ("Failed convert from JSON to object: " + e);
688 // MultiRequests with RequestReference containing single element instead of array
690 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
691 "\"RequestReference\": [" +
693 "\"ReferenceId\" : \"foo1\"" +
696 fail("Request should throw exception");
697 } catch (JSONStructureException e) {
699 } catch (Exception e) {
700 fail ("Failed convert from JSON to object: " + e);
703 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
704 "\"RequestReference\": [" +
706 "\"ReferenceId\" : {\"foo1\"}" +
709 fail("Request should throw exception");
710 } catch (JSONStructureException e) {
712 } catch (Exception e) {
713 fail ("Failed convert from JSON to object: " + e);
716 // MultiRequests with component that is not a RequestReference
718 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
719 "\"RequestReference\": [" +
721 "\"SomeOtherAttribute\" : [\"foo1\",\"bar1\"]" +
724 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
727 fail("Request should throw exception");
728 } catch (JSONStructureException e) {
730 } catch (Exception e) {
731 fail ("Failed convert from JSON to object: " + e);
734 // MultiRequests with component that is not a RequestReference
736 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
737 "\"RequestReference\": [" +
739 "\"ReferenceId\" : [\"foo2\",\"bar2\"]," +
740 "\"SomeOtherAttribute\" : [\"foo1\",\"bar1\"]" +
743 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
746 fail("Request should throw exception");
747 } catch (JSONStructureException e) {
749 } catch (Exception e) {
750 fail ("Failed convert from JSON to object: " + e);
753 // MultiRequest with unknown elements (in addition to RequestReference)
755 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
756 "\"SomeOtherAttribute\": 123," +
757 "\"RequestReference\": [" +
759 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
762 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
765 fail("Request should throw exception");
766 } catch (JSONStructureException e) {
768 } catch (Exception e) {
769 fail ("Failed convert from JSON to object: " + e);
772 // MultiRequest with RequestReferences with ReferenceId NOT a string
774 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
775 "\"RequestReference\": [" +
777 "\"ReferenceId\" : [ true ]" +
780 fail("Request should throw exception");
781 } catch (JSONStructureException e) {
783 } catch (Exception e) {
784 fail ("Failed convert from JSON to object: " + e);
788 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
789 "\"RequestReference\": [" +
791 "\"ReferenceId\" : [ 123 ]" +
794 fail("Request should throw exception");
795 } catch (JSONStructureException e) {
797 } catch (Exception e) {
798 fail ("Failed convert from JSON to object: " + e);
802 // Cannot test with ReferenceId that is NOT referring to a Category object Id property because we may not have read the Category objects yet.
803 // Need to leave this up to the PDP.
806 // extra elements in top-level
808 request = JSONRequest.load((String)"{\"Request\" : {}, \"unknownElement\" : false, \"unk2\" : \"abc\", \"unk3\" : 123 }}");
809 fail("Unknown element should throw exception");
810 } catch (JSONStructureException e) {
812 } catch (Exception e) {
813 fail ("Failed convert from JSON to object: " + e);
816 // extra elements in Request
818 request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \"http://www.w3.org/TR/1999/REC-xpath-19991116\", \"unknownElement\" : false }}");
819 fail("Unknown element should throw exception");
820 } catch (JSONStructureException e) {
822 } catch (Exception e) {
823 fail ("Failed convert from JSON to object: " + e);
828 // Test with every field filled in with multiple values where appropriate
830 public void testAllFieldsRequest() {
832 // convert Response to JSON
834 request = JSONRequest.load(allFieldsRequest);
835 assertEquals("{requestDefaults={xpatherVersion=http://www.w3.org/TR/1999/REC-xpath-19991116},returnPolicyIdList=true,combinedDecision=true,requestAttributes=[{super={category=custom-category,attributes=[{attributeId=document-id,category=custom-category,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#integer,value=123}],includeInResults=false}{attributeId=document-url,category=custom-category,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#anyURI,value=http://somewhere.over.the.com/rainbow}],includeInResults=false}{attributeId=page-list,category=custom-category,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#double,value=1.0}{dataTypeId=http://www.w3.org/2001/XMLSchema#double,value=2.0}{dataTypeId=http://www.w3.org/2001/XMLSchema#double,value=3.0}{dataTypeId=http://www.w3.org/2001/XMLSchema#double,value=4.5}{dataTypeId=http://www.w3.org/2001/XMLSchema#double,value=3.0}{dataTypeId=http://www.w3.org/2001/XMLSchema#double,value=2.0}{dataTypeId=http://www.w3.org/2001/XMLSchema#double,value=1.0}],includeInResults=false}]},xmlId=customId}{super={category=another-custom-cat},xmlId=anotherXmlId}{super={category=urn:oasis:names:tc:xacml:1.0:subject-category:access-subject},contentRoot=[catalog: null]}{super={category=urn:oasis:names:tc:xacml:3.0:attribute-category:resource},contentRoot=[catalog: null]}],multiRequests=[{requestAttributesReferences=[{referenceId=foo1}{referenceId=bar1}]}{requestAttributesReferences=[{referenceId=foo2}{referenceId=bar1}]}]}"
836 , request.toString());
837 } catch (Exception e) {
838 fail ("Failed convert from JSON to object: " + e);
842 // convert example request from spec
844 request = JSONRequest.load(exampleFromSpec);
845 assertEquals("{returnPolicyIdList=false,combinedDecision=false,requestAttributes=[{super={category=urn:oasis:names:tc:xacml:1.0:subject-category:access-subject,attributes=[{attributeId=subject-id,category=urn:oasis:names:tc:xacml:1.0:subject-category:access-subject,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#string,value=Andreas}],includeInResults=false}{attributeId=location,category=urn:oasis:names:tc:xacml:1.0:subject-category:access-subject,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#string,value=Gamla Stan}],includeInResults=false}]}}{super={category=urn:oasis:names:tc:xacml:3.0:attribute-category:action,attributes=[{attributeId=action-id,category=urn:oasis:names:tc:xacml:3.0:attribute-category:action,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#anyURI,value=http://www.xacml.eu/buy}],includeInResults=false}]}}{super={category=urn:oasis:names:tc:xacml:3.0:attribute-category:resource,attributes=[{attributeId=book-title,category=urn:oasis:names:tc:xacml:3.0:attribute-category:resource,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#string,value=Learn German in 90 days}],includeInResults=false}{attributeId=currency,category=urn:oasis:names:tc:xacml:3.0:attribute-category:resource,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#string,value=SEK}],includeInResults=false}{attributeId=price,category=urn:oasis:names:tc:xacml:3.0:attribute-category:resource,values=[{dataTypeId=http://www.w3.org/2001/XMLSchema#double,value=123.34}],includeInResults=false}]}}]}", request.toString());
846 } catch (Exception e) {
847 fail ("Failed convert from JSON to object: " + e);
850 // convert example request from spec containing XPAthExpression
852 request = JSONRequest.load(xPathExampleFromSpec);
853 assertEquals("{returnPolicyIdList=false,combinedDecision=false,requestAttributes=[{super={category=urn:oasis:names:tc:xacml:3.0:attribute-category:resource,attributes=[{attributeId=urn:oasis:names:tc:xacml:3.0:content-selector,category=urn:oasis:names:tc:xacml:3.0:attribute-category:resource,values=[{dataTypeId=urn:oasis:names:tc:xacml:3.0:data-type:xpathExpression,value={path=md:record/md:patient/md:patientDoB,Namespace={[{md,urn:example:med:schemas:record}{urn:oasis:names:tc:xacml:3.0:core:schema:wd-17}]},status=null,xpathExpressionWrapped=null},xpathCategory=urn:oasis:names:tc:xacml:3.0:attribute-category:resource}],includeInResults=false}]}}]}", request.toString());
854 } catch (Exception e) {
855 fail ("Failed convert from JSON to object: " + e);
863 // Duplicates - Each element duplicated
865 public void testDuplicates() {
868 request = JSONRequest.load((String)"{\"Request\" : {}, \"Request\" : {}}");
869 fail("Unknown element should throw exception");
870 } catch (JSONStructureException e) {
872 } catch (Exception e) {
873 fail ("Failed convert from JSON to object: " + e);
878 request = JSONRequest.load((String)"{\"Request\" : {\"ReturnPolicyIdList\" : true, \"ReturnPolicyIdList\" : true }}");
879 fail("Unknown element should throw exception");
880 } catch (JSONStructureException e) {
882 } catch (Exception e) {
883 fail ("Failed convert from JSON to object: " + e);
887 request = JSONRequest.load((String)"{\"Request\" : { \"CombinedDecision\" : true, \"CombinedDecision\" : true }}");
888 fail("Unknown element should throw exception");
889 } catch (JSONStructureException e) {
891 } catch (Exception e) {
892 fail ("Failed convert from JSON to object: " + e);
897 request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
898 "\"CategoryId\": \"another-custom-cat\", " +
899 "\"Id\" : \"anotherXmlId\", " +
900 "\"Attribute\" : []" +
903 + "\"Category\": [{ " +
904 "\"CategoryId\": \"another-custom-cat\", " +
905 "\"Id\" : \"anotherXmlId\", " +
906 "\"Attribute\" : []" +
909 assertEquals("{returnPolicyIdList=false,combinedDecision=false,requestAttributes=[{super={category=another-custom-cat},xmlId=anotherXmlId}]}", request.toString());
910 fail("Unknown element should throw exception");
911 } catch (JSONStructureException e) {
913 } catch (Exception e) {
914 fail ("Failed convert from JSON to object: " + e);
918 request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
919 "\"CategoryId\": \"another-custom-cat\", " +
920 "\"CategoryId\": \"another-custom-cat\", " +
921 "\"Id\" : \"anotherXmlId\", " +
922 "\"Attribute\" : []" +
925 fail("Unknown element should throw exception");
926 } catch (JSONStructureException e) {
928 } catch (Exception e) {
929 fail ("Failed convert from JSON to object: " + e);
933 request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
934 "\"CategoryId\": \"another-custom-cat\", " +
935 "\"Id\" : \"anotherXmlId\", " +
936 "\"Id\" : \"anotherXmlId\", " +
937 "\"Attribute\" : []" +
940 fail("Unknown element should throw exception");
941 } catch (JSONStructureException e) {
943 } catch (Exception e) {
944 fail ("Failed convert from JSON to object: " + e);
948 request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
949 "\"CategoryId\": \"another-custom-cat\", " +
950 "\"Id\" : \"anotherXmlId\", " +
951 "\"Attribute\" : []" +
952 "\"Attribute\" : []" +
955 fail("Unknown element should throw exception");
956 } catch (JSONStructureException e) {
958 } catch (Exception e) {
959 fail ("Failed convert from JSON to object: " + e);
965 request = JSONRequest.load((String)"{\"Request\" : { \"AccessSubject\":{ }, \"AccessSubject\":{ }}}");
966 fail("Unknown element should throw exception");
967 } catch (JSONStructureException e) {
969 } catch (Exception e) {
970 fail ("Failed convert from JSON to object: " + e);
975 request = JSONRequest.load((String)"{\"Request\" : { \"Action\":{ }, \"Action\":{ }}}");
976 fail("Unknown element should throw exception");
977 } catch (JSONStructureException e) {
979 } catch (Exception e) {
980 fail ("Failed convert from JSON to object: " + e);
985 request = JSONRequest.load((String)"{\"Request\" : {\"Resource\":{ }, \"Resource\":{ }}}");
986 fail("Unknown element should throw exception");
987 } catch (JSONStructureException e) {
989 } catch (Exception e) {
990 fail ("Failed convert from JSON to object: " + e);
995 request = JSONRequest.load((String)"{\"Request\" : {\"Environment\":{ }, \"Environment\":{ } }}");
996 fail("Unknown element should throw exception");
997 } catch (JSONStructureException e) {
999 } catch (Exception e) {
1000 fail ("Failed convert from JSON to object: " + e);
1007 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
1008 "\"RequestReference\": [" +
1010 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1013 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1016 + "\"MultiRequests\" : {" +
1017 "\"RequestReference\": [" +
1019 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1022 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1025 fail("Unknown element should throw exception");
1026 } catch (JSONStructureException e) {
1028 } catch (Exception e) {
1029 fail ("Failed convert from JSON to object: " + e);
1033 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
1034 "\"RequestReference\": [" +
1036 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1039 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1041 "\"RequestReference\": [" +
1043 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1046 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1049 fail("Unknown element should throw exception");
1050 } catch (JSONStructureException e) {
1052 } catch (Exception e) {
1053 fail ("Failed convert from JSON to object: " + e);
1057 request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
1058 "\"RequestReference\": [" +
1060 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1061 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1064 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1067 fail("Unknown element should throw exception");
1068 } catch (JSONStructureException e) {
1070 } catch (Exception e) {
1071 fail ("Failed convert from JSON to object: " + e);