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.openecomp.policy.pdp.test;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.math.BigInteger;
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.FunctionArgumentBag;
40 import com.att.research.xacmlatt.pdp.std.StdFunctions;
41 import com.att.research.xacmlatt.pdp.std.functions.*;
44 * Test of PDP Functions (See XACML core spec section A.3)
47 * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
51 public class FunctionDefinitionBagOneAndOnlyTest {
55 * variables useful in the following tests
57 List<FunctionArgument> arguments = new ArrayList<FunctionArgument>();
61 public void testString() {
62 String v1 = new String("abc");
63 String v2 = new String("def");
64 BigInteger vOtherType = BigInteger.valueOf(11);
68 bag1.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));
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));
75 FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
76 FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
77 FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
78 FunctionArgumentBag attrBagOtherType = new FunctionArgumentBag(bagOtherType);
81 FunctionDefinitionBagOneAndOnly<?> fd = (FunctionDefinitionBagOneAndOnly<?>) StdFunctions.FD_STRING_ONE_AND_ONLY;
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());
87 // just to be safe... If tests take too long these can probably be eliminated
88 assertFalse(fd.returnsBag());
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);
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());
110 // bag with exactly one but of other type in it
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());
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());
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());
138 public void testBoolean() {
139 Boolean v1 = new Boolean(true);
140 Boolean v2 = new Boolean(false);
141 BigInteger vOtherType = BigInteger.valueOf(11);
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));
153 FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
154 FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
155 FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
156 FunctionArgumentBag attrBagOtherType = new FunctionArgumentBag(bagOtherType);
159 FunctionDefinitionBagOneAndOnly<?> fd = (FunctionDefinitionBagOneAndOnly<?>) StdFunctions.FD_BOOLEAN_ONE_AND_ONLY;
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());
165 // just to be safe... If tests take too long these can probably be eliminated
166 assertFalse(fd.returnsBag());
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);
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());
188 // bag with exactly one but of other type in it
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());
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());
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());
216 // REST OF DATA TYPES OMITTED
217 // because they "should" all work the same