UserUtil { public static String generateUniqueString(int length) { int totalChars = alphabet.length; BigDecimal exponent = BigDecimal.valueOf(totalChars); exponent = exponent.pow(length); String code = ""; BigDecimal number = new BigDecimal(rand.nextInt(1000000)); BigDecimal num = number.multiply(largePrimeNumber).remainder(exponent); code = baseN(num, totalChars); int codeLenght = code.length(); if (codeLenght < length) { for (int i = codeLenght; i < length; i++) { code = code + alphabet[rand.nextInt(totalChars - 1)]; } } if (NumberUtils.isNumber(code.substring(1, 2)) || NumberUtils.isNumber(code.substring(2, 3))) { return code; } else { code = code.substring(0, 1) + alphabet[rand.nextInt(9)] + code.substring(2); return code; } } private  UserUtil(); @SuppressWarnings("unchecked") static void checkPhoneUniqueness(User user, String opType); @SuppressWarnings("unchecked") static void checkPhoneUniqueness(String phone); static boolean identifierExists(String type, String value); static void checkEmailUniqueness(String email); static Map<String, Object> validateExternalIdsAndReturnActiveUser(
      Map<String, Object> userMap); @SuppressWarnings("unchecked") static Map<String, Object> getUserFromExternalId(Map<String, Object> userMap); static String getUserIdFromExternalId(Map<String, Object> userMap); @SuppressWarnings("unchecked") static void checkEmailUniqueness(User user, String opType); static void validateUserPhoneEmailAndWebPages(User user, String operationType); static String getEncryptedData(String value); static List<Map<String, String>> copyAndConvertExternalIdsToLower(
      List<Map<String, String>> externalIds); static void storeOriginalExternalIdsValue(List<Map<String, String>> externalIds); static List<Map<String, String>> convertExternalIdsValueToLowerCase(
      List<Map<String, String>> externalIds); @SuppressWarnings("unchecked") static void checkExternalIdUniqueness(User user, String operation); static String encryptData(String value); static boolean updatePassword(Map<String, Object> userMap); static void addMaskEmailAndPhone(Map<String, Object> userMap); static void addMaskEmailAndMaskPhone(Map<String, Object> userMap); @SuppressWarnings("unchecked") static Map<String, Object> encryptUserData(Map<String, Object> userMap); static Map<String, Object> checkProfileCompleteness(Map<String, Object> userMap); static void setUserDefaultValueForV3(Map<String, Object> userMap); static String generateUniqueString(int length); static void setUserDefaultValue(Map<String, Object> userMap, String callerId); static void validateExternalIds(User user, String operationType); static void checkEmailSameOrDiff(
      Map<String, Object> userRequestMap, Map<String, Object> userDbRecord); static void validateUserExternalIds(User user); static List<Map<String, Object>> getActiveUserOrgDetails(String userId); static List<Map<String, Object>> getAllUserOrgDetails(String userId); static void toLower(Map<String, Object> userMap);  }
@Test public void generateUniqueStringSuccess() { String val = UserUtil.generateUniqueString(4); assertTrue(val.length() == 4); }
@Test public void generateUniqueStringSecondCharCheck() { String val = UserUtil.generateUniqueString(5); assertTrue(val.length() == 5); assertTrue( NumberUtils.isNumber(val.substring(1, 2)) || NumberUtils.isNumber(val.substring(2, 3))); }