Workflow Version Validation 77/57277/2
authorayalaben <ayala.benzvi@amdocs.com>
Tue, 24 Jul 2018 09:16:38 +0000 (12:16 +0300)
committerayalaben <ayala.benzvi@amdocs.com>
Tue, 24 Jul 2018 13:25:25 +0000 (16:25 +0300)
Change-Id: Ibb72aeed314f39f4c3c29f270a3fe5982d399ea5
Issue-ID: SDC-1518
Signed-off-by: ayalaben <ayala.benzvi@amdocs.com>
workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/SpringBootWebApplication.java
workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java
workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java
workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validator/WorkflowVersionValidator.java [new file with mode: 0644]
workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ParameterEntity.java
workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/Workflow.java

index d5b94a1..91995bd 100644 (file)
@@ -35,8 +35,12 @@ import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
+import org.springframework.validation.Validator;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.WebDataBinder;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.InitBinder;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
@@ -53,11 +57,19 @@ import org.springframework.web.multipart.MultipartFile;
 public class WorkflowVersionController {
 
     private final WorkflowVersionManager workflowVersionManager;
+    private Validator validator;
+
+    @InitBinder
+    private void initBinder(WebDataBinder binder) {
+        binder.addValidators(validator);
+    }
 
     @Autowired
     public WorkflowVersionController(
-            @Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager) {
+            @Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager,
+            @Qualifier("workflowVersionValidator") Validator validator ) {
         this.workflowVersionManager = workflowVersionManager;
+        this.validator = validator;
     }
 
     @GetMapping
@@ -71,7 +83,7 @@ public class WorkflowVersionController {
 
     @PostMapping
     @ApiOperation("Create workflow version")
-    public ResponseEntity<WorkflowVersion> create(@RequestBody WorkflowVersion version,
+    public ResponseEntity<WorkflowVersion> create(@RequestBody @Validated WorkflowVersion version,
             @PathVariable("workflowId") String workflowId,
             @RequestParam(value = "baseVersionId", required = false) String baseVersionId,
             @RequestHeader(USER_ID_HEADER_PARAM) String user) {
@@ -88,7 +100,7 @@ public class WorkflowVersionController {
 
     @PutMapping("/{versionId}")
     @ApiOperation("Update workflow version")
-    public void update(@RequestBody WorkflowVersion version, @PathVariable("workflowId") String workflowId,
+    public void update(@RequestBody @Validated WorkflowVersion version, @PathVariable("workflowId") String workflowId,
             @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) {
         version.setId(versionId);
         workflowVersionManager.update(workflowId, version);
index c8cae36..ca6111d 100644 (file)
@@ -31,6 +31,8 @@ import org.onap.sdc.workflow.services.exceptions.VersionStateModificationExcepti
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
+import org.springframework.validation.FieldError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.ServletRequestBindingException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -59,6 +61,17 @@ public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExce
         return new ResponseEntity<>(exception.getMessage(), BAD_REQUEST);
     }
 
+    //For workflowVersionValidator exception
+    @Override
+    protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException e,
+            final HttpHeaders headers,
+            final HttpStatus status,
+            final WebRequest request) {
+
+        FieldError result = e.getBindingResult().getFieldError();
+       return new ResponseEntity<>(result.getDefaultMessage(), BAD_REQUEST);
+    }
+
     //For missing header exceptions
     @Override
     public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
diff --git a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validator/WorkflowVersionValidator.java b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validator/WorkflowVersionValidator.java
new file mode 100644 (file)
index 0000000..0310104
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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.validator;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+import org.onap.sdc.workflow.persistence.types.ParameterEntity;
+import org.onap.sdc.workflow.persistence.types.WorkflowVersion;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.Errors;
+import org.springframework.validation.Validator;
+
+
+@Component("workflowVersionValidator")
+public class WorkflowVersionValidator implements Validator{
+
+    @Override
+    public boolean supports(Class<?> aClass) {
+        return WorkflowVersion.class.equals(aClass);
+    }
+
+    @Override
+    public void validate(Object o, Errors errors) {
+
+        WorkflowVersion workflowVersion = (WorkflowVersion) o;
+        Collection<ParameterEntity> inputs = workflowVersion.getInputs();
+        Collection<ParameterEntity> outputs = workflowVersion.getOutputs();
+
+        if(containsDuplicates(inputs)){
+            errors.rejectValue("inputs", "duplicateName", new Object[] {inputs}, "Input name must be unique");
+        }
+
+        if(containsDuplicates(outputs)){
+            errors.rejectValue("outputs", "duplicateName", new Object[] {outputs}, "Output name must be unique");
+        }
+    }
+
+    private boolean containsDuplicates(Collection<ParameterEntity> parameters){
+        if(Objects.isNull(parameters) ||  parameters.size() < 2 ) {
+            return false;
+        }
+        Set<String> testSet = new HashSet<>();
+      return parameters.stream().anyMatch(s -> !testSet.add(s.getName()));
+    }
+}
index 4b57ba7..7c957d8 100644 (file)
 
 package org.onap.sdc.workflow.persistence.types;
 
+import javax.validation.constraints.Pattern;
 import lombok.Data;
+import javax.validation.constraints.NotNull;
 
 @Data
 public class ParameterEntity {
 
     private String id;
+    @NotNull(message = "Parameter name may not be null")
+    @Pattern(regexp = "[A-Za-z0-9_]*", message = "The field must contain only letters, digits and underscores")
     private String name;
+    @NotNull
     private ParameterType type;
+    @NotNull
     private boolean mandatory;
 }
index 31f949f..72e6277 100644 (file)
@@ -20,13 +20,18 @@ package org.onap.sdc.workflow.persistence.types;
 import java.util.Collection;
 import java.util.Set;
 import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
 import lombok.Data;
 
+
 @Data
 public class Workflow {
 
     private String id;
-    @NotNull
+    @NotNull(message = "Workflow name may not be null")
+    @Size(min = 6, max = 30, message = "The field must be at least 6 characters, and less than 30 characters")
+    @Pattern(regexp = "[A-Za-z0-9_]*", message = "The field must contain only letters, digits and underscores")
     private String name;
     private String description;
     private Set<WorkflowVersionState> versionStates;