Skip to content
Permalink
Browse files

Adapted the namespace regex and prohibit the use of the empty namespace.

Add more specific exception for invalid persistence ids.
Add unit tests.

Signed-off-by: Stefan Maute <stefan.maute@bosch-si.com>
  • Loading branch information
stmaute committed May 8, 2019
1 parent a5f71e5 commit 5136470dd7928a90c803b63e9d41fb91802984a5
@@ -35,7 +35,7 @@
/**
* The regex pattern for a Namespace.
*/
String NAMESPACE_REGEX = "(?<ns>|(?:(?:[a-zA-Z]\\w*+)(?:\\.[a-zA-Z]\\w*+)*+))";
String NAMESPACE_REGEX = "(?<ns>(?:(?:[a-zA-Z]\\w*+)(?:\\.[a-zA-Z]\\w*+)*+))";

/**
* The regex pattern for an Entity Name. Has to be conform to
@@ -63,10 +63,8 @@ public String getSuffixFromPersistenceId(final String persistenceId) {

final String[] persistenceIdSplitByColons = persistenceId.split(":");

if (persistenceIdSplitByColons.length < 3) {
throw new IllegalStateException(
String.format("Persistence id <%s> is not in the expected format of <prefix:namespace:name>",
persistenceId));
if (persistenceIdSplitByColons.length < 2) {
throw new PersistenceIdInvalidException(persistenceId);
}

final String prefix = persistenceIdSplitByColons[0];
@@ -106,4 +104,13 @@ static String doValidateMongoCharacters(final String input) {
}
return escaped;
}

public static final class PersistenceIdInvalidException extends RuntimeException {

private static final long serialVersionUID = -4789912839628096316L;

private PersistenceIdInvalidException(final String persistenceId){
super(String.format("Persistence id <%s> is not in the expected format of <prefix:namespace:name>", persistenceId));
}
}
}
@@ -26,7 +26,7 @@
@Before
public void setup() {
NamespaceSuffixCollectionNames.resetConfig();
this.sut = new NamespaceSuffixCollectionNames();
sut = new NamespaceSuffixCollectionNames();
}

@Test
@@ -91,4 +91,23 @@ public void validateMongoCharactersThrowsNullpointerExceptionWithoutConfig() {

sut.getSuffixFromPersistenceId(persistenceId);
}

@Test(expected = NamespaceSuffixCollectionNames.PersistenceIdInvalidException.class)
public void getSuffixFromPersistenceIdThrowsIllegalStateException() {
NamespaceSuffixCollectionNames.setConfig(new SuffixBuilderConfig(Collections.singletonList("thing")));
final String persistenceId = "thing:::::::";

sut.getSuffixFromPersistenceId(persistenceId);
}

@Test
public void getSuffixFromPersistenceIdWithNamespaceAndOnlyColons() {
NamespaceSuffixCollectionNames.setConfig(new SuffixBuilderConfig(Collections.singletonList("thing")));
final String persistenceId = "thing:org.eclipse.ditto:::::::";

final String suffix = sut.getSuffixFromPersistenceId(persistenceId);

final String expectedSuffix = "org.eclipse.ditto";
assertThat(suffix).isEqualTo(expectedSuffix);
}
}

0 comments on commit 5136470

Please sign in to comment.
You can’t perform that action at this time.