diff options
author | 2018-08-02 14:32:17 +0300 | |
---|---|---|
committer | 2018-08-02 14:32:17 +0300 | |
commit | d7d290e97e8403e0feffaef20fa7de4fa0e84b91 (patch) | |
tree | f937436222449f8b7a7a480eb857c5ea4cd44e29 /workflow-designer-be/src/main/java/org | |
parent | 9c03995ead1a0f7732c1ce4223c540224b3ba837 (diff) |
Refactor paging and sorting unit tests
Change-Id: I6c6b1e40d0069b78ef4180b719dbc8107732982d
Issue-ID: SDC-1483
Signed-off-by: talig <talig@amdocs.com>
Diffstat (limited to 'workflow-designer-be/src/main/java/org')
9 files changed, 91 insertions, 74 deletions
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/RestUtils.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/RestUtils.java deleted file mode 100644 index d8577c1c..00000000 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/RestUtils.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright © 2018 European Support Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onap.sdc.workflow.api; - -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> formatVersionStates(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 formatted to a set of version states, therefore it is set to empty set"); - filter = Collections.emptySet(); - } - return filter; - } -} 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 77f6e6df..690b2075 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 @@ -20,7 +20,6 @@ import static org.onap.sdc.workflow.api.RestParams.LIMIT; import static org.onap.sdc.workflow.api.RestParams.OFFSET; import static org.onap.sdc.workflow.api.RestParams.SORT; import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER; -import static org.onap.sdc.workflow.api.RestUtils.formatVersionStates; import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_LIMIT; import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_OFFSET; @@ -31,6 +30,7 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.onap.sdc.workflow.api.types.Paging; import org.onap.sdc.workflow.api.types.Sorting; +import org.onap.sdc.workflow.api.types.VersionStatesFormatter; import org.onap.sdc.workflow.persistence.types.Workflow; import org.onap.sdc.workflow.services.WorkflowManager; import org.onap.sdc.workflow.services.WorkflowVersionManager; @@ -53,6 +53,7 @@ import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import springfox.documentation.annotations.ApiIgnore; @RequestMapping("/workflows") @Api("Workflows") @@ -71,6 +72,8 @@ public class WorkflowController { @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation("List workflows") @ApiImplicitParams({ + @ApiImplicitParam(name = "versionState", dataType = "string", paramType = "query", + allowableValues = "DRAFT,CERTIFIED", value = "Filter by version state"), @ApiImplicitParam(name = OFFSET, dataType = "string", paramType = "query", defaultValue = "0", value = "Index of the starting item"), @ApiImplicitParam(name = LIMIT, dataType = "string", paramType = "query", defaultValue = "200", @@ -78,12 +81,11 @@ public class WorkflowController { @ApiImplicitParam(name = SORT, dataType = "string", paramType = "query", defaultValue = "name:asc", value = "Sorting criteria in the format: property:(asc|desc). Default sort order is ascending.", allowableValues = "name:asc,name:desc")}) - public Page<Workflow> list(@ApiParam(value = "Filter by version state", allowableValues = "DRAFT,CERTIFIED") - @RequestParam(name = "versionState", required = false) String versionStateFilter, - @ApiParam(hidden = true) Paging paging, - @ApiParam(hidden = true) Sorting sorting, + public Page<Workflow> list(@ApiIgnore VersionStatesFormatter versionStateFilter, + @ApiIgnore Paging paging, + @ApiIgnore Sorting sorting, @RequestHeader(USER_ID_HEADER) String user) { - return workflowManager.list(formatVersionStates(versionStateFilter), initRequestSpec(paging, sorting)); + return workflowManager.list(versionStateFilter.getVersionStates(), initRequestSpec(paging, sorting)); } @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java index ba15f9f7..df4c9fe3 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowVersionController.java @@ -16,14 +16,14 @@ package org.onap.sdc.workflow.api; -import static org.onap.sdc.workflow.api.RestUtils.formatVersionStates; import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; import org.onap.sdc.workflow.api.types.CollectionResponse; import org.onap.sdc.workflow.api.types.VersionStateDto; +import org.onap.sdc.workflow.api.types.VersionStatesFormatter; import org.onap.sdc.workflow.persistence.types.ArtifactEntity; import org.onap.sdc.workflow.persistence.types.WorkflowVersion; import org.onap.sdc.workflow.services.WorkflowVersionManager; @@ -50,6 +50,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import springfox.documentation.annotations.ApiIgnore; @RequestMapping("/workflows/{workflowId}/versions") @Api("Workflow versions") @@ -71,13 +72,14 @@ public class WorkflowVersionController { this.validator = validator; } + @ApiImplicitParam(name = "state", dataType = "string", paramType = "query", + allowableValues = "DRAFT,CERTIFIED", value = "Filter by state") @GetMapping @ApiOperation("List workflow versions") public CollectionResponse<WorkflowVersion> list(@PathVariable("workflowId") String workflowId, - @ApiParam(value = "Filter by state", allowableValues = "DRAFT,CERTIFIED") - @RequestParam(value = "state", required = false) String stateFilter, + @ApiIgnore VersionStatesFormatter stateFilter, @RequestHeader(USER_ID_HEADER) String user) { - return new CollectionResponse<>(workflowVersionManager.list(workflowId, formatVersionStates(stateFilter))); + return new CollectionResponse<>(workflowVersionManager.list(workflowId, stateFilter.getVersionStates())); } @PostMapping diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/Paging.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/Paging.java index 3d91c8e0..0a27e1e6 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/Paging.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/Paging.java @@ -16,14 +16,16 @@ public class Paging { } public void setLimit(String limit) { - getIntValue(limit).map(integer -> integer > MAX_LIMIT ? MAX_LIMIT : integer) - .ifPresent(integer -> this.limit = integer); + getIntValue(limit).map(integer -> integer > MAX_LIMIT ? MAX_LIMIT : integer).ifPresent(integer -> { + if (integer != 0) { + this.limit = integer; + } + }); } - private Optional<Integer> getIntValue(String value) { - int intValue; + private static Optional<Integer> getIntValue(String value) { try { - intValue = Integer.parseInt(value); + int intValue = Integer.parseInt(value); return intValue < 0 ? Optional.empty() : Optional.of(intValue); } catch (NumberFormatException e) { return Optional.empty(); diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/Sorting.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/Sorting.java index 7bb43d0b..f02a05fe 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/Sorting.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/Sorting.java @@ -18,11 +18,11 @@ public class Sorting { private List<Sort> sorts = Collections.emptyList(); public void setSort(String sortString) { - this.sorts = Arrays.stream(sortString.split(SORTS_DELIMITER)).map(this::formatSort).filter(Objects::nonNull) + this.sorts = Arrays.stream(sortString.split(SORTS_DELIMITER)).map(Sorting::formatSort).filter(Objects::nonNull) .collect(Collectors.toList()); } - private Sort formatSort(String sort) { + private static Sort formatSort(String sort) { String[] tokens = sort.split(DIRECTION_DELIMITER); try { return new Sort(tokens[0], ASCENDING_ORDER.equalsIgnoreCase(tokens[1])); diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionStatesFormatter.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionStatesFormatter.java new file mode 100644 index 00000000..5467dee8 --- /dev/null +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/VersionStatesFormatter.java @@ -0,0 +1,36 @@ +package org.onap.sdc.workflow.api.types; + +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; +import lombok.Getter; +import org.onap.sdc.workflow.persistence.types.WorkflowVersionState; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +@Getter +public class VersionStatesFormatter { + + private static final Logger LOGGER = LoggerFactory.getLogger(VersionStatesFormatter.class); + + private Set<WorkflowVersionState> versionStates = null; + + public void setVersionState(String value) { + this.versionStates = formatString(value); + } + + public void setState(String value) { + this.versionStates = formatString(value); + } + + private static Set<WorkflowVersionState> formatString(String value) { + try { + return value == null ? null : Arrays.stream(value.split(",")).map(WorkflowVersionState::valueOf) + .collect(Collectors.toSet()); + } catch (Exception ignore) { + LOGGER.info( + "value is invalid and cannot be formatted to a set of version states, therefore it set to null"); + return null; + } + } +} diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java index 23979a5d..a3092042 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java @@ -135,14 +135,14 @@ public class WorkflowManagerImpl implements WorkflowManager { itemManager.update(item); } - private RequestSpec getRequestSpec(RequestSpec requestSpec) { + private static RequestSpec getRequestSpec(RequestSpec requestSpec) { if (requestSpec == null) { return WORKSPACES_DEFAULT_REQUEST_SPEC; } if (requestSpec.getPaging() == null) { requestSpec.setPaging(WORKSPACES_DEFAULT_REQUEST_SPEC.getPaging()); - } else if (requestSpec.getPaging().getLimit() > MAX_LIMIT) { - requestSpec.getPaging().setLimit(MAX_LIMIT); + } else { + handlePagingRequestValues(requestSpec.getPaging()); } if (requestSpec.getSorting() == null) { requestSpec.setSorting(WORKSPACES_DEFAULT_REQUEST_SPEC.getSorting()); @@ -150,7 +150,18 @@ public class WorkflowManagerImpl implements WorkflowManager { return requestSpec; } - private Comparator<Workflow> getWorkflowComparator(SortingRequest sorting) { + private static void handlePagingRequestValues(PagingRequest paging) { + if (paging.getOffset() == null) { + paging.setOffset(DEFAULT_OFFSET); + } + if (paging.getLimit() == null) { + paging.setLimit(DEFAULT_LIMIT); + } else if (paging.getLimit() > MAX_LIMIT) { + paging.setLimit(MAX_LIMIT); + } + } + + private static Comparator<Workflow> getWorkflowComparator(SortingRequest sorting) { Boolean byNameAscending = sorting.getSorts().stream() .filter(sort -> WORKSPACES_SORT_PROPERTY.equalsIgnoreCase(sort.getProperty())) .findFirst().map(Sort::isAscendingOrder).orElse(true); diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/types/PagingRequest.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/types/PagingRequest.java index f06822fa..da46cc2c 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/types/PagingRequest.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/types/PagingRequest.java @@ -1,15 +1,23 @@ package org.onap.sdc.workflow.services.types; -import lombok.Data; +import lombok.Getter; -@Data +@Getter public class PagingRequest { - private int offset; - private int limit; + private Integer offset; + private Integer limit; public PagingRequest(int offset, int limit) { - this.offset = offset; - this.limit = limit; + setOffset(offset); + setLimit(limit); + } + + public void setOffset(int offset) { + this.offset = offset < 0 ? null : offset; + } + + public void setLimit(int limit) { + this.limit = limit <= 0 ? null : limit; } } diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/types/Sort.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/types/Sort.java index 563c9ad0..88151a0e 100644 --- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/types/Sort.java +++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/types/Sort.java @@ -1,8 +1,10 @@ package org.onap.sdc.workflow.services.types; +import lombok.EqualsAndHashCode; import lombok.Getter; @Getter +@EqualsAndHashCode public class Sort { private String property; |