PredictionRuleMapperGenerator { public static String getPredictionRuleMapperSource(final String fullRuleName) { final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : ""; CompilationUnit clazz = StaticJavaParser.parse(PredictionRuleMapperGenerator.class.getResourceAsStream(TEMPLATE_JAVA)).clone(); if (!packageName.isEmpty()) { clazz.setPackageDeclaration(packageName); } ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) clazz.getTypes().get(0); FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + TEMPLATE_JAVA + " has been modified.")); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(fullRuleName); ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr); return clazz.toString(); } private  PredictionRuleMapperGenerator(); static String getPredictionRuleMapperSource(final String fullRuleName);  }
@Test void getPredictionRuleMapperSourceWithoutPackage() { final String fullRuleName = "FULL_RULE_NAME"; String retrieved = PredictionRuleMapperGenerator.getPredictionRuleMapperSource(fullRuleName); assertNotNull(retrieved); String expected = String.format("public final static Model model = new %s();", fullRuleName); assertTrue(retrieved.contains(expected)); }
@Test void getPredictionRuleMapperSourceWithPackage() { final String packageName = "PACKAGE"; final String ruleName = "RULE_NAME"; final String fullRuleName = packageName + "." + ruleName; String retrieved = PredictionRuleMapperGenerator.getPredictionRuleMapperSource(fullRuleName); assertNotNull(retrieved); String expected = String.format("package %s;", packageName); assertTrue(retrieved.contains(expected)); expected = String.format("public final static Model model = new %s();", fullRuleName); assertTrue(retrieved.contains(expected)); }