Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionStringNormalizeTest.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  * Test of PDP Functions (See XACML core spec section A.3)
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  *
48  */
49 public class FunctionDefinitionStringNormalizeTest {
50         
51         /*
52          * variables useful in the following tests
53          */
54         List<FunctionArgument> arguments = new ArrayList<>();
55         
56         @Test
57         public void testString_normalize_space() {
58                 String initialString = "  First and last are whitespace         ";
59                 FunctionArgumentAttributeValue attr1 = null;
60                 try {
61                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(initialString));
62                 } catch (Exception e) {
63                         fail("creating attribute e="+ e);
64                 }
65                 
66                 FunctionDefinitionStringNormalize fd = (FunctionDefinitionStringNormalize) StdFunctions.FD_STRING_NORMALIZE_SPACE;
67                 
68                 // check identity and type of the thing created
69                 assertEquals(XACML3.ID_FUNCTION_STRING_NORMALIZE_SPACE, fd.getId());
70                 assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeArgs().getId());
71                 assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeId());
72                 
73                 // just to be safe...  If tests take too long these can probably be eliminated
74                 assertFalse(fd.returnsBag());
75                 assertEquals(new Integer(1), fd.getNumArgs());
76                 
77                 
78                 // test normal add
79                 arguments.add(attr1);
80                 ExpressionResult res = fd.evaluate(null, arguments);
81                 assertTrue(res.isOk());
82                 String resValue = (String)res.getValue().getValue();
83                 assertEquals(initialString.length() - 4, resValue.length());
84                 assertTrue(initialString.trim().equals(resValue));
85         }
86
87         
88         @Test
89         public void testString_normalize_to_lower_case() {
90                 String initialString = "  First and last are whitespace         ";
91                 FunctionArgumentAttributeValue attr1 = null;
92                 try {
93                         attr1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(initialString));
94                 } catch (Exception e) {
95                         fail("creating attribute e="+ e);
96                 }
97                 
98                 FunctionDefinitionStringNormalize fd = (FunctionDefinitionStringNormalize) StdFunctions.FD_STRING_NORMALIZE_TO_LOWER_CASE;
99                 
100                 // check identity and type of the thing created
101                 assertEquals(XACML3.ID_FUNCTION_STRING_NORMALIZE_TO_LOWER_CASE, fd.getId());
102                 assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeArgs().getId());
103                 assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeId());
104                 
105                 // just to be safe...  If tests take too long these can probably be eliminated
106                 assertFalse(fd.returnsBag());
107                 assertEquals(new Integer(1), fd.getNumArgs());
108                 
109                 
110                 // test normal add
111                 arguments.add(attr1);
112                 ExpressionResult res = fd.evaluate(null, arguments);
113                 assertTrue(res.isOk());
114                 String resValue = (String)res.getValue().getValue();
115                 assertTrue(initialString.toLowerCase().equals(resValue));
116         }
117         
118 }