xcaml-pdp sonar issue fixes
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / serialization / TestXacmlJsonMessageBodyHandler.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 com.att.research.xacml.std.json.JSONStructureException;
33 import com.att.research.xacml.std.json.JsonResponseTranslator;
34 import java.io.ByteArrayInputStream;
35 import java.io.ByteArrayOutputStream;
36 import java.io.IOException;
37 import java.util.Iterator;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41
42 public class TestXacmlJsonMessageBodyHandler {
43
44     private static final String PRIMARY_TYPE = "application";
45     private static final String SUB_TYPE = "xacml+json";
46
47     @SuppressWarnings("rawtypes")
48     private static final Class REQUEST_CLASS = Request.class;
49     @SuppressWarnings("rawtypes")
50     private static final Class RESPONSE_CLASS = Response.class;
51
52     private XacmlJsonMessageBodyHandler hdlr;
53
54     @Before
55     public void setUp() {
56         hdlr = new XacmlJsonMessageBodyHandler();
57     }
58
59     @Test
60     public void testIsWriteable() {
61         CommonSerialization.testIsWritableOrReadable(PRIMARY_TYPE, SUB_TYPE, hdlr::isWriteable);
62     }
63
64     @Test
65     public void testWriteTo() throws IOException, DOMStructureException, JSONStructureException {
66         ByteArrayOutputStream stream = new ByteArrayOutputStream();
67         Response resp = DOMResponse.load(ResourceUtils.getResourceAsString(
68                 "src/test/resources/decisions/decision.native.response.xml"));
69         hdlr.writeTo(resp, RESPONSE_CLASS, RESPONSE_CLASS, null, null, null, stream);
70         assertEquals(resp, JsonResponseTranslator.load(new ByteArrayInputStream(stream.toByteArray())));
71     }
72
73     @Test
74     public void testIsReadable() {
75         CommonSerialization.testIsWritableOrReadable(PRIMARY_TYPE, SUB_TYPE, hdlr::isReadable);
76     }
77
78     @Test
79     @SuppressWarnings("unchecked")
80     public void testReadFrom() throws IOException {
81         Request req = hdlr.readFrom(REQUEST_CLASS, REQUEST_CLASS, null, null, null, ResourceUtils.getResourceAsStream(
82                 "src/test/resources/decisions/decision.native.request.json"));
83         assertFalse(req.getCombinedDecision());
84         assertFalse(req.getReturnPolicyIdList());
85         assertEquals(3, req.getRequestAttributes().size());
86         Iterator<RequestAttributes> iter = req.getRequestAttributes().iterator();
87
88         RequestAttributes firstRequestAttributes = iter.next();
89         assertEquals(1, firstRequestAttributes.getAttributes().size());
90         assertEquals("Julius Hibbert", firstRequestAttributes.getAttributes().iterator().next()
91                 .getValues().iterator().next().getValue().toString());
92
93         RequestAttributes secondRequestAttributes = iter.next();
94         assertEquals(1, secondRequestAttributes.getAttributes().size());
95         assertEquals("http://medico.com/record/patient/BartSimpson", secondRequestAttributes.getAttributes()
96                 .iterator().next().getValues().iterator().next().getValue().toString());
97
98         RequestAttributes thirdRequestAttributes = iter.next();
99         assertEquals(1, thirdRequestAttributes.getAttributes().size());
100         assertEquals("read", thirdRequestAttributes.getAttributes().iterator().next()
101                 .getValues().iterator().next().getValue().toString());
102     }
103 }