[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PDP / src / test / java / org / onap / policy / pdp / test / FunctionDefinitionBagIsInTest.java
diff --git a/ONAP-PDP/src/test/java/org/onap/policy/pdp/test/FunctionDefinitionBagIsInTest.java b/ONAP-PDP/src/test/java/org/onap/policy/pdp/test/FunctionDefinitionBagIsInTest.java
new file mode 100644 (file)
index 0000000..09fd95f
--- /dev/null
@@ -0,0 +1,221 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP-PDP
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdp.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.att.research.xacml.api.XACML3;
+import com.att.research.xacml.std.StdAttributeValue;
+import com.att.research.xacml.std.datatypes.DataTypes;
+import com.att.research.xacmlatt.pdp.policy.Bag;
+import com.att.research.xacmlatt.pdp.policy.ExpressionResult;
+import com.att.research.xacmlatt.pdp.policy.FunctionArgument;
+import com.att.research.xacmlatt.pdp.policy.FunctionArgumentAttributeValue;
+import com.att.research.xacmlatt.pdp.policy.FunctionArgumentBag;
+import com.att.research.xacmlatt.pdp.std.StdFunctions;
+import com.att.research.xacmlatt.pdp.std.functions.*;
+
+/**
+ * Test of PDP Functions (See XACML core spec section A.3)
+ * 
+ * TO RUN - use jUnit
+ * In Eclipse select this file or the enclosing directory, right-click and select Run As/JUnit Test
+ * 
+ *
+ */
+public class FunctionDefinitionBagIsInTest {
+
+
+       /*
+        * variables useful in the following tests
+        */
+       List<FunctionArgument> arguments = new ArrayList<>();
+       
+       
+       @Test
+       public void testString() {
+               String v1 = new String("abc");
+               String v2 = new String("def");
+               String notInBag = new String("lmnop");
+               String sameValueV1 = new String("abc");
+               Integer vOtherType = new Integer(11);
+               
+               
+               FunctionArgumentAttributeValue attrV1 = null;
+               FunctionArgumentAttributeValue attrV2 = null;
+               FunctionArgumentAttributeValue attrNotInBag = null;
+               FunctionArgumentAttributeValue attrSameValueV1 = null;
+               FunctionArgumentAttributeValue attrOtherType = null;
+               try {
+                       attrV1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(v1));
+                       attrV2 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(v2));
+                       attrNotInBag = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(notInBag));
+                       attrSameValueV1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(sameValueV1));
+                       attrOtherType = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(vOtherType));
+               } catch (Exception e) {
+                       fail("creating attributes e="+ e);
+               }
+               
+               Bag bag0 = new Bag();
+               Bag bag1 = new Bag();
+                       bag1.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1));
+               Bag bag2 = new Bag();
+                       bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v1)); 
+                       bag2.add(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), v2));;
+
+               
+               
+               FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0);
+               FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1);
+               FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2);
+
+               
+               
+               FunctionDefinitionBagIsIn<?> fd = (FunctionDefinitionBagIsIn<?>) StdFunctions.FD_STRING_IS_IN;
+
+               // check identity and type of the thing created
+               assertEquals(XACML3.ID_FUNCTION_STRING_IS_IN, fd.getId());
+               assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId());
+               
+               // just to be safe...  If tests take too long these can probably be eliminated
+               assertFalse(fd.returnsBag());
+
+               
+               // element is in bag
+               arguments.clear();
+               arguments.add(attrV1);
+               arguments.add(attrBag2);
+               ExpressionResult res = fd.evaluate(null, arguments);
+               assertTrue(res.isOk());
+               assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
+               Boolean resValue = (Boolean)res.getValue().getValue();
+               assertEquals(true, resValue);
+               
+               // element not in bag
+               arguments.clear();
+               arguments.add(attrNotInBag);
+               arguments.add(attrBag2);
+               res = fd.evaluate(null, arguments);
+               assertTrue(res.isOk());
+               assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
+               resValue = (Boolean)res.getValue().getValue();
+               assertEquals(false, resValue);
+               
+               // different element with the same value is in bag
+               arguments.clear();
+               arguments.add(attrSameValueV1);
+               arguments.add(attrBag2);
+               res = fd.evaluate(null, arguments);
+               assertTrue(res.isOk());
+               assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
+               resValue = (Boolean)res.getValue().getValue();
+               assertEquals(true, resValue);
+               
+               // empty bag
+               arguments.clear();
+               arguments.add(attrV1);
+               arguments.add(attrBag0);
+               res = fd.evaluate(null, arguments);
+               assertTrue(res.isOk());
+               assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass());
+               resValue = (Boolean)res.getValue().getValue();
+               assertEquals(false, resValue);
+               
+               // missing arg
+               arguments.clear();
+               arguments.add(attrSameValueV1);
+               res = fd.evaluate(null, arguments);
+               assertFalse(res.isOk());
+               assertEquals("function:string-is-in Expected 2 arguments, got 1", res.getStatus().getStatusMessage());
+               assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
+               
+               // 1st arg is bag
+               arguments.clear();
+               arguments.add(attrBag1);
+               arguments.add(attrBag2);
+               res = fd.evaluate(null, arguments);
+               assertFalse(res.isOk());
+               assertEquals("function:string-is-in Expected a simple value, saw a bag", res.getStatus().getStatusMessage());
+               assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
+               
+               // 2nd arg not bag
+               arguments.clear();
+               arguments.add(attrV1);
+               arguments.add(attrV2);
+               res = fd.evaluate(null, arguments);
+               assertFalse(res.isOk());
+               assertEquals("function:string-is-in Expected a bag, saw a simple value", res.getStatus().getStatusMessage());
+               assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
+               
+               // first arg null
+               arguments.clear();
+               arguments.add(null);
+               arguments.add(attrBag2);
+               res = fd.evaluate(null, arguments);
+               assertFalse(res.isOk());
+               assertEquals("function:string-is-in Got null argument", res.getStatus().getStatusMessage());
+               assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
+               
+               // 2nd arg null
+               arguments.clear();
+               arguments.add(attrV1);
+               arguments.add(null);
+               res = fd.evaluate(null, arguments);
+               assertFalse(res.isOk());
+               assertEquals("function:string-is-in Got null argument", res.getStatus().getStatusMessage());
+               assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
+               
+               // first arg type does not match bag elements
+               arguments.clear();
+               arguments.add(attrOtherType);
+               arguments.add(attrBag2);
+               res = fd.evaluate(null, arguments);
+               assertFalse(res.isOk());
+               assertEquals("function:string-is-in Expected data type 'string' saw 'integer'", res.getStatus().getStatusMessage());
+               assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue());
+               
+               // bag has mixed element types
+// behavior not specified for this case in spec.  It ASSUMES that all elements in bag are same type.
+               
+       }
+       
+
+       
+       
+       //
+       //
+       //  REST OF DATA TYPES OMITTED 
+       //      because they "should" all work the same
+       //
+       //
+       
+       
+       
+
+}