ContractPositionNameGenerator extends AbstractValueGenerator<IdmIdentityContractDto> { @Override public IdmIdentityContractDto generate(IdmIdentityContractDto dto, IdmGenerateValueDto generatorConfiguration) { if (!generatorConfiguration.isRegenerateValue() && StringUtils.isNotEmpty(dto.getPosition())) { return dto; } IdmIdentityDto identityDto = DtoUtils.getEmbedded(dto, IdmIdentityContract_.identity, IdmIdentityDto.class, null); if (identityDto == null) { identityDto = identityService.get(dto.getIdentity()); } if (identityDto == null) { LOG.error("New contract hasn't correctly filed identity ID [{}]", dto.getIdentity()); return dto; } String username = identityDto.getUsername(); StringBuilder result = new StringBuilder(); String prefix = this.getPrefix(generatorConfiguration); String suffix = this.getSuffix(generatorConfiguration); if (StringUtils.isNotEmpty(prefix)) { result.append(prefix); } result.append(username); if (StringUtils.isNotEmpty(suffix)) { result.append(suffix); } dto.setPosition(result.toString()); return dto; }  @Override String getName(); @Override List<String> getPropertyNames(); @Override IdmIdentityContractDto generate(IdmIdentityContractDto dto, IdmGenerateValueDto generatorConfiguration); static final String GENERATOR_NAME; static String POSITION_NAME_SUFFIX; static String POSITION_NAME_PREFIX; }
@Test public void testRegenerateOff() { IdmIdentityDto identityDto = getHelper().createIdentity(); IdmIdentityContractDto contractDto = new IdmIdentityContractDto(); contractDto.setIdentity(identityDto.getId()); String positionName = "positionName" + System.currentTimeMillis(); String prefix = "prefix" + System.currentTimeMillis(); String suffix = "suffix" + System.currentTimeMillis(); contractDto.setPosition(positionName); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( ContractPositionNameGenerator.POSITION_NAME_PREFIX, prefix, ContractPositionNameGenerator.POSITION_NAME_SUFFIX, suffix)), 1, Boolean.FALSE); IdmIdentityContractDto generated = valueGeneratorManager.generate(contractDto); assertNotNull(generated); assertEquals(positionName, generated.getPosition()); }
@Test public void testWithoutSuffix() { IdmIdentityDto identityDto = getHelper().createIdentity(); IdmIdentityContractDto contractDto = new IdmIdentityContractDto(); contractDto.setIdentity(identityDto.getId()); String prefix = "prefix" + System.currentTimeMillis(); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( ContractPositionNameGenerator.POSITION_NAME_PREFIX, prefix)), 1, null); IdmIdentityContractDto generated = valueGeneratorManager.generate(contractDto); assertNotNull(generated); assertEquals(prefix + identityDto.getUsername(), generated.getPosition()); }
@Test public void testWithoutPrefix() { IdmIdentityDto identityDto = getHelper().createIdentity(); IdmIdentityContractDto contractDto = new IdmIdentityContractDto(); contractDto.setIdentity(identityDto.getId()); String suffix = "suffix" + System.currentTimeMillis(); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( ContractPositionNameGenerator.POSITION_NAME_SUFFIX, suffix)), 1, null); IdmIdentityContractDto generated = valueGeneratorManager.generate(contractDto); assertNotNull(generated); assertEquals(identityDto.getUsername() + suffix, generated.getPosition()); }
@Test public void testWithoutPrefixAndSuffix() { IdmIdentityDto identityDto = getHelper().createIdentity(); IdmIdentityContractDto contractDto = new IdmIdentityContractDto(); contractDto.setIdentity(identityDto.getId()); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), null), 1, null); IdmIdentityContractDto generated = valueGeneratorManager.generate(contractDto); assertNotNull(generated); assertEquals(identityDto.getUsername(), generated.getPosition()); }
@Test public void testGreenLine() { IdmIdentityDto identityDto = getHelper().createIdentity(); IdmIdentityContractDto contractDto = new IdmIdentityContractDto(); contractDto.setIdentity(identityDto.getId()); String prefix = "prefix" + System.currentTimeMillis(); String suffix = "suffix" + System.currentTimeMillis(); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( ContractPositionNameGenerator.POSITION_NAME_PREFIX, prefix, ContractPositionNameGenerator.POSITION_NAME_SUFFIX, suffix)), 1, null); IdmIdentityContractDto generated = valueGeneratorManager.generate(contractDto); assertNotNull(generated); assertEquals(prefix + identityDto.getUsername() + suffix, generated.getPosition()); }