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.math.BigInteger;
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.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 FunctionDefinitionBagSizeTest {
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 Integer vOtherType = new Integer(11);
70 bag1.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));
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));
78 FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
79 FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
80 FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
81 FunctionArgumentBag attrBagOtherType = new FunctionArgumentBag(bagOtherType);
84 FunctionDefinitionBagSize<?> fd = (FunctionDefinitionBagSize<?>) StdFunctions.FD_STRING_BAG_SIZE;
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());
90 // just to be safe... If tests take too long these can probably be eliminated
91 assertFalse(fd.returnsBag());
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);
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());
113 // bag with exactly one but of other type in it
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);
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);
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);
150 // REST OF DATA TYPES OMITTED
151 // because they "should" all work the same