@Test public void testBoolean() { Boolean v1 = new Boolean(true); Boolean v2 = new Boolean(false); BigInteger vOtherType = BigInteger.valueOf(11); Bag bag0 = new Bag(); Bag bag1 = new Bag(); bag1.add(new StdAttributeValue<Boolean>(DataTypes.DT_BOOLEAN.getId(), v1)); Bag bag2 = new Bag(); bag2.add(new StdAttributeValue<Boolean>(DataTypes.DT_BOOLEAN.getId(), v1)); bag2.add(new StdAttributeValue<Boolean>(DataTypes.DT_BOOLEAN.getId(), v2)); Bag bagOtherType = new Bag(); bagOtherType.add(new StdAttributeValue<BigInteger>(DataTypes.DT_STRING.getId(), vOtherType)); FunctionArgumentBag attrBag0 = new FunctionArgumentBag(bag0); FunctionArgumentBag attrBag1 = new FunctionArgumentBag(bag1); FunctionArgumentBag attrBag2 = new FunctionArgumentBag(bag2); FunctionArgumentBag attrBagOtherType = new FunctionArgumentBag(bagOtherType); FunctionDefinitionBagOneAndOnly<?> fd = (FunctionDefinitionBagOneAndOnly<?>) StdFunctions.FD_BOOLEAN_ONE_AND_ONLY; assertEquals(XACML3.ID_FUNCTION_BOOLEAN_ONE_AND_ONLY, fd.getId()); assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId()); assertFalse(fd.returnsBag()); arguments.clear(); arguments.add(attrBag1); 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(new Boolean(true), resValue); arguments.clear(); arguments.add(null); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:boolean-one-and-only 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(attrBagOtherType); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:boolean-one-and-only Element in bag of wrong type. Expected boolean got string", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBag0); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:boolean-one-and-only Expected 1 but Bag has 0 elements", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBag2); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:boolean-one-and-only Expected 1 but Bag has 2 elements", 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() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 argument, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument argument = arguments.get(0); ConvertedArgument<Bag> convertedArgument = new ConvertedArgument<Bag>(argument, null, true); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } Bag bag = convertedArgument.getBag(); if (bag.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 but Bag has " + bag.size() + " elements")); } AttributeValue<?> attributeValueOneAndOnly = bag.getAttributeValues().next(); assert(attributeValueOneAndOnly != null); if (!this.getDataTypeId().equals(attributeValueOneAndOnly.getDataTypeId())) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Element in bag of wrong type. Expected " + this.getShortDataTypeId(this.getDataTypeId()) + " got " + this.getShortDataTypeId(attributeValueOneAndOnly.getDataTypeId()))); } return ExpressionResult.newSingle(attributeValueOneAndOnly); }
FunctionDefinitionBagOneAndOnly extends FunctionDefinitionBase<I,I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 argument, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument argument = arguments.get(0); ConvertedArgument<Bag> convertedArgument = new ConvertedArgument<Bag>(argument, null, true); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } Bag bag = convertedArgument.getBag(); if (bag.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 but Bag has " + bag.size() + " elements")); } AttributeValue<?> attributeValueOneAndOnly = bag.getAttributeValues().next(); assert(attributeValueOneAndOnly != null); if (!this.getDataTypeId().equals(attributeValueOneAndOnly.getDataTypeId())) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Element in bag of wrong type. Expected " + this.getShortDataTypeId(this.getDataTypeId()) + " got " + this.getShortDataTypeId(attributeValueOneAndOnly.getDataTypeId()))); } return ExpressionResult.newSingle(attributeValueOneAndOnly); } }
FunctionDefinitionBagOneAndOnly extends FunctionDefinitionBase<I,I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 argument, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument argument = arguments.get(0); ConvertedArgument<Bag> convertedArgument = new ConvertedArgument<Bag>(argument, null, true); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } Bag bag = convertedArgument.getBag(); if (bag.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 but Bag has " + bag.size() + " elements")); } AttributeValue<?> attributeValueOneAndOnly = bag.getAttributeValues().next(); assert(attributeValueOneAndOnly != null); if (!this.getDataTypeId().equals(attributeValueOneAndOnly.getDataTypeId())) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Element in bag of wrong type. Expected " + this.getShortDataTypeId(this.getDataTypeId()) + " got " + this.getShortDataTypeId(attributeValueOneAndOnly.getDataTypeId()))); } return ExpressionResult.newSingle(attributeValueOneAndOnly); } FunctionDefinitionBagOneAndOnly(Identifier idIn, DataType<I> dataTypeArgsIn); }
FunctionDefinitionBagOneAndOnly extends FunctionDefinitionBase<I,I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 argument, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument argument = arguments.get(0); ConvertedArgument<Bag> convertedArgument = new ConvertedArgument<Bag>(argument, null, true); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } Bag bag = convertedArgument.getBag(); if (bag.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 but Bag has " + bag.size() + " elements")); } AttributeValue<?> attributeValueOneAndOnly = bag.getAttributeValues().next(); assert(attributeValueOneAndOnly != null); if (!this.getDataTypeId().equals(attributeValueOneAndOnly.getDataTypeId())) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Element in bag of wrong type. Expected " + this.getShortDataTypeId(this.getDataTypeId()) + " got " + this.getShortDataTypeId(attributeValueOneAndOnly.getDataTypeId()))); } return ExpressionResult.newSingle(attributeValueOneAndOnly); } FunctionDefinitionBagOneAndOnly(Identifier idIn, DataType<I> dataTypeArgsIn); @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments); }
FunctionDefinitionBagOneAndOnly extends FunctionDefinitionBase<I,I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || arguments.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 argument, got " + ((arguments == null) ? "null" : arguments.size()) )); } FunctionArgument argument = arguments.get(0); ConvertedArgument<Bag> convertedArgument = new ConvertedArgument<Bag>(argument, null, true); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } Bag bag = convertedArgument.getBag(); if (bag.size() != 1) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Expected 1 but Bag has " + bag.size() + " elements")); } AttributeValue<?> attributeValueOneAndOnly = bag.getAttributeValues().next(); assert(attributeValueOneAndOnly != null); if (!this.getDataTypeId().equals(attributeValueOneAndOnly.getDataTypeId())) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, getShortFunctionId() + " Element in bag of wrong type. Expected " + this.getShortDataTypeId(this.getDataTypeId()) + " got " + this.getShortDataTypeId(attributeValueOneAndOnly.getDataTypeId()))); } return ExpressionResult.newSingle(attributeValueOneAndOnly); } FunctionDefinitionBagOneAndOnly(Identifier idIn, DataType<I> dataTypeArgsIn); @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments);  }