6475e8d4a062a085be85d374786763e0eedb0b6b
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionStringEqualIgnoreCaseTest.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 package org.openecomp.policy.pdp.test;
22
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;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.junit.Test;
32
33 import com.att.research.xacml.api.XACML3;
34 import com.att.research.xacml.std.datatypes.DataTypes;
35 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
36 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
37 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentAttributeValue;
38 import com.att.research.xacmlatt.pdp.std.StdFunctions;
39 import com.att.research.xacmlatt.pdp.std.functions.*;
40
41 /**
42  * Only one function to test here.  Code copy/pasted from FunctionDefinitionEqualityTest
43  * 
44  * TO RUN - use jUnit
45  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
46  * 
47  * In the first implementation of XACML we had separate files for each XACML Function.
48  * This release combines multiple Functions in fewer files to minimize code duplication.
49  * This file supports the following XACML codes:
50  *              string-equal-ignore-case
51  * 
52  *
53  */
54 public class FunctionDefinitionStringEqualIgnoreCaseTest {
55
56         /*
57          * variables useful in the following tests
58          */
59         List<FunctionArgument> arguments = new ArrayList<FunctionArgument>();
60         
61         FunctionArgumentAttributeValue stringAttr1 = null;
62         FunctionArgumentAttributeValue stringAttr2 = null;
63         FunctionArgumentAttributeValue stringAttr3 = null;
64         FunctionArgumentAttributeValue stringAttr4 = null;
65         
66         FunctionArgumentAttributeValue intAttr1 = null;
67
68         public FunctionDefinitionStringEqualIgnoreCaseTest() {
69                 try {
70                         stringAttr1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("abc"));
71                         stringAttr2 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("abc"));
72                         stringAttr3 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("ABC"));
73                         stringAttr4 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("def"));
74                         intAttr1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(1));
75                 } catch (Exception e) {
76                         fail("creating attribute e="+ e);
77                 }
78         }
79         
80         
81         /**
82          * String match even when Case is different
83          */
84         @Test
85         public void testFunctionDefinitionStringEqualIgnoreCase() {
86                 
87                 FunctionDefinitionEquality<?> fd = (FunctionDefinitionEquality<?>) StdFunctions.FD_STRING_EQUAL_IGNORE_CASE;
88                 
89                 // check identity and type of the thing created
90                 assertEquals(XACML3.ID_FUNCTION_STRING_EQUAL_IGNORE_CASE, fd.getId());
91                 assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeArgs().getId());
92                 
93                 // just to be safe...  If tests take too long these can probably be eliminated
94                 assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId());
95                 assertFalse(fd.returnsBag());
96                 assertEquals(new Integer(2), fd.getNumArgs());
97                 
98
99                 
100                 // test normal equals and non-equals
101                 // check "abc" with "abc"
102                 arguments.add(stringAttr1);
103                 arguments.add(stringAttr2);
104                 ExpressionResult res = fd.evaluate(null, arguments);
105                 assertTrue(res.isOk());
106                 Boolean resValue = (Boolean)res.getValue().getValue();
107                 assertTrue(resValue);
108
109                 // check "abc" with "ABC" (should be same)
110                 arguments.clear();
111                 arguments.add(stringAttr1);
112                 arguments.add(stringAttr3);
113                 res = fd.evaluate(null, arguments);
114                 assertTrue(res.isOk());
115                 resValue = (Boolean)res.getValue().getValue();
116                 assertTrue(resValue);
117                 
118                 // test bad args data types?  Not needed?
119                 arguments.clear();
120                 arguments.add(stringAttr1);
121                 arguments.add(intAttr1);
122                 res = fd.evaluate(null, arguments);
123                 assertFalse(res.isOk());
124
125                 
126 //TODO - null in either first or 2nd arg => NullPointerException
127         }
128
129 }