@Test public void testXPathYearMonthDuration_from_string() { FunctionArgumentAttributeValue attrString1 = null; FunctionArgumentAttributeValue attrStringNeg1 = null; FunctionArgumentAttributeValue attrStringNeg2 = null; FunctionArgumentAttributeValue attrStringNoYear1 = null; FunctionArgumentAttributeValue attrStringNoYear2 = null; FunctionArgumentAttributeValue attrStringNoMonth1 = null; FunctionArgumentAttributeValue attrStringNoMonth2 = null; FunctionArgumentAttributeValue attrStringNoValue = null; FunctionArgumentAttributeValue attrStringNoP = null; FunctionArgumentAttributeValue attrStringBigMonths = null; FunctionArgumentAttributeValue attrStringMissingTOk = null; FunctionArgumentAttributeValue attrStringMissingTBad = null; FunctionArgumentAttributeValue attrStringZeroMonths = null; FunctionArgumentAttributeValue attrStringBadValue = null; FunctionArgumentAttributeValue attrStringBadType = null; try { attrString1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P1Y2M")); attrStringNeg1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("-P1Y2M")); attrStringNeg2 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P-1Y2M")); attrStringNoYear1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P2M")); attrStringNoYear2 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("PY2M")); attrStringNoMonth1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P1Y")); attrStringNoMonth2 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P1YM")); attrStringNoValue = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P")); attrStringNoP = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("1Y2M")); attrStringBigMonths = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P1Y12M")); attrStringMissingTOk = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P3D")); attrStringMissingTBad = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P3D10H30M23S")); attrStringZeroMonths = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("P0M")); attrStringBadValue = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("not valid obj value")); attrStringBadType = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(123)); } catch (Exception e) { fail("creating attribute e="+ e); } FunctionDefinitionStringConversion<?, ?> fd = (FunctionDefinitionStringConversion<?, ?>) StdFunctions.FD_YEARMONTHDURATION_FROM_STRING; assertEquals(XACML3.ID_FUNCTION_YEARMONTHDURATION_FROM_STRING, fd.getId()); assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeArgs().getId()); assertEquals(DataTypes.DT_YEARMONTHDURATION.getId(), fd.getDataTypeId()); assertFalse(fd.returnsBag()); assertEquals(new Integer(1), fd.getNumArgs()); arguments.clear(); arguments.add(attrString1); ExpressionResult res = fd.evaluate(null, arguments); assertTrue(res.isOk()); XPathYearMonthDuration resValue = (XPathYearMonthDuration)res.getValue().getValue(); assertEquals(new XPathYearMonthDuration(1,1, 2), resValue); arguments.clear(); arguments.add(attrStringNeg1); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); resValue = (XPathYearMonthDuration)res.getValue().getValue(); assertEquals(new XPathYearMonthDuration(-1, 1, 2), resValue); arguments.clear(); arguments.add(attrStringNeg2); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Invalid chunk \"P-1Y2M\" at position 1", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrStringNoYear1); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); resValue = (XPathYearMonthDuration)res.getValue().getValue(); assertEquals(new XPathYearMonthDuration(1, 0, 2), resValue); arguments.clear(); arguments.add(attrStringNoYear2); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Invalid chunk \"PY2M\" at position 1", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrStringNoMonth1); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); resValue = (XPathYearMonthDuration)res.getValue().getValue(); assertEquals(new XPathYearMonthDuration(1, 1, 0), resValue); arguments.clear(); arguments.add(attrStringNoMonth2); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Invalid chunk \"P1YM\" at position 3", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrStringNoValue); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Invalid ISO8601 duration string \"P\": No duration components following P", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrStringNoP); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Invalid ISO8601 duration string \"1Y2M\" at position 0", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrStringBigMonths); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); resValue = (XPathYearMonthDuration)res.getValue().getValue(); assertEquals(new XPathYearMonthDuration(1, 1, 12), resValue); arguments.clear(); arguments.add(attrStringZeroMonths); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); resValue = (XPathYearMonthDuration)res.getValue().getValue(); assertEquals(new XPathYearMonthDuration(1, 0, 0), resValue); arguments.clear(); arguments.add(attrStringMissingTOk); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Invalid XPath yearMonthDuraiton \"{durationSign=1years=0months=0days=3hours=0minutes=0seconds=0millis=0}\": includes days, hours, minutes, or seconds", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrStringMissingTBad); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Invalid ISO8601 duration string \"P3D10H30M23S\" at position 6: out of order component", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrStringBadValue); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Invalid ISO8601 duration string \"not valid obj value\" at position 0", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:syntax-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrStringBadType); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:yearMonthDuration-from-string Expected data type 'string' 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);  }