2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pdp.test;
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;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.junit.Test;
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.*;
45 * Test of PDP Functions (See XACML core spec section A.3)
48 * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
52 public class FunctionDefinitionBagIsInTest {
56 * variables useful in the following tests
58 List<FunctionArgument> arguments = new ArrayList<>();
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);
70 FunctionArgumentAttributeValue attrV1 = null;
71 FunctionArgumentAttributeValue attrV2 = null;
72 FunctionArgumentAttributeValue attrNotInBag = null;
73 FunctionArgumentAttributeValue attrSameValueV1 = null;
74 FunctionArgumentAttributeValue attrOtherType = null;
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);
87 bag1.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));
89 bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));
90 bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v2));;
94 FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
95 FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
96 FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
100 FunctionDefinitionBagIsIn<?> fd = (FunctionDefinitionBagIsIn<?>) StdFunctions.FD_STRING_IS_IN;
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());
106 // just to be safe... If tests take too long these can probably be eliminated
107 assertFalse(fd.returnsBag());
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);
120 // element not in bag
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);
130 // different element with the same value is in bag
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);
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);
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());
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());
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());
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());
187 arguments.add(attrV1);
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());
194 // first arg type does not match bag elements
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());
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.
213 // REST OF DATA TYPES OMITTED
214 // because they "should" all work the same