diff options
author | ayalaben <ayala.benzvi@amdocs.com> | 2018-09-20 16:18:35 +0300 |
---|---|---|
committer | ayalaben <ayala.benzvi@amdocs.com> | 2018-09-27 10:46:30 +0300 |
commit | 73e9794f9996a5f633c7d8597d3bde659b687ed4 (patch) | |
tree | cb4470b9989ffc465b969dfa7f8c37a3e4950aac /workflow-designer-be/src/main | |
parent | 1369104088bddbb370441edb3223ed99619e1d77 (diff) |
Configurable name validation
Change-Id: Ifc4a25de21e16ae1e93287554e46cfeaef3c91c3
Issue-ID: SDC-1768
Signed-off-by: ayalaben <ayala.benzvi@amdocs.com>
Diffstat (limited to 'workflow-designer-be/src/main')
4 files changed, 108 insertions, 0 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; |