diff options
Diffstat (limited to 'workflow-designer-be/src')
7 files changed, 134 insertions, 78 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 df4c9fe3..1f0c4fec 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 @@ -24,6 +24,7 @@ import io.swagger.annotations.ApiOperation; 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; @@ -35,12 +36,8 @@ 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; @@ -58,18 +55,13 @@ import springfox.documentation.annotations.ApiIgnore; public class WorkflowVersionController { private final WorkflowVersionManager workflowVersionManager; - private Validator validator; - - @InitBinder("WorkflowVersion") - private void initBinder(WebDataBinder binder) { - binder.addValidators(validator); - } + private final WorkflowVersionValidator versionValidator; @Autowired public WorkflowVersionController(@Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager, - @Qualifier("workflowVersionValidator") Validator validator) { + @Qualifier("WorkflowVersionValidator") WorkflowVersionValidator versionValidator) { this.workflowVersionManager = workflowVersionManager; - this.validator = validator; + this.versionValidator = versionValidator; } @ApiImplicitParam(name = "state", dataType = "string", paramType = "query", @@ -84,10 +76,11 @@ public class WorkflowVersionController { @PostMapping @ApiOperation("Create workflow version") - public ResponseEntity<WorkflowVersion> create(@RequestBody @Validated WorkflowVersion version, + public ResponseEntity<WorkflowVersion> create(@RequestBody 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); } @@ -101,8 +94,9 @@ public class WorkflowVersionController { @PutMapping("/{versionId}") @ApiOperation("Update workflow version") - public void update(@RequestBody @Validated WorkflowVersion version, @PathVariable("workflowId") String workflowId, + public void update(@RequestBody 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 ca6111df..e8622905 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,6 +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.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -82,7 +83,7 @@ public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExce @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class, - VersionStateModificationException.class}) + VersionStateModificationException.class, VersionValidationException.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 new file mode 100644 index 00000000..ef5e06c9 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidator.java @@ -0,0 +1,34 @@ +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/validator/WorkflowVersionValidator.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validator/WorkflowVersionValidator.java deleted file mode 100644 index 03101042..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/validator/WorkflowVersionValidator.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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())); - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionValidationException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionValidationException.java new file mode 100644 index 00000000..bd7d61f2 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionValidationException.java @@ -0,0 +1,10 @@ +package org.onap.sdc.workflow.services.exceptions; + +public class VersionValidationException extends RuntimeException{ + + private static final String MSG = "Error creating or modifying version for workflow with id %s: %s"; + + public VersionValidationException(String workflowId, String detailedMessage) { + super(String.format(MSG, workflowId, detailedMessage)); + } +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java index a390ec75..ec27c4be 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java @@ -1,9 +1,10 @@ package org.onap.sdc.workflow.api; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -25,6 +26,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.sdc.workflow.RestPath; +import org.onap.sdc.workflow.api.types.WorkflowVersionValidator; import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.onap.sdc.workflow.services.WorkflowVersionManager; import org.openecomp.sdc.versioning.dao.types.Version; @@ -49,6 +51,9 @@ public class WorkflowVersionControllerTest { @Mock private WorkflowVersionManager workflowVersionManagerMock; + @Mock + private WorkflowVersionValidator versionValidator; + @InjectMocks private WorkflowVersionController workflowVersionController; @@ -77,6 +82,7 @@ public class WorkflowVersionControllerTest { WorkflowVersion version = new WorkflowVersion(); version.setDescription("VersionDescription"); + doNothing().when(versionValidator).validate(eq(ITEM1_ID),any(WorkflowVersion.class)); mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID) .contentType(APPLICATION_JSON) .content(GSON.toJson(version))) @@ -101,6 +107,7 @@ public class WorkflowVersionControllerTest { public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception { WorkflowVersion version = new WorkflowVersion(); version.setDescription("Updated"); + doNothing().when(versionValidator).validate(eq(ITEM1_ID),any(WorkflowVersion.class)); MockHttpServletResponse result = mockMvc.perform( put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID) diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidatorTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidatorTest.java new file mode 100644 index 00000000..9ea007ce --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/types/WorkflowVersionValidatorTest.java @@ -0,0 +1,71 @@ +/* + * 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.types; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.fail; + +import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +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.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class WorkflowVersionValidatorTest { + + private static final String ITEM1_ID = "item_id_1"; + + @InjectMocks + private WorkflowVersionValidator versionValidator; + + @Test + public void invalidInputs() { + WorkflowVersion workflowVersion = new WorkflowVersion(); + workflowVersion.setDescription("version description"); + ParameterEntity input = new ParameterEntity(); + input.setName("input1"); + workflowVersion.setInputs(Arrays.asList(input, input)); + try { + versionValidator.validate(ITEM1_ID, workflowVersion); + fail("Should have thrown VersionValidationException but did not!"); + + } catch (VersionValidationException ex) { + assertEquals(String.format("Error creating or modifying version for workflow with id %s: %s", ITEM1_ID, + "Input name must be unique"), ex.getMessage()); + } + } + + @Test + public void invalidOtputs(){ + WorkflowVersion workflowVersion = new WorkflowVersion(); + workflowVersion.setDescription("version description"); + ParameterEntity output = new ParameterEntity(); + output.setName("output1"); + workflowVersion.setOutputs(Arrays.asList(output, output)); + try { + versionValidator.validate(ITEM1_ID, workflowVersion); + fail("Should have thrown VersionValidationException but did not!"); + + } catch (VersionValidationException ex) { + assertEquals(String.format("Error creating or modifying version for workflow with id %s: %s", ITEM1_ID, + "Output name must be unique"), ex.getMessage()); + } + } +} |