4ae75387adb69ebb8c1296f5845c9a0ea990237a
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / serialization / TestXacmlXmlMessageBodyHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest.serialization;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import com.att.research.xacml.api.Request;
28 import com.att.research.xacml.api.RequestAttributes;
29 import com.att.research.xacml.api.Response;
30 import com.att.research.xacml.std.dom.DOMResponse;
31 import com.att.research.xacml.std.dom.DOMStructureException;
32 import java.io.ByteArrayInputStream;
33 import java.io.ByteArrayOutputStream;
34 import java.io.IOException;
35 import java.util.Iterator;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39
40 public class TestXacmlXmlMessageBodyHandler {
41
42     private static final String PRIMARY_TYPE = "application";
43     private static final String SUB_TYPE = "xacml+xml";
44
45     @SuppressWarnings("rawtypes")
46     private static final Class REQUEST_CLASS = Request.class;
47     @SuppressWarnings("rawtypes")
48     private static final Class RESPONSE_CLASS = Response.class;
49
50     private XacmlXmlMessageBodyHandler hdlr;
51
52     @Before
53     public void setUp() {
54         hdlr = new XacmlXmlMessageBodyHandler();
55     }
56
57     @Test
58     public void testIsWriteable() {
59         CommonSerialization.testIsWritableOrReadable(PRIMARY_TYPE, SUB_TYPE, hdlr::isWriteable);
60     }
61
62     @Test
63     public void testWriteTo() throws IOException, DOMStructureException {
64         ByteArrayOutputStream stream = new ByteArrayOutputStream();
65         Response resp = DOMResponse.load(ResourceUtils.getResourceAsString(
66                 "src/test/resources/decisions/decision.native.response.xml"));
67         hdlr.writeTo(resp, RESPONSE_CLASS, RESPONSE_CLASS, null, null, null, stream);
68         assertEquals(resp, DOMResponse.load(new ByteArrayInputStream(stream.toByteArray())));
69     }
70
71     @Test
72     public void testIsReadable() {
73         CommonSerialization.testIsWritableOrReadable(PRIMARY_TYPE, SUB_TYPE, hdlr::isReadable);
74     }
75
76     @Test
77     @SuppressWarnings("unchecked")
78     public void testReadFrom() throws IOException {
79         Request req = hdlr.readFrom(REQUEST_CLASS, REQUEST_CLASS, null, null, null, ResourceUtils.getResourceAsStream(
80                 "src/test/resources/decisions/decision.native.request.xml"));
81         assertFalse(req.getCombinedDecision());
82         assertFalse(req.getReturnPolicyIdList());
83         assertTrue(req.getRequestAttributes().size() == 4);
84         Iterator<RequestAttributes> iter = req.getRequestAttributes().iterator();
85
86         RequestAttributes firstRequestAttributes = iter.next();
87         assertTrue(firstRequestAttributes.getAttributes().size() == 1);
88         assertEquals("Julius Hibbert", firstRequestAttributes.getAttributes().iterator().next()
89                 .getValues().iterator().next().getValue().toString());
90
91         RequestAttributes secondRequestAttributes = iter.next();
92         assertTrue(secondRequestAttributes.getAttributes().size() == 1);
93         assertEquals("http://medico.com/record/patient/BartSimpson", secondRequestAttributes.getAttributes()
94                 .iterator().next().getValues().iterator().next().getValue().toString());
95
96         RequestAttributes thirdRequestAttributes = iter.next();
97         assertTrue(thirdRequestAttributes.getAttributes().size() == 1);
98         assertEquals("read", thirdRequestAttributes.getAttributes().iterator().next()
99                 .getValues().iterator().next().getValue().toString());
100     }
101 }