@Test public void testString_from_yearMonthDuration() { FunctionArgumentAttributeValue attrObj1 = null; FunctionArgumentAttributeValue attrStringBadType = null; String objValueString = "P1Y2M"; try { attrObj1 = new FunctionArgumentAttributeValue(DataTypes.DT_YEARMONTHDURATION.createAttributeValue(objValueString)); attrStringBadType = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(123)); } catch (Exception e) { fail("creating attribute e="+ e); } FunctionDefinitionStringConversion<?, ?> fd = (FunctionDefinitionStringConversion<?, ?>) StdFunctions.FD_STRING_FROM_YEARMONTHDURATION; assertEquals(XACML3.ID_FUNCTION_STRING_FROM_YEARMONTHDURATION, fd.getId()); assertEquals(DataTypes.DT_YEARMONTHDURATION.getId(), fd.getDataTypeArgs().getId()); assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeId()); assertFalse(fd.returnsBag()); assertEquals(new Integer(1), fd.getNumArgs()); arguments.clear(); arguments.add(attrObj1); ExpressionResult res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(objValueString, res.getValue().getValue()); arguments.clear(); arguments.add(attrStringBadType); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-from-yearMonthDuration Expected data type 'yearMonthDuration' saw 'integer' at arg index 0", 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) { List<I> convertedArguments = new ArrayList<I>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } if (this.getDataTypeId().equals(DataTypes.DT_STRING.getId())) { try { String output = this.getDataTypeArgs().toStringValue(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<String>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message)); } } else { try { O output = this.getDataType().convert(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<O>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, this.getShortFunctionId() + " " + message )); } } }
FunctionDefinitionStringConversion extends FunctionDefinitionHomogeneousSimple<O, I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<I> convertedArguments = new ArrayList<I>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } if (this.getDataTypeId().equals(DataTypes.DT_STRING.getId())) { try { String output = this.getDataTypeArgs().toStringValue(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<String>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message)); } } else { try { O output = this.getDataType().convert(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<O>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, this.getShortFunctionId() + " " + message )); } } } }
FunctionDefinitionStringConversion extends FunctionDefinitionHomogeneousSimple<O, I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<I> convertedArguments = new ArrayList<I>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } if (this.getDataTypeId().equals(DataTypes.DT_STRING.getId())) { try { String output = this.getDataTypeArgs().toStringValue(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<String>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message)); } } else { try { O output = this.getDataType().convert(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<O>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, this.getShortFunctionId() + " " + message )); } } } FunctionDefinitionStringConversion(Identifier idIn, DataType<O> outputType, DataType<I> argType); }
FunctionDefinitionStringConversion extends FunctionDefinitionHomogeneousSimple<O, I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<I> convertedArguments = new ArrayList<I>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } if (this.getDataTypeId().equals(DataTypes.DT_STRING.getId())) { try { String output = this.getDataTypeArgs().toStringValue(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<String>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message)); } } else { try { O output = this.getDataType().convert(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<O>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, this.getShortFunctionId() + " " + message )); } } } FunctionDefinitionStringConversion(Identifier idIn, DataType<O> outputType, DataType<I> argType); @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments); }
FunctionDefinitionStringConversion extends FunctionDefinitionHomogeneousSimple<O, I> { @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { List<I> convertedArguments = new ArrayList<I>(); Status status = this.validateArguments(arguments, convertedArguments); if (!status.getStatusCode().equals(StdStatusCode.STATUS_CODE_OK)) { return ExpressionResult.newError(getFunctionStatus(status)); } if (this.getDataTypeId().equals(DataTypes.DT_STRING.getId())) { try { String output = this.getDataTypeArgs().toStringValue(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<String>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message)); } } else { try { O output = this.getDataType().convert(convertedArguments.get(0)); return ExpressionResult.newSingle(new StdAttributeValue<O>(this.getDataTypeId(), output)); } catch (Exception e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, this.getShortFunctionId() + " " + message )); } } } FunctionDefinitionStringConversion(Identifier idIn, DataType<O> outputType, DataType<I> argType); @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments);  }