Skip to content

Commit

Permalink
nit: re-arrange some code and remove unused constant
Browse files Browse the repository at this point in the history
  • Loading branch information
salvatore-campagna committed Nov 13, 2024
1 parent 04dea4d commit 0b9a3bd
Showing 1 changed file with 36 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,6 @@

public class TimeBasedUUIDGeneratorTests extends ESTestCase {

private void assertUUIDUniqueness(final UUIDGenerator generator, final int count) {
assertEquals(count, generateUUIDs(generator, count).size());
}

private Set<String> generateUUIDs(final UUIDGenerator generator, final int count) {
return IntStream.range(0, count).mapToObj(i -> generator.getBase64UUID()).collect(HashSet::new, Set::add, Set::addAll);
}

private void assertUUIDFormat(final UUIDGenerator generator, final int count) {
IntStream.range(0, count).forEach(i -> {
final String uuid = generator.getBase64UUID();
assertNotNull(uuid);
assertEquals(20, uuid.length());
assertFalse(uuid.contains("+"));
assertFalse(uuid.contains("/"));
assertFalse(uuid.contains("="));
});
}

private UUIDGenerator createGenerator(
final Supplier<Long> timestampSupplier,
final Supplier<Integer> sequenceIdSupplier,
final Supplier<byte[]> macAddressSupplier
) {
return new TimeBasedUUIDGenerator(timestampSupplier, sequenceIdSupplier, macAddressSupplier);
}

private UUIDGenerator createKOrderedGenerator(
final Supplier<Long> timestampSupplier,
final Supplier<Integer> sequenceIdSupplier,
final Supplier<byte[]> macAddressSupplier
) {
return new TimeBasedKOrderedUUIDGenerator(timestampSupplier, sequenceIdSupplier, macAddressSupplier);
}

public void testTimeBasedUUIDGeneration() {
assertUUIDFormat(createGenerator(() -> Instant.now().toEpochMilli(), () -> 0, new TestRandomMacAddressSupplier()), 100_000);
}
Expand Down Expand Up @@ -157,6 +122,41 @@ private void testUUIDEncodingDecodingHelper(final long timestamp, final int sequ
assertArrayEquals("MAC address does not match", macAddress, decoder.decodeMacAddress());
}

private void assertUUIDUniqueness(final UUIDGenerator generator, final int count) {
assertEquals(count, generateUUIDs(generator, count).size());
}

private Set<String> generateUUIDs(final UUIDGenerator generator, final int count) {
return IntStream.range(0, count).mapToObj(i -> generator.getBase64UUID()).collect(HashSet::new, Set::add, Set::addAll);
}

private void assertUUIDFormat(final UUIDGenerator generator, final int count) {
IntStream.range(0, count).forEach(i -> {
final String uuid = generator.getBase64UUID();
assertNotNull(uuid);
assertEquals(20, uuid.length());
assertFalse(uuid.contains("+"));
assertFalse(uuid.contains("/"));
assertFalse(uuid.contains("="));
});
}

private UUIDGenerator createGenerator(
final Supplier<Long> timestampSupplier,
final Supplier<Integer> sequenceIdSupplier,
final Supplier<byte[]> macAddressSupplier
) {
return new TimeBasedUUIDGenerator(timestampSupplier, sequenceIdSupplier, macAddressSupplier);
}

private UUIDGenerator createKOrderedGenerator(
final Supplier<Long> timestampSupplier,
final Supplier<Integer> sequenceIdSupplier,
final Supplier<byte[]> macAddressSupplier
) {
return new TimeBasedKOrderedUUIDGenerator(timestampSupplier, sequenceIdSupplier, macAddressSupplier);
}

private static class TestRandomMacAddressSupplier implements Supplier<byte[]> {
private final byte[] macAddress = new byte[] { randomByte(), randomByte(), randomByte(), randomByte(), randomByte(), randomByte() };

Expand Down Expand Up @@ -223,7 +223,6 @@ public Long get() {
* A utility class to decode the K-ordered UUID extracting the original timestamp, MAC address and sequence ID.
*/
private static class TestTimeBasedKOrderedUUIDDecoder {
private static final Base64.Decoder UUID_URL_DECODER = Base64.getUrlDecoder();

private final byte[] decodedBytes;

Expand All @@ -233,7 +232,7 @@ private static class TestTimeBasedKOrderedUUIDDecoder {
* @param base64UUID The base64-encoded UUID string to decode.
*/
TestTimeBasedKOrderedUUIDDecoder(final String base64UUID) {
this.decodedBytes = UUID_URL_DECODER.decode(base64UUID);
this.decodedBytes = Base64.getUrlDecoder().decode(base64UUID);
}

/**
Expand Down

0 comments on commit 0b9a3bd

Please sign in to comment.