ExpressionFunctionUtils { static BlockStmt getFieldRefExpressionFromDefineFunctionBlockStmt(final String variableName, final FieldRef fieldRef, final ClassOrInterfaceType returnedType, final LinkedHashMap<String, ClassOrInterfaceType> parameterNameTypeMap) { final BlockStmt toReturn = new BlockStmt(); String fieldNameToRef = fieldRef.getField().getValue(); NameExpr nameExpression = new NameExpr(fieldNameToRef); BinaryExpr condition = new BinaryExpr(); condition.setLeft(nameExpression); condition.setRight(new NullLiteralExpr()); condition.setOperator(BinaryExpr.Operator.NOT_EQUALS); CastExpr thenExpr = new CastExpr(); thenExpr.setType(returnedType); MethodCallExpr converterCallExpression = new MethodCallExpr(); converterCallExpression.setName("convert"); converterCallExpression.setScope(CONVERTER_TYPE_UTIL_FIELD_ACCESSOR_EXPR); ClassExpr expectedClassExpression = new ClassExpr(); expectedClassExpression.setType(returnedType); converterCallExpression.setArguments(NodeList.nodeList(expectedClassExpression, nameExpression)); thenExpr.setExpression(converterCallExpression); com.github.javaparser.ast.expr.Expression elseExpression = fieldRef.getMapMissingTo() != null ? new StringLiteralExpr(fieldRef.getMapMissingTo()) : new NullLiteralExpr(); CastExpr elseExpr = new CastExpr(); elseExpr.setType(returnedType); elseExpr.setExpression(elseExpression); ConditionalExpr initializer = new ConditionalExpr(); initializer.setCondition(condition); initializer.setThenExpr(thenExpr); initializer.setElseExpr(elseExpr); VariableDeclarator variableDeclarator = new VariableDeclarator(); variableDeclarator.setType(returnedType); variableDeclarator.setName(variableName); variableDeclarator.setInitializer(initializer); VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(); variableDeclarationExpr.setVariables(NodeList.nodeList(variableDeclarator)); toReturn.addStatement(variableDeclarationExpr); return toReturn; } private  ExpressionFunctionUtils();   }
@Test public void getFieldRefExpressionFromDefineFunctionBlockStmt() { String variableName = "VARIABLE_NAME"; FieldRef fieldRef = fieldRefSupplier.get(); ParameterField parameterField = new ParameterField(FieldName.create("FIELD_REF")); LinkedHashMap<String, ClassOrInterfaceType> modifiedParametersMap = new LinkedHashMap<>(DEFAULT_PARAMETERTYPE_MAP); modifiedParametersMap.put(parameterField.getName().toString(), parseClassOrInterfaceType(getBoxedClassName(parameterField))); BlockStmt retrieved = ExpressionFunctionUtils.getFieldRefExpressionFromDefineFunctionBlockStmt(variableName, fieldRef, parseClassOrInterfaceType(Object.class.getName()), modifiedParametersMap); String expected = "{\n" + " java.lang.Object VARIABLE_NAME = FIELD_REF != null ? (java.lang.Object) org.kie.pmml.commons.utils.ConverterTypeUtil.convert(java.lang.Object.class, FIELD_REF) : (java.lang.Object) null;\n" + "}"; assertEquals(expected, retrieved.toString()); String mapMissingTo = "MAP_MISSING_TO"; fieldRef.setMapMissingTo(mapMissingTo); retrieved = ExpressionFunctionUtils.getFieldRefExpressionFromDefineFunctionBlockStmt(variableName, fieldRef, parseClassOrInterfaceType(Object.class.getName()), modifiedParametersMap); expected = "{\n" + " java.lang.Object VARIABLE_NAME = FIELD_REF != null ? (java.lang.Object) org.kie.pmml.commons.utils.ConverterTypeUtil.convert(java.lang.Object.class, FIELD_REF) : (java.lang.Object) \"MAP_MISSING_TO\";\n" + "}"; assertEquals(expected, retrieved.toString()); }