diff options
author | ayalaben <ayala.benzvi@amdocs.com> | 2018-06-26 09:29:06 +0300 |
---|---|---|
committer | ayalaben <ayala.benzvi@amdocs.com> | 2018-06-26 09:29:06 +0300 |
commit | e9d97234f17d8ecdbfc0481d6a2ba2698d52ddd0 (patch) | |
tree | 0990c708c3b50c84115efeecab6df5346d430749 /workflow-designer-be | |
parent | 3fb47c47b301bf8f401cabf63b13f17a39182d85 (diff) |
workflow version API
Change-Id: Iadbf5325091084b4feca386740d6655845904d88
Issue-ID: SDC-1445
Signed-off-by: ayalaben <ayala.benzvi@amdocs.com>
Diffstat (limited to 'workflow-designer-be')
10 files changed, 423 insertions, 7 deletions
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java index 95f11cd1..41f47674 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java @@ -22,18 +22,17 @@ public interface WorkflowController { @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation("List workflows") - CollectionWrapper<Workflow> list(@RequestHeader(USER_ID_HEADER_PARAM) String user); + CollectionWrapper<Workflow> list(String user); @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @ApiOperation("Create workflow") - ResponseEntity<?> create(@RequestBody Workflow workflow, @RequestHeader(USER_ID_HEADER_PARAM) String user); + ResponseEntity<?> create(Workflow workflow, String user); @GetMapping(path = "/{id}") @ApiOperation("Get workflow") - Workflow get(@PathVariable("id") String id, @RequestHeader(USER_ID_HEADER_PARAM) String user); + Workflow get(String id, String user); @PutMapping(path = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE) @ApiOperation("Update workflow") - Workflow update(@RequestBody Workflow workflow, @PathVariable("id") String id, - @RequestHeader(USER_ID_HEADER_PARAM) String user); + Workflow update(Workflow workflow, String id, String user); } 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 new file mode 100644 index 00000000..eb57353b --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java @@ -0,0 +1,41 @@ +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.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") +@Api("Workflow versions") +public interface WorkflowVersionController { + + + @GetMapping + @ApiOperation("List workflow versions") + CollectionWrapper<Version> list(String id, String user); + + @PostMapping + @ApiOperation("Create workflow version") + ResponseEntity<?> create(String id, Version version, String user); + + @GetMapping("/{versionId}") + @ApiOperation("Get workflow version") + Version get(String id,String versionId, String user); + + @PutMapping("/{versionId}") + @ApiOperation("Update workflow version") + void update(String id, String versionId,Version version, 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 c29d2ab1..978be192 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 @@ -4,6 +4,7 @@ import static org.springframework.http.HttpStatus.NOT_FOUND; import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY; 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.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -21,9 +22,9 @@ public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExce return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY); } - @ExceptionHandler(WorkflowNotFoundException.class) + @ExceptionHandler({WorkflowNotFoundException.class, VersionNotFoundException.class}) public final ResponseEntity<String> handleWorkflowNotFoundException( - WorkflowNotFoundException exception) { + Exception exception) { return new ResponseEntity<>(exception.getMessage(), NOT_FOUND); } } 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 new file mode 100644 index 00000000..49c31f5b --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowVersionControllerImpl.java @@ -0,0 +1,63 @@ +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.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.Resource; +import org.springframework.http.HttpStatus; +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.RestController; +import org.springframework.web.multipart.MultipartFile; + +@RestController("workflowsVersionController") +public class WorkflowVersionControllerImpl implements WorkflowVersionController { + + private final WorkflowVersionManager workflowVersionManager; + + @Autowired + public WorkflowVersionControllerImpl(@Qualifier("workflowVersionManager") WorkflowVersionManager workflowVersionManager) { + this.workflowVersionManager = workflowVersionManager; + } + + @Override + public CollectionWrapper<Version> list(@PathVariable("id") String id,@RequestHeader(USER_ID_HEADER_PARAM) String user) { + return new CollectionWrapper<>(workflowVersionManager.list(id)); + } + + @Override + public ResponseEntity<?> create(@PathVariable("id") String id,@RequestBody Version version, + @RequestHeader(USER_ID_HEADER_PARAM) String user) { + + Version createdVersion = workflowVersionManager.create(id, version); + + return new ResponseEntity<>(createdVersion, HttpStatus.CREATED); + } + + @Override + public Version get(@PathVariable("id") String id,@PathVariable("versionId") String versionId, + @RequestHeader(USER_ID_HEADER_PARAM) String user) { + Version version = new Version(versionId); + return workflowVersionManager.get(id,version); + } + + @Override + public void update(@PathVariable("id") String id, @PathVariable("versionId") String versionId, + @RequestBody Version version, @RequestHeader(USER_ID_HEADER_PARAM) String user) { + + version.setId(versionId); + workflowVersionManager.update(id,version); + } + +} 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 new file mode 100644 index 00000000..b288b287 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/WorkflowVersionManager.java @@ -0,0 +1,16 @@ +package org.onap.sdc.workflow.services; + +import java.util.Collection; +import org.openecomp.sdc.versioning.dao.types.Version; + +public interface WorkflowVersionManager { + + Collection<Version> list(String id); + + Version get(String id, Version version); + + Version create(String id, Version version); + + void update(String id,Version 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 new file mode 100644 index 00000000..d98a15d7 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/VersionNotFoundException.java @@ -0,0 +1,7 @@ +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/impl/WorkflowVersionManagerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java new file mode 100644 index 00000000..cda17914 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java @@ -0,0 +1,60 @@ +package org.onap.sdc.workflow.services.impl; + +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import org.onap.sdc.workflow.services.WorkflowVersionManager; +import org.onap.sdc.workflow.services.exceptions.VersionNotFoundException; +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; + +@Service("workflowVersionManager") +public class WorkflowVersionManagerImpl implements WorkflowVersionManager { + + private final VersioningManager versioningManager; + + @Autowired + public WorkflowVersionManagerImpl(VersioningManager versioningManager) { + this.versioningManager = versioningManager; + } + + @Override + public Collection<Version> list(String id) { + return versioningManager.list(id); + } + + @Override + public Version get(String id,Version version) { + + try { + return versioningManager.get(id, version); + } catch (Exception e){ + throw new VersionNotFoundException(id,version.getId()); + } + } + + @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); + } + + @Override + public void update(String id,Version version) { + + versioningManager.updateVersion(id,version); + } + + private Version getLatestVersion(String itemId) { + List<Version> list = versioningManager.list(itemId); + Optional<Version> max = list.stream().max(Version::compareTo); + + return max.orElse(null); + } + +} 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 new file mode 100644 index 00000000..96c31c82 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java @@ -0,0 +1,118 @@ +package org.onap.sdc.workflow.api; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.google.gson.Gson; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.sdc.workflow.api.impl.WorkflowVersionControllerImpl; +import org.onap.sdc.workflow.services.WorkflowVersionManager; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.springframework.http.HttpStatus; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(MockitoJUnitRunner.class) +public class WorkflowVersionControllerTest { + + private static final String USER_ID = "cs0008"; + 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; + + private static final Gson GSON = new Gson(); + + private MockMvc mockMvc; + + @Mock + private WorkflowVersionManager workflowVersionManagerMock; + + @InjectMocks + private WorkflowVersionControllerImpl workflowVersionController; + + @Before + public void setUp() { + versionList = Arrays.asList( new Version(VERSION1_ID),new Version(VERSION2_ID)); + mockMvc = MockMvcBuilders.standaloneSetup(workflowVersionController).build(); + } + + @Test + public void shouldReturnVersionListOfWorkflow() throws Exception { + + doReturn(versionList).when(workflowVersionManagerMock).list(ITEM1_ID); + mockMvc.perform(get("/workflows/item_id_1/versions").header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON)).andExpect(status().isOk()) + .andExpect(jsonPath("$.results", hasSize(2))) + .andExpect(jsonPath("$.results[0].id", equalTo(VERSION1_ID))) + .andExpect(jsonPath("$.results[1].id", equalTo(VERSION2_ID))); + + verify(workflowVersionManagerMock, times(1)).list(ITEM1_ID); + } + + + @Test + public void shouldCreateWorkflowVersion() throws Exception { + + Version version = new Version(); + version.setDescription("VersionDescription"); + mockMvc.perform(post("/workflows/item_id_1/versions").header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON) + .content(GSON.toJson(version))) + .andExpect(status().isCreated()); + + verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, version); + } + + + @Test + public void shouldReturnWorkflowVersionWhenExists() throws Exception { + Version version = new Version(VERSION1_ID); + doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, version); + mockMvc.perform( + get("/workflows/item_id_1/versions/" + 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); + } + + @Test + public void shouldUpdateWorkflowVersion() throws Exception { + Version version = new Version(); + version.setDescription("Updated"); + + MockHttpServletResponse result = mockMvc.perform( + put("/workflows/item_id_1/versions/" + VERSION1_ID).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID) + .contentType(APPLICATION_JSON) + .content(GSON.toJson(version))).andReturn() + .getResponse(); + + assertEquals(HttpStatus.OK.value(), result.getStatus()); + version.setId(VERSION1_ID); + verify(workflowVersionManagerMock, times(1)).update(ITEM1_ID, version); + + } + +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/WorkflowManagerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/WorkflowManagerTest.java new file mode 100644 index 00000000..02035a12 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/WorkflowManagerTest.java @@ -0,0 +1,41 @@ +package org.onap.sdc.workflow.services; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.sdc.workflow.services.impl.WorkflowManagerImpl; +import org.openecomp.sdc.versioning.ItemManager; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(MockitoJUnitRunner.class) +public class WorkflowManagerTest { + + private MockMvc mockMvc; + + @Mock + private ItemManager itemManager; + + @Mock + private UniqueValueService uniqueValueService; + + @InjectMocks + private WorkflowManagerImpl workflowManager; + + + @Before + public void setUp(){ + mockMvc = MockMvcBuilders.standaloneSetup(workflowManager).build(); + } + + + @Test + public void testCreate(){ + + } + +} diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/WorkflowVersionManagerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/WorkflowVersionManagerTest.java new file mode 100644 index 00000000..d9031265 --- /dev/null +++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/WorkflowVersionManagerTest.java @@ -0,0 +1,70 @@ +package org.onap.sdc.workflow.services; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.Arrays; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.sdc.workflow.api.RestConstants; +import org.onap.sdc.workflow.services.impl.WorkflowVersionManagerImpl; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(MockitoJUnitRunner.class) +public class WorkflowVersionManagerTest { + + private static final String USER_ID = "cs0008"; + 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 static VersioningManager versioningManagerMock; + + @TestConfiguration + static class WorkflowVersionManagerTestContextConfiguration { + + @Bean + public WorkflowVersionManager WorkflowVersionManagerImpl() { + return new WorkflowVersionManagerImpl(versioningManagerMock); + } + } + + @Autowired + private WorkflowVersionManager workflowVersionManager; + + + + @Before + public void setUp(){ + versionList = Arrays.asList( new Version(VERSION1_ID),new Version(VERSION2_ID)); + + } + + + @Test + public void shouldReturnWorkflowVersionList(){ + + + } + +} |