diff options
Diffstat (limited to 'workflow')
6 files changed, 67 insertions, 68 deletions
diff --git a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/RestUtils.java b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/RestUtils.java new file mode 100644 index 00000000..b6259ccc --- /dev/null +++ b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/RestUtils.java @@ -0,0 +1,31 @@ +package org.onap.sdc.workflow; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Set; +import java.util.stream.Collectors; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +public class RestUtils { + + private RestUtils() { + } + + private static final Logger LOGGER = LoggerFactory.getLogger(RestUtils.class); + + public static Set<WorkflowVersionState> mapVersionStateFilter(String versionStateFilter) { + Set<WorkflowVersionState> filter; + try { + filter = versionStateFilter == null ? null : + Arrays.stream(versionStateFilter.split(",")).map(WorkflowVersionState::valueOf) + .collect(Collectors.toSet()); + } catch (Exception e) { + LOGGER.info( + "version state filter value is invalid and cannot be mapped to a set of version states, therefore it is set to empty set"); + filter = Collections.emptySet(); + } + return filter; + } +} diff --git a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java index 0a06ce79..44ce90ae 100644 --- a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java +++ b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java @@ -1,5 +1,6 @@ package org.onap.sdc.workflow.api; +import static org.onap.sdc.workflow.RestUtils.mapVersionStateFilter; import static org.onap.sdc.workflow.api.RestConstants.SIZE_DEFAULT; import static org.onap.sdc.workflow.api.RestConstants.SORT_FIELD_NAME; import static org.onap.sdc.workflow.api.RestConstants.SORT_PARAM; @@ -10,16 +11,11 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import java.util.Arrays; -import java.util.Collections; import java.util.Set; -import java.util.stream.Collectors; import org.onap.sdc.workflow.api.types.CollectionWrapper; import org.onap.sdc.workflow.persistence.types.Workflow; -import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; import org.onap.sdc.workflow.services.WorkflowManager; import org.onap.sdc.workflow.services.exceptions.InvalidPaginationParameterException; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.domain.PageRequest; @@ -46,7 +42,6 @@ import org.springframework.web.bind.annotation.RestController; @RestController("workflowController") public class WorkflowController { - private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowController.class); private final WorkflowManager workflowManager; @Autowired @@ -63,19 +58,8 @@ public class WorkflowController { @SortDefault.SortDefaults({@SortDefault(sort = SORT_FIELD_NAME, direction = Sort.Direction.ASC)}) Pageable pageable, @RequestHeader(USER_ID_HEADER_PARAM) String user) { PageRequest pageRequest = createPageRequest(pageable); - - Set<WorkflowVersionState> filter; - try { - filter = versionStateFilter == null ? null : - Arrays.stream(versionStateFilter.split(",")).map(WorkflowVersionState::valueOf) - .collect(Collectors.toSet()); - } catch (Exception e) { - LOGGER.info("Invalid versionState filter value - return empty list of workflows"); - return new CollectionWrapper<>(Collections.emptyList()); - } - return new CollectionWrapper<>(pageRequest.getPageSize(), pageRequest.getPageNumber(), - workflowManager.list(filter, pageRequest)); + workflowManager.list(mapVersionStateFilter(versionStateFilter), pageRequest)); } @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @@ -103,6 +87,7 @@ public class WorkflowController { return workflow; } + private PageRequest createPageRequest(Pageable pageable) { Set<String> validSortFields = ImmutableSet.of(SORT_FIELD_NAME); Sort sort = pageable.getSort(); diff --git a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java index e7ee7571..57b8e13b 100644 --- a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java +++ b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java @@ -1,22 +1,16 @@ package org.onap.sdc.workflow.api; +import static org.onap.sdc.workflow.RestUtils.mapVersionStateFilter; import static org.onap.sdc.workflow.api.RestConstants.USER_ID_HEADER_PARAM; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; -import java.util.Arrays; -import java.util.Collections; -import java.util.Set; -import java.util.stream.Collectors; import org.onap.sdc.workflow.api.types.CollectionWrapper; import org.onap.sdc.workflow.api.types.VersionStateDto; import org.onap.sdc.workflow.persistence.types.ArtifactEntity; import org.onap.sdc.workflow.persistence.types.WorkflowVersion; -import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; import org.onap.sdc.workflow.services.WorkflowVersionManager; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.core.io.InputStreamResource; @@ -42,7 +36,6 @@ import org.springframework.web.multipart.MultipartFile; @RestController("workflowsVersionController") public class WorkflowVersionController { - private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowVersionController.class); private final WorkflowVersionManager workflowVersionManager; @Autowired @@ -57,16 +50,7 @@ public class WorkflowVersionController { @ApiParam(value = "Filter by state", allowableValues = "DRAFT,CERTIFIED") @RequestParam(value = "state", required = false) String stateFilter, @RequestHeader(USER_ID_HEADER_PARAM) String user) { - Set<WorkflowVersionState> filter; - try { - filter = stateFilter == null ? null : - Arrays.stream(stateFilter.split(",")).map(WorkflowVersionState::valueOf) - .collect(Collectors.toSet()); - } catch (Exception e) { - LOGGER.info("Invalid state filter value - return empty list of workflow versions"); - return new CollectionWrapper<>(Collections.emptyList()); - } - return new CollectionWrapper<>(workflowVersionManager.list(workflowId, filter)); + return new CollectionWrapper<>(workflowVersionManager.list(workflowId, mapVersionStateFilter(stateFilter))); } @PostMapping diff --git a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java index 4ce7d9f3..ee966fa4 100644 --- a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java +++ b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java @@ -29,7 +29,6 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; @Service("workflowManager") public class WorkflowManagerImpl implements WorkflowManager { @@ -37,7 +36,7 @@ public class WorkflowManagerImpl implements WorkflowManager { public static final String WORKFLOW_TYPE = "WORKFLOW"; private static final String WORKFLOW_NOT_FOUND_ERROR_MSG = "Workflow with id '%s' does not exist"; private static final String WORKFLOW_NAME_UNIQUE_TYPE = "WORKFLOW_NAME"; - static final Predicate<Item> WORKFLOW_ITEM_FILTER = item -> WORKFLOW_TYPE.equals(item.getType()); + private static final Predicate<Item> WORKFLOW_ITEM_FILTER = item -> WORKFLOW_TYPE.equals(item.getType()); private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowManagerImpl.class); private final ItemManager itemManager; @@ -58,14 +57,17 @@ public class WorkflowManagerImpl implements WorkflowManager { @Override public Collection<Workflow> list(Set<WorkflowVersionState> versionStatesFilter, Pageable pageRequest) { Set<VersionStatus> versionStatusesFilter = - CollectionUtils.isEmpty(versionStatesFilter) ? Collections.emptySet() : + versionStatesFilter == null ? null : versionStatesFilter.stream().map(versionStateMapper::workflowVersionStateToVersionStatus) .collect(Collectors.toSet()); + List<Workflow> workflows = itemManager.list(getFilter(versionStatusesFilter)).stream() .map(workflowMapper::itemToWorkflow) + .sorted(pageRequest.getSort().getOrderFor(SORT_FIELD_NAME).getDirection() + == Sort.Direction.ASC ? getWorkflowsComparator() : + Collections.reverseOrder(getWorkflowsComparator())) .collect(Collectors.toList()); - sortWorkflows(workflows, pageRequest); return applyLimitAndOffset(workflows, pageRequest); } @@ -129,24 +131,14 @@ public class WorkflowManagerImpl implements WorkflowManager { return selectedWorkflows; } - private void sortWorkflows(List<Workflow> workflows, Pageable pageRequest) { - Comparator<Workflow> comparator = getWorkflowsComparator(); - if (pageRequest.getSort().getOrderFor(SORT_FIELD_NAME).getDirection() == Sort.Direction.ASC) { - workflows.sort(comparator); - } else { - workflows.sort(Collections.reverseOrder(comparator)); - } - } - private Comparator<Workflow> getWorkflowsComparator() { //More comparators can be added if required based on sort field name return new WorkflowNameComparator(); } private static Predicate<Item> getFilter(Set<VersionStatus> versionStatuses) { - return CollectionUtils.isEmpty(versionStatuses) - ? WORKFLOW_ITEM_FILTER - : WORKFLOW_ITEM_FILTER.and(item -> item.getVersionStatusCounters().keySet().stream() - .anyMatch(versionStatuses::contains)); + return WORKFLOW_ITEM_FILTER.and(item -> versionStatuses == null + || item.getVersionStatusCounters().keySet().stream() + .anyMatch(versionStatuses::contains)); } } diff --git a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java index a3e775ec..2c1b7894 100644 --- a/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java +++ b/workflow/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowVersionManagerImpl.java @@ -27,15 +27,16 @@ import org.onap.sdc.workflow.services.exceptions.VersionModificationException; import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException; import org.onap.sdc.workflow.services.impl.mappers.VersionMapper; import org.onap.sdc.workflow.services.impl.mappers.VersionStateMapper; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; 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.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; @Service("workflowVersionManager") public class WorkflowVersionManagerImpl implements WorkflowVersionManager { @@ -61,8 +62,15 @@ public class WorkflowVersionManagerImpl implements WorkflowVersionManager { @Override public Collection<WorkflowVersion> list(String workflowId, Set<WorkflowVersionState> stateFilter) { - return versioningManager.list(workflowId).stream().map(versionMapper::versionToWorkflowVersion) - .filter(workflowVersion -> stateFilter.contains(workflowVersion.getState())) + Set<VersionStatus> versionStatusFilter = + stateFilter == null ? null : + stateFilter.stream().map(versionStateMapper::workflowVersionStateToVersionStatus) + .collect(Collectors.toSet()); + + return versioningManager.list(workflowId).stream() + .filter(version -> versionStatusFilter == null || versionStatusFilter.contains( + version.getStatus())) + .map(versionMapper::versionToWorkflowVersion) .peek(workflowVersion -> loadAndAddParameters(workflowId, workflowVersion)) .collect(Collectors.toList()); } diff --git a/workflow/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java b/workflow/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java index fc91dc71..0105fe83 100644 --- a/workflow/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java +++ b/workflow/workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImplTest.java @@ -9,7 +9,6 @@ import static org.mockito.Mockito.verify; import static org.onap.sdc.workflow.TestUtil.createItem; import static org.onap.sdc.workflow.TestUtil.createWorkflow; import static org.onap.sdc.workflow.api.RestConstants.SORT_FIELD_NAME; -import static org.onap.sdc.workflow.services.impl.WorkflowManagerImpl.WORKFLOW_ITEM_FILTER; import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Certified; import java.util.ArrayList; @@ -72,7 +71,7 @@ public class WorkflowManagerImplTest { @Test public void shouldReturnWorkflowVersionList() { - doReturn(ITEMS).when(itemManagerMock).list(WORKFLOW_ITEM_FILTER); + doReturn(ITEMS).when(itemManagerMock).list(any()); for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } @@ -95,8 +94,8 @@ public class WorkflowManagerImplTest { doReturn(MAPPED_WORKFLOWS.get(0)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(0)); doReturn(MAPPED_WORKFLOWS.get(2)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(2)); - Collection<Workflow> workflows = - workflowManager.list(Collections.singleton(WorkflowVersionState.CERTIFIED), createPageRequest(20, 0, Sort.Direction.ASC, SORT_FIELD_NAME)); + Collection<Workflow> workflows = workflowManager.list(Collections.singleton(WorkflowVersionState.CERTIFIED), + createPageRequest(20, 0, Sort.Direction.ASC, SORT_FIELD_NAME)); Map<String, Workflow> workflowById = workflows.stream().collect(Collectors.toMap(Workflow::getId, Function.identity())); @@ -163,7 +162,7 @@ public class WorkflowManagerImplTest { @Test public void shouldListAllWorkflowsWhenLimitAndOffsetAreValid() { PageRequest pageRequest = createPageRequest(5, 0, Sort.Direction.ASC, SORT_FIELD_NAME); - doReturn(ITEMS).when(itemManagerMock).list(WORKFLOW_ITEM_FILTER); + doReturn(ITEMS).when(itemManagerMock).list(any()); for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } @@ -173,7 +172,7 @@ public class WorkflowManagerImplTest { @Test public void shouldListLimitFilteredWorkflowsInFirstOffsetRange() { PageRequest pageRequest = createPageRequest(3, 0, Sort.Direction.ASC, SORT_FIELD_NAME); - doReturn(ITEMS).when(itemManagerMock).list(WORKFLOW_ITEM_FILTER); + doReturn(ITEMS).when(itemManagerMock).list(any()); for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } @@ -183,7 +182,7 @@ public class WorkflowManagerImplTest { @Test public void shouldListLimitFilteredWorkflowsInSecondOffsetRange() { PageRequest pageRequest = createPageRequest(3, 1, Sort.Direction.ASC, SORT_FIELD_NAME); - doReturn(ITEMS).when(itemManagerMock).list(WORKFLOW_ITEM_FILTER); + doReturn(ITEMS).when(itemManagerMock).list(any()); for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } @@ -193,7 +192,7 @@ public class WorkflowManagerImplTest { @Test public void shouldListAllWorkflowsWhenLimitGreaterThanTotalRecordsAndOffsetInRange() { PageRequest pageRequest = createPageRequest(10, 0, Sort.Direction.ASC, SORT_FIELD_NAME); - doReturn(ITEMS).when(itemManagerMock).list(WORKFLOW_ITEM_FILTER); + doReturn(ITEMS).when(itemManagerMock).list(any()); for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } @@ -203,7 +202,7 @@ public class WorkflowManagerImplTest { @Test public void shouldNotListWorkflowsIfOffsetGreaterThanTotalRecords() { PageRequest pageRequest = createPageRequest(3, 6, Sort.Direction.ASC, SORT_FIELD_NAME); - doReturn(ITEMS).when(itemManagerMock).list(WORKFLOW_ITEM_FILTER); + doReturn(ITEMS).when(itemManagerMock).list(any()); for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } @@ -213,7 +212,7 @@ public class WorkflowManagerImplTest { @Test public void shouldNotListWorkflowsBothLimitAndOffsetGreaterThanTotalRecords() { PageRequest pageRequest = createPageRequest(10, 10, Sort.Direction.ASC, SORT_FIELD_NAME); - doReturn(ITEMS).when(itemManagerMock).list(WORKFLOW_ITEM_FILTER); + doReturn(ITEMS).when(itemManagerMock).list(any()); for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } @@ -223,7 +222,7 @@ public class WorkflowManagerImplTest { @Test public void shouldListLimitOffsetAppliedWorkflowsSortedInDescOrder() { PageRequest pageRequest = createPageRequest(2, 1, Sort.Direction.DESC, SORT_FIELD_NAME); - doReturn(ITEMS).when(itemManagerMock).list(WORKFLOW_ITEM_FILTER); + doReturn(ITEMS).when(itemManagerMock).list(any()); for (int i = 0; i < ITEMS.size(); i++) { doReturn(MAPPED_WORKFLOWS.get(i)).when(workflowMapperMock).itemToWorkflow(ITEMS.get(i)); } |