Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / FunctionDefinitionURIStringConcatenateTest.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.net.URI;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.junit.Test;
33
34 import com.att.research.xacml.api.XACML2;
35 import com.att.research.xacml.std.datatypes.DataTypes;
36 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
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.std.StdFunctions;
40 import com.att.research.xacmlatt.pdp.std.functions.*;
41
42 /**
43  * Test of PDP Functions (See XACML core spec section A.3)
44  * 
45  * TO RUN - use jUnit
46  * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
47  * 
48  *
49  */
50 public class FunctionDefinitionURIStringConcatenateTest {
51
52         /*
53          * THE FUNCTION BEING TESTED BY THIS CLASS IS DEPRECATED
54          * uri-string-concatenate has been deprecated in XACML 3.0
55          */
56
57         /*
58          * variables useful in the following tests
59          */
60         List<FunctionArgument> arguments = new ArrayList<>();
61         
62
63         @SuppressWarnings("deprecation")
64         @Test
65         public void testURI_string_concatenate() {
66
67                 // URI args
68                 FunctionArgumentAttributeValue attrURI1 = null;
69
70                 
71                 FunctionArgumentAttributeValue attrStrAbc = null;
72                 FunctionArgumentAttributeValue attrStrSlashMno = null;
73                 FunctionArgumentAttributeValue attrStrSlashInMiddle = null;
74                 FunctionArgumentAttributeValue attrStrWithSpace = null;
75
76                 
77                 try {
78                         attrURI1 = new FunctionArgumentAttributeValue(DataTypes.DT_ANYURI.createAttributeValue("http://someplace"));
79                 
80                         
81                         attrStrAbc = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("Abc"));
82                         attrStrSlashMno = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("/Mno"));
83                         attrStrSlashInMiddle = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("hij/pqr"));
84                         attrStrWithSpace = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("x y z"));
85                         
86
87                 } catch (Exception e) {
88                         fail("creating attributes e="+ e);
89                 }
90                 
91                 // deprecation marking in the following line is correct - this function IS deprecated but still valid for XACML 3.0
92                 FunctionDefinitionURIStringConcatenate fd = (FunctionDefinitionURIStringConcatenate) StdFunctions.FD_URI_STRING_CONCATENATE;
93
94                 // check identity and type of the thing created
95                 assertEquals(XACML2.ID_FUNCTION_URI_STRING_CONCATENATE, fd.getId());
96                 assertEquals(DataTypes.DT_ANYURI.getId(), fd.getDataTypeId());
97                 
98                 // just to be safe...  If tests take too long these can probably be eliminated
99                 assertFalse(fd.returnsBag());
100
101                 
102                 // add one string to uri
103                 arguments.clear();
104                 arguments.add(attrURI1);
105                 arguments.add(attrStrAbc);
106                 ExpressionResult res = fd.evaluate(null, arguments);
107                 assertTrue(res.isOk());
108                 assertEquals(java.net.URI.class, res.getValue().getValue().getClass());
109                 URI resValue = (URI)res.getValue().getValue();
110                 assertEquals("http://someplaceAbc", resValue.toString());
111                 
112                 
113                 // add 2 strings to uri
114                 arguments.clear();
115                 arguments.add(attrURI1);
116                 arguments.add(attrStrAbc);
117                 arguments.add(attrStrSlashMno);
118                 res = fd.evaluate(null, arguments);
119                 assertTrue(res.isOk());
120                 assertEquals(java.net.URI.class, res.getValue().getValue().getClass());
121                 resValue = (URI)res.getValue().getValue();
122                 assertEquals("http://someplaceAbc/Mno", resValue.toString());
123                 
124                 // slash in middle of string
125                 arguments.clear();
126                 arguments.add(attrURI1);
127                 arguments.add(attrStrSlashInMiddle);
128                 arguments.add(attrStrSlashMno);
129                 res = fd.evaluate(null, arguments);
130                 assertTrue(res.isOk());
131                 assertEquals(java.net.URI.class, res.getValue().getValue().getClass());
132                 resValue = (URI)res.getValue().getValue();
133                 assertEquals("http://someplacehij/pqr/Mno", resValue.toString());
134                 
135                 // create bad uri
136                 arguments.clear();
137                 arguments.add(attrURI1);
138                 arguments.add(attrStrWithSpace);
139                 arguments.add(attrStrSlashMno);
140                 res = fd.evaluate(null, arguments);
141                 assertFalse(res.isOk());
142                 assertEquals("function:uri-string-concatenate Final string 'http://someplacex y z/Mno' not URI, Illegal character in authority at index 7: http://someplacex y z/Mno", res.getStatus().getStatusMessage());
143                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
144                 
145                 // no args
146                 arguments.clear();
147                 res = fd.evaluate(null, arguments);
148                 assertFalse(res.isOk());
149                 assertEquals("function:uri-string-concatenate Expected 2 or more arguments, got 0", res.getStatus().getStatusMessage());
150                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
151                 
152                 // one arg
153                 arguments.clear();
154                 arguments.add(attrURI1);
155                 res = fd.evaluate(null, arguments);
156                 assertFalse(res.isOk());
157                 assertEquals("function:uri-string-concatenate Expected 2 or more arguments, got 1", res.getStatus().getStatusMessage());
158                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
159                 
160                 
161                 // first arg not uri
162                 arguments.clear();
163                 arguments.add(attrStrAbc);
164                 arguments.add(attrURI1);
165                 res = fd.evaluate(null, arguments);
166                 assertFalse(res.isOk());
167                 assertEquals("function:uri-string-concatenate Expected data type 'anyURI' saw 'string' at arg index 0", res.getStatus().getStatusMessage());
168                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
169                 
170                 
171                 // 2nd arg not string
172                 arguments.clear();
173                 arguments.add(attrURI1);
174                 arguments.add(attrURI1);
175                 res = fd.evaluate(null, arguments);
176                 assertFalse(res.isOk());
177                 assertEquals("function:uri-string-concatenate Expected data type 'string' saw 'anyURI' at arg index 1", res.getStatus().getStatusMessage());
178                 assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
179                 
180         }
181         
182
183         
184
185 }