2a36fa280218cd00be2d62a867a1e9c2ae159321
[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 package org.onap.policy.xacml.action;
21
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
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;
47
48 import org.onap.policy.drools.http.server.HttpServletServer;
49 import org.onap.policy.drools.utils.NetworkUtil;
50
51 public class FindActionTest {
52         
53
54         String xPathExampleFromSpec = "{ " +
55                         "\"Request\" : { " +
56                                 "\"Resource\" : { " +
57                                         "\"Attribute\": [ " +
58                                                 "{ " +
59                                                         "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " +
60                                                         "\"DataType\" : \"xpathExpression\", " +
61                                             "\"Value\" : { " +
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\" " +
65                                                         "}, " +
66                                                     "{ " +
67                                                         "\"Prefix\" : \"md\", " +
68                                                         "\"Namespace\" : \"urn:example:med:schemas:record\" " +
69                                                     "} " +
70                                                 "], " +
71                                                 "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " +
72                                             "} " +
73                                         "} " +
74                                         "] " +
75                                 "} " +
76                         "} " +
77                 "} ";
78         
79         String jsonResponse;
80         Request request;
81         private static final int MOCK_SERVER_PORT = 6670;
82         
83         @BeforeClass
84         public static void setUpServer() {
85         try {
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) {
93                 fail(e.getMessage());
94         }
95                 
96         }
97         
98     @AfterClass
99     public static void tearDownSimulator() {
100         HttpServletServer.factory.destroy();
101     }
102     
103         @Before
104         public void setUp() throws Exception {
105                 new OnapFunctionDefinitionFactory();
106                 request = JSONRequest.load(xPathExampleFromSpec);
107
108                 try {   
109                         XACMLProperties.reloadProperties();
110                         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.properties");
111                         XACMLProperties.getProperties();
112                         
113                         assertTrue(true);
114                 } catch (Exception e) {
115                         fail();
116                         
117                 }
118         }
119         
120
121         @Test
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);
141                 // set the decision
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);
149
150                 StdMutableObligation obligation = new StdMutableObligation();
151                 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
152                 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
153                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
154                                 XACML3.ID_SUBJECT, 
155                                 "obligation-issuer1", 
156                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
157                 result.addObligation(obligation);
158
159                 StdMutableAdvice advice = new StdMutableAdvice();
160                 advice.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
161                 advice.addAttributeAssignment(new StdMutableAttributeAssignment(
162                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
163                                 XACML3.ID_SUBJECT, 
164                                 "advice-issuer1", 
165                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
166                 response.add(result);
167                 
168                         //      The logic below exercises the  callRest and takeAction methods in FindAction
169                         //      GET request
170                 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
171                 result = new StdMutableResult(status);
172                 result.setDecision(Decision.PERMIT);
173                 
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"), 
179                                 "obligation-issuer", 
180                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
181                 
182                 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
183                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
184                                 new IdentifierImpl("URL"), 
185                                 "obligation-issuer", 
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"), 
190                                 "obligation-issuer", 
191                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "GET")));
192                 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
193                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
194                                 new IdentifierImpl("headers"), 
195                                 "obligation-issuer", 
196                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "foobar")));
197
198                 
199                 result.addObligation(obligation);
200                 response.add(result);
201         
202                         //      POST request
203                 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
204                 result = new StdMutableResult(status);
205                 result.setDecision(Decision.PERMIT);
206                 
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"), 
212                                 "obligation-issuer", 
213                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
214                 
215                 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
216                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
217                                 new IdentifierImpl("URL"), 
218                                 "obligation-issuer", 
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"), 
223                                 "obligation-issuer", 
224                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "POST")));
225                 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
226                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
227                                 new IdentifierImpl("body"), 
228                                 "obligation-issuer", 
229                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
230                 
231                 result.addObligation(obligation);
232                 response.add(result);
233                 
234                         //PUT request
235                 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
236                 result = new StdMutableResult(status);
237                 result.setDecision(Decision.PERMIT);
238                 
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"), 
244                                 "obligation-issuer", 
245                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
246                 
247                 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
248                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
249                                 new IdentifierImpl("URL"), 
250                                 "obligation-issuer", 
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"), 
255                                 "obligation-issuer", 
256                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PUT")));
257                 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
258                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
259                                 new IdentifierImpl("body"), 
260                                 "obligation-issuer", 
261                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
262                 
263                 result.addObligation(obligation);
264                 response.add(result);
265                 
266                 try {
267                         assertTrue(action.run(response, request) != null);
268                 } catch (Exception e) {
269                         fail("operation failed, e="+e);
270                 }
271         }
272
273 }