Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionBagOneAndOnlyTest.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.util.ArrayList;
28 import java.util.List;
29 import java.math.BigInteger;
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 FunctionDefinitionBagOneAndOnlyTest {
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                 BigInteger vOtherType = BigInteger.valueOf(11);
65                 
66                 Bag bag0 = new Bag();
67                 Bag bag1 = new Bag();
68                         bag1.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));  
69                 Bag bag2 = new Bag();
70                         bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));  
71                         bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v2));
72                 Bag bagOtherType = new Bag();
73                         bagOtherType.add(new StdAttributeValue<BigInteger>(DataTypes.DT_INTEGER.getId(), vOtherType));
74                 
75                 FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
76                 FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
77                 FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
78                 FunctionArgumentBag attrBagOtherType = new FunctionArgumentBag(bagOtherType);
79                 
80                 
81                 FunctionDefinitionBagOneAndOnly<?> fd = (FunctionDefinitionBagOneAndOnly<?>) StdFunctions.FD_STRING_ONE_AND_ONLY;
82
83                 // check identity and type of the thing created
84                 assertEquals(XACML3.ID_FUNCTION_STRING_ONE_AND_ONLY, fd.getId());
85                 assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeId());
86                 
87                 // just to be safe...  If tests take too long these can probably be eliminated
88                 assertFalse(fd.returnsBag());
89
90                 
91                 
92                 
93                 // bag with only one
94                 arguments.clear();
95                 arguments.add(attrBag1);
96                 ExpressionResult res = fd.evaluate(null, arguments);
97                 assertTrue(res.isOk());
98                 assertEquals(java.lang.String.class, res.getValue().getValue().getClass());
99                 String resValue = (String)res.getValue().getValue();
100                 assertEquals(v1, resValue);
101                 
102                 // null bag
103                 arguments.clear();
104                 arguments.add(null);
105                 res = fd.evaluate(null, arguments);
106                 assertFalse(res.isOk());
107                 assertEquals("function:string-one-and-only Got null argument", res.getStatus().getStatusMessage());
108                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
109                 
110                 // bag with exactly one but of other type in it
111                 arguments.clear();
112                 arguments.add(attrBagOtherType);
113                 res = fd.evaluate(null, arguments);
114                 assertFalse(res.isOk());
115                 assertEquals("function:string-one-and-only Element in bag of wrong type. Expected string got integer", res.getStatus().getStatusMessage());
116                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
117                 
118                 // bag with none
119                 arguments.clear();
120                 arguments.add(attrBag0);
121                 res = fd.evaluate(null, arguments);
122                 assertFalse(res.isOk());
123                 assertEquals("function:string-one-and-only Expected 1 but Bag has 0 elements", res.getStatus().getStatusMessage());
124                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
125                 
126                 // bag with multiple
127                 arguments.clear();
128                 arguments.add(attrBag2);
129                 res = fd.evaluate(null, arguments);
130                 assertFalse(res.isOk());
131                 assertEquals("function:string-one-and-only Expected 1 but Bag has 2 elements", res.getStatus().getStatusMessage());
132                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
133         }
134         
135         
136         
137         @Test
138         public void testBoolean() {
139                 Boolean v1 = new Boolean(true);
140                 Boolean v2 = new Boolean(false);
141                 BigInteger vOtherType = BigInteger.valueOf(11);
142                 
143                 Bag bag0 = new Bag();
144                 Bag bag1 = new Bag();
145                         bag1.add(new StdAttributeValue<Boolean>(DataTypes.DT_BOOLEAN.getId(), v1));  
146                 Bag bag2 = new Bag();
147                         bag2.add(new StdAttributeValue<Boolean>(DataTypes.DT_BOOLEAN.getId(), v1));  
148                         bag2.add(new StdAttributeValue<Boolean>(DataTypes.DT_BOOLEAN.getId(), v2)); 
149                 Bag bagOtherType = new Bag();
150                         bagOtherType.add(new StdAttributeValue<BigInteger>(DataTypes.DT_STRING.getId(), vOtherType));  
151
152                 
153                 FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
154                 FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
155                 FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
156                 FunctionArgumentBag attrBagOtherType = new FunctionArgumentBag(bagOtherType);
157                 
158                 
159                 FunctionDefinitionBagOneAndOnly<?> fd = (FunctionDefinitionBagOneAndOnly<?>) StdFunctions.FD_BOOLEAN_ONE_AND_ONLY;
160
161                 // check identity and type of the thing created
162                 assertEquals(XACML3.ID_FUNCTION_BOOLEAN_ONE_AND_ONLY, fd.getId());
163                 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId());
164                 
165                 // just to be safe...  If tests take too long these can probably be eliminated
166                 assertFalse(fd.returnsBag());
167
168                 
169                 
170                 
171                 // bag with only one
172                 arguments.clear();
173                 arguments.add(attrBag1);
174                 ExpressionResult res = fd.evaluate(null, arguments);
175                 assertTrue(res.isOk());
176                 assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
177                 Boolean resValue = (Boolean)res.getValue().getValue();
178                 assertEquals(new Boolean(true), resValue);
179                 
180                 // null bag
181                 arguments.clear();
182                 arguments.add(null);
183                 res = fd.evaluate(null, arguments);
184                 assertFalse(res.isOk());
185                 assertEquals("function:boolean-one-and-only Got null argument", res.getStatus().getStatusMessage());
186                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
187                 
188                 // bag with exactly one but of other type in it
189                 arguments.clear();
190                 arguments.add(attrBagOtherType);
191                 res = fd.evaluate(null, arguments);
192                 assertFalse(res.isOk());
193                 assertEquals("function:boolean-one-and-only Element in bag of wrong type. Expected boolean got string", res.getStatus().getStatusMessage());
194                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
195                 
196                 // bag with none
197                 arguments.clear();
198                 arguments.add(attrBag0);
199                 res = fd.evaluate(null, arguments);
200                 assertFalse(res.isOk());
201                 assertEquals("function:boolean-one-and-only Expected 1 but Bag has 0 elements", res.getStatus().getStatusMessage());
202                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
203                 
204                 // bag with multiple
205                 arguments.clear();
206                 arguments.add(attrBag2);
207                 res = fd.evaluate(null, arguments);
208                 assertFalse(res.isOk());
209                 assertEquals("function:boolean-one-and-only Expected 1 but Bag has 2 elements", res.getStatus().getStatusMessage());
210                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
211         }
212         
213         
214         //
215         //
216         //  REST OF DATA TYPES OMITTED 
217         //      because they "should" all work the same
218         //
219         //
220         
221         
222         
223
224 }