2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.endpoints.http.server.HttpServletServerFactoryInstance;
50 import org.onap.policy.common.utils.network.NetworkUtil;
51 import org.onap.policy.xacml.custom.OnapFunctionDefinitionFactory;
53 public class FindActionTest {
55 String xpathexamplefromspec = "{ " + "\"Request\" : { " + "\"Resource\" : { " + "\"Attribute\": [ " + "{ "
56 + "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " + "\"DataType\" : \"xpathExpression\", "
57 + "\"Value\" : { " + "\"XPathCategory\" : \"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\", "
58 + "\"Namespaces\" : [{ " + "\"Namespace\" : \"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" " + "}, "
59 + "{ " + "\"Prefix\" : \"md\", " + "\"Namespace\" : \"urn:example:med:schemas:record\" " + "} " + "], "
60 + "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " + "} " + "} " + "] " + "} " + "} " + "} ";
64 private static final int MOCK_SERVER_PORT = 6670;
67 * Setup server before test class.
70 public static void setUpServer() {
72 final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build("dmaapSim",
73 "localhost", MOCK_SERVER_PORT, "/", false, true);
74 testServer.addServletClass("/*", DummyRest.class.getName());
75 testServer.waitedStart(2000);
76 if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L)) {
77 throw new IllegalStateException("cannot connect to port " + testServer.getPort());
79 } catch (final Exception e) {
86 public static void tearDownSimulator() {
87 HttpServletServerFactoryInstance.getServerFactory().destroy();
91 * Setup before test case execution.
93 * @throws Exception if any error occurs
96 public void setUp() throws Exception {
97 new OnapFunctionDefinitionFactory();
98 request = JSONRequest.load(xpathexamplefromspec);
101 XACMLProperties.reloadProperties();
102 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.properties");
103 XACMLProperties.getProperties();
106 } catch (final Exception e) {
114 public final void testRun() {
115 final FindAction action = new FindAction();
116 // fully-loaded multiple response
117 final StdMutableResponse response = new StdMutableResponse();
118 // create a Status object
119 StdMutableStatus status = new StdMutableStatus(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE);
120 status.setStatusMessage("some status message");
121 final StdMutableStatusDetail statusDetailIn = new StdMutableStatusDetail();
122 final StdMutableMissingAttributeDetail mad = new StdMutableMissingAttributeDetail();
123 mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PEPACTION"));
124 mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_INTEGER.getId(), "PDPACTION"));
125 mad.setAttributeId(XACML3.ID_ACTION_PURPOSE);
126 mad.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION);
127 mad.setDataTypeId(XACML3.ID_DATATYPE_STRING);
128 mad.setIssuer("an Issuer");
129 statusDetailIn.addMissingAttributeDetail(mad);
130 status.setStatusDetail(statusDetailIn);
131 // create a single result object
132 StdMutableResult result = new StdMutableResult(status);
134 result.setDecision(Decision.INDETERMINATE);
135 // put the Result into the Response
136 response.add(result);
137 // create a new Result with a different Decision
138 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
139 result = new StdMutableResult(status);
140 result.setDecision(Decision.DENY);
142 StdMutableObligation obligation = new StdMutableObligation();
143 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
144 obligation.addAttributeAssignment(
145 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, XACML3.ID_SUBJECT,
146 "obligation-issuer1", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
147 result.addObligation(obligation);
149 final StdMutableAdvice advice = new StdMutableAdvice();
150 advice.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
151 advice.addAttributeAssignment(
152 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, XACML3.ID_SUBJECT,
153 "advice-issuer1", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
154 response.add(result);
156 // The logic below exercises the callRest and takeAction methods in FindAction
158 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
159 result = new StdMutableResult(status);
160 result.setDecision(Decision.PERMIT);
162 obligation = new StdMutableObligation();
163 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
164 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
165 new IdentifierImpl("performer"), "obligation-issuer",
166 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
168 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
169 new IdentifierImpl("URL"), "obligation-issuer",
170 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
171 obligation.addAttributeAssignment(
172 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"),
173 "obligation-issuer", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "GET")));
174 obligation.addAttributeAssignment(
175 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("headers"),
176 "obligation-issuer", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "foobar")));
179 result.addObligation(obligation);
180 response.add(result);
183 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
184 result = new StdMutableResult(status);
185 result.setDecision(Decision.PERMIT);
187 obligation = new StdMutableObligation();
188 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
189 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
190 new IdentifierImpl("performer"), "obligation-issuer",
191 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
193 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
194 new IdentifierImpl("URL"), "obligation-issuer",
195 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
196 obligation.addAttributeAssignment(
197 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"),
198 "obligation-issuer", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "POST")));
199 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
200 new IdentifierImpl("body"), "obligation-issuer", new StdAttributeValue<String>(
201 DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
203 result.addObligation(obligation);
204 response.add(result);
207 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
208 result = new StdMutableResult(status);
209 result.setDecision(Decision.PERMIT);
211 obligation = new StdMutableObligation();
212 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
213 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
214 new IdentifierImpl("performer"), "obligation-issuer",
215 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
217 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
218 new IdentifierImpl("URL"), "obligation-issuer",
219 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
220 obligation.addAttributeAssignment(
221 new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"),
222 "obligation-issuer", new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PUT")));
223 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE,
224 new IdentifierImpl("body"), "obligation-issuer", new StdAttributeValue<String>(
225 DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
227 result.addObligation(obligation);
228 response.add(result);
231 assertTrue(action.run(response, request) != null);
232 } catch (final Exception e) {
233 fail("operation failed, e=" + e);