Skip to content

Commit

Permalink
feat(rest): added pagination for vulnerability tracking status page.
Browse files Browse the repository at this point in the history
Signed-off-by: Rudra Chopra <prabhuchopra@gmail.com>
  • Loading branch information
rudra-superrr committed Apr 13, 2024
1 parent 3746fa1 commit 944a716
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -508,10 +509,11 @@ private Set<Release> getReleaseRelationsInfo(Vulnerability vulnerability, User u
}
)
@GetMapping(value = VULNERABILITIES_URL + "/trackingStatus" + "/{projectId}")
public ResponseEntity<List<Map<String, String>>> getVulnerabilitiesTrackingStatus(
public ResponseEntity<Map<String, Object>> getVulnerabilitiesTrackingStatus(
@Parameter(description = "The ID of the project to be retrieved.") @PathVariable("projectId") String projectId,
@Parameter(description = "Based on sort param data will get sorted.") @RequestParam(value = "sortBy", required = false, defaultValue = "name") String sortBy,
@Parameter(description = "Based on sortorder param data will get sorted.") @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder)
@Parameter(description = "Based on sortorder param data will get sorted.") @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder,
Pageable pageable)
throws TException {
List<Map<String, String>> vulTrackingStatusList = new ArrayList<>();
try {
Expand Down Expand Up @@ -540,15 +542,34 @@ public ResponseEntity<List<Map<String, String>>> getVulnerabilitiesTrackingStatu
sortBy = "name";
}
getSortedList(sortBy, sortOrder, vulTrackingStatusList);

return ResponseEntity.ok(vulTrackingStatusList);
Map<String, Object> createPagination = createPaginationMetadata(pageable, vulTrackingStatusList);
return ResponseEntity.ok(createPagination);
} catch (SW360Exception e) {
throw new TException(e.why);
} catch (Exception ex) {
throw new TException(ex.getMessage());
}
}

public Map<String, Object> createPaginationMetadata(Pageable pageable, List<Map<String, String>> listOfItems) {
int pageSize = pageable.getPageSize();
int pageNumber = pageable.getPageNumber();
int start = pageNumber * pageSize;
int end = Math.min(start + pageSize, listOfItems.size());
List<Map<String, String>> paginatedList = listOfItems.subList(start, end);
int totalPages = (int) Math.ceil((double) listOfItems.size() / pageSize);
Map<String, Integer> pagination = Map.of(
"size", pageSize,
"totalElements", listOfItems.size(),
"totalPages", totalPages,
"number", pageNumber
);
Map<String, Object> responseBody = new HashMap<>();
responseBody.put("page", pagination);
responseBody.put("vulnerabilityTrackingStatus", paginatedList);
return responseBody;
}

private void getSortedList(String sortBy, String sortOrder, List<Map<String, String>> vulTrackingStatusList) {
Collections.sort(vulTrackingStatusList, new Comparator<Map<String, String>>() {
public int compare(Map<String, String> m1, Map<String, String> m2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,23 +540,32 @@ public void should_document_create_release_vulnerability_relation() throws Excep
public void should_document_get_vulnerabilities_tracking_status() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(get("/api/vulnerabilities/trackingStatus/" + project.getId())
.param("page", "0")
.param("page_entries", "5")
.param("sortBy", "name")
.param("sortOrder", "desc")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andDo(this.documentationHandler.document(
requestParameters(
parameterWithName("page").description("Page number"),
parameterWithName("page_entries").description("Amount of items per page"),
parameterWithName("sortBy").description("Based on sortBy param sorting should happens"),
parameterWithName("sortOrder").description("sortOrder would be asc/desc")
),
responseFields(
fieldWithPath("[].name").description("The name of the release"),
fieldWithPath("[].projectOrigin").description("The project origin"),
fieldWithPath("[].releaseId").description("The id of the release"),
fieldWithPath("[].shortStatus").description("The short status"),
fieldWithPath("[].svmTrackingStatus").description("The SVM tracking status"),
fieldWithPath("[].type").description("The component type")
fieldWithPath("vulnerabilityTrackingStatus[].name").description("The name of the release"),
fieldWithPath("vulnerabilityTrackingStatus[].projectOrigin").description("The project origin"),
fieldWithPath("vulnerabilityTrackingStatus[].releaseId").description("The id of the release"),
fieldWithPath("vulnerabilityTrackingStatus[].shortStatus").description("The short status"),
fieldWithPath("vulnerabilityTrackingStatus[].svmTrackingStatus").description("The SVM tracking status"),
fieldWithPath("vulnerabilityTrackingStatus[].type").description("The component type"),
fieldWithPath("page").description("Additional paging information"),
fieldWithPath("page.size").description("Number of items per page"),
fieldWithPath("page.totalElements").description("Total number of items"),
fieldWithPath("page.totalPages").description("Total number of pages"),
fieldWithPath("page.number").description("Number of the current page")
)));
}
}

0 comments on commit 944a716

Please sign in to comment.