IdentityEmailGenerator 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.getEmail())) { return dto; } String transformedUsername = null; if (isGenerateFromUsername(valueGenerator)) { transformedUsername = StringUtils.stripAccents(dto.getUsername()); transformedUsername = replaceAllWhiteSpaces(valueGenerator, transformedUsername); } else { transformedUsername = super.generateUsername(dto, valueGenerator); } if (StringUtils.isEmpty(transformedUsername) ) { return dto; } if (StringUtils.contains(transformedUsername, AT_CONSTANT)) { return dto; } String emailSuffix = getEmailSuffix(valueGenerator); if (StringUtils.isEmpty(emailSuffix)) { return dto; } StringBuilder result = new StringBuilder(); result.append(transformedUsername); result.append(getTransformedSuffix(valueGenerator, emailSuffix)); dto.setEmail(result.toString()); 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 EMAIL_SUFFIX; static String GENERATE_FROM_USERNAME; }
@Test public void testWithoutUsername() { String emailSuffix = "@example.tld"; IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(this.getHelper().createName()); identityDto.setLastName(this.getHelper().createName()); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 1, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNull(generatedDto.getEmail()); }
@Test public void testGreenLine() { String emailSuffix = "@example.tld"; 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( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 1, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getEmail()); assertEquals(username + emailSuffix, generatedDto.getEmail()); }
@Test public void testGreenLineWithoutAt() { String emailSuffix = "example.tld"; 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( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 1, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getEmail()); assertEquals(username + "@" + emailSuffix, generatedDto.getEmail()); }
@Test public void testUsernameContainsAt() { String emailSuffix = "example.tld"; String username = "test@username"; 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( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 1, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNull(generatedDto.getEmail()); }
@Test public void testOrder() { String emailSuffix = "ex.tld"; String emailSuffix1 = "@test.tld"; String emailSuffix2 = "bcvsolutions.tld"; 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( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 10, null); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix1, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 20, null); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix2, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 30, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getEmail()); assertEquals(username + "@" + emailSuffix2, generatedDto.getEmail()); }
@Test public void testRegenerate() { String emailSuffix = "ex.tld"; String emailSuffix1 = "@test.tld"; String emailSuffix2 = "bcvsolutions.tld"; String username = this.getHelper().createName(); String email = "email@example.tdl"; IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(this.getHelper().createName()); identityDto.setLastName(this.getHelper().createName()); identityDto.setUsername(username); identityDto.setEmail(email); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 10, Boolean.FALSE); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix1, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 20, Boolean.FALSE); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix2, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 30, Boolean.FALSE); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getEmail()); assertEquals(email, generatedDto.getEmail()); }
@Test public void testDisable() { String emailSuffix = "ex.tld"; String emailSuffix1 = "@test.tld"; String emailSuffix2 = "bcvsolutions.tld"; 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( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 10, null); IdmGenerateValueDto createGenerator = this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix1, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 20, null); createGenerator.setDisabled(true); this.generatedAttributeService.save(createGenerator); createGenerator = this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix2)), 30, null); createGenerator.setDisabled(true); this.generatedAttributeService.save(createGenerator); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getEmail()); assertEquals(username + "@" + emailSuffix, generatedDto.getEmail()); }
@Test public void testWhiteSpacesInUsername() { String firstName = " first name-test "; String lastName = "l astNa me"; String firstNameResult = "firstname-test"; String lastNameResult = "lastname"; String emailSuffix = "@example.tld"; IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); identityDto.setLastName(lastName); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.FALSE.toString())), 1, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getEmail()); assertEquals(lastNameResult + firstNameResult + emailSuffix, generatedDto.getEmail()); }
@Test public void testWhiteSpacesInEmailuSuffix() { String firstName = "firstname-test"; String lastName = "lastName"; String username = " userna me--"; String finalUsername = "username--"; String emailSuffix = " @exam ple.tld"; String emailSuffixResult = "@example.tld"; IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); identityDto.setLastName(lastName); identityDto.setUsername(username); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString())), 1, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getEmail()); assertEquals(finalUsername + emailSuffixResult, generatedDto.getEmail()); }
@Test public void testWhiteSpacesInEmailuSuffixWithReplaceCharacter() { String firstName = "firstname-test"; String lastName = "lastName"; String username = " use rna me--"; String finalUsername = "..use....rna..me--"; String emailSuffix = " @exam ple.tld"; String emailSuffixResult = "..@exam..ple.tld"; IdmIdentityDto identityDto = new IdmIdentityDto(); identityDto.setFirstName(firstName); identityDto.setLastName(lastName); identityDto.setUsername(username); ValueGeneratorDto generator = getGenerator(); this.createGenerator(getDtoType(), getGeneratorType(), this.createConfiguration(generator.getFormDefinition(), ImmutableMap.of( IdentityEmailGenerator.EMAIL_SUFFIX, emailSuffix, IdentityEmailGenerator.GENERATE_FROM_USERNAME, Boolean.TRUE.toString(), IdentityEmailGenerator.REPLACE_WHITE_SPACES_CHARACTER, "..")), 1, null); IdmIdentityDto generatedDto = valueGeneratorManager.generate(identityDto); assertNotNull(generatedDto.getEmail()); assertEquals(finalUsername + emailSuffixResult, generatedDto.getEmail()); }