Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionBagIsInTest.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.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.FunctionArgumentAttributeValue;
40 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentBag;
41 import com.att.research.xacmlatt.pdp.std.StdFunctions;
42 import com.att.research.xacmlatt.pdp.std.functions.*;
43
44 /**
45  * Test of PDP Functions (See XACML core spec section A.3)
46  * 
47  * TO RUN - use jUnit
48  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
49  * 
50  *
51  */
52 public class FunctionDefinitionBagIsInTest {
53
54
55         /*
56          * variables useful in the following tests
57          */
58         List<FunctionArgument> arguments = new ArrayList<>();
59         
60         
61         @Test
62         public void testString() {
63                 String v1 = new String("abc");
64                 String v2 = new String("def");
65                 String notInBag = new String("lmnop");
66                 String sameValueV1 = new String("abc");
67                 Integer vOtherType = new Integer(11);
68                 
69                 
70                 FunctionArgumentAttributeValue attrV1 = null;
71                 FunctionArgumentAttributeValue attrV2 = null;
72                 FunctionArgumentAttributeValue attrNotInBag = null;
73                 FunctionArgumentAttributeValue attrSameValueV1 = null;
74                 FunctionArgumentAttributeValue attrOtherType = null;
75                 try {
76                         attrV1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(v1));
77                         attrV2 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(v2));
78                         attrNotInBag = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(notInBag));
79                         attrSameValueV1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(sameValueV1));
80                         attrOtherType = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(vOtherType));
81                 } catch (Exception e) {
82                         fail("creating attributes e="+ e);
83                 }
84                 
85                 Bag bag0 = new Bag();
86                 Bag bag1 = new Bag();
87                         bag1.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));
88                 Bag bag2 = new Bag();
89                         bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1)); 
90                         bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v2));;
91
92                 
93                 
94                 FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
95                 FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
96                 FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
97
98                 
99                 
100                 FunctionDefinitionBagIsIn<?> fd = (FunctionDefinitionBagIsIn<?>) StdFunctions.FD_STRING_IS_IN;
101
102                 // check identity and type of the thing created
103                 assertEquals(XACML3.ID_FUNCTION_STRING_IS_IN, fd.getId());
104                 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId());
105                 
106                 // just to be safe...  If tests take too long these can probably be eliminated
107                 assertFalse(fd.returnsBag());
108
109                 
110                 // element is in bag
111                 arguments.clear();
112                 arguments.add(attrV1);
113                 arguments.add(attrBag2);
114                 ExpressionResult res = fd.evaluate(null, arguments);
115                 assertTrue(res.isOk());
116                 assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
117                 Boolean resValue = (Boolean)res.getValue().getValue();
118                 assertEquals(true, resValue);
119                 
120                 // element not in bag
121                 arguments.clear();
122                 arguments.add(attrNotInBag);
123                 arguments.add(attrBag2);
124                 res = fd.evaluate(null, arguments);
125                 assertTrue(res.isOk());
126                 assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
127                 resValue = (Boolean)res.getValue().getValue();
128                 assertEquals(false, resValue);
129                 
130                 // different element with the same value is in bag
131                 arguments.clear();
132                 arguments.add(attrSameValueV1);
133                 arguments.add(attrBag2);
134                 res = fd.evaluate(null, arguments);
135                 assertTrue(res.isOk());
136                 assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
137                 resValue = (Boolean)res.getValue().getValue();
138                 assertEquals(true, resValue);
139                 
140                 // empty bag
141                 arguments.clear();
142                 arguments.add(attrV1);
143                 arguments.add(attrBag0);
144                 res = fd.evaluate(null, arguments);
145                 assertTrue(res.isOk());
146                 assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
147                 resValue = (Boolean)res.getValue().getValue();
148                 assertEquals(false, resValue);
149                 
150                 // missing arg
151                 arguments.clear();
152                 arguments.add(attrSameValueV1);
153                 res = fd.evaluate(null, arguments);
154                 assertFalse(res.isOk());
155                 assertEquals("function:string-is-in Expected 2 arguments, got 1", res.getStatus().getStatusMessage());
156                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
157                 
158                 // 1st arg is bag
159                 arguments.clear();
160                 arguments.add(attrBag1);
161                 arguments.add(attrBag2);
162                 res = fd.evaluate(null, arguments);
163                 assertFalse(res.isOk());
164                 assertEquals("function:string-is-in Expected a simple value, saw a bag", res.getStatus().getStatusMessage());
165                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
166                 
167                 // 2nd arg not bag
168                 arguments.clear();
169                 arguments.add(attrV1);
170                 arguments.add(attrV2);
171                 res = fd.evaluate(null, arguments);
172                 assertFalse(res.isOk());
173                 assertEquals("function:string-is-in Expected a bag, saw a simple value", res.getStatus().getStatusMessage());
174                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
175                 
176                 // first arg null
177                 arguments.clear();
178                 arguments.add(null);
179                 arguments.add(attrBag2);
180                 res = fd.evaluate(null, arguments);
181                 assertFalse(res.isOk());
182                 assertEquals("function:string-is-in Got null argument", res.getStatus().getStatusMessage());
183                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
184                 
185                 // 2nd arg null
186                 arguments.clear();
187                 arguments.add(attrV1);
188                 arguments.add(null);
189                 res = fd.evaluate(null, arguments);
190                 assertFalse(res.isOk());
191                 assertEquals("function:string-is-in Got null argument", res.getStatus().getStatusMessage());
192                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
193                 
194                 // first arg type does not match bag elements
195                 arguments.clear();
196                 arguments.add(attrOtherType);
197                 arguments.add(attrBag2);
198                 res = fd.evaluate(null, arguments);
199                 assertFalse(res.isOk());
200                 assertEquals("function:string-is-in Expected data type 'string' saw 'integer'", res.getStatus().getStatusMessage());
201                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
202                 
203                 // bag has mixed element types
204 // behavior not specified for this case in spec.  It ASSUMES that all elements in bag are same type.
205                 
206         }
207         
208
209         
210         
211         //
212         //
213         //  REST OF DATA TYPES OMITTED 
214         //      because they "should" all work the same
215         //
216         //
217         
218         
219         
220
221 }