From 8660fef75d965fa92f0a6d28187469009abcf3ad Mon Sep 17 00:00:00 2001 From: ayalaben Date: Wed, 4 Jul 2018 13:11:54 +0300 Subject: Version Artifact API Change-Id: Iae2dace7d9b31a633e60c7eebcbc50ee3f6ac7d6 Issue-ID: SDC-1462 Signed-off-by: ayalaben --- .../workflow/api/WorkflowVersionController.java | 26 ++-- .../CustomizedResponseEntityExceptionHandler.java | 22 +++- .../api/impl/WorkflowVersionControllerImpl.java | 58 ++++++--- .../sdc/workflow/api/types/VersionRequestDto.java | 11 ++ .../workflow/persistence/ArtifactRepository.java | 18 +++ .../persistence/impl/ArtifactRepositoryImpl.java | 104 +++++++++++++++ .../workflow/persistence/types/ArtifactEntity.java | 16 +++ .../workflow/persistence/types/VersionStatus.java | 7 + .../persistence/types/WorkflowVersion.java | 26 ++++ .../workflow/services/WorkflowVersionManager.java | 19 ++- .../exceptions/CreateVersionException.java | 9 ++ .../exceptions/EntityNotFoundException.java | 8 ++ .../exceptions/InvalidArtifactException.java | 8 ++ .../exceptions/VersionModificationException.java | 9 ++ .../exceptions/VersionNotFoundException.java | 8 -- .../exceptions/WorkflowNotFoundException.java | 8 -- .../services/impl/WorkflowManagerImpl.java | 7 +- .../services/impl/WorkflowVersionManagerImpl.java | 144 ++++++++++++++++++--- .../workflow/services/mappers/VersionMapper.java | 15 +++ .../api/WorkflowVersionControllerTest.java | 12 +- .../persistence/impl/ArtifactRepositoryTest.java | 29 +++++ .../services/impl/WorkflowManagerTest.java | 8 +- .../services/impl/WorkflowVersionManagerTest.java | 119 ++++++++++++++--- 23 files changed, 595 insertions(+), 96 deletions(-) create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionRequestDto.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/ArtifactRepository.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryImpl.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ArtifactEntity.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/VersionStatus.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/CreateVersionException.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/EntityNotFoundException.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/InvalidArtifactException.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java delete mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionNotFoundException.java delete mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/WorkflowNotFoundException.java create mode 100644 workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/mappers/VersionMapper.java create mode 100644 workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryTest.java (limited to 'workflow-designer-be/src') 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 eb57353b..cb1fb1f3 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 @@ -3,39 +3,49 @@ package org.onap.sdc.workflow.api; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import java.util.Collection; import org.onap.sdc.workflow.api.types.CollectionWrapper; -import org.openecomp.sdc.versioning.dao.types.Version; +import org.onap.sdc.workflow.api.types.VersionRequestDto; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; -@RequestMapping("/workflows/{id}/versions") +@RequestMapping("/workflows/{workflowId}/versions") @Api("Workflow versions") public interface WorkflowVersionController { @GetMapping @ApiOperation("List workflow versions") - CollectionWrapper list(String id, String user); + CollectionWrapper list(String workflowId,String user); @PostMapping @ApiOperation("Create workflow version") - ResponseEntity create(String id, Version version, String user); + ResponseEntity create(String workflowId, VersionRequestDto versionRequest, String user); @GetMapping("/{versionId}") @ApiOperation("Get workflow version") - Version get(String id,String versionId, String user); + WorkflowVersion get(String workflowId,String versionId, String user); @PutMapping("/{versionId}") @ApiOperation("Update workflow version") - void update(String id, String versionId,Version version, String user); + void update(WorkflowVersion version,String workflowId, String versionId, String user); + @PutMapping("/{versionId}/artifact") + @ApiOperation("Create/update artifact of a version") + void createArtifact(MultipartFile fileToUpload, String workflowId, String versionId, String user); + + @GetMapping("/{versionId}/artifact") + @ApiOperation("Download workflow version artifact") + ResponseEntity getArtifact(String workflowId, String versionId, String user); + + @DeleteMapping("/{versionId}/artifact") + @ApiOperation("Delete workflow version artifact") + void deleteArtifact(String workflowId, String versionId, String user); } 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 978be192..0b5278ac 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 @@ -1,11 +1,14 @@ package org.onap.sdc.workflow.api.exceptionshandlers; +import static org.springframework.http.HttpStatus.FORBIDDEN; import static org.springframework.http.HttpStatus.NOT_FOUND; import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY; +import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; +import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException; import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException; -import org.onap.sdc.workflow.services.exceptions.VersionNotFoundException; -import org.onap.sdc.workflow.services.exceptions.WorkflowNotFoundException; +import org.onap.sdc.workflow.services.exceptions.CreateVersionException; +import org.onap.sdc.workflow.services.exceptions.VersionModificationException; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -22,9 +25,22 @@ public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExce return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY); } - @ExceptionHandler({WorkflowNotFoundException.class, VersionNotFoundException.class}) + @ExceptionHandler(EntityNotFoundException.class) public final ResponseEntity handleWorkflowNotFoundException( Exception exception) { return new ResponseEntity<>(exception.getMessage(), NOT_FOUND); } + + @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class}) + public final ResponseEntity handleInvalidArtifactException( + Exception exception) { + return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY); + } + + + @ExceptionHandler(CreateVersionException.class) + public final ResponseEntity handleVersioningErrorException( + CreateVersionException exception) { + return new ResponseEntity<>(exception.getMessage(), FORBIDDEN); + } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java index 49c31f5b..0570ac85 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java @@ -2,22 +2,24 @@ package org.onap.sdc.workflow.api.impl; import static org.onap.sdc.workflow.api.RestConstants.USER_ID_HEADER_PARAM; -import java.util.Collection; -import java.util.List; -import java.util.Optional; import org.onap.sdc.workflow.api.WorkflowVersionController; import org.onap.sdc.workflow.api.types.CollectionWrapper; +import org.onap.sdc.workflow.api.types.VersionRequestDto; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.onap.sdc.workflow.services.WorkflowVersionManager; -import org.openecomp.sdc.versioning.dao.types.Version; -import org.openecomp.sdc.versioning.types.VersionCreationMethod; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @@ -32,32 +34,56 @@ public class WorkflowVersionControllerImpl implements WorkflowVersionController } @Override - public CollectionWrapper list(@PathVariable("id") String id,@RequestHeader(USER_ID_HEADER_PARAM) String user) { - return new CollectionWrapper<>(workflowVersionManager.list(id)); + public CollectionWrapper list(@PathVariable("workflowId") String workflowId, + @RequestHeader(USER_ID_HEADER_PARAM) String user) { + return new CollectionWrapper<>(workflowVersionManager.list(workflowId)); } @Override - public ResponseEntity create(@PathVariable("id") String id,@RequestBody Version version, + public ResponseEntity create(@PathVariable("workflowId") String workflowId,@RequestBody VersionRequestDto versionRequest, @RequestHeader(USER_ID_HEADER_PARAM) String user) { - Version createdVersion = workflowVersionManager.create(id, version); - + WorkflowVersion createdVersion = workflowVersionManager.create(workflowId, versionRequest); return new ResponseEntity<>(createdVersion, HttpStatus.CREATED); } @Override - public Version get(@PathVariable("id") String id,@PathVariable("versionId") String versionId, + public WorkflowVersion get(@PathVariable("workflowId") String workflowId,@PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { - Version version = new Version(versionId); - return workflowVersionManager.get(id,version); + return workflowVersionManager.get(workflowId,versionId); } @Override - public void update(@PathVariable("id") String id, @PathVariable("versionId") String versionId, - @RequestBody Version version, @RequestHeader(USER_ID_HEADER_PARAM) String user) { + public void update(@RequestBody WorkflowVersion version,@PathVariable("workflowId") String workflowId, + @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { version.setId(versionId); - workflowVersionManager.update(id,version); + workflowVersionManager.update(workflowId,version); + } + + @Override + public void createArtifact(@RequestBody MultipartFile fileToUpload, @PathVariable("workflowId") String workflowId, + @PathVariable("versionId") String versionId,@RequestHeader(USER_ID_HEADER_PARAM) String user) { + + workflowVersionManager.uploadArtifact(workflowId,new WorkflowVersion(versionId),fileToUpload); + + } + + @Override + public ResponseEntity getArtifact(@PathVariable("workflowId") String workflowId, + @PathVariable("versionId") String versionId, @RequestHeader(USER_ID_HEADER_PARAM) String user) { + WorkflowVersion requestedVersion = new WorkflowVersion(versionId); + ArtifactEntity artifact = workflowVersionManager.getArtifact(workflowId, requestedVersion); + + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + artifact.getFileName()) + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .body(new InputStreamResource(artifact.getArtifactData())); } + @Override + public void deleteArtifact(@PathVariable("workflowId") String workflowId, @PathVariable("versionId") String versionId, + @RequestHeader(USER_ID_HEADER_PARAM) String user) { + + } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionRequestDto.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionRequestDto.java new file mode 100644 index 00000000..92ac3a73 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionRequestDto.java @@ -0,0 +1,11 @@ +package org.onap.sdc.workflow.api.types; + +import lombok.Data; + +@Data +public class VersionRequestDto { + + private String description; + private String baseVersionId; + +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/ArtifactRepository.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/ArtifactRepository.java new file mode 100644 index 00000000..a473f166 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/ArtifactRepository.java @@ -0,0 +1,18 @@ +package org.onap.sdc.workflow.persistence; + + +import java.util.Optional; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; + + +public interface ArtifactRepository { + + void update(String id, String versionId,ArtifactEntity artifactEntity); + + Optional get(String id, String versionId); + + void createStructure(String id, String versionId); + + void delete(String id, String versionId); + +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryImpl.java new file mode 100644 index 00000000..9a449f0d --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryImpl.java @@ -0,0 +1,104 @@ +package org.onap.sdc.workflow.persistence.impl; + +import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement; +import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext; + +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.Action; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Map; +import java.util.Optional; +import org.apache.commons.io.IOUtils; +import org.onap.sdc.workflow.persistence.ArtifactRepository; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; +import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory; +import org.openecomp.sdc.datatypes.model.ElementType; +import org.springframework.stereotype.Repository; + +@Repository +public class ArtifactRepositoryImpl implements ArtifactRepository { + + private static final String FILE_NAME_PROPERTY = "fileName"; + private static final String EMPTY_DATA = "{}"; + private ZusammenAdaptor zusammenAdaptor = ZusammenAdaptorFactory.getInstance().createInterface(); + + + @Override + public void update(String id, String versionId, ArtifactEntity artifactEntity) { + + ZusammenElement artifactElement = buildStructuralElement(ElementType.Artifact, Action.UPDATE); + artifactElement.setData(artifactEntity.getArtifactData()); + artifactElement.getInfo().addProperty(FILE_NAME_PROPERTY, artifactEntity.getFileName()); + + SessionContext context = createSessionContext(); + ElementContext elementContext = new ElementContext(id, versionId); + + zusammenAdaptor + .saveElement(context, elementContext, artifactElement, "Update WorkflowVersion Artifact Element"); + } + + @Override + public Optional get(String id, String versionId) { + SessionContext context = createSessionContext(); + ElementContext elementContext = new ElementContext(id, versionId); + + Optional elementOptional = + zusammenAdaptor.getElementByName(context, elementContext, null, ElementType.Artifact.name()); + + if (!elementOptional.isPresent() || hasEmptyData(elementOptional.get().getData())) { + return Optional.empty(); + } + + Element artifactElement = elementOptional.get(); + + ArtifactEntity artifact = new ArtifactEntity(artifactElement.getInfo().getProperty(FILE_NAME_PROPERTY), + artifactElement.getData()); + + return Optional.of(artifact); + } + + @Override + public void createStructure(String id, String versionId) { + SessionContext context = createSessionContext(); + ElementContext elementContext = new ElementContext(id, versionId); + + ZusammenElement artifactElement = buildStructuralElement(ElementType.Artifact, Action.CREATE); + artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes())); + + zusammenAdaptor + .saveElement(context, elementContext, artifactElement, "Create WorkflowVersion Artifact Element"); + + } + + @Override + public void delete(String id, String versionId) { + SessionContext context = createSessionContext(); + ElementContext elementContext = new ElementContext(id, versionId); + + ZusammenElement artifactElement = buildStructuralElement(ElementType.Artifact, Action.UPDATE); + artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes())); + artifactElement.getInfo().getProperties().remove(FILE_NAME_PROPERTY); + + zusammenAdaptor + .saveElement(context, elementContext, artifactElement, "Update WorkflowVersion Artifact Element"); + + } + + private boolean hasEmptyData(InputStream elementData) { + + byte[] byteElementData; + try { + byteElementData = IOUtils.toByteArray(elementData); + } catch (IOException ex) { + return false; + } + return Arrays.equals(EMPTY_DATA.getBytes(), byteElementData); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ArtifactEntity.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ArtifactEntity.java new file mode 100644 index 00000000..8f24ecc9 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ArtifactEntity.java @@ -0,0 +1,16 @@ +package org.onap.sdc.workflow.persistence.types; + +import java.io.InputStream; +import lombok.Data; + +@Data +public class ArtifactEntity { + + private String fileName; + private InputStream artifactData; + + public ArtifactEntity(String fileName, InputStream artifactData) { + this.fileName = fileName; + this.artifactData = artifactData; + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/VersionStatus.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/VersionStatus.java new file mode 100644 index 00000000..a3e8a38a --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/VersionStatus.java @@ -0,0 +1,7 @@ +package org.onap.sdc.workflow.persistence.types; + +public enum VersionStatus { + + Draft, + Certified; +} 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 new file mode 100644 index 00000000..3c667bed --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java @@ -0,0 +1,26 @@ +package org.onap.sdc.workflow.persistence.types; + +import java.util.Date; +import lombok.Data; + + +@Data +public class WorkflowVersion { + + private String id; + private String name; + private String description; + private String baseId; + private Date creationTime; + private Date modificationTime; + private VersionStatus status; + + + public WorkflowVersion(String id) { + this.id = id; + this.status = VersionStatus.Draft; + } + + public WorkflowVersion() { + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java index b288b287..fc60b27a 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java @@ -1,16 +1,25 @@ package org.onap.sdc.workflow.services; import java.util.Collection; -import org.openecomp.sdc.versioning.dao.types.Version; +import org.onap.sdc.workflow.api.types.VersionRequestDto; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; + public interface WorkflowVersionManager { - Collection list(String id); + Collection list(String workflowId); + + WorkflowVersion get(String workflowId,String versionId); + + WorkflowVersion create(String workflowId, VersionRequestDto versionRequest); + + void update(String id,WorkflowVersion version); - Version get(String id, Version version); + void uploadArtifact(String workflowId,WorkflowVersion version, MultipartFile artifact); - Version create(String id, Version version); + ArtifactEntity getArtifact(String workflowId,WorkflowVersion version); - void update(String id,Version version); + void deleteArtifact(String workflowId,WorkflowVersion version); } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/CreateVersionException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/CreateVersionException.java new file mode 100644 index 00000000..cadabb4c --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/CreateVersionException.java @@ -0,0 +1,9 @@ +package org.onap.sdc.workflow.services.exceptions; + +public class CreateVersionException extends RuntimeException { + + public CreateVersionException(String workflowId, String version) { + super(String.format("Error creating new version for workflow with id %s. There is already a draft version %s", + workflowId, version)); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/EntityNotFoundException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/EntityNotFoundException.java new file mode 100644 index 00000000..1ed3df5a --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/EntityNotFoundException.java @@ -0,0 +1,8 @@ +package org.onap.sdc.workflow.services.exceptions; + +public class EntityNotFoundException extends RuntimeException { + + public EntityNotFoundException(String message) { + super(message); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/InvalidArtifactException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/InvalidArtifactException.java new file mode 100644 index 00000000..89c5fd5f --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/InvalidArtifactException.java @@ -0,0 +1,8 @@ +package org.onap.sdc.workflow.services.exceptions; + +public class InvalidArtifactException extends RuntimeException { + + public InvalidArtifactException(String message) { + super("Invalid artifact file can not be processed. Error: " + message); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java new file mode 100644 index 00000000..dc7d62fd --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java @@ -0,0 +1,9 @@ +package org.onap.sdc.workflow.services.exceptions; + +public class VersionModificationException extends RuntimeException { + + public VersionModificationException(String workflowId, String version) { + super(String.format("Error while trying to modify version for workflow id %s. " + + "Version %s is Certified and can not be edited", workflowId, version)); + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionNotFoundException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionNotFoundException.java deleted file mode 100644 index 3bf86ad1..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionNotFoundException.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.onap.sdc.workflow.services.exceptions; - -public class VersionNotFoundException extends RuntimeException { - - public VersionNotFoundException(String workflowId, String versioId) { - super(String.format("version with id '%s' does not exist for workflow with id %s",versioId,workflowId)); - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/WorkflowNotFoundException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/WorkflowNotFoundException.java deleted file mode 100644 index 13c8021f..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/WorkflowNotFoundException.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.onap.sdc.workflow.services.exceptions; - -public class WorkflowNotFoundException extends RuntimeException { - - public WorkflowNotFoundException(String workflowId) { - super(String.format("Workflow with id '%s' does not exist", workflowId)); - } -} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java index 8bb5f709..cf1a7e18 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java @@ -3,11 +3,11 @@ package org.onap.sdc.workflow.services.impl; import java.util.Collection; import java.util.function.Predicate; import java.util.stream.Collectors; +import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; import org.onap.sdc.workflow.services.mappers.WorkflowMapper; import org.onap.sdc.workflow.persistence.types.Workflow; import org.onap.sdc.workflow.services.UniqueValueService; import org.onap.sdc.workflow.services.WorkflowManager; -import org.onap.sdc.workflow.services.exceptions.WorkflowNotFoundException; import org.openecomp.sdc.versioning.ItemManager; import org.openecomp.sdc.versioning.types.Item; import org.openecomp.sdc.versioning.types.ItemStatus; @@ -19,6 +19,7 @@ import org.springframework.stereotype.Service; public class WorkflowManagerImpl implements WorkflowManager { private static final String WORKFLOW_TYPE = "WORKFLOW"; + private static final String WORKFLOW_NOT_FOUND_ERROR_MSG = "Workflow with id '%s' does not exist"; protected static final Predicate ITEM_PREDICATE = item -> WORKFLOW_TYPE.equals(item.getType()); private static final String WORKFLOW_NAME_UNIQUE_TYPE = "WORKFLOW_NAME"; private final ItemManager itemManager; @@ -43,7 +44,7 @@ public class WorkflowManagerImpl implements WorkflowManager { public Workflow get(Workflow workflow) { Item retrievedItem = itemManager.get(workflow.getId()); if (retrievedItem == null) { - throw new WorkflowNotFoundException(workflow.getId()); + throw new EntityNotFoundException(String.format(WORKFLOW_NOT_FOUND_ERROR_MSG, workflow.getId())); } return this.workflowMapper.itemToWorkflow(retrievedItem); } @@ -63,7 +64,7 @@ public class WorkflowManagerImpl implements WorkflowManager { public void update(Workflow workflow) { Item retrievedItem = itemManager.get(workflow.getId()); if (retrievedItem == null) { - throw new WorkflowNotFoundException(workflow.getId()); + throw new EntityNotFoundException(String.format(WORKFLOW_NOT_FOUND_ERROR_MSG, workflow.getId())); } uniqueValueService.updateUniqueValue(WORKFLOW_NAME_UNIQUE_TYPE, retrievedItem.getName(), workflow.getName()); diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java index b0a14fd6..99b64c7f 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java @@ -1,57 +1,165 @@ package org.onap.sdc.workflow.services.impl; +import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Certified; + +import java.io.IOException; +import java.io.InputStream; import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.onap.sdc.workflow.api.types.VersionRequestDto; +import org.onap.sdc.workflow.persistence.ArtifactRepository; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; +import org.onap.sdc.workflow.persistence.types.VersionStatus; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.onap.sdc.workflow.services.WorkflowVersionManager; -import org.onap.sdc.workflow.services.exceptions.VersionNotFoundException; +import org.onap.sdc.workflow.services.exceptions.CreateVersionException; +import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; +import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException; +import org.onap.sdc.workflow.services.exceptions.VersionModificationException; +import org.onap.sdc.workflow.services.mappers.VersionMapper; import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.types.VersionCreationMethod; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; @Service("workflowVersionManager") -public class WorkflowVersionManagerImpl implements WorkflowVersionManager { +public class WorkflowVersionManagerImpl implements WorkflowVersionManager { private final VersioningManager versioningManager; + private final ArtifactRepository artifactRepository; + private VersionMapper versionMapper; + @Autowired - public WorkflowVersionManagerImpl(VersioningManager versioningManager) { + public WorkflowVersionManagerImpl(VersioningManager versioningManager, ArtifactRepository artifactRepository, + VersionMapper versionMapper) { this.versioningManager = versioningManager; + this.artifactRepository = artifactRepository; + this.versionMapper = versionMapper; } @Override - public Collection list(String id) { - return versioningManager.list(id); + public Collection list(String workflowId) { + + return versioningManager.list(workflowId).stream() + .map(version -> versionMapper.versionToWorkflowVersion(version)) + .collect(Collectors.toList()); } @Override - public Version get(String id,Version version) { + public WorkflowVersion get(String workflowId, String versionId) { + Version version = new Version(versionId); try { - return versioningManager.get(id, version); - } catch (Exception e){ - throw new VersionNotFoundException(id,version.getId()); + return this.versionMapper.versionToWorkflowVersion(versioningManager.get(workflowId, version)); + } catch (Exception e) { + throw new EntityNotFoundException( + String.format("version with id '%s' does not exist for workflow with id %s", version.getId(), + workflowId)); + } + } + + @Override + public WorkflowVersion create(String workflowId, VersionRequestDto versionRequest) { + + List versions = versioningManager.list(workflowId); + + if (versionRequest.getBaseVersionId() != null) { + Version baseVersion = findVersion(versions, versionRequest.getBaseVersionId()).orElseThrow( + () -> new EntityNotFoundException( + String.format("version with id '%s' does not exist for workflow with id %s", + versionRequest.getBaseVersionId(), workflowId))); + + if(!Certified.equals(baseVersion.getStatus())){ + throw new CreateVersionException(workflowId, versionRequest.getBaseVersionId()); + } + } + + Version version = new Version(); + version.setDescription(versionRequest.getDescription()); + version.setBaseId(versionRequest.getBaseVersionId()); + Version createdVersion = versioningManager.create(workflowId, version, VersionCreationMethod.major); + + if (versions.isEmpty()){ + artifactRepository.createStructure(workflowId, createdVersion.getId()); + } + + versioningManager.publish(workflowId, createdVersion, "Initial version"); + + return versionMapper.versionToWorkflowVersion(createdVersion); + } + + private Optional findVersion(List versions, String baseVersionId) { + return versions.stream().filter(version -> baseVersionId.equals(version.getId())).findFirst(); + } + + @Override + public void update(String id, WorkflowVersion version) { + + Version versionToUpdate = mapVersionInfo(id, version); + versioningManager.updateVersion(id, versionToUpdate); + versioningManager.publish(id, versionToUpdate, "Update version"); + } + + + @Override + public void uploadArtifact(String workflowId, WorkflowVersion version, MultipartFile artifact) { + WorkflowVersion retrievedVersion = get(workflowId, version.getId()); + if (VersionStatus.Certified.equals(retrievedVersion.getStatus())) { + throw new VersionModificationException(workflowId, version.getId()); + } + + try (InputStream artifactData = artifact.getInputStream()) { + ArtifactEntity artifactEntity = + new ArtifactEntity(StringUtils.cleanPath(artifact.getOriginalFilename()), artifactData); + artifactRepository.update(workflowId, version.getId(), artifactEntity); + versioningManager.publish(workflowId, versionMapper.workflowVersionToVersion(version), "Update Artifact"); + + } catch (IOException e) { + throw new InvalidArtifactException(e.getMessage()); } } @Override - public Version create(String id, Version version) { - if (Objects.nonNull(getLatestVersion(id))) - version.setBaseId(getLatestVersion(id).getId()); - return versioningManager.create(id,version, VersionCreationMethod.major); + public ArtifactEntity getArtifact(String workflowId, WorkflowVersion version) { + WorkflowVersion retrievedVersion = get(workflowId, version.getId()); + Optional artifactOptional = artifactRepository.get(workflowId, retrievedVersion.getId()); + if (!artifactOptional.isPresent()) { + throw new EntityNotFoundException( + String.format("Artifact for workflow id %S version id %S was not found", workflowId, + version.getId())); + } + return artifactOptional.get(); } @Override - public void update(String id,Version version) { + public void deleteArtifact(String workflowId, WorkflowVersion version) { + WorkflowVersion retrievedVersion = get(workflowId, version.getId()); + if (VersionStatus.Certified.equals(retrievedVersion.getStatus())) { + throw new VersionModificationException(workflowId, version.getId()); + } + + artifactRepository.delete(workflowId, version.getId()); + } - versioningManager.updateVersion(id,version); + private boolean isFirstVersionCreation(String id, Version createdVersion) { + return Objects.isNull(createdVersion.getBaseId()) && versioningManager.list(id).size() == 1; } - protected Version getLatestVersion(String itemId) { - List list = versioningManager.list(itemId); - return list.stream().max(Version::compareTo).orElse(null); + protected Version mapVersionInfo(String id, WorkflowVersion workflowVersion) { + WorkflowVersion retrievedVersion = get(id, workflowVersion.getId()); + Version version = new Version(workflowVersion.getId()); + version.setBaseId(retrievedVersion.getBaseId()); + version.setName(retrievedVersion.getName()); + return version; } + } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/mappers/VersionMapper.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/mappers/VersionMapper.java new file mode 100644 index 00000000..4f894f7f --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/mappers/VersionMapper.java @@ -0,0 +1,15 @@ +package org.onap.sdc.workflow.services.mappers; + +import org.mapstruct.InheritInverseConfiguration; +import org.mapstruct.Mapper; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; +import org.openecomp.sdc.versioning.dao.types.Version; + +@Mapper(componentModel = "spring") +public interface VersionMapper { + + WorkflowVersion versionToWorkflowVersion(Version version); + + @InheritInverseConfiguration + Version workflowVersionToVersion(WorkflowVersion workflowVersion); +} 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 b77e11a2..d38261b7 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 @@ -26,6 +26,8 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.sdc.workflow.RestPath; import org.onap.sdc.workflow.api.impl.WorkflowVersionControllerImpl; +import org.onap.sdc.workflow.api.types.VersionRequestDto; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.onap.sdc.workflow.services.WorkflowVersionManager; import org.openecomp.sdc.versioning.dao.types.Version; import org.springframework.http.HttpStatus; @@ -75,7 +77,7 @@ public class WorkflowVersionControllerTest { @Test public void shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception { - Version version = new Version(); + VersionRequestDto version = new VersionRequestDto(); version.setDescription("VersionDescription"); mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) .contentType(APPLICATION_JSON) @@ -88,18 +90,18 @@ public class WorkflowVersionControllerTest { @Test public void shouldReturnWorkflowVersionWhenExists() throws Exception { - Version version = new Version(VERSION1_ID); - doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, version); + WorkflowVersion version = new WorkflowVersion(VERSION1_ID); + doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID); mockMvc.perform( get(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) .contentType(APPLICATION_JSON)).andDo(print()) .andExpect(status().isOk()).andExpect(jsonPath("$.id", is(version.getId()))); - verify(workflowVersionManagerMock, times(1)).get(ITEM1_ID, version); + verify(workflowVersionManagerMock, times(1)).get(ITEM1_ID, VERSION1_ID); } @Test public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception { - Version version = new Version(); + WorkflowVersion version = new WorkflowVersion(); version.setDescription("Updated"); MockHttpServletResponse result = mockMvc.perform( diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryTest.java new file mode 100644 index 00000000..4bec8b97 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryTest.java @@ -0,0 +1,29 @@ +package org.onap.sdc.workflow.persistence.impl; + +import java.io.IOException; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; + +public class ArtifactRepositoryTest { + + private static final String FILE_NAME_PROPERTY = "fileName"; + private static final String EMPTY_DATA = "{}"; + private static final String ITEM1_ID = "item_id_1"; + private static final String VERSION1_ID = "version_id_1"; + + + @Mock + private ZusammenAdaptor zusammenAdaptorMock; + + @InjectMocks + private ArtifactRepositoryImpl artifactRepository; + + @Test + public void shouldUpdateArtifact() throws IOException { + + } + + +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerTest.java index 74097b1e..11c50cde 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerTest.java @@ -16,7 +16,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.onap.sdc.workflow.persistence.types.Workflow; import org.onap.sdc.workflow.services.UniqueValueService; -import org.onap.sdc.workflow.services.exceptions.WorkflowNotFoundException; +import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; import org.onap.sdc.workflow.services.mappers.WorkflowMapper; import org.openecomp.sdc.versioning.ItemManager; import org.openecomp.sdc.versioning.types.Item; @@ -32,7 +32,7 @@ public class WorkflowManagerTest { private List itemList; @Mock - WorkflowMapper workflowMapperMock; + private WorkflowMapper workflowMapperMock; @Mock private ItemManager itemManagerMock; @@ -59,7 +59,7 @@ public class WorkflowManagerTest { verify(itemManagerMock).list(WorkflowManagerImpl.ITEM_PREDICATE); } - @Test(expected = WorkflowNotFoundException.class) + @Test(expected = EntityNotFoundException.class) public void shouldThrowExceptionWhenWorkflowDontExist() { Workflow nonExistingWorkflow = new Workflow(); nonExistingWorkflow.setId(ITEM1_ID); @@ -110,7 +110,7 @@ public class WorkflowManagerTest { } - @Test(expected = WorkflowNotFoundException.class) + @Test(expected = EntityNotFoundException.class) public void shouldThrowExceptionWhenWorkflowToUpdateNotFound() { doReturn(null).when(itemManagerMock).get(ITEM1_ID); workflowManager.update(createWorkflow(1, true)); diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerTest.java index 75146e28..543ca82a 100644 --- a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerTest.java +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerTest.java @@ -1,24 +1,41 @@ package org.onap.sdc.workflow.services.impl; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.onap.sdc.workflow.persistence.types.VersionStatus.*; +import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; -import java.util.Collection; +import java.util.Collections; +import java.util.Date; import java.util.List; -import org.junit.Before; +import java.util.Optional; +import java.util.Set; +import org.apache.commons.io.IOUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.onap.sdc.workflow.services.exceptions.VersionNotFoundException; +import org.onap.sdc.workflow.api.types.VersionRequestDto; +import org.onap.sdc.workflow.persistence.ArtifactRepository; +import org.onap.sdc.workflow.persistence.types.ArtifactEntity; +import org.onap.sdc.workflow.persistence.types.WorkflowVersion; +import org.onap.sdc.workflow.services.exceptions.CreateVersionException; +import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException; +import org.onap.sdc.workflow.services.mappers.VersionMapper; import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; import org.openecomp.sdc.versioning.types.VersionCreationMethod; +import org.springframework.mock.web.MockMultipartFile; @RunWith(MockitoJUnitRunner.class) public class WorkflowVersionManagerTest { @@ -27,59 +44,125 @@ public class WorkflowVersionManagerTest { private static final String ITEM1_ID = "item_id_1"; private static final String VERSION1_ID = "version_id_1"; private static final String VERSION2_ID = "version_id_2"; - private List versionList; + + @Mock + private VersionMapper versionMapperMock; @Mock private VersioningManager versioningManagerMock; + @Mock + private ArtifactRepository artifactRepositoryMock; + @Spy @InjectMocks private WorkflowVersionManagerImpl workflowVersionManager; - @Before - public void setUp(){ - versionList = Arrays.asList( new Version(VERSION1_ID),new Version(VERSION2_ID)); - } - - @Test(expected = VersionNotFoundException.class) + @Test(expected = EntityNotFoundException.class) public void shouldThrowExceptionWhenVersionDontExist(){ Version nonExistingVersion = new Version(VERSION1_ID); doThrow(new RuntimeException()).when(versioningManagerMock).get(ITEM1_ID, nonExistingVersion); - workflowVersionManager.get(ITEM1_ID, nonExistingVersion); + workflowVersionManager.get(ITEM1_ID, VERSION1_ID); } @Test public void shouldReturnWorkflowVersionWhenExist(){ Version version = new Version(VERSION1_ID); doReturn(version).when(versioningManagerMock).get(ITEM1_ID,version); - workflowVersionManager.get(ITEM1_ID,version); + workflowVersionManager.get(ITEM1_ID,VERSION1_ID); verify(versioningManagerMock).get(ITEM1_ID,version); } @Test public void shouldReturnWorkflowVersionList(){ + List versionList = + Arrays.asList(new Version(VERSION1_ID), new Version(VERSION2_ID)); + versionList.forEach(version -> { + version.setBaseId("baseVersionId"); + version.setDescription("Version description"); + version.setName("name"); + version.setCreationTime(new Date()); + version.setModificationTime(new Date()); + }); doReturn(versionList).when(versioningManagerMock).list(ITEM1_ID); - Collection result = workflowVersionManager.list(ITEM1_ID); + workflowVersionManager.list(ITEM1_ID); verify(versioningManagerMock).list(ITEM1_ID); - assertEquals(versionList,result); + verify(versionMapperMock ,times(2)).versionToWorkflowVersion(any(Version.class)); } @Test public void shouldUpdateWorkflowVersion(){ Version version = new Version(VERSION1_ID); - workflowVersionManager.update(ITEM1_ID,version); + version.setName("1.0"); + version.setDescription("WorkflowVersion description"); + WorkflowVersion workflowVersion = new WorkflowVersion(VERSION1_ID); + workflowVersion.setName("1.0"); + workflowVersion.setDescription("WorkflowVersion description"); + doReturn(version).when(workflowVersionManager).mapVersionInfo(ITEM1_ID,workflowVersion); + workflowVersionManager.update(ITEM1_ID,workflowVersion); verify(versioningManagerMock).updateVersion(ITEM1_ID,version); } @Test public void shouldCreateWorkflowVersion(){ - Version version = new Version(); - workflowVersionManager.create(ITEM1_ID,version); + Version version = new Version(VERSION1_ID); + version.setDescription("version desc"); + doReturn(version).when(versioningManagerMock).create(ITEM1_ID,version, VersionCreationMethod.major); + VersionRequestDto workflowVersion = new VersionRequestDto(); + workflowVersion.setDescription("version desc"); + workflowVersionManager.create(ITEM1_ID,workflowVersion); verify(versioningManagerMock).create(ITEM1_ID,version, VersionCreationMethod.major); - verify(workflowVersionManager).getLatestVersion(ITEM1_ID); + + } + + @Test(expected = CreateVersionException.class) + public void shouldTrowExceptionWhenDraftVersionExists(){ + VersionRequestDto versionRequestDto = new VersionRequestDto(); + versionRequestDto.setBaseVersionId(VERSION2_ID); + + Version baseVersion = new Version(VERSION2_ID); + baseVersion.setStatus(VersionStatus.Draft); + List versions = Collections.singletonList(baseVersion); + doReturn(versions).when(versioningManagerMock).list(ITEM1_ID); + + workflowVersionManager.create(ITEM1_ID,versionRequestDto); + } + + @Test + public void shouldUploadArtifact() throws IOException { + + String mockFileName = "filename.txt"; + MockMultipartFile mockFile = + new MockMultipartFile("data", mockFileName, "text/plain", "some xml".getBytes()); + WorkflowVersion version = new WorkflowVersion(VERSION1_ID); + version.setStatus(org.onap.sdc.workflow.persistence.types.VersionStatus.Draft); + doReturn(version).when(workflowVersionManager).get(ITEM1_ID,VERSION1_ID); + workflowVersionManager.uploadArtifact(ITEM1_ID,version,mockFile); + + verify(artifactRepositoryMock).update(eq(ITEM1_ID),eq(VERSION1_ID),any(ArtifactEntity.class)); } + @Test(expected = EntityNotFoundException.class) + public void shouldThrowExceptionWhenArtifactNotFound(){ + WorkflowVersion version = new WorkflowVersion(VERSION1_ID); + version.setStatus(Draft); + doReturn(version).when(workflowVersionManager).get(ITEM1_ID,VERSION1_ID); + doReturn(Optional.empty()).when(artifactRepositoryMock).get(ITEM1_ID,VERSION1_ID); + workflowVersionManager.getArtifact(ITEM1_ID,version); + } + @Test + public void shouldReturnArtifact() throws IOException { + WorkflowVersion version = new WorkflowVersion(VERSION1_ID); + doReturn(version).when(workflowVersionManager).get(ITEM1_ID,VERSION1_ID); + + InputStream inputStreamMock = + IOUtils.toInputStream("some test data for my input stream", "UTF-8"); + ArtifactEntity artifactMock = new ArtifactEntity("fileName.txt",inputStreamMock); + doReturn(Optional.of(artifactMock)).when(artifactRepositoryMock).get(ITEM1_ID,VERSION1_ID); + ArtifactEntity returnedArtifact = workflowVersionManager.getArtifact(ITEM1_ID, new WorkflowVersion(VERSION1_ID)); + assertEquals(artifactMock,returnedArtifact); + } } -- cgit 1.2.3-korg