Version Artifact API 25/55825/5
authorayalaben <ayala.benzvi@amdocs.com>
Wed, 4 Jul 2018 10:11:54 +0000 (13:11 +0300)
committerayalaben <ayala.benzvi@amdocs.com>
Tue, 10 Jul 2018 12:05:42 +0000 (15:05 +0300)
Change-Id: Iae2dace7d9b31a633e60c7eebcbc50ee3f6ac7d6
Issue-ID: SDC-1462
Signed-off-by: ayalaben <ayala.benzvi@amdocs.com>
27 files changed:
workflow-bdd/resources/json/createVLM.json [deleted file]
workflow-bdd/resources/json/createVSP.json [deleted file]
workflow-bdd/stepDefinitions/VLM_steps.js [deleted file]
workflow-bdd/stepDefinitions/VSP_steps.js [deleted file]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionRequestDto.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/ArtifactRepository.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryImpl.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/ArtifactEntity.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/VersionStatus.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/persistence/types/WorkflowVersion.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/CreateVersionException.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/EntityNotFoundException.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/InvalidArtifactException.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionModificationException.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionNotFoundException.java [deleted file]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/WorkflowNotFoundException.java [deleted file]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/mappers/VersionMapper.java [new file with mode: 0644]
workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java
workflow-designer-be/src/test/java/org/onap/sdc/workflow/persistence/impl/ArtifactRepositoryTest.java [new file with mode: 0644]
workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerTest.java
workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerTest.java

diff --git a/workflow-bdd/resources/json/createVLM.json b/workflow-bdd/resources/json/createVLM.json
deleted file mode 100644 (file)
index 9431c07..0000000
+++ /dev/null
@@ -1 +0,0 @@
-{"vendorName":"RANDOM","description":"VLM Description","iconRef":"icon"}
diff --git a/workflow-bdd/resources/json/createVSP.json b/workflow-bdd/resources/json/createVSP.json
deleted file mode 100644 (file)
index 6f63261..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-{
-        "vendorId":"REPLACE",
-        "name": "RANDOM",
-        "category":"resourceNewCategory.generic",
-        "subCategory":"resourceNewCategory.generic.abstract",
-        "onboardingMethod":"REPLACE",
-        "description":"for testing",
-        "vendorName":"REPLACE",
-        "icon":"icon",
-        "licensingData":{}
-}
diff --git a/workflow-bdd/stepDefinitions/VLM_steps.js b/workflow-bdd/stepDefinitions/VLM_steps.js
deleted file mode 100644 (file)
index 35e78b2..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Copyright © 2016-2017 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.
- */
-const {Then, When, Given} = require('cucumber');
-const assert = require('assert');
-const util = require('./Utils.js');
-
-/**
- * @module VLM
- * @description Creates a new VLM with a random name and saves the id and versionId on the context item object and the context vlm object<br>
- *     Input data  will be taken from the 'resources/json/createVLM.json' file.
- *@exampleFile Example_VLM.feature
- * @step I want to create a VLM
- **/
-When('I want to create a VLM', function()  {
-       let inputData = util.getJSONFromFile('resources/json/createVLM.json');
-       inputData.vendorName = util.random();
-       let path = '/vendor-license-models';
-       return util.request(this.context, 'POST', path, inputData).then(result => {
-               this.context.item ={id : result.data.itemId, versionId: result.data.version.id};
-               this.context.vlm = {id : result.data.itemId, name : inputData.vendorName};
-       });
-});
-
-/**
- * @module VLM
- * @exampleFile Example_VLM.feature
- * @step I want to submit this VLM
- **/
-Then('I want to submit this VLM', function()  {
-       let inputData = {action: 'Submit'};
-       let path = '/vendor-license-models/' + this.context.item.id + '/versions/' + this.context.item.versionId + '/actions';
-       return util.request(this.context, 'PUT', path, inputData);
-});
-
-/**
- * @module VLM
- * @exampleFile DeleteVLMDraft.feature
- * @step I want to delete this VLM
- **/
-Then('I want to delete this VLM', function()  {
-       let path = '/vendor-license-models/' + this.context.item.id ;
-       return util.request(this.context, 'DELETE', path);
-});
-
-
-/**
- * @module VLM
- * @exampleFile ArchiveItem.feature
- * @step I want to list Archived VLMs
- **/
-Then('I want to list Archived VLMs', function()  {
-       let path = '/vendor-license-models/?Status=ARCHIVED';
-       return util.request(this.context, 'GET', path);
-});
-
-/**
- * @module VLM
- * @exampleFile ArchiveItem.feature
- * @step I want to list Active VLMs
- **/
-Then('I want to list Active VLMs', function()  {
-       let path = '/vendor-license-models';
-       return util.request(this.context, 'GET', path);
-});
-
-
diff --git a/workflow-bdd/stepDefinitions/VSP_steps.js b/workflow-bdd/stepDefinitions/VSP_steps.js
deleted file mode 100644 (file)
index 1f753fe..0000000
+++ /dev/null
@@ -1,182 +0,0 @@
-/*
- * Copyright © 2016-2017 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.
- */
-const {Then, When} = require('cucumber');
-const assert = require('assert');
-const util = require('./Utils.js');
-const _ = require('lodash');
-
-
-/**
- * @module VSP
- * @description Creates a new VSP with a random name and saves the id and versionId on the context item object and the context vsp object<br>
- *     Input data will be taken from the 'resources/json/createVSP.json' file.
- *     Vendor id and name are taken from the vlm on the context (requires a VLM to be created first).
- *     @exampleFile Example_VSP.feature
- * @step I want to create a VSP with onboarding type {string}
- **/
-When('I want to create a VSP with onboarding type {string}', function(string)  {
-       let inputData = util.getJSONFromFile('resources/json/createVSP.json');
-       inputData.onboardingMethod = string;
-       inputData.vendorName = this.context.vlm.name;
-       inputData.vendorId = this.context.vlm.id;
-       inputData.name = util.random();
-       let path = '/vendor-software-products';
-       return util.request(this.context, 'POST', path, inputData).then(result => {
-               this.context.item = {id : result.data.itemId, versionId: result.data.version.id};
-               this.context.vsp = {id : result.data.itemId, versionId: result.data.version.id};
-       });
-});
-
-/**
- * @module VSP
- * @description Creates a new VSP with the 'NetowrkPackage' onboarding type and with a random name and saves the id and versionId on the context item object and the context vsp object<br>
- *     Input data will be taken from the 'resources/json/createVSP.json' file.
- *     Vendor id and name are taken from the vlm on the context (requires a VLM to be created first).
- *     @exampleFile Example_VSP.feature
- * @step I want to create a VSP with onboarding type {string}
- **/
-When('I want to create a VSP', function()  {
-       let inputData = util.getJSONFromFile('resources/json/createVSP.json');
-       inputData.vendorName = this.context.vlm.name;
-       inputData.vendorId = this.context.vlm.id;
-       inputData.name = util.random();
-       let path = '/vendor-software-products';
-       return util.request(this.context, 'POST', path, inputData).then(result => {
-               this.context.item = {id : result.data.itemId, versionId: result.data.version.id};
-               this.context.vsp = {id : result.data.itemId, versionId: result.data.version.id};
-       });
-});
-
-
-/**
- * @module VSP
- * @exampleFile Example_VSP.feature
- * @step I want to submit this VSP
- **/
-Then('I want to submit this VSP', function () {
-       let path = '/vendor-software-products/' + this.context.item.id + '/versions/' + this.context.item.versionId + '/actions';
-       let inputData = {action: 'Submit'};
-       return util.request(this.context, 'PUT', path, inputData);
-});
-
-/**
- * @module VSP
- * @exampleFile Example_VSP.feature
- * @step I want to package this VSP
- **/
-Then('I want to package this VSP', function () {
-       let path = '/vendor-software-products/' + this.context.item.id + '/versions/' + this.context.item.versionId + '/actions';
-       let inputData = {action: 'Create_Package'};
-       return util.request(this.context, 'PUT', path, inputData);
-});
-
-/**
- * @module VSP
- * @description Adds a component to the current item
- * @exampleFile Example_VSP.feature
- * @step I want to add a component
- **/
-Then('I want to add a component', function () {
-       let path = '/vendor-software-products/' + this.context.item.id + '/versions/' + this.context.item.versionId + '/components';
-       let inputData = {name: 'Cucumber Name', displayName: 'Cucumber', description: 'Cucumber Description'};
-       return util.request(this.context, 'POST', path, inputData).then(result => {
-               this.context.componentId = result.data.vfcId;
-       });
-});
-
-
-/**
- * @module VSP
- * @description Downloads the packaged file for this component to the given path
- * @exampleFile Example_VSP.feature
- * @step I want to get the package for this Item to path {string}
- **/
-When('I want to get the package for this Item to path {string}', function (string, callback) {
-       let path =  '/vendor-software-products/packages/' + this.context.item.id;
-       return [util.download(this.context, path, string, callback)];
-});
-
-
-/**
- * @module VSP
- * @exampleFile DeleteVSPDraft.feature
- * @step I want to delete this VSP
- **/
-Then('I want to delete this VSP', function()  {
-       let path = '/vendor-software-products/' + this.context.item.id ;
-       return util.request(this.context, 'DELETE', path);
-});
-
-/**
- * @module VSP
- * @exampleFile ArchiveItem.feature
- * @step I want to list Archived VSPs
- **/
-Then('I want to list Archived VSPs', function()  {
-       let path = '/vendor-software-products/?Status=ARCHIVED';
-       return util.request(this.context, 'GET', path);
-});
-
-/**
- * @module VSP
- * @exampleFile ArchiveItem.feature
- * @step I want to list Active VSPs
- **/
-Then('I want to list Active VSPs', function()  {
-       let path = '/vendor-software-products';
-       return util.request(this.context, 'GET', path);
-});
-
-
-/**
- * @module VSP
- * @exampleFile FilterArchivedVSPpackage.feature
- * @step I want to list Archived VSPs packages
- **/
-Then('I want to list Archived VSPs packages', function()  {
-       let path = '/vendor-software-products/packages?Status=ARCHIVED';
-       return util.request(this.context, 'GET', path);
-});
-
-/**
- * @module VSP
- * @exampleFile FilterArchivedVSPpackage.feature
- * @step I want to list Active VSPs packages
- **/
-Then('I want to list Active VSPs packages', function()  {
-       let path = '/vendor-software-products/packages';
-       return util.request(this.context, 'GET', path);
-
-});
-
-/**
- * @module VSP
- * @exampleFile FilterArchivedVSPpackage.feature
- * @step I want to check that VSP package exits in response
- **/
-Then('I want to check that VSP package exits in response', function()  {
-
-      const packages = this.context.responseData.results;
-      const id = this.context.item.id;
-      var testResult = false;
-
-      for(var i=0; i< packages.length; i++){
-          if (id == packages[i].packageId){
-               testResult = true;
-          }
-      }
-      assert.equal(testResult,true);
-});
\ No newline at end of file
index eb57353..cb1fb1f 100644 (file)
@@ -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<Version> list(String id, String user);
+    CollectionWrapper<WorkflowVersion> list(String workflowId,String user);
 
     @PostMapping
     @ApiOperation("Create workflow version")
-    ResponseEntity<?> create(String id, Version version, String user);
+    ResponseEntity<WorkflowVersion> 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<Resource> getArtifact(String workflowId, String versionId, String user);
+
+    @DeleteMapping("/{versionId}/artifact")
+    @ApiOperation("Delete workflow version artifact")
+    void deleteArtifact(String workflowId, String versionId, String user);
 }
index 978be19..0b5278a 100644 (file)
@@ -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<String> handleWorkflowNotFoundException(
             Exception exception) {
         return new ResponseEntity<>(exception.getMessage(), NOT_FOUND);
     }
+
+    @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class})
+    public final ResponseEntity<String> handleInvalidArtifactException(
+            Exception exception) {
+        return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
+    }
+
+
+    @ExceptionHandler(CreateVersionException.class)
+    public final ResponseEntity<String> handleVersioningErrorException(
+            CreateVersionException exception) {
+        return new ResponseEntity<>(exception.getMessage(), FORBIDDEN);
+    }
 }
index 49c31f5..0570ac8 100644 (file)
@@ -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<Version> list(@PathVariable("id") String id,@RequestHeader(USER_ID_HEADER_PARAM) String user) {
-        return new CollectionWrapper<>(workflowVersionManager.list(id));
+    public CollectionWrapper<WorkflowVersion> 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<WorkflowVersion> 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<Resource> 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 (file)
index 0000000..92ac3a7
--- /dev/null
@@ -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 (file)
index 0000000..a473f16
--- /dev/null
@@ -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<ArtifactEntity> 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 (file)
index 0000000..9a449f0
--- /dev/null
@@ -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<ArtifactEntity> get(String id, String versionId) {
+        SessionContext context = createSessionContext();
+        ElementContext elementContext = new ElementContext(id, versionId);
+
+        Optional<Element> 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 (file)
index 0000000..8f24ecc
--- /dev/null
@@ -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 (file)
index 0000000..a3e8a38
--- /dev/null
@@ -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 (file)
index 0000000..3c667be
--- /dev/null
@@ -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() {
+    }
+}
index b288b28..fc60b27 100644 (file)
@@ -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<Version> list(String id);
+    Collection<WorkflowVersion> 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 (file)
index 0000000..cadabb4
--- /dev/null
@@ -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 (file)
index 0000000..1ed3df5
--- /dev/null
@@ -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 (file)
index 0000000..89c5fd5
--- /dev/null
@@ -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 (file)
index 0000000..dc7d62f
--- /dev/null
@@ -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 (file)
index 3bf86ad..0000000
+++ /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 (file)
index 13c8021..0000000
+++ /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));
-    }
-}
index 8bb5f70..cf1a7e1 100644 (file)
@@ -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> 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());
index b0a14fd..99b64c7 100644 (file)
 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<Version> list(String id) {
-        return versioningManager.list(id);
+    public Collection<WorkflowVersion> 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<Version> 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<Version> findVersion(List<Version> 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<ArtifactEntity> 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<Version> 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 (file)
index 0000000..4f894f7
--- /dev/null
@@ -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);
+}
index b77e11a..d38261b 100644 (file)
@@ -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 (file)
index 0000000..4bec8b9
--- /dev/null
@@ -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 {
+
+    }
+
+
+}
index 74097b1..11c50cd 100644 (file)
@@ -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<Item> 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));
index 75146e2..543ca82 100644 (file)
@@ -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<Version> 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<Version> 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<Version> 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<Version> 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);
+    }
 
 }