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