2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.policy.pdp.test;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.junit.Test;
33 import com.att.research.xacml.api.XACML3;
34 import com.att.research.xacml.std.datatypes.DataTypes;
35 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
36 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
37 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentAttributeValue;
38 import com.att.research.xacmlatt.pdp.std.StdFunctions;
39 import com.att.research.xacmlatt.pdp.std.functions.*;
42 * Test of PDP Functions (See XACML core spec section A.3)
45 * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
49 public class FunctionDefinitionLogicalTest {
52 * variables useful in the following tests
54 List<FunctionArgument> arguments = new ArrayList<>();
56 // use the same args for each test
57 FunctionArgumentAttributeValue attrT = null;
58 FunctionArgumentAttributeValue attrF = null;
59 public FunctionDefinitionLogicalTest () {
61 attrT = new FunctionArgumentAttributeValue(DataTypes.DT_BOOLEAN.createAttributeValue(true));
62 attrF = new FunctionArgumentAttributeValue(DataTypes.DT_BOOLEAN.createAttributeValue(false));
63 } catch (Exception e) {
64 fail("creating attribute e="+ e);
70 public void testOR() {
71 FunctionArgumentAttributeValue attr5 = null;
73 attr5 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
75 } catch (Exception e) {
76 fail("creating attribute e="+ e);
79 FunctionDefinitionLogical fd = (FunctionDefinitionLogical) StdFunctions.FD_OR;
81 // check identity and type of the thing created
82 assertEquals(XACML3.ID_FUNCTION_OR, fd.getId());
83 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeArgs().getId());
84 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId());
86 // just to be safe... If tests take too long these can probably be eliminated
87 assertFalse(fd.returnsBag());
93 ExpressionResult res = fd.evaluate(null, arguments);
94 assertTrue(res.isOk());
95 Boolean resValue = (Boolean)res.getValue().getValue();
96 assertEquals(new Boolean(true), resValue);
100 res = fd.evaluate(null, arguments);
101 assertTrue(res.isOk());
102 resValue = (Boolean)res.getValue().getValue();
103 assertEquals(new Boolean(false), resValue);
107 res = fd.evaluate(null, arguments);
108 assertTrue(res.isOk());
109 resValue = (Boolean)res.getValue().getValue();
110 assertEquals(new Boolean(false), resValue);
112 // first true, second error
114 arguments.add(attrT);
116 res = fd.evaluate(null, arguments);
117 assertTrue(res.isOk());
118 resValue = (Boolean)res.getValue().getValue();
119 assertEquals(new Boolean(true), resValue);
121 // first false, second error
123 arguments.add(attrF);
125 res = fd.evaluate(null, arguments);
126 assertFalse(res.getStatus().isOk());
127 assertEquals( "function:or Got null argument", res.getStatus().getStatusMessage());
128 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
132 arguments.add(attrF);
133 arguments.add(attrF);
134 arguments.add(attrF);
135 arguments.add(attrF);
136 arguments.add(attrF);
137 res = fd.evaluate(null, arguments);
138 assertTrue(res.isOk());
139 resValue = (Boolean)res.getValue().getValue();
140 assertEquals(new Boolean(false), resValue);
144 arguments.add(attrF);
145 arguments.add(attr5);
146 res = fd.evaluate(null, arguments);
147 assertFalse(res.getStatus().isOk());
148 assertEquals( "function:or Expected data type 'boolean' saw 'integer'", res.getStatus().getStatusMessage());
149 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
154 res = fd.evaluate(null, arguments);
155 assertFalse(res.getStatus().isOk());
156 assertEquals( "function:or Got null argument", res.getStatus().getStatusMessage());
157 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
163 public void testAND() {
164 FunctionArgumentAttributeValue attr5 = null;
166 attr5 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
168 } catch (Exception e) {
169 fail("creating attribute e="+ e);
173 FunctionDefinitionLogical fd = (FunctionDefinitionLogical) StdFunctions.FD_AND;
175 // check identity and type of the thing created
176 assertEquals(XACML3.ID_FUNCTION_AND, fd.getId());
177 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeArgs().getId());
178 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId());
180 // just to be safe... If tests take too long these can probably be eliminated
181 assertFalse(fd.returnsBag());
185 arguments.add(attrT);
186 arguments.add(attrF);
187 ExpressionResult res = fd.evaluate(null, arguments);
188 assertTrue(res.isOk());
189 Boolean resValue = (Boolean)res.getValue().getValue();
190 assertEquals(new Boolean(false), resValue);
193 arguments.add(attrF);
194 res = fd.evaluate(null, arguments);
195 assertTrue(res.isOk());
196 resValue = (Boolean)res.getValue().getValue();
197 assertEquals(new Boolean(false), resValue);
201 res = fd.evaluate(null, arguments);
202 assertTrue(res.isOk());
203 resValue = (Boolean)res.getValue().getValue();
204 assertEquals(new Boolean(true), resValue);
206 // first true, second error
208 arguments.add(attrT);
210 res = fd.evaluate(null, arguments);
211 assertFalse(res.getStatus().isOk());
212 assertEquals( "function:and Got null argument", res.getStatus().getStatusMessage());
213 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
215 // first false, second error
217 arguments.add(attrF);
219 res = fd.evaluate(null, arguments);
220 assertTrue(res.isOk());
221 resValue = (Boolean)res.getValue().getValue();
222 assertEquals(new Boolean(false), resValue);
226 arguments.add(attrT);
227 arguments.add(attrT);
228 arguments.add(attrT);
229 arguments.add(attrT);
230 arguments.add(attrT);
231 res = fd.evaluate(null, arguments);
232 assertTrue(res.isOk());
233 resValue = (Boolean)res.getValue().getValue();
234 assertEquals(new Boolean(true), resValue);
238 arguments.add(attrT);
239 arguments.add(attr5);
240 res = fd.evaluate(null, arguments);
241 assertFalse(res.getStatus().isOk());
242 assertEquals("function:and Expected data type 'boolean' saw 'integer'", res.getStatus().getStatusMessage());
243 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
248 res = fd.evaluate(null, arguments);
249 assertFalse(res.getStatus().isOk());
250 assertEquals("function:and Got null argument", res.getStatus().getStatusMessage());
251 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
259 public void testN_of() {
260 FunctionArgumentAttributeValue attr0 = null;
261 FunctionArgumentAttributeValue attr2 = null;
263 attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(0));
264 attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(2));
265 } catch (Exception e) {
266 fail("creating attribute e="+ e);
270 FunctionDefinitionLogical fd = (FunctionDefinitionLogical) StdFunctions.FD_N_OF;
272 // check identity and type of the thing created
273 assertEquals(XACML3.ID_FUNCTION_N_OF, fd.getId());
274 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeArgs().getId());
275 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId());
277 // just to be safe... If tests take too long these can probably be eliminated
278 assertFalse(fd.returnsBag());
282 arguments.add(attr2);
283 arguments.add(attrT);
284 arguments.add(attrF);
285 arguments.add(attrT);
286 ExpressionResult res = fd.evaluate(null, arguments);
287 assertTrue(res.isOk());
288 Boolean resValue = (Boolean)res.getValue().getValue();
289 assertEquals(new Boolean(true), resValue);
293 arguments.add(attr2);
294 arguments.add(attrT);
295 arguments.add(attrF);
296 res = fd.evaluate(null, arguments);
297 assertTrue(res.isOk());
298 resValue = (Boolean)res.getValue().getValue();
299 assertEquals(new Boolean(false), resValue);
305 arguments.add(attrT);
306 arguments.add(attrF);
307 res = fd.evaluate(null, arguments);
308 assertTrue(res.isOk());
309 resValue = (Boolean)res.getValue().getValue();
310 assertEquals(new Boolean(true), resValue);
314 arguments.add(attr0);
315 arguments.add(attrT);
316 arguments.add(attrF);
317 res = fd.evaluate(null, arguments);
318 assertTrue(res.isOk());
319 resValue = (Boolean)res.getValue().getValue();
320 assertEquals(new Boolean(true), resValue);
322 // bad object type for count
324 arguments.add(attrT);
325 arguments.add(attrT);
326 arguments.add(attrF);
327 res = fd.evaluate(null, arguments);
328 assertFalse(res.getStatus().isOk());
329 assertEquals( "function:n-of For input string: \"true\"", res.getStatus().getStatusMessage());
330 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
332 // count larger than list
334 arguments.add(attr2);
335 arguments.add(attrT);
336 res = fd.evaluate(null, arguments);
337 assertFalse(res.getStatus().isOk());
338 assertEquals( "function:n-of Expected 2 arguments but only 1 arguments in list after the count", res.getStatus().getStatusMessage());
339 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
341 // aborts after find ok
343 arguments.add(attr2);
344 arguments.add(attrT);
345 arguments.add(attrT);
347 res = fd.evaluate(null, arguments);
348 assertTrue(res.isOk());
349 resValue = (Boolean)res.getValue().getValue();
350 assertEquals(new Boolean(true), resValue);
352 // error before find ok
354 arguments.add(attr2);
356 arguments.add(attrT);
357 res = fd.evaluate(null, arguments);
358 assertFalse(res.getStatus().isOk());
359 assertEquals( "function:n-of Got null argument", res.getStatus().getStatusMessage());
360 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
363 // non-boolean in list
365 arguments.add(attr2);
366 arguments.add(attrT);
367 arguments.add(attr0);
368 res = fd.evaluate(null, arguments);
369 assertFalse(res.getStatus().isOk());
370 assertEquals( "function:n-of Expected data type 'boolean' saw 'integer'", res.getStatus().getStatusMessage());
371 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
377 public void testNot() {
379 FunctionDefinitionLogical fd = (FunctionDefinitionLogical) StdFunctions.FD_NOT;
381 // check identity and type of the thing created
382 assertEquals(XACML3.ID_FUNCTION_NOT, fd.getId());
383 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeArgs().getId());
384 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId());
386 // just to be safe... If tests take too long these can probably be eliminated
387 assertFalse(fd.returnsBag());
392 arguments.add(attrT);
393 ExpressionResult res = fd.evaluate(null, arguments);
394 assertTrue(res.isOk());
395 Boolean resValue = (Boolean)res.getValue().getValue();
396 assertEquals(new Boolean(false), resValue);
399 arguments.add(attrF);
400 res = fd.evaluate(null, arguments);
401 assertTrue(res.isOk());
402 resValue = (Boolean)res.getValue().getValue();
403 assertEquals(new Boolean(true), resValue);
408 res = fd.evaluate(null, arguments);
409 assertFalse(res.getStatus().isOk());
410 assertEquals( "function:not Expected 1 argument, got 0", res.getStatus().getStatusMessage());
411 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
415 arguments.add(attrT);
416 arguments.add(attrF);
417 res = fd.evaluate(null, arguments);
418 assertFalse(res.getStatus().isOk());
419 assertEquals( "function:not Expected 1 argument, got 2", res.getStatus().getStatusMessage());
420 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());