Skip to content

Commit

Permalink
Adapted the namespace regex and prohibit the use of the empty namespace.
Browse files Browse the repository at this point in the history
Add more specific exception for invalid persistence ids.
Add unit tests.

Signed-off-by: Stefan Maute <stefan.maute@bosch-si.com>
  • Loading branch information
Stefan Maute committed May 8, 2019
1 parent a5f71e5 commit 5136470
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Expand Up @@ -35,7 +35,7 @@ public interface Entity<T extends Revision<T>> extends Jsonifiable.WithFieldSele
/**
* 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
Expand Down
Expand Up @@ -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];
Expand Down Expand Up @@ -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));
}
}
}
Expand Up @@ -26,7 +26,7 @@ public class NamespaceSuffixCollectionNamesTest {
@Before
public void setup() {
NamespaceSuffixCollectionNames.resetConfig();
this.sut = new NamespaceSuffixCollectionNames();
sut = new NamespaceSuffixCollectionNames();
}

@Test
Expand Down Expand Up @@ -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.