RandomGenerator { public String generateUniqueId(String prefix) { return String.format("%s-%013d-%s", prefix, time.get().toEpochMilli(), generateNumber()); } RandomGenerator(Time time); String generateNumber(); String generateUniqueId(String prefix); static final RandomGenerator DEFAULT; final Time time; }
@Test public void shouldGenerateRandomId() { String prefix = "test"; String id = randomGenerator.generateUniqueId(prefix); assertThat(id, startsWith(prefix + "-" + time + "-")); }
@Test public void shouldGenerateDifferentIds() { String prefix = "test"; String id1 = randomGenerator.generateUniqueId(prefix); time = 1471616832630L; String id2 = randomGenerator.generateUniqueId(prefix); assertNotEquals(id1, id2); }