SumFunction extends BaseFEELFunction { public FEELFnResult<BigDecimal> invoke(@ParameterName("list") List list) { if ( list == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "the list cannot be null")); } if (list.isEmpty()) { return FEELFnResult.ofResult(null); } BigDecimal sum = BigDecimal.ZERO; for ( Object element : list ) { if ( element instanceof BigDecimal ) { sum = sum.add( (BigDecimal) element ); } else if ( element instanceof Number ) { BigDecimal value = EvalHelper.getBigDecimalOrNull( element ); if (value == null) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "an element in the list is not suitable for the sum")); } else { sum = sum.add( value ); } } else { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "an element in the list is not a number")); } } return FEELFnResult.ofResult( sum ); } SumFunction(); FEELFnResult<BigDecimal> invoke(@ParameterName("list") List list); FEELFnResult<BigDecimal> invoke(@ParameterName("list") Number single); FEELFnResult<BigDecimal> invoke(@ParameterName("n") Object[] list);  }
@Test public void invokeNumberParamNull() { FunctionTestUtil.assertResultError(sumFunction.invoke((Number) null), InvalidParametersEvent.class); }
@Test public void invokeNumberParamUnsupportedNumber() { FunctionTestUtil.assertResultError(sumFunction.invoke(Double.NaN), InvalidParametersEvent.class); }
@Test public void invokeNumberParamSupportedNumber() { FunctionTestUtil.assertResult(sumFunction.invoke(BigDecimal.TEN), BigDecimal.TEN); FunctionTestUtil.assertResult(sumFunction.invoke(10), BigDecimal.TEN); FunctionTestUtil.assertResult(sumFunction.invoke(-10), BigDecimal.valueOf(-10)); FunctionTestUtil.assertResult(sumFunction.invoke(10.12), BigDecimal.valueOf(10.12)); }
@Test public void invokeListParam() { FunctionTestUtil.assertResultError(sumFunction.invoke((List) null), InvalidParametersEvent.class); }
@Test public void invokeListParamContainsUnsupportedNumber() { FunctionTestUtil.assertResultError(sumFunction.invoke(Arrays.asList(10, 2, Double.NaN)), InvalidParametersEvent.class); }
@Test public void invokeListParamContainsUnsupportedType() { FunctionTestUtil.assertResultError(sumFunction.invoke(Arrays.asList(10, "test", 2)), InvalidParametersEvent.class); }
@Test public void invokeListParamSupportedTypes() { FunctionTestUtil.assertResult(sumFunction.invoke(Arrays.asList(4, -1, 12.1, (long) 5, BigDecimal.TEN)), BigDecimal.valueOf(30.1)); }
@Test public void invokeArrayParam() { FunctionTestUtil.assertResultError(sumFunction.invoke((Object[]) null), InvalidParametersEvent.class); }
@Test public void invokeArrayParamContainsUnsupportedNumber() { FunctionTestUtil.assertResultError(sumFunction.invoke(new Object[]{10, 2, Double.NaN}), InvalidParametersEvent.class); }
@Test public void invokeArrayParamContainsUnsupportedType() { FunctionTestUtil.assertResultError(sumFunction.invoke(new Object[]{10, "test", 2}), InvalidParametersEvent.class); }
@Test public void invokeArrayParamSupportedTypes() { FunctionTestUtil.assertResult(sumFunction.invoke(new Object[]{4, -1, 12.1, (long) 5, BigDecimal.TEN}), BigDecimal.valueOf(30.1)); }