AccountValidator { public String normalizeAccountName(String name, String prefix) throws ServerException { String normalized = ILLEGAL_ACCOUNT_NAME_CHARACTERS.matcher(name).replaceAll(""); String candidate = normalized.isEmpty() ? NameGenerator.generate(prefix, 4) : normalized; int i = 1; try { while (accountExists(candidate)) { candidate = normalized.isEmpty() ? NameGenerator.generate(prefix, 4) : normalized + String.valueOf(i++); } } catch (ServerException e) { LOG.warn("Error occurred during account name normalization", e); throw e; } return candidate; } @Inject  AccountValidator(AccountManager accountManager); boolean isValidName(String name); String normalizeAccountName(String name, String prefix);  }
@Test(dataProvider = "namesToNormalize") public void testNormalizeAccountName(String input, String expected) throws Exception { doThrow(NotFoundException.class).when(accountManager).getByName(anyString()); Assert.assertEquals(accountValidator.normalizeAccountName(input, "account"), expected); }
@Test public void testNormalizeAccountNameWhenInputDoesNotContainAnyValidCharacter() throws Exception { doThrow(NotFoundException.class).when(accountManager).getByName(anyString()); Assert.assertTrue(accountValidator.normalizeAccountName("#", "name").startsWith("name")); }