IdentityUsernameGenerator extends AbstractIdentityValueGenerator { @Override public IdmIdentityDto generate(IdmIdentityDto dto, IdmGenerateValueDto valueGenerator) { Assert.notNull(dto, "DTO is required."); Assert.notNull(valueGenerator, "Value generator is required."); if (!valueGenerator.isRegenerateValue() && StringUtils.isNotEmpty(dto.getUsername())) { return dto; } String resultUsername = generateUsername(dto, valueGenerator); if (StringUtils.isEmpty(resultUsername)) { return dto; } if (isSearchUniqueUsername(valueGenerator)) { dto.setUsername(getUniqueUsername(resultUsername)); } else { dto.setUsername(resultUsername); } return dto; }  @Override String getName(); @Override List<String> getPropertyNames(); @Override IdmIdentityDto generate(IdmIdentityDto dto, IdmGenerateValueDto valueGenerator); @Override List<IdmFormAttributeDto> getFormAttributes(); static final String GENERATOR_NAME; static String SEARCH_UNIQUE_USERNAME; }
@Test public void generateMissingFirstName() { String lastName = "lastName-" + System.currentTimeMillis(); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityUsernameGenerator.FIRST_NAME_FIRST, Boolean.TRUE.toString())), 1, null); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setLastName(lastName); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertEquals(identityDto.getLastName(), generatedDto.getLastName()); assertEquals(identityDto.getUsername(), generatedDto.getUsername()); assertEquals(identityDto.getFirstName(), generatedDto.getFirstName()); assertNull(generatedDto.getUsername()); }
@Test public void generateMissingLastName() { String firstName = "firstName-" + System.currentTimeMillis(); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityUsernameGenerator.FIRST_NAME_FIRST, Boolean.TRUE.toString())), 1, null); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertEquals(identityDto.getLastName(), generatedDto.getLastName()); assertEquals(identityDto.getUsername(), generatedDto.getUsername()); assertEquals(identityDto.getFirstName(), generatedDto.getFirstName()); assertNull(generatedDto.getUsername()); }
@Test public void generateGreenLine() { String firstName = "firstName-" + System.currentTimeMillis(); String lastName = "lastName-" + System.currentTimeMillis(); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityUsernameGenerator.FIRST_NAME_FIRST, Boolean.TRUE.toString())), 1, null); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); identityDto.setLastName(lastName); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertEquals(identityDto.getLastName(), generatedDto.getLastName()); assertEquals(identityDto.getFirstName(), generatedDto.getFirstName()); assertNotNull(generatedDto.getUsername()); assertEquals(firstName.toLowerCase() + lastName.toLowerCase(), generatedDto.getUsername()); }
@Test public void generateDiacriticAndUpper() { String firstName = "áčďéěžšǍÁ"; String lastName = "óÉČŘčř-"; String newFirstName = "acdeezsaa"; String newLastName = "oecrcr-"; ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityUsernameGenerator.FIRST_NAME_FIRST, Boolean.TRUE.toString())), 1, null); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); identityDto.setLastName(lastName); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertEquals(identityDto.getLastName(), generatedDto.getLastName()); assertEquals(identityDto.getFirstName(), generatedDto.getFirstName()); assertNotNull(generatedDto.getUsername()); assertEquals(newFirstName + newLastName, generatedDto.getUsername()); }
@Test public void generateShorterProperties() { String firstName = "firstName"; String lastName = "lastName"; String newFirstName = "first"; String newLastName = "last"; ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityUsernameGenerator.FIRST_NAME_CHARACTERS_COUNT, "5", IdentityUsernameGenerator.LAST_NAME_CHARACTERS_COUNT, "4", IdentityUsernameGenerator.FIRST_NAME_FIRST, Boolean.TRUE.toString())), 1, null); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); identityDto.setLastName(lastName); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertEquals(identityDto.getLastName(), generatedDto.getLastName()); assertEquals(identityDto.getFirstName(), generatedDto.getFirstName()); assertNotNull(generatedDto.getUsername()); assertEquals(newFirstName + newLastName, generatedDto.getUsername()); }
@Test public void generateShorterPropertiesWithEmptyString() { String firstName = "firstName"; String lastName = "lastName"; ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityUsernameGenerator.FIRST_NAME_CHARACTERS_COUNT, "", IdentityUsernameGenerator.LAST_NAME_CHARACTERS_COUNT, "", IdentityUsernameGenerator.FIRST_NAME_FIRST, Boolean.TRUE.toString())), 1, null); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); identityDto.setLastName(lastName); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertEquals(identityDto.getLastName(), generatedDto.getLastName()); assertEquals(identityDto.getFirstName(), generatedDto.getFirstName()); assertNotNull(generatedDto.getUsername()); assertEquals(firstName.toLowerCase() + lastName.toLowerCase(), generatedDto.getUsername()); }
@Test public void generateShorterProperties2() { String firstName = "firstName" + System.currentTimeMillis(); String lastName = "lastName" + System.currentTimeMillis(); String newFirstName = "f"; String newLastName = "l"; ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityUsernameGenerator.FIRST_NAME_CHARACTERS_COUNT, "1", IdentityUsernameGenerator.LAST_NAME_CHARACTERS_COUNT, "1", IdentityUsernameGenerator.CONNECTING_CHARACTER, "--", IdentityUsernameGenerator.FIRST_NAME_FIRST, Boolean.TRUE.toString())), 1, null); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); identityDto.setLastName(lastName); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertEquals(identityDto.getLastName(), generatedDto.getLastName()); assertEquals(identityDto.getFirstName(), generatedDto.getFirstName()); assertNotNull(generatedDto.getUsername()); assertEquals(newFirstName + "--" + newLastName, generatedDto.getUsername()); }
@Test public void generateMoreThan255() { StringBuilder firstName = new StringBuilder(); for (int index = 0; index < 130; index++) { firstName.append("F"); } StringBuilder lastName = new StringBuilder(); for (int index = 0; index < 130; index++) { lastName.append("L"); } ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityUsernameGenerator.FIRST_NAME_FIRST, Boolean.TRUE.toString())), 1, null); IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName.toString()); identityDto.setLastName(lastName.toString()); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertEquals(identityDto.getLastName(), generatedDto.getLastName()); assertEquals(identityDto.getFirstName(), generatedDto.getFirstName()); assertNotNull(generatedDto.getUsername()); assertEquals(255, generatedDto.getUsername().length()); }