Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionBagSizeTest.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
27 import java.math.BigInteger;
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.StdAttributeValue;
35 import com.att.research.xacml.std.datatypes.DataTypes;
36 import com.att.research.xacmlatt.pdp.policy.Bag;
37 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
38 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
39 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentBag;
40 import com.att.research.xacmlatt.pdp.std.StdFunctions;
41 import com.att.research.xacmlatt.pdp.std.functions.*;
42
43 /**
44  * Test of PDP Functions (See XACML core spec section A.3)
45  * 
46  * TO RUN - use jUnit
47  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
48  * 
49  *
50  */
51 public class FunctionDefinitionBagSizeTest {
52
53
54         /*
55          * variables useful in the following tests
56          */
57         List<FunctionArgument> arguments = new ArrayList<>();
58         
59         
60         @Test
61         public void testString() {
62                 String v1 = new String("abc");
63                 String v2 = new String("def");
64                 Integer vOtherType = new Integer(11);
65                 
66
67                 
68                 Bag bag0 = new Bag();
69                 Bag bag1 = new Bag();
70                         bag1.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));  
71                 Bag bag2 = new Bag();
72                         bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));  
73                         bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v2)); 
74                 Bag bagOtherType = new Bag();
75                         bagOtherType.add(new StdAttributeValue<Integer>(DataTypes.DT_INTEGER.getId(), vOtherType));  
76
77                 
78                 FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
79                 FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
80                 FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
81                 FunctionArgumentBag attrBagOtherType = new FunctionArgumentBag(bagOtherType);
82                 
83                 
84                 FunctionDefinitionBagSize<?> fd = (FunctionDefinitionBagSize<?>) StdFunctions.FD_STRING_BAG_SIZE;
85
86                 // check identity and type of the thing created
87                 assertEquals(XACML3.ID_FUNCTION_STRING_BAG_SIZE, fd.getId());
88                 assertEquals(DataTypes.DT_INTEGER.getId(), fd.getDataTypeId());
89                 
90                 // just to be safe...  If tests take too long these can probably be eliminated
91                 assertFalse(fd.returnsBag());
92
93                 
94                 
95                 
96                 // bag with only one
97                 arguments.clear();
98                 arguments.add(attrBag1);
99                 ExpressionResult res = fd.evaluate(null, arguments);
100                 assertTrue(res.isOk());
101                 assertEquals(java.math.BigInteger.class, res.getValue().getValue().getClass());
102                 BigInteger resValue = (BigInteger)res.getValue().getValue();
103                 assertEquals(BigInteger.valueOf(1), resValue);
104                 
105                 // null bag
106                 arguments.clear();
107                 arguments.add(null);
108                 res = fd.evaluate(null, arguments);
109                 assertFalse(res.isOk());
110                 assertEquals("function:string-bag-size Got null argument", res.getStatus().getStatusMessage());
111                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
112                 
113                 // bag with exactly one but of other type in it
114                 arguments.clear();
115                 arguments.add(attrBagOtherType);
116                 res = fd.evaluate(null, arguments);
117                 // NOTE: Size does not care about content type!
118                 assertTrue(res.isOk());
119                 assertEquals(java.math.BigInteger.class, res.getValue().getValue().getClass());
120                 resValue = (BigInteger)res.getValue().getValue();
121                 assertEquals(BigInteger.valueOf(1), resValue);
122                 
123                 // bag with none
124                 arguments.clear();
125                 arguments.add(attrBag0);
126                 res = fd.evaluate(null, arguments);
127                 assertTrue(res.isOk());
128                 assertEquals(java.math.BigInteger.class, res.getValue().getValue().getClass());
129                 resValue = (BigInteger)res.getValue().getValue();
130                 assertEquals(BigInteger.valueOf(0), resValue);
131                 
132                 // bag with multiple
133                 arguments.clear();
134                 arguments.add(attrBag2);
135                 res = fd.evaluate(null, arguments);
136                 assertTrue(res.isOk());
137                 assertEquals(java.math.BigInteger.class, res.getValue().getValue().getClass());
138                 resValue = (BigInteger)res.getValue().getValue();
139                 assertEquals(BigInteger.valueOf(2), resValue);
140         }
141         
142         
143
144         
145         
146         
147         
148         //
149         //
150         //  REST OF DATA TYPES OMITTED 
151         //      because they "should" all work the same
152         //
153         //
154         
155         
156         
157
158 }