diff options
Diffstat (limited to 'workflow-designer-be/src/main')
6 files changed, 65 insertions, 54 deletions
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java index 1f0c4fec..e7c9f637 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java @@ -21,10 +21,11 @@ import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import javax.validation.Valid; import org.onap.sdc.workflow.api.types.CollectionResponse; import org.onap.sdc.workflow.api.types.VersionStateDto; import org.onap.sdc.workflow.api.types.VersionStatesFormatter; -import org.onap.sdc.workflow.api.types.WorkflowVersionValidator; import org.onap.sdc.workflow.persistence.types.ArtifactEntity; import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.onap.sdc.workflow.services.WorkflowVersionManager; @@ -55,32 +56,28 @@ import springfox.documentation.annotations.ApiIgnore; public class WorkflowVersionController { private final WorkflowVersionManager workflowVersionManager; - private final WorkflowVersionValidator versionValidator; @Autowired - public WorkflowVersionController(@Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager, - @Qualifier("WorkflowVersionValidator") WorkflowVersionValidator versionValidator) { + public WorkflowVersionController( + @Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager) { this.workflowVersionManager = workflowVersionManager; - this.versionValidator = versionValidator; } - @ApiImplicitParam(name = "state", dataType = "string", paramType = "query", - allowableValues = "DRAFT,CERTIFIED", value = "Filter by state") + @ApiImplicitParam(name = "state", dataType = "string", paramType = "query", allowableValues = "DRAFT,CERTIFIED", + value = "Filter by state") @GetMapping @ApiOperation("List workflow versions") public CollectionResponse<WorkflowVersion> list(@PathVariable("workflowId") String workflowId, - @ApiIgnore VersionStatesFormatter stateFilter, - @RequestHeader(USER_ID_HEADER) String user) { + @ApiIgnore VersionStatesFormatter stateFilter, @RequestHeader(USER_ID_HEADER) String user) { return new CollectionResponse<>(workflowVersionManager.list(workflowId, stateFilter.getVersionStates())); } @PostMapping @ApiOperation("Create workflow version") - public ResponseEntity<WorkflowVersion> create(@RequestBody WorkflowVersion version, + public ResponseEntity<WorkflowVersion> create(@RequestBody @Valid WorkflowVersion version, @PathVariable("workflowId") String workflowId, @RequestParam(value = "baseVersionId", required = false) String baseVersionId, @RequestHeader(USER_ID_HEADER) String user) { - versionValidator.validate(workflowId,version); WorkflowVersion createdVersion = workflowVersionManager.create(workflowId, baseVersionId, version); return new ResponseEntity<>(createdVersion, HttpStatus.CREATED); } @@ -94,9 +91,8 @@ public class WorkflowVersionController { @PutMapping("/{versionId}") @ApiOperation("Update workflow version") - public void update(@RequestBody WorkflowVersion version, @PathVariable("workflowId") String workflowId, + public void update(@RequestBody @Valid WorkflowVersion version, @PathVariable("workflowId") String workflowId, @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER) String user) { - versionValidator.validate(workflowId,version); version.setId(versionId); workflowVersionManager.update(workflowId, version); } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java index e8622905..02d68fd6 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java @@ -28,7 +28,7 @@ import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException; import org.onap.sdc.workflow.services.exceptions.VersionCreationException; import org.onap.sdc.workflow.services.exceptions.VersionModificationException; import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException; -import org.onap.sdc.workflow.services.exceptions.VersionValidationException; +import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -37,6 +37,7 @@ 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; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; @@ -62,15 +63,18 @@ public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExce return new ResponseEntity<>(exception.getMessage(), BAD_REQUEST); } - //For workflowVersionValidator exception @Override - protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException e, + protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException exception, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { - FieldError result = e.getBindingResult().getFieldError(); - return new ResponseEntity<>(result.getDefaultMessage(), BAD_REQUEST); + String errorMsg = exception.getBindingResult().getFieldErrors().stream() + .map(DefaultMessageSourceResolvable::getDefaultMessage) + .findFirst() + .orElse(exception.getMessage()); + + return new ResponseEntity<>(errorMsg, BAD_REQUEST); } //For missing header exceptions @@ -83,7 +87,7 @@ public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExce @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class, - VersionStateModificationException.class, VersionValidationException.class}) + VersionStateModificationException.class}) public final ResponseEntity<String> handleInvalidArtifactException( Exception exception) { return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY); diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidator.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidator.java deleted file mode 100644 index ef5e06c9..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidator.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.onap.sdc.workflow.api.types; - -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.onap.sdc.workflow.services.exceptions.VersionValidationException; -import org.springframework.stereotype.Component; - -@Component("WorkflowVersionValidator") -public class WorkflowVersionValidator { - - public void validate(String workflowId, WorkflowVersion workflowVersion) { - - if(containsDuplicates( workflowVersion.getInputs())){ - throw new VersionValidationException(workflowId,"Input name must be unique"); - - } - - if(containsDuplicates(workflowVersion.getOutputs())){ - throw new VersionValidationException(workflowId ,"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())); - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/NoDuplicates.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/NoDuplicates.java new file mode 100644 index 00000000..ca4932ac --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/NoDuplicates.java @@ -0,0 +1,21 @@ +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 = {NoDuplicatesValidator.class}) +public @interface NoDuplicates { + String message(); + + Class<?>[] groups() default {}; + + Class<? extends Payload>[] payload() default {}; +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/NoDuplicatesValidator.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/NoDuplicatesValidator.java new file mode 100644 index 00000000..13bbf0e9 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validation/NoDuplicatesValidator.java @@ -0,0 +1,21 @@ +package org.onap.sdc.workflow.api.validation; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import org.onap.sdc.workflow.persistence.types.ParameterEntity; + +public class NoDuplicatesValidator implements ConstraintValidator<NoDuplicates, Collection<ParameterEntity>> { + + @Override + public boolean isValid(Collection<ParameterEntity> parameterEntities, ConstraintValidatorContext context) { + if (Objects.isNull(parameterEntities) || parameterEntities.size() < 2) { + return true; + } + Set<String> testSet = new HashSet<>(); + return !parameterEntities.stream().anyMatch(s -> !testSet.add(s.getName())); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java index e3bbd646..b19f4a98 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.Date; import javax.validation.Valid; import lombok.Data; +import org.onap.sdc.workflow.api.validation.NoDuplicates; @Data @@ -31,11 +32,13 @@ public class WorkflowVersion { private String description; private String baseId; private WorkflowVersionState state; - private boolean hasArtifact; @Valid + @NoDuplicates(message = "Inputs names must be unique") private Collection<ParameterEntity> inputs = Collections.emptyList(); @Valid + @NoDuplicates(message = "Outputs names must be unique") private Collection<ParameterEntity> outputs = Collections.emptyList(); + private boolean hasArtifact; private Date creationTime; private Date modificationTime; |