JUnit additions for PDP,PDP-REST,SDK,XACML
[policy/engine.git] / ONAP-PDP / src / test / java / org / onap / policy / xacml / pdp / std / functions / FunctionDefinitionCustomRegexpMatchTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP
4  * ================================================================================
5  * Copyright (C) 2018 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.onap.policy.xacml.pdp.std.functions;
22
23 import static org.junit.Assert.assertEquals;
24
25 import com.att.research.xacml.api.Identifier;
26 import com.att.research.xacml.api.XACML;
27 import com.att.research.xacml.std.IdentifierImpl;
28 import com.att.research.xacml.std.StdAttributeValue;
29 import com.att.research.xacml.std.StdStatusCode;
30 import com.att.research.xacml.std.datatypes.DataTypes;
31 import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
32 import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
33 import com.att.research.xacmlatt.pdp.policy.FunctionArgumentAttributeValue;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import org.junit.Test;
39
40 public class FunctionDefinitionCustomRegexpMatchTest {
41     @Test
42     public final void testRegexp() {
43         // Setup
44         final String testVal = "testVal,testVal2";
45         final String testId = "function:testId";
46         final IdentifierImpl testFnId = new IdentifierImpl(testId);
47         final Identifier identifier = XACML.ID_DATATYPE_STRING;
48         final StdAttributeValue<String> attValue = new StdAttributeValue<String>(identifier, testVal);
49         final FunctionArgument fArg = new FunctionArgumentAttributeValue(attValue);
50         final List<FunctionArgument> listFa = new ArrayList<FunctionArgument>();
51         listFa.add(fArg);
52         listFa.add(fArg);
53         final FunctionDefinitionCustomRegexpMatch<String> regexpMatch =
54                 new FunctionDefinitionCustomRegexpMatch<String>(testFnId, DataTypes.DT_STRING);
55
56         // Try a match
57         final ExpressionResult result = regexpMatch.evaluate(null, listFa);
58         assertEquals(true, result.getValue().getValue());
59
60         // Try error case 1
61         assertEquals(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
62                 regexpMatch.evaluate(null, null).getStatus().getStatusCode());
63
64         // Try error case 2
65         final Identifier identifier2 = XACML.ID_DATATYPE_BOOLEAN;
66         final StdAttributeValue<String> attValue2 = new StdAttributeValue<String>(identifier2, testVal);
67         final FunctionArgument fArg2 = new FunctionArgumentAttributeValue(attValue2);
68         final List<FunctionArgument> listFa2 = new ArrayList<FunctionArgument>();
69         listFa2.add(fArg2);
70         listFa2.add(fArg2);
71         assertEquals(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
72                 regexpMatch.evaluate(null, listFa2).getStatus().getStatusCode());
73
74         // Try error case 3
75         final List<FunctionArgument> listFa3 = new ArrayList<FunctionArgument>();
76         listFa3.add(fArg);
77         listFa3.add(fArg2);
78         assertEquals(StdStatusCode.STATUS_CODE_PROCESSING_ERROR,
79                 regexpMatch.evaluate(null, listFa3).getStatus().getStatusCode());
80
81         // Try a mismatch
82         final String testVal4 = "testVal3";
83         final StdAttributeValue<String> attValue4 = new StdAttributeValue<String>(identifier, testVal4);
84         final FunctionArgument fArg4 = new FunctionArgumentAttributeValue(attValue4);
85         final List<FunctionArgument> listFa4 = new ArrayList<FunctionArgument>();
86         listFa4.add(fArg);
87         listFa4.add(fArg4);
88         assertEquals(false, regexpMatch.evaluate(null, listFa4).getValue().getValue());
89
90         // Try a comma match
91         final String testVal5 = "testVal2";
92         final StdAttributeValue<String> attValue5 = new StdAttributeValue<String>(identifier, testVal5);
93         final FunctionArgument fArg5 = new FunctionArgumentAttributeValue(attValue5);
94         final List<FunctionArgument> listFa5 = new ArrayList<FunctionArgument>();
95         listFa5.add(fArg);
96         listFa5.add(fArg5);
97         assertEquals(true, regexpMatch.evaluate(null, listFa5).getValue().getValue());
98     }
99 }