Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-TEST / src / test / java / org / openecomp / policy / pdp / test / std / json / RequestMainTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-TEST
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.pdp.test.std.json;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
26 import org.junit.Test;
27
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;
31 /**
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)
33  * 
34  * TO RUN - use jUnit
35  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
36  * 
37  * NOTE:
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.
45  * 
46  *
47  */
48 public class RequestMainTest {
49         
50         // The request object output from each test conversion from JSON string
51         Request request;
52
53         
54         /*
55          * Request that uses all fields with both single and multiple  entries
56          */
57         String allFieldsRequest = 
58                         "{\"Request\": {" +
59                                         "\"ReturnPolicyIdList\" : true ," +
60                                         "\"CombinedDecision\" : true ," +
61                                         "\"XPathVersion\" : \"http://www.w3.org/TR/1999/REC-xpath-19991116\"," +
62                                         "\"MultiRequests\" : {" +
63                                     "\"RequestReference\": [" +
64                                         "{ " +
65                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
66                                         "}," +
67                                         "{" +
68                                                 "\"ReferenceId\" : [\"foo2\",\"bar1\"]" +
69                                         "}]" +   
70                                   "}," +
71
72                                         "\"Category\": [" +
73                                                 "{ " +
74                                                 "\"CategoryId\": \"custom-category\", " +
75                                                 "\"Id\" : \"customId\", " +
76                                                 "\"Attribute\" : [" +
77                                                         "{" +
78                                                                 "\"AttributeId\"                : \"document-id\", " +
79                                                                 "\"DataType\"   : \"integer\", " +
80                                                                 "\"Value\"      : 123 " +
81                                                                 "}, " +
82                                                                 "{" +
83                                                                 "\"AttributeId\"                : \"document-url\", " +
84                                                                 "\"DataType\"   : \"anyURI\", " +
85                                                                 "\"Value\"      : \"http://somewhere.over.the.com/rainbow\" " +
86                                                         "}, " +
87                                                                 "{" +
88                                                                 "\"AttributeId\"                : \"page-list\", " +
89                                                                 "\"Value\"      : [1, 2, 3, 4.5, 3, 2, 1] " +
90                                                         "} " +
91                                                 "]" +
92                                             "}, " +
93                                             "{ " +
94                                                 "\"CategoryId\": \"another-custom-cat\", " +
95                                                 "\"Id\" : \"anotherXmlId\", " +
96                                                 "\"Attribute\" : []" +
97                                             "} " +
98                                         "], " +
99                                             
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\" : []" +
106                                         "}, " +
107                                         
108                                         "\"Resource\" : {" +
109                                                 "\"Content\" : \"PD94bWwgdmVyc2lvbj0iMS4wIj8+PGNhdGFsb2c+PGJvb2sgaWQ9ImJrMTAxIj48YXV0aG9yPkdhbWJhcmRlbGxhLCBNYXR0aGV3PC9hdXRob3I+PHRpdGxlPlhNT" +
110                                                         "CBEZXZlbG9wZXIncyBHdWlkZTwvdGl0bGU+PGdlbnJlPkNvbXB1dGVyPC9nZW5yZT48cHJpY2U+NDQuOTU8L3ByaWNlPjxwdWJsaXNoX2RhdGU+MjAwMC0xMC0wMTwvcHVibGlzaF"+
111                                                         "9kYXRlPjxkZXNjcmlwdGlvbj5BbiBpbi1kZXB0aCBsb29rIGF0IGNyZWF0aW5nIGFwcGxpY2F0aW9ucyB3aXRoIFhNTC48L2Rlc2NyaXB0aW9uPjwvYm9vaz48L2NhdGFsb2c+\"" +
112
113
114                                         "} " +
115
116                                   
117                         "}}";
118         
119         /*
120          * The following example comes directly from the JSON Profile Spec
121          */
122         String exampleFromSpec = "{ " +
123                         "\"Request\" : { " +
124                                 "\"AccessSubject\" : { " +
125                                         "\"Attribute\": [ " +
126                                                 "{ " +
127                                                         "\"Id\" : \"subject-id\", " +
128                                                         "\"Value\" : \"Andreas\" " +
129                                                 "}, " +
130                                                 "{ " +
131                                                         "\"Id\" : \"location\", " +
132                                                         "\"Value\" : \"Gamla Stan\" " +
133                                                 "} " +
134                                         "] " +
135                                 "}, " +
136                                 "\"Action\" : { " +
137                                         "\"Attribute\":  " +
138                                                 "{ " +
139                                                         "\"Id\" : \"action-id\", " +
140                                                         "\"Value\" : \"http://www.xacml.eu/buy\", " +
141                                                         "\"DataType\" : \"anyURI\" " +
142                                                 "} " +
143                                 "}, " +
144                                 "\"Resource\" : { " +
145                                         "\"Attribute\": [ " +
146                                                 "{ " +
147                                                         "\"Id\" : \"book-title\", " +
148                                                         "\"Value\" : \"Learn German in 90 days\" " +
149                                                 "}, " +
150                                                 "{ " +
151                                                         "\"Id\" : \"currency\", " +
152                                                         "\"Value\" : \"SEK\" " +
153                                                 "}, " +
154                                                 "{ " +
155                                                         "\"Id\" : \"price\", " +
156                                                         "\"Value\" : 123.34 " +
157                                                 "} " +
158                                                 "] " +
159                                         "} " +
160                                 "} " +
161                         "} ";
162
163         
164         /*
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.
167          */
168         String xPathExampleFromSpec = "{ " +
169                         "\"Request\" : { " +
170                                 "\"Resource\" : { " +
171                                         "\"Attribute\": [ " +
172                                                 "{ " +
173                                                         "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " +
174                                             "\"DataType\" : \"xpathExpression\", " +
175                                             "\"Value\" : { " +
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\" " +
179                                                         "}, " +
180                                                     "{ " +
181                                                         "\"Prefix\" : \"md\", " +
182                                                         "\"Namespace\" : \"urn:example:med:schemas:record\" " +
183                                                     "} " +
184                                                 "], " +
185                                                 "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " +
186                                             "} " +
187                                         "} " +
188                                         "] " +
189                                 "} " +
190                         "} " +
191                 "} ";
192
193         
194         
195         // test various ways that request might be empty
196         @Test
197         public void testEmptyRequest() {
198                 // null request
199                 try {
200                         request = JSONRequest.load((String)null);
201                         fail("Request should throw exception");
202                 } catch (JSONStructureException e) {
203                         // correct response
204                 } catch (Exception e) {
205                         fail ("Failed convert from JSON to object: " + e);
206                 }
207                 
208                 // empty request
209                 try {
210                         request = JSONRequest.load((String)"");
211                         fail("Request should throw exception");
212                 } catch (JSONStructureException e) {
213                         // correct response
214                 } catch (Exception e) {
215                         fail ("Failed convert from JSON to object: " + e);
216                 }
217                 
218                 try {
219                         request = JSONRequest.load((String)" ");
220                         fail("Request should throw exception");
221                 } catch (JSONStructureException e) {
222                         // correct response
223                 } catch (Exception e) {
224                         fail ("Failed convert from JSON to object: " + e);
225                 }
226                 
227                 // empty JSON request
228                 try {
229                         request = JSONRequest.load((String)"{}");
230                         fail("Request should throw exception");
231                 } catch (JSONStructureException e) {
232                         // correct response
233                 } catch (Exception e) {
234                         fail ("Failed convert from JSON to object: " + e);
235                 }
236                 
237                 try {
238                         request = JSONRequest.load((String)"{{}}");
239                         fail("Request should throw exception");
240                 } catch (JSONStructureException e) {
241                         // correct response
242                 } catch (Exception e) {
243                         fail ("Failed convert from JSON to object: " + e);
244                 }
245                 
246                 // garbage input
247                 try {
248                         request = JSONRequest.load((String)"Some non-JSON string");
249                         fail("Request should throw exception");
250                 } catch (JSONStructureException e) {
251                         // correct response
252                 } catch (Exception e) {
253                         fail ("Failed convert from JSON to object: " + e);
254                 }
255                 
256                 try {
257                         request = JSONRequest.load((String)"{something non-JSON}");
258                         fail("Request should throw exception");
259                 } catch (JSONStructureException e) {
260                         // correct response
261                 } catch (Exception e) {
262                         fail ("Failed convert from JSON to object: " + e);
263                 }
264                 
265                 // bad syntax (Request with no content)
266                 try {
267                         request = JSONRequest.load((String)"{\"Request\"}");
268                         fail("Request should throw exception");
269                 } catch (JSONStructureException e) {
270                         // correct response
271                 } catch (Exception e) {
272                         fail ("Failed convert from JSON to object: " + e);
273                 }
274                 
275                 // bad syntax (no :field after Request)
276                 try {
277                         request = JSONRequest.load((String)"{\"Request\" : }");
278                         fail("Request should throw exception");
279                 } catch (JSONStructureException e) {
280                         // correct response
281                 } catch (Exception e) {
282                         fail ("Failed convert from JSON to object: " + e);
283                 }
284                 
285                 // bad syntax (no " around Request)
286                 try {
287                         request = JSONRequest.load((String)"{Request}");
288                         fail("Request should throw exception");
289                 } catch (JSONStructureException e) {
290                         // correct response
291                 } catch (Exception e) {
292                         fail ("Failed convert from JSON to object: " + e);
293                 }
294                 
295                 // empty content in Request
296                 try {
297                         request = JSONRequest.load((String)"{\"Request\" : \"\"}");
298                         fail("Request should throw exception");
299                 } catch (JSONStructureException e) {
300                         // correct response
301                 } catch (Exception e) {
302                         fail ("Failed convert from JSON to object: " + e);
303                 }
304                 
305                 // content is not an object
306                 try {
307                         request = JSONRequest.load((String)"{\"Request\" : \"CombinedDecision\" : true }");
308                         fail("Request should throw exception");
309                 } catch (JSONStructureException e) {
310                         // correct response
311                 } catch (Exception e) {
312                         fail ("Failed convert from JSON to object: " + e);
313                 }
314                 
315                 // too many } at end
316                 // Jackson parser does not treat this as an error
317                 try {
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);
322                 }
323                 
324                 // too few } at end
325                 try {
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) {
329                         // correct response
330                 } catch (Exception e) {
331                         fail ("Failed convert from JSON to object: " + e);
332                 }
333                 
334                 
335                 // misplaced } in middle
336                 try {
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) {
340                         // correct response
341                 } catch (Exception e) {
342                         fail ("Failed convert from JSON to object: " + e);
343                 }
344                 
345
346         }
347         
348
349         
350         // Test double braces around request
351         @Test
352         public void testDoubleBraces() {
353                 
354                 try {
355                         request = JSONRequest.load((String)"{{\"Request\" }");
356                         fail("Request should throw exception");
357                 } catch (JSONStructureException e) {
358                         // correct response
359                 } catch (Exception e) {
360                         fail ("Failed convert from JSON to object: " + e);
361                 }
362                 
363                 try {
364                         request = JSONRequest.load((String)"{{\"Request\" : }");
365                         fail("Request should throw exception");
366                 } catch (JSONStructureException e) {
367                         // correct response
368                 } catch (Exception e) {
369                         fail ("Failed convert from JSON to object: " + e);
370                 }
371                 
372                 try {
373                         request = JSONRequest.load((String)"{{\"Request\" : {\"CombinedDecision\" : true }}");
374                         fail("Request should throw exception");
375                 } catch (JSONStructureException e) {
376                         // correct response
377                 } catch (Exception e) {
378                         fail ("Failed convert from JSON to object: " + e);
379                 }
380         }
381         
382         
383         
384         // test elements missing from top-level Request and arrays where single elements should be
385         @Test
386         public void testMissingFields() {
387                 
388                 // Request containing empty array
389                 try {
390                         request = JSONRequest.load((String)"{\"Request\" : []}");
391                         fail("Request should throw exception");
392                 } catch (JSONStructureException e) {
393                         // correct response
394                 } catch (Exception e) {
395                         fail ("Failed convert from JSON to object: " + e);
396                 }
397                 
398                 // array of one element
399                 try {
400                         request = JSONRequest.load((String)"{\"Request\" : [{\"CombinedDecision\" : true }]}");
401                         fail("Request should throw exception");
402                 } catch (JSONStructureException e) {
403                         // correct response
404                 } catch (Exception e) {
405                         fail ("Failed convert from JSON to object: " + e);
406                 }
407                 
408                 // correctly formatted empty request gives request with defaults set
409                 try {
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);
414                 }
415                 
416                 
417                 // space in front of name (inside quotes)
418                 try {
419                         request = JSONRequest.load((String)"{\" Request\" : {\"XPathVersion\" : \"http://some/other/default/uri\" }}");
420                         fail("Request should throw exception");
421                 } catch (JSONStructureException e) {
422                         // correct response
423                 } catch (Exception e) {
424                         fail ("Failed convert from JSON to object: " + e);
425                 }
426                 
427                 // space at end of name (inside quotes)
428                 try {
429                         request = JSONRequest.load((String)"{\"Request \" : {\"XPathVersion\" : \"http://some/other/default/uri\" }}");
430                         fail("Request should throw exception");
431                 } catch (JSONStructureException e) {
432                         // correct response
433                 } catch (Exception e) {
434                         fail ("Failed convert from JSON to object: " + e);
435                 }
436                 
437                 // space in front of value (inside quotes) - valid String but not valid URI
438                 try {
439                         request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \" http://some/other/default/uri\" }}");
440                         fail("Request should throw exception");
441                 } catch (JSONStructureException e) {
442                         // correct response
443                 } catch (Exception e) {
444                         fail ("Failed convert from JSON to object: " + e);
445                 }
446                 
447                 // space at end of value (inside quotes) - valid String but not valid URI
448                 try {
449                         request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \"http://some/other/default/uri \" }}");
450                         fail("Request should throw exception");
451                 } catch (JSONStructureException e) {
452                         // correct response
453                 } catch (Exception e) {
454                         fail ("Failed convert from JSON to object: " + e);
455                 }
456                 
457         }
458         
459         
460         
461         // test just one of each top-level element.
462         // For simple elements also test for incorrect type
463         @Test
464         public void testTopLevelElements() {
465                 
466                 // empty request
467                 try {
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);
472                 }
473                 
474
475                 
476                 // ReturnPolicyIdList
477                 try {
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);
482                 }
483                 
484                 try {
485                         request = JSONRequest.load((String)"{\"Request\" : {\"ReturnPolicyIdList\" : \"abc\"  }}");
486                         fail("Request should throw exception");
487                 } catch (JSONStructureException e) {
488                         // correct response
489                 } catch (Exception e) {
490                         fail ("Failed convert from JSON to object: " + e);
491                 }
492                 try {
493                         request = JSONRequest.load((String)"{\"Request\" : {\"ReturnPolicyIdList\" : 123  }}");
494                         fail("Request should throw exception");
495                 } catch (JSONStructureException e) {
496                         // correct response
497                 } catch (Exception e) {
498                         fail ("Failed convert from JSON to object: " + e);
499                 }
500                 
501                 // CombinedDecision
502                 try {
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);
507                 }
508                 
509                 try {
510                         request = JSONRequest.load((String)"{\"Request\" : {\"CombinedDecision\" : \"abc\"  }}");
511                         fail("Request should throw exception");
512                 } catch (JSONStructureException e) {
513                         // correct response
514                 } catch (Exception e) {
515                         fail ("Failed convert from JSON to object: " + e);
516                 }
517                 try {
518                         request = JSONRequest.load((String)"{\"Request\" : {\"CombinedDecision\" : 123  }}");
519                         fail("Request should throw exception");
520                 } catch (JSONStructureException e) {
521                         // correct response
522                 } catch (Exception e) {
523                         fail ("Failed convert from JSON to object: " + e);
524                 }
525                 
526                 // XPathVersion
527                 try {
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);
532                 }
533                 
534                 try {
535                         request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : true  }}");
536                         fail("Request should throw exception");
537                 } catch (JSONStructureException e) {
538                         // correct response
539                 } catch (Exception e) {
540                         fail ("Failed convert from JSON to object: " + e);
541                 }
542                 try {
543                         request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : 123  }}");
544                         fail("Request should throw exception");
545                 } catch (JSONStructureException e) {
546                         // correct response
547                 } catch (Exception e) {
548                         fail ("Failed convert from JSON to object: " + e);
549                 }
550                 
551                 try {
552                         request = JSONRequest.load((String)"{\"Request\" : {\"XPathVersion\" : \"not a uri\" }}");
553                         fail("Request should throw exception");
554                 } catch (JSONStructureException e) {
555                         // correct response
556                 } catch (Exception e) {
557                         fail ("Failed convert from JSON to object: " + e);
558                 }
559                 
560                 
561                 // Category
562                 try {
563                         request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
564                                                 "\"CategoryId\": \"another-custom-cat\", " +
565                                                 "\"Id\" : \"anotherXmlId\", " +
566                                                 "\"Attribute\" : []" +
567                                             "} " +
568                                         "] }}");
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);
572                 }
573                 
574                 // AccessSubject
575                 try {
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);
580                 }
581                 
582                 // Action
583                 try {
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);
588                 }
589                 
590                 // Resource
591                 try {
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);
596                 }
597                 
598                 // Environment
599                 try {
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);
604                 }
605                 
606                 // MultiRequests
607                 try {
608                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
609                                     "\"RequestReference\": [" +
610                                         "{ " +
611                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
612                                         "}," +
613                                         "{" +
614                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
615                                         "}]" +   
616                                   "} } }");
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);
620                 }
621                 
622                 // MultiRequest with 1
623                 try {
624                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
625                                     "\"RequestReference\": [" +
626                                         "{" +
627                                                 "\"ReferenceId\" : [\"bar2\"]" +
628                                         "}]" +   
629                                   "} } }");
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);
633                 }
634                 
635                 // MultiRequest with RequestReferences with no ReferenceId
636                 try {
637                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
638                                     "\"RequestReference\": [" +
639                                         "{" +
640                                                 "\"ReferenceId\" : []" +
641                                         "}]" +   
642                                   "} } }");
643                         assertEquals("{returnPolicyIdList=false,combinedDecision=false}", request.toString());
644                 } catch (Exception e) {
645                         fail ("Failed convert from JSON to object: " + e);
646                 }
647                 
648
649                 // MultiRequests with no RequestReference
650                 try {
651                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
652                                     "\"RequestReference\": []" +   
653                                   "} } }");
654                         fail("Request should throw exception");
655                 } catch (JSONStructureException e) {
656                         // correct response
657                 } catch (Exception e) {
658                         fail ("Failed convert from JSON to object: " + e);
659                 }
660                 
661                 // MultiRequests with something other than RequestReference
662                 try {
663                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
664                                     "\"SomeOtherAttribute\": 123" +   
665                                   "} } }");
666                         fail("Request should throw exception");
667                 } catch (JSONStructureException e) {
668                         // correct response
669                 } catch (Exception e) {
670                         fail ("Failed convert from JSON to object: " + e);
671                 }
672                 
673                 // MultiRequests with single RequestReference rather than array
674                 try {
675                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
676                                     "\"RequestReference\": " +
677                                         "{" +
678                                                 "\"ReferenceId\" : []" +
679                                         "}" +   
680                                   "} } }");
681                         fail("Request should throw exception");
682                 } catch (JSONStructureException e) {
683                         // correct response
684                 } catch (Exception e) {
685                         fail ("Failed convert from JSON to object: " + e);
686                 }
687                 
688                 // MultiRequests with RequestReference containing single element instead of array
689                 try {
690                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
691                                     "\"RequestReference\": [" +
692                                         "{" +
693                                                 "\"ReferenceId\" : \"foo1\"" +
694                                         "}]" +   
695                                   "} } }");
696                         fail("Request should throw exception");
697                 } catch (JSONStructureException e) {
698                         // correct response
699                 } catch (Exception e) {
700                         fail ("Failed convert from JSON to object: " + e);
701                 }
702                 try {
703                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
704                                     "\"RequestReference\": [" +
705                                         "{" +
706                                                 "\"ReferenceId\" : {\"foo1\"}" +
707                                         "}]" +   
708                                   "} } }");
709                         fail("Request should throw exception");
710                 } catch (JSONStructureException e) {
711                         // correct response
712                 } catch (Exception e) {
713                         fail ("Failed convert from JSON to object: " + e);
714                 }
715                 
716                 // MultiRequests with component that is not a RequestReference
717                 try {
718                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
719                                     "\"RequestReference\": [" +
720                                         "{ " +
721                                                 "\"SomeOtherAttribute\" : [\"foo1\",\"bar1\"]" +
722                                         "}," +
723                                         "{" +
724                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
725                                         "}]" +   
726                                   "} } }");
727                         fail("Request should throw exception");
728                 } catch (JSONStructureException e) {
729                         // correct response
730                 } catch (Exception e) {
731                         fail ("Failed convert from JSON to object: " + e);
732                 }
733                 
734                 // MultiRequests with component that is not a RequestReference
735                 try {
736                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
737                                     "\"RequestReference\": [" +
738                                         "{ " +
739                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]," +
740                                                 "\"SomeOtherAttribute\" : [\"foo1\",\"bar1\"]" +
741                                         "}," +
742                                         "{" +
743                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
744                                         "}]" +   
745                                   "} } }");
746                         fail("Request should throw exception");
747                 } catch (JSONStructureException e) {
748                         // correct response
749                 } catch (Exception e) {
750                         fail ("Failed convert from JSON to object: " + e);
751                 }
752                 
753                 // MultiRequest with unknown elements (in addition to RequestReference)
754                 try {
755                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
756                                 "\"SomeOtherAttribute\": 123," +   
757                                     "\"RequestReference\": [" +
758                                         "{ " +
759                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
760                                         "}," +
761                                         "{" +
762                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
763                                         "}]" +   
764                                   "} } }");
765                         fail("Request should throw exception");
766                 } catch (JSONStructureException e) {
767                         // correct response
768                 } catch (Exception e) {
769                         fail ("Failed convert from JSON to object: " + e);
770                 }
771                 
772                 // MultiRequest with RequestReferences with  ReferenceId NOT a string
773                 try {
774                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
775                                     "\"RequestReference\": [" +
776                                         "{" +
777                                                 "\"ReferenceId\" : [ true ]" +
778                                         "}]" +   
779                                   "} } }");
780                         fail("Request should throw exception");
781                 } catch (JSONStructureException e) {
782                         // correct response
783                 } catch (Exception e) {
784                         fail ("Failed convert from JSON to object: " + e);
785                 }
786                 
787                 try {
788                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
789                                     "\"RequestReference\": [" +
790                                         "{" +
791                                                 "\"ReferenceId\" : [ 123 ]" +
792                                         "}]" +   
793                                   "} } }");
794                         fail("Request should throw exception");
795                 } catch (JSONStructureException e) {
796                         // correct response
797                 } catch (Exception e) {
798                         fail ("Failed convert from JSON to object: " + e);
799                 }
800                 
801                 
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.
804
805                 
806                 // extra elements in top-level
807                 try {
808                         request = JSONRequest.load((String)"{\"Request\" : {}, \"unknownElement\" : false, \"unk2\" : \"abc\", \"unk3\" : 123 }}");
809                         fail("Unknown element should throw exception");
810                 } catch (JSONStructureException e) {
811                         // correct response
812                 } catch (Exception e) {
813                         fail ("Failed convert from JSON to object: " + e);
814                 }
815                 
816                 // extra elements in Request
817                 try {
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) {
821                         // correct response
822                 } catch (Exception e) {
823                         fail ("Failed convert from JSON to object: " + e);
824                 }
825         }
826         
827         
828         // Test with every field filled in with multiple values where appropriate
829         @Test
830         public void testAllFieldsRequest() {    
831         
832                 // convert Response to JSON
833                 try {
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);
839                 }
840                 
841                 
842                 // convert example request from spec
843                 try {
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);
848                 }
849                 
850                 // convert example request from spec containing XPAthExpression
851                 try {
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);
856                 }
857                 
858         
859         }
860         
861         
862         
863         // Duplicates - Each element duplicated
864         @Test
865         public void testDuplicates() {
866                 // duplicate Request
867                 try {
868                         request = JSONRequest.load((String)"{\"Request\" : {}, \"Request\" : {}}");
869                         fail("Unknown element should throw exception");
870                 } catch (JSONStructureException e) {
871                         // correct response
872                 } catch (Exception e) {
873                         fail ("Failed convert from JSON to object: " + e);
874                 }
875                 
876                 
877                 try {
878                         request = JSONRequest.load((String)"{\"Request\" : {\"ReturnPolicyIdList\" : true, \"ReturnPolicyIdList\" : true   }}");
879                         fail("Unknown element should throw exception");
880                 } catch (JSONStructureException e) {
881                         // correct response
882                 } catch (Exception e) {
883                         fail ("Failed convert from JSON to object: " + e);
884                 }
885                 
886                 try {
887                         request = JSONRequest.load((String)"{\"Request\" : { \"CombinedDecision\" : true, \"CombinedDecision\" : true }}");
888                         fail("Unknown element should throw exception");
889                 } catch (JSONStructureException e) {
890                         // correct response
891                 } catch (Exception e) {
892                         fail ("Failed convert from JSON to object: " + e);
893                 }
894                 
895                 
896                 try {
897                         request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
898                                                 "\"CategoryId\": \"another-custom-cat\", " +
899                                                 "\"Id\" : \"anotherXmlId\", " +
900                                                 "\"Attribute\" : []" +
901                                             "} " +
902                                         "],"
903                                         + "\"Category\": [{ " +
904                                                 "\"CategoryId\": \"another-custom-cat\", " +
905                                                 "\"Id\" : \"anotherXmlId\", " +
906                                                 "\"Attribute\" : []" +
907                                             "} " +
908                                         "] }}");
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) {
912                         // correct response
913                 } catch (Exception e) {
914                         fail ("Failed convert from JSON to object: " + e);
915                 }
916                 
917                 try {
918                         request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
919                                                 "\"CategoryId\": \"another-custom-cat\", " +
920                                                 "\"CategoryId\": \"another-custom-cat\", " +
921                                                 "\"Id\" : \"anotherXmlId\", " +
922                                                 "\"Attribute\" : []" +
923                                             "} " +
924                                         "] }}");
925                         fail("Unknown element should throw exception");
926                 } catch (JSONStructureException e) {
927                         // correct response
928                 } catch (Exception e) {
929                         fail ("Failed convert from JSON to object: " + e);
930                 }
931                 
932                 try {
933                         request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
934                                                 "\"CategoryId\": \"another-custom-cat\", " +
935                                                 "\"Id\" : \"anotherXmlId\", " +
936                                                 "\"Id\" : \"anotherXmlId\", " +
937                                                 "\"Attribute\" : []" +
938                                             "} " +
939                                         "] }}");
940                         fail("Unknown element should throw exception");
941                 } catch (JSONStructureException e) {
942                         // correct response
943                 } catch (Exception e) {
944                         fail ("Failed convert from JSON to object: " + e);
945                 }
946                 
947                 try {
948                         request = JSONRequest.load((String)"{\"Request\" : {\"Category\": [{ " +
949                                                 "\"CategoryId\": \"another-custom-cat\", " +
950                                                 "\"Id\" : \"anotherXmlId\", " +
951                                                 "\"Attribute\" : []" +
952                                                 "\"Attribute\" : []" +
953                                             "} " +
954                                         "] }}");
955                         fail("Unknown element should throw exception");
956                 } catch (JSONStructureException e) {
957                         // correct response
958                 } catch (Exception e) {
959                         fail ("Failed convert from JSON to object: " + e);
960                 }
961                 
962                 
963                 // AccessSubject
964                 try {
965                         request = JSONRequest.load((String)"{\"Request\" : { \"AccessSubject\":{ }, \"AccessSubject\":{ }}}");
966                         fail("Unknown element should throw exception");
967                 } catch (JSONStructureException e) {
968                         // correct response
969                 } catch (Exception e) {
970                         fail ("Failed convert from JSON to object: " + e);
971                 }
972                 
973                 // Action
974                 try {
975                         request = JSONRequest.load((String)"{\"Request\" : { \"Action\":{ }, \"Action\":{ }}}");
976                         fail("Unknown element should throw exception");
977                 } catch (JSONStructureException e) {
978                         // correct response
979                 } catch (Exception e) {
980                         fail ("Failed convert from JSON to object: " + e);
981                 }
982                 
983                 // Resource
984                 try {
985                         request = JSONRequest.load((String)"{\"Request\" : {\"Resource\":{ }, \"Resource\":{ }}}");
986                         fail("Unknown element should throw exception");
987                 } catch (JSONStructureException e) {
988                         // correct response
989                 } catch (Exception e) {
990                         fail ("Failed convert from JSON to object: " + e);
991                 }
992                 
993                 // Environment
994                 try {
995                         request = JSONRequest.load((String)"{\"Request\" : {\"Environment\":{ }, \"Environment\":{ } }}");
996                         fail("Unknown element should throw exception");
997                 } catch (JSONStructureException e) {
998                         // correct response
999                 } catch (Exception e) {
1000                         fail ("Failed convert from JSON to object: " + e);
1001                 }
1002                 
1003                 // MultiRequests
1004
1005                 
1006                 try {
1007                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
1008                                     "\"RequestReference\": [" +
1009                                         "{ " +
1010                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1011                                         "}," +
1012                                         "{" +
1013                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1014                                         "}]" +   
1015                                   "},"
1016                                   + "\"MultiRequests\" : {" +
1017                                     "\"RequestReference\": [" +
1018                                         "{ " +
1019                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1020                                         "}," +
1021                                         "{" +
1022                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1023                                         "}]" +   
1024                                   "}  } }");
1025                         fail("Unknown element should throw exception");
1026                 } catch (JSONStructureException e) {
1027                         // correct response
1028                 } catch (Exception e) {
1029                         fail ("Failed convert from JSON to object: " + e);
1030                 }
1031                 
1032                 try {
1033                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
1034                                     "\"RequestReference\": [" +
1035                                         "{ " +
1036                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1037                                         "}," +
1038                                         "{" +
1039                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1040                                         "}]," + 
1041                                         "\"RequestReference\": [" +
1042                                         "{ " +
1043                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1044                                         "}," +
1045                                         "{" +
1046                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1047                                         "}]" +
1048                                   "} } }");
1049                         fail("Unknown element should throw exception");
1050                 } catch (JSONStructureException e) {
1051                         // correct response
1052                 } catch (Exception e) {
1053                         fail ("Failed convert from JSON to object: " + e);
1054                 }
1055                 
1056                 try {
1057                         request = JSONRequest.load((String)"{\"Request\" : {\"MultiRequests\" : {" +
1058                                     "\"RequestReference\": [" +
1059                                         "{ " +
1060                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1061                                                 "\"ReferenceId\" : [\"foo1\",\"bar1\"]" +
1062                                         "}," +
1063                                         "{" +
1064                                                 "\"ReferenceId\" : [\"foo2\",\"bar2\"]" +
1065                                         "}]" +   
1066                                   "} } }");
1067                         fail("Unknown element should throw exception");
1068                 } catch (JSONStructureException e) {
1069                         // correct response
1070                 } catch (Exception e) {
1071                         fail ("Failed convert from JSON to object: " + e);
1072                 }
1073         }
1074         
1075
1076 }