Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class IdGeneratorBase {
private final List<IdValidationConstraint> globalConstraints = new ArrayList<>();
private final Map<String, Domain> registeredDomains = new ConcurrentHashMap<>(Map.of(Domain.DEFAULT_DOMAIN_NAME, Domain.DEFAULT));
private final FailsafeExecutor<GenerationResult> retryer;
private final String ID_REGEX = "^[a-zA-Z]+$";

protected final IdFormatter idFormatter;
protected final NonceGenerator nonceGenerator;
Expand Down Expand Up @@ -84,6 +85,7 @@ public final synchronized void registerDomainSpecificConstraints(
}

public final Id getIdFromIdInfo(final NonceInfo nonceInfo, final String namespace, final IdFormatter idFormatter) {
validateIdRegex(namespace);
val dateTime = new DateTime(nonceInfo.getTime());
val id = String.format("%s%s", namespace, idFormatter.format(dateTime, getNodeId(), nonceInfo.getExponent()));
return Id.builder()
Expand Down Expand Up @@ -192,4 +194,13 @@ public final void setNodeId(int nodeId) {
}
this.nodeId = nodeId;
}

private void validateIdRegex(final String namespace) {
Preconditions.checkArgument(
namespace != null && !namespace.isEmpty(),
"Namespace cannot be null or empty");
Preconditions.checkArgument(
namespace.matches(ID_REGEX),
"Prefix does not match the required regex: " + ID_REGEX);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,39 @@ void testParseSuccessAfterGeneration() {
Assertions.assertEquals(parsedId.getGeneratedDate(), generatedId.getGeneratedDate());
}

@Test
void testGenerateWithNumericalPrefix() {
val prefix = "10";
val exception = Assertions.assertThrows(IllegalArgumentException.class, () -> IdGenerator.generate(prefix));
Assertions.assertEquals("Prefix does not match the required regex: ^[a-zA-Z]+$", exception.getMessage());
}

@Test
void testGenerateWithEmptyPrefix() {
val prefix = "";
val exception = Assertions.assertThrows(IllegalArgumentException.class, () -> IdGenerator.generate(prefix));
Assertions.assertEquals("Namespace cannot be null or empty", exception.getMessage());
}

@Test
void testGenerateWithConstraintsWithNumericalPrefix() {
val prefix = "10";
val domain = "TEST";
IdGenerator.registerDomainSpecificConstraints(domain, Collections.singletonList(id -> true));
val exception = Assertions.assertThrows(IllegalArgumentException.class,
() -> IdGenerator.generateWithConstraints(prefix, domain));
Assertions.assertEquals("Prefix does not match the required regex: ^[a-zA-Z]+$", exception.getMessage());
}

@Test
void testGenerateWithConstraintsWithEmptyPrefix() {
val prefix = "";
val domain = "TEST";
IdGenerator.registerDomainSpecificConstraints(domain, Collections.singletonList(id -> true));
val exception = Assertions.assertThrows(IllegalArgumentException.class,
() -> IdGenerator.generateWithConstraints(prefix, domain));
Assertions.assertEquals("Namespace cannot be null or empty", exception.getMessage());
}

@SuppressWarnings("SameParameterValue")
private Date generateDate(int year, int month, int day, int hour, int min, int sec, int ms, ZoneId zoneId) {
Expand Down