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;
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;
29 import java.util.ArrayList;
30 import java.util.List;
32 import org.junit.Test;
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.*;
43 * Test of PDP Functions (See XACML core spec section A.3)
46 * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
50 public class FunctionDefinitionURIStringConcatenateTest {
53 * THE FUNCTION BEING TESTED BY THIS CLASS IS DEPRECATED
54 * uri-string-concatenate has been deprecated in XACML 3.0
58 * variables useful in the following tests
60 List<FunctionArgument> arguments = new ArrayList<FunctionArgument>();
63 @SuppressWarnings("deprecation")
65 public void testURI_string_concatenate() {
68 FunctionArgumentAttributeValue attrURI1 = null;
71 FunctionArgumentAttributeValue attrStrAbc = null;
72 FunctionArgumentAttributeValue attrStrSlashMno = null;
73 FunctionArgumentAttributeValue attrStrSlashInMiddle = null;
74 FunctionArgumentAttributeValue attrStrWithSpace = null;
78 attrURI1 = new FunctionArgumentAttributeValue(DataTypes.DT_ANYURI.createAttributeValue("http://someplace"));
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"));
87 } catch (Exception e) {
88 fail("creating attributes e="+ e);
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;
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());
98 // just to be safe... If tests take too long these can probably be eliminated
99 assertFalse(fd.returnsBag());
102 // add one string to uri
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());
113 // add 2 strings to uri
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());
124 // slash in middle of string
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());
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());
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());
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());
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());
171 // 2nd arg not string
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());