diff options
author | Oren Kleks <orenkle@amdocs.com> | 2018-10-02 07:40:13 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-10-02 07:40:13 +0000 |
commit | 1c758acea9ce43fee28b0f8dcffa1234358b42c8 (patch) | |
tree | 00aa6918e397c508d717484a05639b246211d3a1 /workflow-designer-be/src/test/java | |
parent | ced52387d2d333d860dbee2fa1ee0a7a0108a960 (diff) | |
parent | 73e9794f9996a5f633c7d8597d3bde659b687ed4 (diff) |
Merge "Configurable name validation"
Diffstat (limited to 'workflow-designer-be/src/test/java')
-rw-r--r-- | workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/ActivitySpecParameterNameValidatorTest.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/ActivitySpecParameterNameValidatorTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/ActivitySpecParameterNameValidatorTest.java new file mode 100644 index 00000000..a62b6ac5 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/validation/ActivitySpecParameterNameValidatorTest.java @@ -0,0 +1,112 @@ +package org.onap.sdc.workflow.api.validation; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import javax.validation.ConstraintValidatorContext; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.sdc.workflow.persistence.types.ActivitySpecParameter; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class ActivitySpecParameterNameValidatorTest { + + class AnnotationWrapper { + + @ValidName(message = "test message") + public ActivitySpecParameter parameter; + } + + private String noSpacesMessage = "Input and output names must not contain any spaces"; + private String matchPatternMessage = "Input and output names must match the validation pattern"; + + @Mock + private ConstraintValidatorContext context; + @Mock + private ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder; + @Mock + private ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext nodeBuilderCustomizableContext; + + private ActivitySpecParameterNameValidator validator; + + + @Before + public void setup() throws NoSuchFieldException { + MockitoAnnotations.initMocks(this); + when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(constraintViolationBuilder); + when(constraintViolationBuilder.addPropertyNode(anyString())).thenReturn(nodeBuilderCustomizableContext); + validator = initializeValidator(ActivitySpecParameterNameValidatorTest.AnnotationWrapper.class); + } + + @Test + public void shouldPassIfNoSpaces() { + validator.setValidationRegex("^\\S*$"); + assertTrue(validator.isValid(createParameter("validName"), context)); + } + + @Test + public void shouldFailIfNameHasSpaces() { + validator.setValidationRegex("^\\S*$"); + validator.setValidationMessage(noSpacesMessage); + assertFalse(validator.isValid(createParameter("not a valid name"), context)); + verify(context).disableDefaultConstraintViolation(); + verify(context).buildConstraintViolationWithTemplate(noSpacesMessage); + } + + + @Test + public void shouldFailIfNameHasSpacesInStart() { + validator.setValidationRegex("^\\S*$"); + validator.setValidationMessage(noSpacesMessage); + assertFalse(validator.isValid(createParameter(" name"), context)); + verify(context).disableDefaultConstraintViolation(); + verify(context).buildConstraintViolationWithTemplate(noSpacesMessage); + } + + @Test + public void shouldFailIfNameHasSpacesInEnd() { + validator.setValidationRegex("^\\S*$"); + validator.setValidationMessage(noSpacesMessage); + assertFalse(validator.isValid(createParameter("name "), context)); + verify(context).disableDefaultConstraintViolation(); + verify(context).buildConstraintViolationWithTemplate(noSpacesMessage); + } + + @Test + public void shouldFailIfDoesNotMatchRegex() { + validator.setValidationRegex("^[a-zA-Z0-9-]*$"); + validator.setValidationMessage(matchPatternMessage); + assertFalse(validator.isValid(createParameter("NotValid$$##"), context)); + verify(context).disableDefaultConstraintViolation(); + verify(context).buildConstraintViolationWithTemplate(matchPatternMessage); + } + + @Test + public void shouldPassIfMatchRegex() { + validator.setValidationRegex("^[a-zA-Z0-9-]*$"); + assertTrue(validator.isValid(createParameter("validName"), context)); + } + + private ActivitySpecParameterNameValidator initializeValidator(Class<?> classWithAnnotation) + throws NoSuchFieldException { + ValidName constraint = classWithAnnotation.getField("parameter").getAnnotation(ValidName.class); + ActivitySpecParameterNameValidator validator = new ActivitySpecParameterNameValidator(); + validator.initialize(constraint); + return validator; + } + + private ActivitySpecParameter createParameter(String name) { + ActivitySpecParameter parameter = new ActivitySpecParameter(); + parameter.setName(name); + parameter.setValue("value"); + parameter.setType("type"); + return parameter; + } +} |