2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.xacml.action;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.onap.policy.xacml.custom.OnapFunctionDefinitionFactory;
30 import com.att.research.xacml.api.Decision;
31 import com.att.research.xacml.api.Request;
32 import com.att.research.xacml.api.XACML3;
33 import com.att.research.xacml.std.IdentifierImpl;
34 import com.att.research.xacml.std.StdAttributeValue;
35 import com.att.research.xacml.std.StdMutableAdvice;
36 import com.att.research.xacml.std.StdMutableAttributeAssignment;
37 import com.att.research.xacml.std.StdMutableMissingAttributeDetail;
38 import com.att.research.xacml.std.StdMutableObligation;
39 import com.att.research.xacml.std.StdMutableResponse;
40 import com.att.research.xacml.std.StdMutableResult;
41 import com.att.research.xacml.std.StdMutableStatus;
42 import com.att.research.xacml.std.StdMutableStatusDetail;
43 import com.att.research.xacml.std.StdStatusCode;
44 import com.att.research.xacml.std.datatypes.DataTypes;
45 import com.att.research.xacml.std.json.JSONRequest;
46 import com.att.research.xacml.util.XACMLProperties;
48 import org.onap.policy.drools.http.server.HttpServletServer;
49 import org.onap.policy.drools.utils.NetworkUtil;
51 public class FindActionTest {
54 String xPathExampleFromSpec = "{ " +
59 "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " +
60 "\"DataType\" : \"xpathExpression\", " +
62 "\"XPathCategory\" : \"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\", " +
63 "\"Namespaces\" : [{ " +
64 "\"Namespace\" : \"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" " +
67 "\"Prefix\" : \"md\", " +
68 "\"Namespace\" : \"urn:example:med:schemas:record\" " +
71 "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " +
81 private static final int MOCK_SERVER_PORT = 6670;
84 public static void setUpServer() {
86 final HttpServletServer testServer = HttpServletServer.factory.build("dmaapSim",
87 "localhost", MOCK_SERVER_PORT, "/", false, true);
88 testServer.addServletClass("/*", DummyRest.class.getName());
89 testServer.waitedStart(2000);
90 if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L))
91 throw new IllegalStateException("cannot connect to port " + testServer.getPort());
92 } catch (final Exception e) {
99 public static void tearDownSimulator() {
100 HttpServletServer.factory.destroy();
104 public void setUp() throws Exception {
105 new OnapFunctionDefinitionFactory();
106 request = JSONRequest.load(xPathExampleFromSpec);
109 XACMLProperties.reloadProperties();
110 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.properties");
111 XACMLProperties.getProperties();
114 } catch (Exception e) {
122 public final void testRun() {
123 FindAction action = new FindAction();
124 // fully-loaded multiple response
125 StdMutableResponse response = new StdMutableResponse();
126 // create a Status object
127 StdMutableStatus status = new StdMutableStatus(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE);
128 status.setStatusMessage("some status message");
129 StdMutableStatusDetail statusDetailIn = new StdMutableStatusDetail();
130 StdMutableMissingAttributeDetail mad = new StdMutableMissingAttributeDetail();
131 mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PEPACTION"));
132 mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_INTEGER.getId(), "PDPACTION"));
133 mad.setAttributeId(XACML3.ID_ACTION_PURPOSE);
134 mad.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION);
135 mad.setDataTypeId(XACML3.ID_DATATYPE_STRING);
136 mad.setIssuer("an Issuer");
137 statusDetailIn.addMissingAttributeDetail(mad);
138 status.setStatusDetail(statusDetailIn);
139 // create a single result object
140 StdMutableResult result = new StdMutableResult(status);
142 result.setDecision(Decision.INDETERMINATE);
143 // put the Result into the Response
144 response.add(result);
145 // create a new Result with a different Decision
146 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
147 result = new StdMutableResult(status);
148 result.setDecision(Decision.DENY);
150 StdMutableObligation obligation = new StdMutableObligation();
151 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
152 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
153 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
155 "obligation-issuer1",
156 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
157 result.addObligation(obligation);
159 StdMutableAdvice advice = new StdMutableAdvice();
160 advice.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
161 advice.addAttributeAssignment(new StdMutableAttributeAssignment(
162 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
165 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
166 response.add(result);
168 // The logic below exercises the callRest and takeAction methods in FindAction
170 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
171 result = new StdMutableResult(status);
172 result.setDecision(Decision.PERMIT);
174 obligation = new StdMutableObligation();
175 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
176 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
177 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
178 new IdentifierImpl("performer"),
180 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
182 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
183 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
184 new IdentifierImpl("URL"),
186 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
187 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
188 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
189 new IdentifierImpl("method"),
191 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "GET")));
192 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
193 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
194 new IdentifierImpl("headers"),
196 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "foobar")));
199 result.addObligation(obligation);
200 response.add(result);
203 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
204 result = new StdMutableResult(status);
205 result.setDecision(Decision.PERMIT);
207 obligation = new StdMutableObligation();
208 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
209 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
210 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
211 new IdentifierImpl("performer"),
213 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
215 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
216 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
217 new IdentifierImpl("URL"),
219 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
220 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
221 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
222 new IdentifierImpl("method"),
224 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "POST")));
225 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
226 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
227 new IdentifierImpl("body"),
229 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
231 result.addObligation(obligation);
232 response.add(result);
235 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
236 result = new StdMutableResult(status);
237 result.setDecision(Decision.PERMIT);
239 obligation = new StdMutableObligation();
240 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
241 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
242 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
243 new IdentifierImpl("performer"),
245 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
247 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
248 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
249 new IdentifierImpl("URL"),
251 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
252 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
253 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
254 new IdentifierImpl("method"),
256 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PUT")));
257 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
258 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
259 new IdentifierImpl("body"),
261 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
263 result.addObligation(obligation);
264 response.add(result);
267 assertTrue(action.run(response, request) != null);
268 } catch (Exception e) {
269 fail("operation failed, e="+e);