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=========================================================
21 package org.onap.policy.xacml.action;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
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;
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;
52 public class FindActionTest {
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\" " + "} " + "} " + "] " + "} " + "} " + "} ";
63 private static final int MOCK_SERVER_PORT = 6670;
66 * Setup server before test class.
69 public static void setUpServer() {
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());
78 } catch (final Exception e) {
85 public static void tearDownSimulator() {
86 HttpServletServer.factory.destroy();
90 * Setup before test case execution.
92 * @throws Exception if any error occurs
95 public void setUp() throws Exception {
96 new OnapFunctionDefinitionFactory();
97 request = JSONRequest.load(xpathexamplefromspec);
100 XACMLProperties.reloadProperties();
101 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.properties");
102 XACMLProperties.getProperties();
105 } catch (final Exception e) {
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);
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);
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);
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);
155 // The logic below exercises the callRest and takeAction methods in FindAction
157 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
158 result = new StdMutableResult(status);
159 result.setDecision(Decision.PERMIT);
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")));
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")));
178 result.addObligation(obligation);
179 response.add(result);
182 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
183 result = new StdMutableResult(status);
184 result.setDecision(Decision.PERMIT);
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")));
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")));
202 result.addObligation(obligation);
203 response.add(result);
206 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
207 result = new StdMutableResult(status);
208 result.setDecision(Decision.PERMIT);
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")));
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")));
226 result.addObligation(obligation);
227 response.add(result);
230 assertTrue(action.run(response, request) != null);
231 } catch (final Exception e) {
232 fail("operation failed, e=" + e);