SHAUtil { public static String buildUniqueLink(String accountToken, String endUserEmail, long date, String randomString) { String unixTimestamp = String.valueOf(date); String randomText = randomString; String text = accountToken + endUserEmail + unixTimestamp + randomText; MessageDigest mDigest = null; String uniqueLink = null; try { mDigest = MessageDigest.getInstance("SHA-256"); byte[] result = mDigest.digest(text.getBytes()); uniqueLink = new String(Hex.encodeHex(result)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return uniqueLink; } private  SHAUtil(); static String buildUniqueLink(String accountToken, String endUserEmail, long date, String randomString); static String randomString();  }
@Test public void whenBuildUniqueLinkHasAllParameters() throws Exception { endUser.setEmail("test@example.com"); String randomString = "16charrandstring"; String uniqueLink = SHAUtil.buildUniqueLink(user.getAccountToken(), endUser.getEmail(), 1234567890, randomString); assertThat(uniqueLink).isNotNull(); assertThat(uniqueLink.equals("1ed9f1c96018e2d577b3f864dc59dffe2baccc7103f6dcdadc40c3b6ec98cb0b")).isTrue(); }
@Test public void whenBuildUniqueLinkIsMissingEndUserEmail() throws Exception { String randomString = "16charrandstring"; String uniqueLink = SHAUtil.buildUniqueLink(user.getAccountToken(), endUser.getEmail(), 1234567890, randomString); assertThat(uniqueLink).isNotNull(); assertThat(uniqueLink.equals("1ed9f1c96018e2d577b3f864dc59dffe2baccc7103f6dcdadc40c3b6ec98cb0b")).isFalse(); }
@Test public void whenBuildUniqueLinkIsMissingRandomString() throws Exception { endUser.setEmail("test@example.com"); String randomString = ""; String uniqueLink = SHAUtil.buildUniqueLink(user.getAccountToken(), endUser.getEmail(), 1234567890, randomString); assertThat(uniqueLink).isNotNull(); assertThat(uniqueLink.equals("1ed9f1c96018e2d577b3f864dc59dffe2baccc7103f6dcdadc40c3b6ec98cb0b")).isFalse(); }