diff options
Diffstat (limited to 'cps-service')
-rw-r--r-- | cps-service/src/main/java/org/onap/cps/utils/CpsValidator.java | 11 | ||||
-rw-r--r-- | cps-service/src/test/groovy/org/onap/cps/utils/CpsValidatorSpec.groovy | 14 |
2 files changed, 25 insertions, 0 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/utils/CpsValidator.java b/cps-service/src/main/java/org/onap/cps/utils/CpsValidator.java index dd16495638..28b49c9666 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/CpsValidator.java +++ b/cps-service/src/main/java/org/onap/cps/utils/CpsValidator.java @@ -22,6 +22,7 @@ package org.onap.cps.utils; import com.google.common.collect.Lists; import java.util.Collection; +import java.util.regex.Pattern; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -32,6 +33,8 @@ import org.onap.cps.spi.exceptions.DataValidationException; public final class CpsValidator { private static final char[] UNSUPPORTED_NAME_CHARACTERS = "!\" #$%&'()*+,./\\:;<=>?@[]^`{|}~".toCharArray(); + private static final Pattern TOPIC_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9]([._-](?![._-])|" + + "[a-zA-Z0-9]){0,120}[a-zA-Z0-9]$"); /** * Validate characters in names within cps. @@ -48,4 +51,12 @@ public final class CpsValidator { } } } + + /** + * Validate kafka topic name pattern. + * @param topicName name of the topic to be validated + */ + public static boolean validateTopicName(final String topicName) { + return topicName != null && TOPIC_NAME_PATTERN.matcher(topicName).matches(); + } } diff --git a/cps-service/src/test/groovy/org/onap/cps/utils/CpsValidatorSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/utils/CpsValidatorSpec.groovy index 191472ceea..ce728ef1c1 100644 --- a/cps-service/src/test/groovy/org/onap/cps/utils/CpsValidatorSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/utils/CpsValidatorSpec.groovy @@ -45,4 +45,18 @@ class CpsValidatorSpec extends Specification { 'position 5' | 'name with spaces' || 'name with spaces invalid token encountered at position 5' 'position 9' | 'nameWith Space' || 'nameWith Space invalid token encountered at position 9' } + + def 'Validating topic names.'() { + when: 'the topic name is validated' + def isValidTopicName = CpsValidator.validateTopicName(topicName) + then: 'boolean response will be returned for #scenario' + assert isValidTopicName == booleanResponse + where: 'the following names are used' + scenario | topicName || booleanResponse + 'valid topic' | 'my-topic-name' || true + 'empty topic' | '' || false + 'blank topic' | ' ' || false + 'null topic' | null || false + 'invalid non empty topic' | '1_5_*_#' || false + } } |