IdentityScriptValueGenerator extends AbstractValueGenerator<IdmIdentityDto> { @Override public IdmIdentityDto generate(IdmIdentityDto dto, IdmGenerateValueDto valueGenerator) { Assert.notNull(dto, "DTO is required."); Assert.notNull(valueGenerator, "Value generator is required."); String scriptCode = getScriptCode(valueGenerator); Object returnValue = systemScriptEvaluator.evaluate(new AbstractScriptEvaluator.Builder() .setScriptCode(scriptCode) .addParameter(ENTITY_KEY, dto) .addParameter(VALUE_GENERATOR_KEY, valueGenerator)); if (returnValue == null || !(returnValue instanceof IdmIdentityDto)) { throw new ResultCodeException(CoreResultCode.GENERATOR_SCRIPT_RETURN_NULL_OR_BAD_DTO_TYPE, ImmutableMap.of( "scriptCode", scriptCode == null ? "null" : scriptCode, "returnedValue", returnValue == null ? "null" : returnValue)); } return (IdmIdentityDto) returnValue; }  @Override String getName(); @Override IdmIdentityDto generate(IdmIdentityDto dto, IdmGenerateValueDto valueGenerator); @Override List<String> getPropertyNames(); @Override List<IdmFormAttributeDto> getFormAttributes(); static final String GENERATOR_NAME; static String SCRIPT_CODE; static String ENTITY_KEY; static String VALUE_GENERATOR_KEY; }
@Test public void testGreenLine() { IdmScriptDto script = createScript( "import " + StringUtils.class.getCanonicalName() + ";" + System.lineSeparator() + "if (!valueGenerator.isRegenerateValue() && StringUtils.isNotEmpty(entity.getDescription())) {" + System.lineSeparator() + " return entity;" + System.lineSeparator() + "}" + System.lineSeparator() + "" + System.lineSeparator() + "entity.setDescription(entity.getUsername() + 123);" + System.lineSeparator()); String username = this.getHelper().createName(); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(this.getHelper().createName()); identityDto.setLastName(this.getHelper().createName()); identityDto.setUsername(username); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityScriptValueGenerator.SCRIPT_CODE, script.getCode())), 1, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getDescription()); assertEquals(username + 123, generatedDto.getDescription()); }