DurationFunction extends BaseFEELFunction { public FEELFnResult<TemporalAmount> invoke(@ParameterName( "from" ) String val) { if ( val == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "cannot be null")); } if ( val.indexOf("-") > 0) { return FEELFnResult.ofError( new InvalidParametersEvent(Severity.ERROR, "from", "negative values for units are not allowed.") ); } try { return FEELFnResult.ofResult( Duration.parse( val ) ); } catch( DateTimeParseException e ) { try { return FEELFnResult.ofResult(ComparablePeriod.parse(val).normalized()); } catch( DateTimeParseException e2 ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "date-parsing exception", new RuntimeException(new Throwable() { public final List<Throwable> causes = Arrays.asList( new Throwable[]{e, e2} ); } ))); } } } DurationFunction(); FEELFnResult<TemporalAmount> invoke(@ParameterName( "from" ) String val); FEELFnResult<TemporalAmount> invoke(@ParameterName( "from" ) TemporalAmount val);  }
@Test public void invokeParamStringNull() { FunctionTestUtil.assertResultError(durationFunction.invoke((String) null), InvalidParametersEvent.class); }
@Test public void invokeParamStringInvalid() { FunctionTestUtil.assertResultError(durationFunction.invoke("test"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(durationFunction.invoke("test HHH"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(durationFunction.invoke("testP2Y3D"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(durationFunction.invoke("test P2Y3D"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(durationFunction.invoke("P2Y3DD"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(durationFunction.invoke("P3DD"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(durationFunction.invoke("PT3HH"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(durationFunction.invoke("P2Y3M4DT3H"), InvalidParametersEvent.class); }
@Test public void invokeParamStringDuration() { FunctionTestUtil.assertResult(durationFunction.invoke("P2D"), Duration.of(2, ChronoUnit.DAYS)); FunctionTestUtil.assertResult(durationFunction.invoke("P2DT3H"), Duration.of(2, ChronoUnit.DAYS).plusHours(3)); FunctionTestUtil.assertResult( durationFunction.invoke("P2DT3H28M"), Duration.of(2, ChronoUnit.DAYS).plusHours(3).plusMinutes(28)); FunctionTestUtil.assertResult( durationFunction.invoke("P2DT3H28M15S"), Duration.of(2, ChronoUnit.DAYS).plusHours(3).plusMinutes(28).plusSeconds(15)); }
@Test public void invokeParamStringPeriod() { FunctionTestUtil.assertResult(durationFunction.invoke("P2Y3M"), ComparablePeriod.of(2, 3, 0)); FunctionTestUtil.assertResult(durationFunction.invoke("P2Y3M4D"), ComparablePeriod.of(2, 3, 4)); }
@Test public void invokeParamTemporalAmountNull() { FunctionTestUtil.assertResultError(durationFunction.invoke((TemporalAmount) null), InvalidParametersEvent.class); }
@Test public void invokeParamTemporalDuration() { FunctionTestUtil.assertResult( durationFunction.invoke(Duration.parse("P2DT3H28M15S")), Duration.of(2, ChronoUnit.DAYS).plusHours(3).plusMinutes(28).plusSeconds(15)); }
@Test public void invokeParamTemporalPeriod() { FunctionTestUtil.assertResult(durationFunction.invoke(ComparablePeriod.parse("P2Y3M4D")), ComparablePeriod.of(2, 3, 4)); }