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.std.functions;
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.Status;
34 import com.att.research.xacml.api.XACML3;
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.FunctionArgument;
38 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentAttributeValue;
39 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentBag;
40 import com.att.research.xacmlatt.pdp.std.functions.FunctionDefinitionEquality;
43 * FunctionDefinitionHomogeneousSimple is an abstract class, so we have to test it by creating a sub-class.
44 * The constructor is tested by default when an instance of the sub-class is created for other tests.
46 * Each of these functions needs to be tested for each type of function to be sure the values are correct,
47 * so this is just a simple test to see that the mechanism works.
50 * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
54 public class FunctionDefinitionHomogeneousSimpleTest {
59 public void testGetDataTypeArgs() {
61 // test a simple instance using the Equality class
62 FunctionDefinitionEquality<String> fd = new FunctionDefinitionEquality<String>(XACML3.ID_FUNCTION_STRING_EQUAL, DataTypes.DT_STRING);
63 assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeArgs().getId());
67 public void testGetNumArgs() {
68 // test a simple instance using the Equality class
69 FunctionDefinitionEquality<String> fd = new FunctionDefinitionEquality<String>(XACML3.ID_FUNCTION_STRING_EQUAL, DataTypes.DT_STRING);
70 assertEquals(new Integer(2), fd.getNumArgs());
74 public void testValidateArguments() {
75 // create some arguments to use later
76 FunctionArgumentAttributeValue stringAttr1 = null;
77 FunctionArgumentAttributeValue stringAttr2 = null;
78 FunctionArgumentAttributeValue stringAttr3 = null;
79 FunctionArgumentAttributeValue intAttr = null;
81 stringAttr1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("abc"));
82 stringAttr2 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("def"));
83 stringAttr3 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("ghi"));
84 intAttr = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(1));
85 } catch (Exception e) {
86 fail("creating attribute e="+ e);
89 FunctionDefinitionEquality<String> fd = new FunctionDefinitionEquality<String>(XACML3.ID_FUNCTION_STRING_EQUAL, DataTypes.DT_STRING);
90 List<String> convertedValues = new ArrayList<String>();
91 List<FunctionArgument> listFunctionArguments = new ArrayList<FunctionArgument>();
93 // test correct # of args, both of them strings
94 listFunctionArguments.add(stringAttr1);
95 listFunctionArguments.add(stringAttr2);
96 Status status = fd.validateArguments(listFunctionArguments, convertedValues);
97 assertTrue(status.isOk());
98 assertEquals(convertedValues.size(),2);
101 listFunctionArguments.remove(1);
102 status = fd.validateArguments(listFunctionArguments, convertedValues);
103 assertFalse(status.isOk());
104 assertEquals("Expected 2 arguments, got 1", status.getStatusMessage());
105 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
107 // test too many args
108 listFunctionArguments.add(stringAttr2);
109 listFunctionArguments.add(stringAttr3);
110 status = fd.validateArguments(listFunctionArguments, convertedValues);
111 assertFalse(status.isOk());
112 assertEquals("Expected 2 arguments, got 3", status.getStatusMessage());
113 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
115 // test with null arg
116 listFunctionArguments.clear();
117 listFunctionArguments.add(null);
118 listFunctionArguments.add(stringAttr1);
119 status = fd.validateArguments(listFunctionArguments, convertedValues);
120 assertFalse(status.isOk());
121 assertEquals("Got null argument at arg index 0", status.getStatusMessage());
122 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
124 // test function that takes 0 args
125 //TODO test with func that specifies 0 args? ASSUME for now that there are no such functions since a function needs to operate on something
126 // fail("need to test function with 0 args and various inputs - see validateArguments code");
129 // test with one is a bag
130 listFunctionArguments.clear();
131 listFunctionArguments.add(stringAttr1);
133 FunctionArgument bagArg = new FunctionArgumentBag(bag);
134 listFunctionArguments.add(bagArg);
135 status = fd.validateArguments(listFunctionArguments, convertedValues);
136 assertFalse(status.isOk());
137 assertEquals("Expected a simple value, saw a bag at arg index 1", status.getStatusMessage());
138 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
140 // test with string and int
141 listFunctionArguments.clear();
142 listFunctionArguments.add(stringAttr1);
143 listFunctionArguments.add(intAttr);
144 status = fd.validateArguments(listFunctionArguments, convertedValues);
145 assertFalse(status.isOk());
146 assertEquals("Expected data type 'string' saw 'integer' at arg index 1", status.getStatusMessage());
147 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());