diff options
Diffstat (limited to 'workflow-designer-be')
6 files changed, 227 insertions, 1 deletions
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/activityspec/ActivitySpecRequest.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/activityspec/ActivitySpecRequest.java index fd29fdf3..972e99cc 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/activityspec/ActivitySpecRequest.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/activityspec/ActivitySpecRequest.java @@ -18,6 +18,7 @@ package org.onap.sdc.workflow.api.types.activityspec; import io.swagger.annotations.ApiModel; import java.util.List; +import javax.validation.Valid; import lombok.EqualsAndHashCode; import org.onap.sdc.workflow.persistence.types.ActivitySpecParameter; @@ -27,7 +28,9 @@ import org.onap.sdc.workflow.persistence.types.ActivitySpecParameter; public class ActivitySpecRequest extends ActivitySpecBase { private String description; + @Valid private List<ActivitySpecParameter> inputs; + @Valid private List<ActivitySpecParameter> outputs; private String type; private String content; diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/ActivitySpecParameterNameValidator.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/ActivitySpecParameterNameValidator.java new file mode 100644 index 00000000..06818c9d --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/ActivitySpecParameterNameValidator.java @@ -0,0 +1,65 @@ +/* + * Copyright © 2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.workflow.api.validation; + +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import org.onap.sdc.workflow.persistence.types.ActivitySpecParameter; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component("ActivitySpecParameterNameValidator") +public class ActivitySpecParameterNameValidator implements ConstraintValidator<ValidName, ActivitySpecParameter> { + + private String validationRegex; + private static final String defaultValidationRegex = "^\\S*$"; + private String validationMessage; + private static final String defaultValidationMessage = "Input and Output names must not contain any spaces"; + + @Override + public boolean isValid(ActivitySpecParameter parameter, ConstraintValidatorContext context) { + Pattern pattern = Pattern.compile(validationRegex); + Matcher matcher = pattern.matcher(parameter.getName()); + boolean isValid = matcher.find(); + if (!isValid) { + context.disableDefaultConstraintViolation(); + context.buildConstraintViolationWithTemplate(validationMessage).addConstraintViolation(); + } + return isValid; + } + + @Value("${activitySpec.parameterName.validation:" + defaultValidationRegex + "}") + void setValidationRegex(String regex) { + if (Objects.isNull(regex) || regex.equals("")) { + Objects.requireNonNull(regex); + } else { + validationRegex = regex; + } + } + + @Value("${activitySpec.parameterName.validationMessage:" + defaultValidationMessage + "}") + void setValidationMessage(String message) { + if (Objects.isNull(message) || message.equals("")) { + Objects.requireNonNull(message); + } else { + validationMessage = message; + } + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/ValidName.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/ValidName.java new file mode 100644 index 00000000..2d90aeba --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/ValidName.java @@ -0,0 +1,38 @@ +/* + * Copyright © 2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.sdc.workflow.api.validation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import javax.validation.Constraint; +import javax.validation.Payload; + +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Constraint(validatedBy = {ActivitySpecParameterNameValidator.class}) +public @interface ValidName { + + String message() default ""; + + Class<?>[] groups() default {}; + + Class<? extends Payload>[] payload() default {}; +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ActivitySpecParameter.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ActivitySpecParameter.java index 3f2562e3..ec1c2681 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ActivitySpecParameter.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ActivitySpecParameter.java @@ -19,10 +19,12 @@ package org.onap.sdc.workflow.persistence.types; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import org.onap.sdc.workflow.api.validation.ValidName; @Data @NoArgsConstructor @AllArgsConstructor +@ValidName public class ActivitySpecParameter { private String name; 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; + } +} diff --git a/workflow-designer-be/src/test/resources/application-test.properties b/workflow-designer-be/src/test/resources/application-test.properties index 4d3e874b..686013f0 100644 --- a/workflow-designer-be/src/test/resources/application-test.properties +++ b/workflow-designer-be/src/test/resources/application-test.properties @@ -1,4 +1,10 @@ sdc.be.protocol=${SDC_PROTOCOL:http} sdc.be.endpoint=${SDC_ENDPOINT:localhost:8080} sdc.be.external.user=${SDC_USER:cs0008} -sdc.be.external.password=${SDC_PASSWORD:Aa123456}
\ No newline at end of file +sdc.be.external.password=${SDC_PASSWORD:Aa123456} + +#CASSANDRA +spring.data.cassandra.contact-points=${CS_HOSTS:localhost} +spring.data.cassandra.keyspace-name=workflow +spring.data.cassandra.port=${CS_PORT:9042} +zusammen.cassandra.isAuthenticate=${CS_AUTHENTICATE:false}
\ No newline at end of file |