@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; assertEquals(XACML3.ID_FUNCTION_STRING_IS_IN, fd.getId()); assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId()); assertFalse(fd.returnsBag()); 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); 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); 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); 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); 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()); 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()); 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()); 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()); 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()); 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()); }
@Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 2) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected 2 arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument elementArgument = arguments.get(0); ConvertedArgument<I> convertedTargetArgument = new ConvertedArgument<I>(elementArgument, this.getDataTypeArgs(), false); if ( ! convertedTargetArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedTargetArgument.getStatus())); } AttributeValue<?> attributeValueElement = elementArgument.getValue(); FunctionArgument bagArgument = arguments.get(1); ConvertedArgument<Bag> convertedBagArgument = new ConvertedArgument<Bag>(bagArgument, null, true); if ( ! convertedBagArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedBagArgument.getStatus())); } Bag bag = convertedBagArgument.getBag(); Iterator<AttributeValue<?>> iterBagContents = bag.getAttributeValues(); while (iterBagContents.hasNext()) { AttributeValue<?> attributeValueBagContents = iterBagContents.next(); if (attributeValueElement.equals(attributeValueBagContents)) { return ER_TRUE; } } return ER_FALSE; }
FunctionDefinitionBagIsIn extends FunctionDefinitionBase<Boolean, I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 2) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected 2 arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument elementArgument = arguments.get(0); ConvertedArgument<I> convertedTargetArgument = new ConvertedArgument<I>(elementArgument, this.getDataTypeArgs(), false); if ( ! convertedTargetArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedTargetArgument.getStatus())); } AttributeValue<?> attributeValueElement = elementArgument.getValue(); FunctionArgument bagArgument = arguments.get(1); ConvertedArgument<Bag> convertedBagArgument = new ConvertedArgument<Bag>(bagArgument, null, true); if ( ! convertedBagArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedBagArgument.getStatus())); } Bag bag = convertedBagArgument.getBag(); Iterator<AttributeValue<?>> iterBagContents = bag.getAttributeValues(); while (iterBagContents.hasNext()) { AttributeValue<?> attributeValueBagContents = iterBagContents.next(); if (attributeValueElement.equals(attributeValueBagContents)) { return ER_TRUE; } } return ER_FALSE; } }
FunctionDefinitionBagIsIn extends FunctionDefinitionBase<Boolean, I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 2) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected 2 arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument elementArgument = arguments.get(0); ConvertedArgument<I> convertedTargetArgument = new ConvertedArgument<I>(elementArgument, this.getDataTypeArgs(), false); if ( ! convertedTargetArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedTargetArgument.getStatus())); } AttributeValue<?> attributeValueElement = elementArgument.getValue(); FunctionArgument bagArgument = arguments.get(1); ConvertedArgument<Bag> convertedBagArgument = new ConvertedArgument<Bag>(bagArgument, null, true); if ( ! convertedBagArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedBagArgument.getStatus())); } Bag bag = convertedBagArgument.getBag(); Iterator<AttributeValue<?>> iterBagContents = bag.getAttributeValues(); while (iterBagContents.hasNext()) { AttributeValue<?> attributeValueBagContents = iterBagContents.next(); if (attributeValueElement.equals(attributeValueBagContents)) { return ER_TRUE; } } return ER_FALSE; } FunctionDefinitionBagIsIn(Identifier idIn, DataType<I> dataTypeArgsIn); }
FunctionDefinitionBagIsIn extends FunctionDefinitionBase<Boolean, I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 2) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected 2 arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument elementArgument = arguments.get(0); ConvertedArgument<I> convertedTargetArgument = new ConvertedArgument<I>(elementArgument, this.getDataTypeArgs(), false); if ( ! convertedTargetArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedTargetArgument.getStatus())); } AttributeValue<?> attributeValueElement = elementArgument.getValue(); FunctionArgument bagArgument = arguments.get(1); ConvertedArgument<Bag> convertedBagArgument = new ConvertedArgument<Bag>(bagArgument, null, true); if ( ! convertedBagArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedBagArgument.getStatus())); } Bag bag = convertedBagArgument.getBag(); Iterator<AttributeValue<?>> iterBagContents = bag.getAttributeValues(); while (iterBagContents.hasNext()) { AttributeValue<?> attributeValueBagContents = iterBagContents.next(); if (attributeValueElement.equals(attributeValueBagContents)) { return ER_TRUE; } } return ER_FALSE; } FunctionDefinitionBagIsIn(Identifier idIn, DataType<I> dataTypeArgsIn); @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments); }
FunctionDefinitionBagIsIn extends FunctionDefinitionBase<Boolean, I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 2) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected 2 arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument elementArgument = arguments.get(0); ConvertedArgument<I> convertedTargetArgument = new ConvertedArgument<I>(elementArgument, this.getDataTypeArgs(), false); if ( ! convertedTargetArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedTargetArgument.getStatus())); } AttributeValue<?> attributeValueElement = elementArgument.getValue(); FunctionArgument bagArgument = arguments.get(1); ConvertedArgument<Bag> convertedBagArgument = new ConvertedArgument<Bag>(bagArgument, null, true); if ( ! convertedBagArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedBagArgument.getStatus())); } Bag bag = convertedBagArgument.getBag(); Iterator<AttributeValue<?>> iterBagContents = bag.getAttributeValues(); while (iterBagContents.hasNext()) { AttributeValue<?> attributeValueBagContents = iterBagContents.next(); if (attributeValueElement.equals(attributeValueBagContents)) { return ER_TRUE; } } return ER_FALSE; } FunctionDefinitionBagIsIn(Identifier idIn, DataType<I> dataTypeArgsIn); @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments);  }