Copy policy-endpoints from drools-pdp to common
[policy/engine.git] / ONAP-PDP / src / test / java / org / onap / policy / xacml / action / FindActionTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.onap.policy.xacml.action;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import com.att.research.xacml.api.Decision;
27 import com.att.research.xacml.api.Request;
28 import com.att.research.xacml.api.XACML3;
29 import com.att.research.xacml.std.IdentifierImpl;
30 import com.att.research.xacml.std.StdAttributeValue;
31 import com.att.research.xacml.std.StdMutableAdvice;
32 import com.att.research.xacml.std.StdMutableAttributeAssignment;
33 import com.att.research.xacml.std.StdMutableMissingAttributeDetail;
34 import com.att.research.xacml.std.StdMutableObligation;
35 import com.att.research.xacml.std.StdMutableResponse;
36 import com.att.research.xacml.std.StdMutableResult;
37 import com.att.research.xacml.std.StdMutableStatus;
38 import com.att.research.xacml.std.StdMutableStatusDetail;
39 import com.att.research.xacml.std.StdStatusCode;
40 import com.att.research.xacml.std.datatypes.DataTypes;
41 import com.att.research.xacml.std.json.JSONRequest;
42 import com.att.research.xacml.util.XACMLProperties;
43
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
49 import org.onap.policy.common.utils.network.NetworkUtil;
50 import org.onap.policy.xacml.custom.OnapFunctionDefinitionFactory;
51
52 public class FindActionTest {
53
54     String xpathexamplefromspec = "{ " + "\"Request\" : { " + "\"Resource\" : { " + "\"Attribute\": [ " + "{ "
55             + "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " + "\"DataType\" : \"xpathExpression\", "
56             + "\"Value\" : { " + "\"XPathCategory\" : \"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\", "
57             + "\"Namespaces\" : [{ " + "\"Namespace\" : \"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" " + "}, "
58             + "{ " + "\"Prefix\" : \"md\", " + "\"Namespace\" : \"urn:example:med:schemas:record\" " + "} " + "], "
59             + "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " + "} " + "} " + "] " + "} " + "} " + "} ";
60
61     String jsonResponse;
62     Request request;
63     private static final int MOCK_SERVER_PORT = 6670;
64
65     /**
66      * Setup server before test class.
67      */
68     @BeforeClass
69     public static void setUpServer() {
70         try {
71             final HttpServletServer testServer =
72                     HttpServletServer.factory.build("dmaapSim", "localhost", MOCK_SERVER_PORT, "/", false, true);
73             testServer.addServletClass("/*", DummyRest.class.getName());
74             testServer.waitedStart(2000);
75             if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L)) {
76                 throw new IllegalStateException("cannot connect to port " + testServer.getPort());
77             }
78         } catch (final Exception e) {
79             fail(e.getMessage());
80         }
81
82     }
83
84     @AfterClass
85     public static void tearDownSimulator() {
86         HttpServletServer.factory.destroy();
87     }
88
89     /**
90      * Setup before test case execution.
91      *
92      * @throws Exception if any error occurs
93      */
94     @Before
95     public void setUp() throws Exception {
96         new OnapFunctionDefinitionFactory();
97         request = JSONRequest.load(xpathexamplefromspec);
98
99         try {
100             XACMLProperties.reloadProperties();
101             System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.properties");
102             XACMLProperties.getProperties();
103
104             assertTrue(true);
105         } catch (final Exception e) {
106             fail();
107
108         }
109     }
110
111
112     @Test
113     public final void testRun() {
114         final FindAction action = new FindAction();
115         // fully-loaded multiple response
116         final StdMutableResponse response = new StdMutableResponse();
117         // create a Status object
118         StdMutableStatus status = new StdMutableStatus(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE);
119         status.setStatusMessage("some status message");
120         final StdMutableStatusDetail statusDetailIn = new StdMutableStatusDetail();
121         final StdMutableMissingAttributeDetail mad = new StdMutableMissingAttributeDetail();
122         mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PEPACTION"));
123         mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_INTEGER.getId(), "PDPACTION"));
124         mad.setAttributeId(XACML3.ID_ACTION_PURPOSE);
125         mad.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION);
126         mad.setDataTypeId(XACML3.ID_DATATYPE_STRING);
127         mad.setIssuer("an Issuer");
128         statusDetailIn.addMissingAttributeDetail(mad);
129         status.setStatusDetail(statusDetailIn);
130         // create a single result object
131         StdMutableResult result = new StdMutableResult(status);
132         // set the decision
133         result.setDecision(Decision.INDETERMINATE);
134         // put the Result into the Response
135         response.add(result);
136         // create a new Result with a different Decision
137         status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
138         result = new StdMutableResult(status);
139         result.setDecision(Decision.DENY);
140
141         StdMutableObligation obligation = new StdMutableObligation();
142         obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
143         obligation.addAttributeAssignment(
144                 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, XACML3.ID_SUBJECT,
145                         "obligation-issuer1", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
146         result.addObligation(obligation);
147
148         final StdMutableAdvice advice = new StdMutableAdvice();
149         advice.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
150         advice.addAttributeAssignment(
151                 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, XACML3.ID_SUBJECT,
152                         "advice-issuer1", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
153         response.add(result);
154
155         // The logic below exercises the callRest and takeAction methods in FindAction
156         // GET request
157         status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
158         result = new StdMutableResult(status);
159         result.setDecision(Decision.PERMIT);
160
161         obligation = new StdMutableObligation();
162         obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
163         obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
164                 new IdentifierImpl("performer"), "obligation-issuer",
165                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
166
167         obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
168                 new IdentifierImpl("URL"), "obligation-issuer",
169                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
170         obligation.addAttributeAssignment(
171                 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"),
172                         "obligation-issuer", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "GET")));
173         obligation.addAttributeAssignment(
174                 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("headers"),
175                         "obligation-issuer", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "foobar")));
176
177
178         result.addObligation(obligation);
179         response.add(result);
180
181         // POST request
182         status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
183         result = new StdMutableResult(status);
184         result.setDecision(Decision.PERMIT);
185
186         obligation = new StdMutableObligation();
187         obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
188         obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
189                 new IdentifierImpl("performer"), "obligation-issuer",
190                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
191
192         obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
193                 new IdentifierImpl("URL"), "obligation-issuer",
194                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
195         obligation.addAttributeAssignment(
196                 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"),
197                         "obligation-issuer", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "POST")));
198         obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
199                 new IdentifierImpl("body"), "obligation-issuer", new StdAttributeValue<String>(
200                         DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
201
202         result.addObligation(obligation);
203         response.add(result);
204
205         // PUT request
206         status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
207         result = new StdMutableResult(status);
208         result.setDecision(Decision.PERMIT);
209
210         obligation = new StdMutableObligation();
211         obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
212         obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
213                 new IdentifierImpl("performer"), "obligation-issuer",
214                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
215
216         obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
217                 new IdentifierImpl("URL"), "obligation-issuer",
218                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
219         obligation.addAttributeAssignment(
220                 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"),
221                         "obligation-issuer", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PUT")));
222         obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
223                 new IdentifierImpl("body"), "obligation-issuer", new StdAttributeValue<String>(
224                         DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
225
226         result.addObligation(obligation);
227         response.add(result);
228
229         try {
230             assertTrue(action.run(response, request) != null);
231         } catch (final Exception e) {
232             fail("operation failed, e=" + e);
233         }
234     }
235
236 }