6aee3383b5831cc2035a4704c5328fd6824aa0ae
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionHomogeneousSimpleTest.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
22 package org.openecomp.policy.pdp.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.junit.Test;
33
34 import com.att.research.xacml.api.Status;
35 import com.att.research.xacml.api.XACML3;
36 import com.att.research.xacml.std.datatypes.DataTypes;
37 import com.att.research.xacmlatt.pdp.policy.Bag;
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.functions.*;
42
43 /**
44  * FunctionDefinitionHomogeneousSimple is an abstract class, so we have to test it by creating a sub-class.
45  * The constructor is tested by default when an instance of the sub-class is created for other tests.
46  * 
47  * Each of these functions needs to be tested for each type of function to be sure the values are correct,
48  * so this is just a simple test to see that the mechanism works.
49  * 
50  * TO RUN - use jUnit
51  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
52  * 
53  *
54  */
55 public class FunctionDefinitionHomogeneousSimpleTest {
56
57
58
59         @Test
60         public void testGetDataTypeArgs() {
61                 
62                 // test a simple instance using the Equality class
63                 FunctionDefinitionEquality<String> fd   = new FunctionDefinitionEquality<String>(XACML3.ID_FUNCTION_STRING_EQUAL, DataTypes.DT_STRING);
64                 assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeArgs().getId());
65         }
66
67         @Test
68         public void testGetNumArgs() {
69                 // test a simple instance using the Equality class
70                 FunctionDefinitionEquality<String> fd   = new FunctionDefinitionEquality<String>(XACML3.ID_FUNCTION_STRING_EQUAL, DataTypes.DT_STRING);
71                 assertEquals(new Integer(2), fd.getNumArgs());
72         }
73
74         @Test
75         public void testValidateArguments() {
76                 // create some arguments to use later
77                 FunctionArgumentAttributeValue stringAttr1 = null;
78                 FunctionArgumentAttributeValue stringAttr2 = null;
79                 FunctionArgumentAttributeValue stringAttr3 = null;
80                 FunctionArgumentAttributeValue intAttr = null;
81                 try {
82                         stringAttr1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("abc"));
83                         stringAttr2 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("def"));
84                         stringAttr3 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("ghi"));
85                         intAttr = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(1));
86                 } catch (Exception e) {
87                         fail("creating attribute e="+ e);
88                 }
89                 
90                 FunctionDefinitionEquality<String> fd   = new FunctionDefinitionEquality<String>(XACML3.ID_FUNCTION_STRING_EQUAL, DataTypes.DT_STRING);
91                 List<String> convertedValues = new ArrayList<String>();
92                 List<FunctionArgument> listFunctionArguments = new ArrayList<FunctionArgument>();
93                 
94                 // test correct # of args, both of them strings
95                 listFunctionArguments.add(stringAttr1);
96                 listFunctionArguments.add(stringAttr2);
97                 Status status = fd.validateArguments(listFunctionArguments, convertedValues);
98                 assertTrue(status.isOk());
99                 assertEquals(convertedValues.size(),2);
100                 
101                 // test too few args
102                 listFunctionArguments.remove(1);
103                 status = fd.validateArguments(listFunctionArguments, convertedValues);
104                 assertFalse(status.isOk());
105                 assertEquals("Expected 2 arguments, got 1", status.getStatusMessage());
106                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
107                 
108                 // test too many args
109                 listFunctionArguments.add(stringAttr2);
110                 listFunctionArguments.add(stringAttr3);
111                 status = fd.validateArguments(listFunctionArguments, convertedValues);
112                 assertFalse(status.isOk());
113                 assertEquals("Expected 2 arguments, got 3", status.getStatusMessage());
114                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
115                 
116                 // test with null arg
117                 listFunctionArguments.clear();
118                 listFunctionArguments.add(null);
119                 listFunctionArguments.add(stringAttr1);
120                 status = fd.validateArguments(listFunctionArguments, convertedValues);
121                 assertFalse(status.isOk());
122                 assertEquals("Got null argument at arg index 0", status.getStatusMessage());
123                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
124
125                 // test function that takes 0 args
126 //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
127 //              fail("need to test function with 0 args and various inputs - see validateArguments code");
128                 
129
130                 // test with one is a bag
131                 listFunctionArguments.clear();
132                 listFunctionArguments.add(stringAttr1);
133                 Bag bag = new Bag();
134                 FunctionArgument bagArg = new FunctionArgumentBag(bag);
135                 listFunctionArguments.add(bagArg);
136                 status = fd.validateArguments(listFunctionArguments, convertedValues);
137                 assertFalse(status.isOk());
138                 assertEquals("Expected a simple value, saw a bag at arg index 1", status.getStatusMessage());
139                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
140                 
141                 // test with string and int
142                 listFunctionArguments.clear();
143                 listFunctionArguments.add(stringAttr1);
144                 listFunctionArguments.add(intAttr);
145                 status = fd.validateArguments(listFunctionArguments, convertedValues);
146                 assertFalse(status.isOk());
147                 assertEquals("Expected data type 'string' saw 'integer' at arg index 1", status.getStatusMessage());
148                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", status.getStatusCode().getStatusCodeValue().stringValue());
149         }
150
151 }