Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionArithmeticTest.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.math.BigInteger;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.junit.Test;
33
34 import com.att.research.xacml.api.XACML3;
35 import com.att.research.xacml.std.datatypes.DataTypes;
36 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
37 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
38 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentAttributeValue;
39 import com.att.research.xacmlatt.pdp.std.StdFunctions;
40 import com.att.research.xacmlatt.pdp.std.functions.*;
41
42 /**
43  * Test of PDP Functions (See XACML core spec section A.3)
44  * 
45  * TO RUN - use jUnit
46  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
47  * 
48  *
49  */
50 public class FunctionDefinitionArithmeticTest {
51
52         /*
53          * variables useful in the following tests
54          */
55         List<FunctionArgument> arguments = new ArrayList<>();
56         
57         @Test
58         public void testInteger_add() {
59                 
60                 FunctionArgumentAttributeValue attr1 = null;
61                 FunctionArgumentAttributeValue attr2 = null;
62                 FunctionArgumentAttributeValue attrBadType = null;
63                 try {
64                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(1));
65                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(1));
66                         attrBadType = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.1));
67                 } catch (Exception e) {
68                         fail("creating attributes e="+ e);
69                 }
70                 
71                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_INTEGER_ADD;
72                 
73                 // check identity and type of the thing created
74                 assertEquals(XACML3.ID_FUNCTION_INTEGER_ADD, fd.getId());
75                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeArgs().getId());
76                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
77                 
78                 // just to be safe...  If tests take too long these can probably be eliminated
79                 assertFalse(fd.returnsBag());
80                 assertEquals(new Integer(2), fd.getNumArgs());
81                 
82                 
83                 // test normal add
84                 arguments.add(attr1);
85                 arguments.add(attr2);
86                 ExpressionResult res = fd.evaluate(null, arguments);
87                 assertTrue(res.isOk());
88                 BigInteger resValue = (BigInteger)res.getValue().getValue();
89                 assertEquals(new BigInteger("2"), resValue);
90                 
91                 arguments.clear();
92                 arguments.add(attr1);
93                 arguments.add(attrBadType);
94                 res = fd.evaluate(null, arguments);
95                 assertFalse(res.isOk());
96                 assertEquals("function:integer-add Expected data type 'integer' saw 'double' at arg index 1", res.getStatus().getStatusMessage());
97                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
98                 
99         }
100
101         
102         @Test
103         public void testDouble_add() {
104                 
105                 FunctionArgumentAttributeValue attr1 = null;
106                 FunctionArgumentAttributeValue attr2  = null;
107
108                 try {
109                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.5));
110                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(2.5));
111                 } catch (Exception e) {
112                         fail("creating attributes e="+e);
113                 }
114                 
115                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_DOUBLE_ADD;
116                 
117                 // check identity and type of the thing created
118                 assertEquals(XACML3.ID_FUNCTION_DOUBLE_ADD, fd.getId());
119                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
120                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
121                 
122                 // just to be safe...  If tests take too long these can probably be eliminated
123                 assertFalse(fd.returnsBag());
124                 assertEquals(new Integer(2), fd.getNumArgs());
125                 
126                 
127                 // test normal add
128                 arguments.add(attr1);
129                 arguments.add(attr2);
130                 ExpressionResult res = fd.evaluate(null, arguments);
131                 assertTrue(res.isOk());
132                 Double resValue = (Double)res.getValue().getValue();
133                 assertEquals(new Double(4.0), resValue);
134                 
135         }
136         
137         
138         @Test
139         public void testInteger_subtract() {
140                 
141                 FunctionArgumentAttributeValue attr1 = null;
142                 FunctionArgumentAttributeValue attr2 = null;
143                 try {
144                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(6));
145                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(1));
146                 } catch (Exception e) {
147                         fail("creating attributes e="+ e);
148                 }
149                 
150                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_INTEGER_SUBTRACT;
151                 
152                 // check identity and type of the thing created
153                 assertEquals(XACML3.ID_FUNCTION_INTEGER_SUBTRACT, fd.getId());
154                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeArgs().getId());
155                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
156                 
157                 // just to be safe...  If tests take too long these can probably be eliminated
158                 assertFalse(fd.returnsBag());
159                 assertEquals(new Integer(2), fd.getNumArgs());
160                 
161                 
162                 // test normal 
163                 arguments.add(attr1);
164                 arguments.add(attr2);
165                 ExpressionResult res = fd.evaluate(null, arguments);
166                 assertTrue(res.isOk());
167                 BigInteger resValue = (BigInteger)res.getValue().getValue();
168                 assertEquals(new BigInteger("5"), resValue);
169                 
170         }
171
172         
173         @Test
174         public void testDouble_subtract() {
175                 
176                 FunctionArgumentAttributeValue attr1 = null;
177                 FunctionArgumentAttributeValue attr2  = null;
178
179                 try {
180                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(8.5));
181                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(2.3));
182                 } catch (Exception e) {
183                         fail("creating attributes e="+e);
184                 }
185                 
186                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_DOUBLE_SUBTRACT;
187                 
188                 // check identity and type of the thing created
189                 assertEquals(XACML3.ID_FUNCTION_DOUBLE_SUBTRACT, fd.getId());
190                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
191                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
192                 
193                 // just to be safe...  If tests take too long these can probably be eliminated
194                 assertFalse(fd.returnsBag());
195                 assertEquals(new Integer(2), fd.getNumArgs());
196                 
197                 
198                 // test normal 
199                 arguments.add(attr1);
200                 arguments.add(attr2);
201                 ExpressionResult res = fd.evaluate(null, arguments);
202                 assertTrue(res.isOk());
203                 Double resValue = (Double)res.getValue().getValue();
204                 assertEquals(new Double(6.2), resValue);
205                 
206         }
207         
208         
209         @Test
210         public void testInteger_multiply() {
211                 
212                 FunctionArgumentAttributeValue attr0 = null;
213                 FunctionArgumentAttributeValue attr1 = null;
214                 FunctionArgumentAttributeValue attr2 = null;
215                 try {
216                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(0));
217                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
218                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(2));
219                 } catch (Exception e) {
220                         fail("creating attributes e="+ e);
221                 }
222                 
223                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_INTEGER_MULTIPLY;
224                 
225                 // check identity and type of the thing created
226                 assertEquals(XACML3.ID_FUNCTION_INTEGER_MULTIPLY, fd.getId());
227                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeArgs().getId());
228                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
229                 
230                 // just to be safe...  If tests take too long these can probably be eliminated
231                 assertFalse(fd.returnsBag());
232                 assertEquals(new Integer(2), fd.getNumArgs());
233                 
234                 
235                 // test normal 
236                 arguments.add(attr1);
237                 arguments.add(attr2);
238                 ExpressionResult res = fd.evaluate(null, arguments);
239                 assertTrue(res.isOk());
240                 BigInteger resValue = (BigInteger)res.getValue().getValue();
241                 assertEquals(new BigInteger("10"), resValue);
242                 
243                 
244                 // test 0
245                 arguments.clear();
246                 arguments.add(attr1);
247                 arguments.add(attr0);
248                 res = fd.evaluate(null, arguments);
249                 assertTrue(res.isOk());
250                 resValue = (BigInteger)res.getValue().getValue();
251                 assertEquals(new BigInteger("0"), resValue);
252         }
253
254         
255         @Test
256         public void testDouble_multiply() {
257                 
258                 FunctionArgumentAttributeValue attr0 = null;
259                 FunctionArgumentAttributeValue attr1 = null;
260                 FunctionArgumentAttributeValue attr2  = null;
261
262                 try {
263                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(0));
264                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.5));
265                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(2.5));
266                 } catch (Exception e) {
267                         fail("creating attributes e="+e);
268                 }
269                 
270                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_DOUBLE_MULTIPLY;
271                 
272                 // check identity and type of the thing created
273                 assertEquals(XACML3.ID_FUNCTION_DOUBLE_MULTIPLY, fd.getId());
274                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
275                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
276                 
277                 // just to be safe...  If tests take too long these can probably be eliminated
278                 assertFalse(fd.returnsBag());
279                 assertEquals(new Integer(2), fd.getNumArgs());
280                 
281                 
282                 // test normal add
283                 arguments.add(attr1);
284                 arguments.add(attr2);
285                 ExpressionResult res = fd.evaluate(null, arguments);
286                 assertTrue(res.isOk());
287                 Double resValue = (Double)res.getValue().getValue();
288                 assertEquals(new Double(3.75), resValue);
289                 
290                 // test multiply by 0
291                 arguments.clear();
292                 arguments.add(attr1);
293                 arguments.add(attr0);
294                 res = fd.evaluate(null, arguments);
295                 assertTrue(res.isOk());
296                 resValue = (Double)res.getValue().getValue();
297                 assertEquals(new Double(0), resValue);
298         }
299         
300         
301         @Test
302         public void testInteger_divide() {
303                 
304                 FunctionArgumentAttributeValue attr0 = null;
305                 FunctionArgumentAttributeValue attr1 = null;
306                 FunctionArgumentAttributeValue attr2 = null;
307                 try {
308                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(0));
309                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
310                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(2));
311                 } catch (Exception e) {
312                         fail("creating attributes e="+ e);
313                 }
314                 
315                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_INTEGER_DIVIDE;
316                 
317                 // check identity and type of the thing created
318                 assertEquals(XACML3.ID_FUNCTION_INTEGER_DIVIDE, fd.getId());
319                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeArgs().getId());
320                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
321                 
322                 // just to be safe...  If tests take too long these can probably be eliminated
323                 assertFalse(fd.returnsBag());
324                 assertEquals(new Integer(2), fd.getNumArgs());
325                 
326                 
327                 // test normal 
328                 arguments.add(attr1);
329                 arguments.add(attr2);
330                 ExpressionResult res = fd.evaluate(null, arguments);
331                 assertTrue(res.isOk());
332                 BigInteger resValue = (BigInteger)res.getValue().getValue();
333                 assertEquals(new BigInteger("2"), resValue);
334                 
335                 
336                 // test 0
337                 arguments.clear();
338                 arguments.add(attr1);
339                 arguments.add(attr0);
340                 res = fd.evaluate(null, arguments);
341                 assertFalse(res.isOk());
342                 assertEquals("function:integer-divide Divide by 0 error: 5, 0", res.getStatus().getStatusMessage());
343                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
344
345         }
346
347         
348         @Test
349         public void testDouble_divide() {
350                 
351                 FunctionArgumentAttributeValue attr0 = null;
352                 FunctionArgumentAttributeValue attr1 = null;
353                 FunctionArgumentAttributeValue attr2  = null;
354
355                 try {
356                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(0));
357                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.5));
358                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(2.5));
359
360                 } catch (Exception e) {
361                         fail("creating attributes e="+e);
362                 }
363                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_DOUBLE_DIVIDE;
364                 
365                 // check identity and type of the thing created
366                 assertEquals(XACML3.ID_FUNCTION_DOUBLE_DIVIDE, fd.getId());
367                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
368                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
369                 
370                 // just to be safe...  If tests take too long these can probably be eliminated
371                 assertFalse(fd.returnsBag());
372                 assertEquals(new Integer(2), fd.getNumArgs());
373                 
374                 
375                 // test normal 
376                 arguments.add(attr1);
377                 arguments.add(attr2);
378                 ExpressionResult res = fd.evaluate(null, arguments);
379                 assertTrue(res.isOk());
380                 Double resValue = (Double)res.getValue().getValue();
381                 assertEquals(new Double(0.6), resValue);
382                 
383                 // test multiply by 0
384                 arguments.clear();
385                 arguments.add(attr1);
386                 arguments.add(attr0);
387                 res = fd.evaluate(null, arguments);
388                 assertFalse(res.isOk());
389                 assertEquals("function:double-divide Divide by 0 error: 1.5, 0.0", res.getStatus().getStatusMessage());
390                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
391
392         }
393         
394         
395         
396         
397         
398         
399         @Test
400         public void testInteger_mod() {
401                 
402                 FunctionArgumentAttributeValue attr0 = null;
403                 FunctionArgumentAttributeValue attr1 = null;
404                 FunctionArgumentAttributeValue attr2 = null;
405                 try {
406                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(0));
407                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(28));
408                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
409                 } catch (Exception e) {
410                         fail("creating attributes e="+ e);
411                 }
412                 
413                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_INTEGER_MOD;
414                 
415                 // check identity and type of the thing created
416                 assertEquals(XACML3.ID_FUNCTION_INTEGER_MOD, fd.getId());
417                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeArgs().getId());
418                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
419                 
420                 // just to be safe...  If tests take too long these can probably be eliminated
421                 assertFalse(fd.returnsBag());
422                 assertEquals(new Integer(2), fd.getNumArgs());
423                 
424                 
425                 // test normal 
426                 arguments.add(attr1);
427                 arguments.add(attr2);
428                 ExpressionResult res = fd.evaluate(null, arguments);
429                 assertTrue(res.isOk());
430                 BigInteger resValue = (BigInteger)res.getValue().getValue();
431                 assertEquals(new BigInteger("3"), resValue);
432                 
433                 
434                 // test 0
435                 arguments.clear();
436                 arguments.add(attr1);
437                 arguments.add(attr0);
438                 res = fd.evaluate(null, arguments);
439                 assertFalse(res.isOk());
440                 assertEquals("function:integer-mod Divide by 0 error: 28, 0", res.getStatus().getStatusMessage());
441                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
442
443         }
444         
445
446         @Test
447         public void testInteger_abs() {
448
449                 FunctionArgumentAttributeValue attr0 = null;
450                 FunctionArgumentAttributeValue attr1 = null;
451                 FunctionArgumentAttributeValue attrM1 = null;
452                 try {
453                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(0));
454                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
455                         attrM1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(-7));
456                 } catch (Exception e) {
457                         fail("creating attributes e="+ e);
458                 }
459                 
460                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_INTEGER_ABS;
461                 
462                 // check identity and type of the thing created
463                 assertEquals(XACML3.ID_FUNCTION_INTEGER_ABS, fd.getId());
464                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeArgs().getId());
465                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
466                 
467                 // just to be safe...  If tests take too long these can probably be eliminated
468                 assertFalse(fd.returnsBag());
469                 assertEquals(new Integer(1), fd.getNumArgs());
470                 
471                 
472                 // test normal 
473                 arguments.add(attr1);
474                 ExpressionResult res = fd.evaluate(null, arguments);
475                 assertTrue(res.isOk());
476                 BigInteger resValue = (BigInteger)res.getValue().getValue();
477                 assertEquals(new BigInteger("5"), resValue);
478                 
479                 arguments.clear();
480                 arguments.add(attrM1);
481                 res = fd.evaluate(null, arguments);
482                 assertTrue(res.isOk());
483                 resValue = (BigInteger)res.getValue().getValue();
484                 assertEquals(new BigInteger("7"), resValue);
485
486                 arguments.clear();
487                 arguments.add(attr0);
488                 res = fd.evaluate(null, arguments);
489                 assertTrue(res.isOk());
490                 resValue = (BigInteger)res.getValue().getValue();
491                 assertEquals(new BigInteger("0"), resValue);
492         }
493
494         
495         @Test
496         public void testDouble_abs() {
497                 
498                 FunctionArgumentAttributeValue attr0 = null;
499                 FunctionArgumentAttributeValue attr1 = null;
500                 FunctionArgumentAttributeValue attr2  = null;
501
502                 try {
503                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(0));
504                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.5));
505                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(-2.5));
506
507                 } catch (Exception e) {
508                         fail("creating attributes e="+e);
509                 }
510                 
511                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_DOUBLE_ABS;
512                 
513                 // check identity and type of the thing created
514                 assertEquals(XACML3.ID_FUNCTION_DOUBLE_ABS, fd.getId());
515                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
516                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
517                 
518                 // just to be safe...  If tests take too long these can probably be eliminated
519                 assertFalse(fd.returnsBag());
520                 assertEquals(new Integer(1), fd.getNumArgs());
521                 
522                 
523                 // test normal 
524                 arguments.add(attr1);
525                 ExpressionResult res = fd.evaluate(null, arguments);
526                 assertTrue(res.isOk());
527                 Double resValue = (Double)res.getValue().getValue();
528                 assertEquals(new Double(1.5), resValue);
529                 
530                 arguments.clear();
531                 arguments.add(attr2);
532                 res = fd.evaluate(null, arguments);
533                 assertTrue(res.isOk());
534                 resValue = (Double)res.getValue().getValue();
535                 assertEquals(new Double(2.5), resValue);
536                 
537                 arguments.clear();
538                 arguments.add(attr0);
539                 res = fd.evaluate(null, arguments);
540                 assertTrue(res.isOk());
541                 resValue = (Double)res.getValue().getValue();
542                 assertEquals(new Double(0), resValue);
543
544         }
545         
546         
547         @Test
548         public void testDouble_round() {
549
550                 FunctionArgumentAttributeValue attr0 = null;
551                 FunctionArgumentAttributeValue attr1 = null;
552                 FunctionArgumentAttributeValue attr2  = null;
553                 FunctionArgumentAttributeValue attr3 = null;
554                 FunctionArgumentAttributeValue attr4 = null;
555                 FunctionArgumentAttributeValue attr5 = null;
556                 FunctionArgumentAttributeValue attr6 = null;
557                 try {
558                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(0));
559                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.5));
560                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.49));
561                         attr3 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.51));
562                         attr4 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(-2.5));
563                         attr5 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(-2.49));
564                         attr6 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(-2.51));
565                 } catch (Exception e) {
566                         fail("creating attributes e="+e);
567                 }
568                 
569                 
570                 
571                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_ROUND;
572                 
573                 // check identity and type of the thing created
574                 assertEquals(XACML3.ID_FUNCTION_ROUND, fd.getId());
575                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
576                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
577                 
578                 // just to be safe...  If tests take too long these can probably be eliminated
579                 assertFalse(fd.returnsBag());
580                 assertEquals(new Integer(1), fd.getNumArgs());
581                 
582                 
583                 // test normal 
584                 arguments.add(attr0);
585                 ExpressionResult res = fd.evaluate(null, arguments);
586                 assertTrue(res.isOk());
587                 Double resValue = (Double)res.getValue().getValue();
588                 assertEquals(new Double(0), resValue);
589                 
590                 arguments.clear();
591                 arguments.add(attr1);
592                 res = fd.evaluate(null, arguments);
593                 assertTrue(res.isOk());
594                 resValue = (Double)res.getValue().getValue();
595                 assertEquals(new Double(2), resValue);
596                 
597                 arguments.clear();
598                 arguments.add(attr2);
599                 res = fd.evaluate(null, arguments);
600                 assertTrue(res.isOk());
601                 resValue = (Double)res.getValue().getValue();
602                 assertEquals(new Double(1), resValue);
603                 
604                 arguments.clear();
605                 arguments.add(attr3);
606                 res = fd.evaluate(null, arguments);
607                 assertTrue(res.isOk());
608                 resValue = (Double)res.getValue().getValue();
609                 assertEquals(new Double(2), resValue);
610                 
611                 arguments.clear();
612                 arguments.add(attr4);
613                 res = fd.evaluate(null, arguments);
614                 assertTrue(res.isOk());
615                 resValue = (Double)res.getValue().getValue();
616                 assertEquals(new Double(-2), resValue);
617         
618                 arguments.clear();
619                 arguments.add(attr5);
620                 res = fd.evaluate(null, arguments);
621                 assertTrue(res.isOk());
622                 resValue = (Double)res.getValue().getValue();
623                 assertEquals(new Double(-2), resValue);
624                 
625                 arguments.clear();
626                 arguments.add(attr6);
627                 res = fd.evaluate(null, arguments);
628                 assertTrue(res.isOk());
629                 resValue = (Double)res.getValue().getValue();
630                 assertEquals(new Double(-3), resValue);
631         }
632         
633         
634         @Test
635         public void testDouble_floor() {
636                 FunctionArgumentAttributeValue attr0 = null;
637                 FunctionArgumentAttributeValue attr1 = null;
638                 FunctionArgumentAttributeValue attr2  = null;
639                 FunctionArgumentAttributeValue attr3 = null;
640                 FunctionArgumentAttributeValue attr4 = null;
641                 FunctionArgumentAttributeValue attr5 = null;
642                 FunctionArgumentAttributeValue attr6 = null;
643                 try {
644                         attr0 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(0));
645                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.5));
646                         attr2 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.49));
647                         attr3 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(1.51));
648                         attr4 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(-2.5));
649                         attr5 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(-2.49));
650                         attr6 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(-2.51));
651                 } catch (Exception e) {
652                         fail("creating attributes e="+e);
653                 }
654                 
655                 FunctionDefinitionArithmetic<?> fd = (FunctionDefinitionArithmetic<?>) StdFunctions.FD_FLOOR;
656                 
657                 // check identity and type of the thing created
658                 assertEquals(XACML3.ID_FUNCTION_FLOOR, fd.getId());
659                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
660                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
661                 
662                 // just to be safe...  If tests take too long these can probably be eliminated
663                 assertFalse(fd.returnsBag());
664                 assertEquals(new Integer(1), fd.getNumArgs());
665                 
666                 
667                 // test normal 
668                 arguments.add(attr0);
669                 ExpressionResult res = fd.evaluate(null, arguments);
670                 assertTrue(res.isOk());
671                 Double resValue = (Double)res.getValue().getValue();
672                 assertEquals(new Double(0), resValue);
673                 
674                 arguments.clear();
675                 arguments.add(attr1);
676                 res = fd.evaluate(null, arguments);
677                 assertTrue(res.isOk());
678                 resValue = (Double)res.getValue().getValue();
679                 assertEquals(new Double(1), resValue);
680                 
681                 arguments.clear();
682                 arguments.add(attr2);
683                 res = fd.evaluate(null, arguments);
684                 assertTrue(res.isOk());
685                 resValue = (Double)res.getValue().getValue();
686                 assertEquals(new Double(1), resValue);
687                 
688                 arguments.clear();
689                 arguments.add(attr3);
690                 res = fd.evaluate(null, arguments);
691                 assertTrue(res.isOk());
692                 resValue = (Double)res.getValue().getValue();
693                 assertEquals(new Double(1), resValue);
694                 
695                 arguments.clear();
696                 arguments.add(attr4);
697                 res = fd.evaluate(null, arguments);
698                 assertTrue(res.isOk());
699                 resValue = (Double)res.getValue().getValue();
700                 assertEquals(new Double(-3), resValue);
701         
702                 arguments.clear();
703                 arguments.add(attr5);
704                 res = fd.evaluate(null, arguments);
705                 assertTrue(res.isOk());
706                 resValue = (Double)res.getValue().getValue();
707                 assertEquals(new Double(-3), resValue);
708                 
709                 arguments.clear();
710                 arguments.add(attr6);
711                 res = fd.evaluate(null, arguments);
712                 assertTrue(res.isOk());
713                 resValue = (Double)res.getValue().getValue();
714                 assertEquals(new Double(-3), resValue);
715         }
716         
717 }