Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionNumberTypeConversionTest.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  * Tests for various classes containing only one function.
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 FunctionDefinitionNumberTypeConversionTest {
51         
52         /*
53          * variables useful in the following tests
54          */
55         List<FunctionArgument> arguments = new ArrayList<>();
56         
57         @Test
58         public void testDouble_to_integer() {
59                 FunctionArgumentAttributeValue attr1 = null;
60                 try {
61                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(5.432));
62
63                 } catch (Exception e) {
64                         fail("creating attribute e="+ e);
65                 }
66                 
67                 FunctionDefinitionNumberTypeConversion<?, ?> fd = (FunctionDefinitionNumberTypeConversion<?, ?>) StdFunctions.FD_DOUBLE_TO_INTEGER;
68                 
69                 // check identity and type of the thing created
70                 assertEquals(XACML3.ID_FUNCTION_DOUBLE_TO_INTEGER, fd.getId());
71                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeArgs().getId());
72                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
73                 
74                 // just to be safe...  If tests take too long these can probably be eliminated
75                 assertFalse(fd.returnsBag());
76                 assertEquals(new Integer(1), fd.getNumArgs());
77                 
78                 
79                 // test normal add
80                 arguments.add(attr1);
81                 ExpressionResult res = fd.evaluate(null, arguments);
82                 assertTrue(res.isOk());
83                 BigInteger resValue = (BigInteger)res.getValue().getValue();
84                 assertEquals(BigInteger.valueOf(5), resValue);
85         }
86
87         
88         @Test
89         public void testInteger_to_double() {
90                 FunctionArgumentAttributeValue attr1 = null;
91                 try {
92                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(5));
93
94                 } catch (Exception e) {
95                         fail("creating attribute e="+ e);
96                 }
97                 
98                 FunctionDefinitionNumberTypeConversion<?, ?> fd = (FunctionDefinitionNumberTypeConversion<?, ?>) StdFunctions.FD_INTEGER_TO_DOUBLE;
99                 
100                 // check identity and type of the thing created
101                 assertEquals(XACML3.ID_FUNCTION_INTEGER_TO_DOUBLE, fd.getId());
102                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeArgs().getId());
103                 assertEquals(DataTypes.DT_DOUBLE.getId(), fd.getDataTypeId());
104                 
105                 // just to be safe...  If tests take too long these can probably be eliminated
106                 assertFalse(fd.returnsBag());
107                 assertEquals(new Integer(1), fd.getNumArgs());
108                 
109                 
110                 // test normal add
111                 arguments.add(attr1);
112                 ExpressionResult res = fd.evaluate(null, arguments);
113                 assertTrue(res.isOk());
114                 Double resValue = (Double)res.getValue().getValue();
115                 assertEquals(new Double(5.0), resValue);
116         }
117         
118         
119 }