Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionLogicalTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP
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
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
21 package org.openecomp.policy.pdp.test;
22
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;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.junit.Test;
32
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.*;
40
41 /**
42  * Test of PDP Functions (See XACML core spec section A.3)
43  * 
44  * TO RUN - use jUnit
45  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
46  * 
47  *
48  */
49 public class FunctionDefinitionLogicalTest {
50
51         /*
52          * variables useful in the following tests
53          */
54         List<FunctionArgument> arguments = new ArrayList<>();
55         
56         // use the same args for each test
57         FunctionArgumentAttributeValue attrT = null;
58         FunctionArgumentAttributeValue attrF = null;
59         public FunctionDefinitionLogicalTest () {
60                 try {
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);
65                 }
66         }
67         
68         
69         @Test
70         public void testOR() {
71                 FunctionArgumentAttributeValue attr5 = null;
72                 try {
73                         attr5 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
74
75                 } catch (Exception e) {
76                         fail("creating attribute e="+ e);
77                 }
78                 
79                 FunctionDefinitionLogical fd = (FunctionDefinitionLogical) StdFunctions.FD_OR;
80                 
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());
85                 
86                 // just to be safe...  If tests take too long these can probably be eliminated
87                 assertFalse(fd.returnsBag());
88                 
89                 
90                 // test normal 
91                 arguments.add(attrT);
92                 arguments.add(attrF);
93                 ExpressionResult res = fd.evaluate(null, arguments);
94                 assertTrue(res.isOk());
95                 Boolean resValue = (Boolean)res.getValue().getValue();
96                 assertEquals(new Boolean(true), resValue);
97                 
98                 arguments.clear();
99                 arguments.add(attrF);
100                 res = fd.evaluate(null, arguments);
101                 assertTrue(res.isOk());
102                 resValue = (Boolean)res.getValue().getValue();
103                 assertEquals(new Boolean(false), resValue);
104                 
105                 //      test no args
106                 arguments.clear();
107                 res = fd.evaluate(null, arguments);
108                 assertTrue(res.isOk());
109                 resValue = (Boolean)res.getValue().getValue();
110                 assertEquals(new Boolean(false), resValue);
111                 
112                 // first true, second error
113                 arguments.clear();
114                 arguments.add(attrT);
115                 arguments.add(null);
116                 res = fd.evaluate(null, arguments);
117                 assertTrue(res.isOk());
118                 resValue = (Boolean)res.getValue().getValue();
119                 assertEquals(new Boolean(true), resValue);
120                 
121                 // first false, second error
122                 arguments.clear();
123                 arguments.add(attrF);
124                 arguments.add(null);
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());
129                 
130                 // multiple false
131                 arguments.clear();
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);
141                 
142                 // non-boolean
143                 arguments.clear();
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());
150                 
151                 // first arg error
152                 arguments.clear();
153                 arguments.add(null);
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());
158                 
159         }
160
161         
162         @Test
163         public void testAND() {
164                 FunctionArgumentAttributeValue attr5 = null;
165                 try {
166                         attr5 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
167
168                 } catch (Exception e) {
169                         fail("creating attribute e="+ e);
170                 }
171                 
172                 
173                 FunctionDefinitionLogical fd = (FunctionDefinitionLogical) StdFunctions.FD_AND;
174                 
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());
179                 
180                 // just to be safe...  If tests take too long these can probably be eliminated
181                 assertFalse(fd.returnsBag());
182                 
183                 
184                 // test normal 
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);
191                 
192                 arguments.clear();
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);
198                 
199                 //      test no args
200                 arguments.clear();
201                 res = fd.evaluate(null, arguments);
202                 assertTrue(res.isOk());
203                 resValue = (Boolean)res.getValue().getValue();
204                 assertEquals(new Boolean(true), resValue);
205                 
206                 // first true, second error
207                 arguments.clear();
208                 arguments.add(attrT);
209                 arguments.add(null);
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());
214                 
215                 // first false, second error
216                 arguments.clear();
217                 arguments.add(attrF);
218                 arguments.add(null);
219                 res = fd.evaluate(null, arguments);
220                 assertTrue(res.isOk());
221                 resValue = (Boolean)res.getValue().getValue();
222                 assertEquals(new Boolean(false), resValue);
223                 
224                 // multiple true
225                 arguments.clear();
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);
235                 
236                 // non-boolean
237                 arguments.clear();
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());
244                 
245                 // first arg error
246                 arguments.clear();
247                 arguments.add(null);
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());
252                 
253         }
254         
255         
256         
257         
258         @Test
259         public void testN_of() {
260                 FunctionArgumentAttributeValue attr0 = null;
261                 FunctionArgumentAttributeValue attr2 = null;
262                 try {
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);
267                 }
268                 
269                 
270                 FunctionDefinitionLogical fd = (FunctionDefinitionLogical) StdFunctions.FD_N_OF;
271                 
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());
276                 
277                 // just to be safe...  If tests take too long these can probably be eliminated
278                 assertFalse(fd.returnsBag());
279                 
280                 
281                 // test normal 
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);
290                 
291                 // normal fail
292                 arguments.clear();
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);
300                 
301                 
302                 // null count
303                 arguments.clear();
304                 arguments.add(null);
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);
311                 
312                 // 0 count
313                 arguments.clear();
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);
321                 
322                 // bad object type for count
323                 arguments.clear();
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());
331                 
332                 // count larger than list
333                 arguments.clear();
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());
340                 
341                 // aborts after find ok
342                 arguments.clear();
343                 arguments.add(attr2);
344                 arguments.add(attrT);
345                 arguments.add(attrT);
346                 arguments.add(null);
347                 res = fd.evaluate(null, arguments);
348                 assertTrue(res.isOk());
349                 resValue = (Boolean)res.getValue().getValue();
350                 assertEquals(new Boolean(true), resValue);
351                 
352                 // error before find ok
353                 arguments.clear();
354                 arguments.add(attr2);
355                 arguments.add(null);
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());
361                 
362                 
363                 // non-boolean in list
364                 arguments.clear();
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());
372                                 
373         }
374         
375         
376         @Test
377         public void testNot() {
378                 
379                 FunctionDefinitionLogical fd = (FunctionDefinitionLogical) StdFunctions.FD_NOT;
380                 
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());
385                 
386                 // just to be safe...  If tests take too long these can probably be eliminated
387                 assertFalse(fd.returnsBag());
388                 
389                 
390                 // test normal 
391                 arguments.clear();
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);
397                 
398                 arguments.clear();
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);
404                 
405                 
406                 // test null/0 args
407                 arguments.clear();
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());
412                 
413                 // test 2 args
414                 arguments.clear();
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());
421         }
422 }