@Test public void getIssuesDownloadTest() throws Exception { when(downloadRepository.getFiltersFromDb(anyInt())).thenReturn( CommonTestUtil.getListMapObject()); ReflectionTestUtils.setField(downloadController, "serviceDnsName", "serviceDnsName"); String response = "{\"data\":{\"response\":[{\"PolicyName\":" + "\"EC2 instance should not have guard duty findings\",\"IssueId\":\"IssueId\"," + "\"ResourceId\":\"ResourceId\",\"Severity\":\"high\",\"RuleCategory\":\"security\"," + "\"AccountName\":\"AccountName\",\"AccountId\":\"AccountId\",\"Region\":\"Region\"," + "\"Application\":\"Application\",\"Environment\":\"Environment\",\"CreatedOn\":" + "\"2018-07-06T18:23:15.486Z\",\"ModifiedOn\":\"2018-07-10T06:23:33.911Z\"," + "\"Status\":\"open\",\"resourcetType\":\"ec2\",\"description\":\"Guard Duty findings exists!!\"," + "\"nonDisplayableAttributes\":{\"RuleId\":\"RuleId\"," + "\"PolicyId\":\"PolicyId\"}}],\"total\":106762},\"message\":\"success\"}"; mockStatic(PacHttpUtils.class); when(PacHttpUtils.getBase64AuthorizationHeader(anyObject())).thenReturn("124"); when(PacHttpUtils.doHttpsPost(anyString(), anyString(),anyObject())).thenReturn( response); servletResponse = mock(HttpServletResponse.class); servletRequest = mock(HttpServletRequest.class); assertThat(downloadController.getIssuesDownload(servletRequest,servletResponse, "csv", 1, CommonTestUtil.getRequest()), is(notNullValue())); assertThat(downloadController.getIssuesDownload(servletRequest,servletResponse, "", 1, CommonTestUtil.getRequestEmpty()), is(notNullValue())); assertThat(downloadController.getIssuesDownload(servletRequest,servletResponse, "", 1, CommonTestUtil.getWithoutSizeRequest()), is(notNullValue())); when(downloadRepository.getFiltersFromDb(anyInt())).thenReturn(new ArrayList<Map<String,Object>>()); assertThat(downloadController.getIssuesDownload(servletRequest,servletResponse, "csv", 1, CommonTestUtil.getRequest()), is(notNullValue())); when(downloadRepository.getFiltersFromDb(anyInt())).thenThrow( new DataException()); ResponseEntity<Object> responseObj = downloadController .getIssuesDownload(servletRequest,servletResponse, "csv", 1, CommonTestUtil.getRequest()); assertTrue(responseObj.getStatusCode() == HttpStatus.FORBIDDEN); }
@ApiOperation(httpMethod = "POST", value = "Download Service Details in CSV or Excel") @RequestMapping(path = "/v1/download/services", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<Object> getIssuesDownload(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, @ApiParam(value = "Provide Download File Format (excel or csv). Default is csv", required = false) @RequestParam(name = "fileFormat", required = false) String fileFormat, @ApiParam(value = "Provide serviceId 1-policyViolationList,2-policyOverviewList,3-patchingList,4-taggingList,5-certificateList,6-vulnerabilitiesList", required = false) @RequestParam("serviceId") int serviceId, @ApiParam(value = "Provide Service Search Filter", required = false) @RequestBody(required = false) Request request) throws Exception { try { String assetGroup = request.getAg(); JsonArray responseArray = null; String serviceName = null; String serviceEndpoint = null; if (Strings.isNullOrEmpty(assetGroup)) { return ResponseUtils.buildFailureResponse(new Exception(ASSET_MANDATORY)); } if (request.getSize() <= 0 || request.getSize() > 100000) { return ResponseUtils.buildFailureResponse(new Exception( "enter valid size/ you can max download 100k records")); } List<Map<String, Object>> filterMap = downloadRepository.getFiltersFromDb(serviceId); if (!filterMap.isEmpty()) { for (Map<String, Object> filterMethodName : filterMap) { if (filterMethodName.get("serviceName") != null && filterMethodName.get("serviceEndpoint") != null) { serviceName = filterMethodName.get("serviceName").toString(); serviceEndpoint = filterMethodName.get("serviceEndpoint").toString(); } } Gson gson = new GsonBuilder().disableHtmlEscaping().create(); String jsonString = gson.toJson(request); if (!StringUtils.isEmpty(serviceEndpoint) && !StringUtils.isEmpty(serviceDnsName) && !StringUtils.isEmpty(jsonString)) { String serviceResponse = PacHttpUtils.doHttpsPost(serviceDnsName+serviceEndpoint, jsonString, getTokenHeader(servletRequest)); if(!StringUtil.isNullOrEmpty(serviceResponse)){ responseArray = getServiceDetails(serviceResponse); downloadFileService.downloadData(servletResponse, responseArray, fileFormat, serviceName); return new ResponseEntity<>(HttpStatus.OK); }else{ return new ResponseEntity<>(HttpStatus.FORBIDDEN); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceEndpoint or urlParameters")); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceName and serviceEndpoint")); } } catch (Exception e) { LOGGER.error(e.toString()); return new ResponseEntity<>(HttpStatus.FORBIDDEN); } }
DownloadController implements Constants { @ApiOperation(httpMethod = "POST", value = "Download Service Details in CSV or Excel") @RequestMapping(path = "/v1/download/services", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<Object> getIssuesDownload(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, @ApiParam(value = "Provide Download File Format (excel or csv). Default is csv", required = false) @RequestParam(name = "fileFormat", required = false) String fileFormat, @ApiParam(value = "Provide serviceId 1-policyViolationList,2-policyOverviewList,3-patchingList,4-taggingList,5-certificateList,6-vulnerabilitiesList", required = false) @RequestParam("serviceId") int serviceId, @ApiParam(value = "Provide Service Search Filter", required = false) @RequestBody(required = false) Request request) throws Exception { try { String assetGroup = request.getAg(); JsonArray responseArray = null; String serviceName = null; String serviceEndpoint = null; if (Strings.isNullOrEmpty(assetGroup)) { return ResponseUtils.buildFailureResponse(new Exception(ASSET_MANDATORY)); } if (request.getSize() <= 0 || request.getSize() > 100000) { return ResponseUtils.buildFailureResponse(new Exception( "enter valid size/ you can max download 100k records")); } List<Map<String, Object>> filterMap = downloadRepository.getFiltersFromDb(serviceId); if (!filterMap.isEmpty()) { for (Map<String, Object> filterMethodName : filterMap) { if (filterMethodName.get("serviceName") != null && filterMethodName.get("serviceEndpoint") != null) { serviceName = filterMethodName.get("serviceName").toString(); serviceEndpoint = filterMethodName.get("serviceEndpoint").toString(); } } Gson gson = new GsonBuilder().disableHtmlEscaping().create(); String jsonString = gson.toJson(request); if (!StringUtils.isEmpty(serviceEndpoint) && !StringUtils.isEmpty(serviceDnsName) && !StringUtils.isEmpty(jsonString)) { String serviceResponse = PacHttpUtils.doHttpsPost(serviceDnsName+serviceEndpoint, jsonString, getTokenHeader(servletRequest)); if(!StringUtil.isNullOrEmpty(serviceResponse)){ responseArray = getServiceDetails(serviceResponse); downloadFileService.downloadData(servletResponse, responseArray, fileFormat, serviceName); return new ResponseEntity<>(HttpStatus.OK); }else{ return new ResponseEntity<>(HttpStatus.FORBIDDEN); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceEndpoint or urlParameters")); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceName and serviceEndpoint")); } } catch (Exception e) { LOGGER.error(e.toString()); return new ResponseEntity<>(HttpStatus.FORBIDDEN); } } }
DownloadController implements Constants { @ApiOperation(httpMethod = "POST", value = "Download Service Details in CSV or Excel") @RequestMapping(path = "/v1/download/services", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<Object> getIssuesDownload(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, @ApiParam(value = "Provide Download File Format (excel or csv). Default is csv", required = false) @RequestParam(name = "fileFormat", required = false) String fileFormat, @ApiParam(value = "Provide serviceId 1-policyViolationList,2-policyOverviewList,3-patchingList,4-taggingList,5-certificateList,6-vulnerabilitiesList", required = false) @RequestParam("serviceId") int serviceId, @ApiParam(value = "Provide Service Search Filter", required = false) @RequestBody(required = false) Request request) throws Exception { try { String assetGroup = request.getAg(); JsonArray responseArray = null; String serviceName = null; String serviceEndpoint = null; if (Strings.isNullOrEmpty(assetGroup)) { return ResponseUtils.buildFailureResponse(new Exception(ASSET_MANDATORY)); } if (request.getSize() <= 0 || request.getSize() > 100000) { return ResponseUtils.buildFailureResponse(new Exception( "enter valid size/ you can max download 100k records")); } List<Map<String, Object>> filterMap = downloadRepository.getFiltersFromDb(serviceId); if (!filterMap.isEmpty()) { for (Map<String, Object> filterMethodName : filterMap) { if (filterMethodName.get("serviceName") != null && filterMethodName.get("serviceEndpoint") != null) { serviceName = filterMethodName.get("serviceName").toString(); serviceEndpoint = filterMethodName.get("serviceEndpoint").toString(); } } Gson gson = new GsonBuilder().disableHtmlEscaping().create(); String jsonString = gson.toJson(request); if (!StringUtils.isEmpty(serviceEndpoint) && !StringUtils.isEmpty(serviceDnsName) && !StringUtils.isEmpty(jsonString)) { String serviceResponse = PacHttpUtils.doHttpsPost(serviceDnsName+serviceEndpoint, jsonString, getTokenHeader(servletRequest)); if(!StringUtil.isNullOrEmpty(serviceResponse)){ responseArray = getServiceDetails(serviceResponse); downloadFileService.downloadData(servletResponse, responseArray, fileFormat, serviceName); return new ResponseEntity<>(HttpStatus.OK); }else{ return new ResponseEntity<>(HttpStatus.FORBIDDEN); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceEndpoint or urlParameters")); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceName and serviceEndpoint")); } } catch (Exception e) { LOGGER.error(e.toString()); return new ResponseEntity<>(HttpStatus.FORBIDDEN); } }  }
DownloadController implements Constants { @ApiOperation(httpMethod = "POST", value = "Download Service Details in CSV or Excel") @RequestMapping(path = "/v1/download/services", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<Object> getIssuesDownload(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, @ApiParam(value = "Provide Download File Format (excel or csv). Default is csv", required = false) @RequestParam(name = "fileFormat", required = false) String fileFormat, @ApiParam(value = "Provide serviceId 1-policyViolationList,2-policyOverviewList,3-patchingList,4-taggingList,5-certificateList,6-vulnerabilitiesList", required = false) @RequestParam("serviceId") int serviceId, @ApiParam(value = "Provide Service Search Filter", required = false) @RequestBody(required = false) Request request) throws Exception { try { String assetGroup = request.getAg(); JsonArray responseArray = null; String serviceName = null; String serviceEndpoint = null; if (Strings.isNullOrEmpty(assetGroup)) { return ResponseUtils.buildFailureResponse(new Exception(ASSET_MANDATORY)); } if (request.getSize() <= 0 || request.getSize() > 100000) { return ResponseUtils.buildFailureResponse(new Exception( "enter valid size/ you can max download 100k records")); } List<Map<String, Object>> filterMap = downloadRepository.getFiltersFromDb(serviceId); if (!filterMap.isEmpty()) { for (Map<String, Object> filterMethodName : filterMap) { if (filterMethodName.get("serviceName") != null && filterMethodName.get("serviceEndpoint") != null) { serviceName = filterMethodName.get("serviceName").toString(); serviceEndpoint = filterMethodName.get("serviceEndpoint").toString(); } } Gson gson = new GsonBuilder().disableHtmlEscaping().create(); String jsonString = gson.toJson(request); if (!StringUtils.isEmpty(serviceEndpoint) && !StringUtils.isEmpty(serviceDnsName) && !StringUtils.isEmpty(jsonString)) { String serviceResponse = PacHttpUtils.doHttpsPost(serviceDnsName+serviceEndpoint, jsonString, getTokenHeader(servletRequest)); if(!StringUtil.isNullOrEmpty(serviceResponse)){ responseArray = getServiceDetails(serviceResponse); downloadFileService.downloadData(servletResponse, responseArray, fileFormat, serviceName); return new ResponseEntity<>(HttpStatus.OK); }else{ return new ResponseEntity<>(HttpStatus.FORBIDDEN); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceEndpoint or urlParameters")); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceName and serviceEndpoint")); } } catch (Exception e) { LOGGER.error(e.toString()); return new ResponseEntity<>(HttpStatus.FORBIDDEN); } }  @ApiOperation(httpMethod = "POST", value = "Download Service Details in CSV or Excel") @RequestMapping(path = "/v1/download/services", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE }) ResponseEntity<Object> getIssuesDownload(final HttpServletRequest servletRequest,
            final HttpServletResponse servletResponse,
            @ApiParam(value = "Provide Download File Format (excel or csv). Default is csv", required = false) @RequestParam(name = "fileFormat", required = false) String fileFormat,
            @ApiParam(value = "Provide serviceId 1-policyViolationList,2-policyOverviewList,3-patchingList,4-taggingList,5-certificateList,6-vulnerabilitiesList", required = false) @RequestParam("serviceId") int serviceId,
            @ApiParam(value = "Provide Service Search Filter", required = false) @RequestBody(required = false) Request request); }
DownloadController implements Constants { @ApiOperation(httpMethod = "POST", value = "Download Service Details in CSV or Excel") @RequestMapping(path = "/v1/download/services", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<Object> getIssuesDownload(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, @ApiParam(value = "Provide Download File Format (excel or csv). Default is csv", required = false) @RequestParam(name = "fileFormat", required = false) String fileFormat, @ApiParam(value = "Provide serviceId 1-policyViolationList,2-policyOverviewList,3-patchingList,4-taggingList,5-certificateList,6-vulnerabilitiesList", required = false) @RequestParam("serviceId") int serviceId, @ApiParam(value = "Provide Service Search Filter", required = false) @RequestBody(required = false) Request request) throws Exception { try { String assetGroup = request.getAg(); JsonArray responseArray = null; String serviceName = null; String serviceEndpoint = null; if (Strings.isNullOrEmpty(assetGroup)) { return ResponseUtils.buildFailureResponse(new Exception(ASSET_MANDATORY)); } if (request.getSize() <= 0 || request.getSize() > 100000) { return ResponseUtils.buildFailureResponse(new Exception( "enter valid size/ you can max download 100k records")); } List<Map<String, Object>> filterMap = downloadRepository.getFiltersFromDb(serviceId); if (!filterMap.isEmpty()) { for (Map<String, Object> filterMethodName : filterMap) { if (filterMethodName.get("serviceName") != null && filterMethodName.get("serviceEndpoint") != null) { serviceName = filterMethodName.get("serviceName").toString(); serviceEndpoint = filterMethodName.get("serviceEndpoint").toString(); } } Gson gson = new GsonBuilder().disableHtmlEscaping().create(); String jsonString = gson.toJson(request); if (!StringUtils.isEmpty(serviceEndpoint) && !StringUtils.isEmpty(serviceDnsName) && !StringUtils.isEmpty(jsonString)) { String serviceResponse = PacHttpUtils.doHttpsPost(serviceDnsName+serviceEndpoint, jsonString, getTokenHeader(servletRequest)); if(!StringUtil.isNullOrEmpty(serviceResponse)){ responseArray = getServiceDetails(serviceResponse); downloadFileService.downloadData(servletResponse, responseArray, fileFormat, serviceName); return new ResponseEntity<>(HttpStatus.OK); }else{ return new ResponseEntity<>(HttpStatus.FORBIDDEN); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceEndpoint or urlParameters")); } } else { return ResponseUtils.buildFailureResponse(new Exception( "Please configure the serviceName and serviceEndpoint")); } } catch (Exception e) { LOGGER.error(e.toString()); return new ResponseEntity<>(HttpStatus.FORBIDDEN); } }  @ApiOperation(httpMethod = "POST", value = "Download Service Details in CSV or Excel") @RequestMapping(path = "/v1/download/services", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE }) ResponseEntity<Object> getIssuesDownload(final HttpServletRequest servletRequest,
            final HttpServletResponse servletResponse,
            @ApiParam(value = "Provide Download File Format (excel or csv). Default is csv", required = false) @RequestParam(name = "fileFormat", required = false) String fileFormat,
            @ApiParam(value = "Provide serviceId 1-policyViolationList,2-policyOverviewList,3-patchingList,4-taggingList,5-certificateList,6-vulnerabilitiesList", required = false) @RequestParam("serviceId") int serviceId,
            @ApiParam(value = "Provide Service Search Filter", required = false) @RequestBody(required = false) Request request);  }