@Test public void downloadDataTest() throws Exception { Gson gson = new Gson(); JsonArray array = new JsonArray(); JsonObject jsonObject = new JsonObject(); jsonObject.add("column1", gson.fromJson("column1", JsonElement.class)); jsonObject.add("column2", gson.fromJson("column2", JsonElement.class)); jsonObject.add("column3", gson.fromJson("column3", JsonElement.class)); jsonObject.add("column4", gson.fromJson("column4", JsonElement.class)); array.add(jsonObject); httpServletResponse = mock(HttpServletResponse.class); servletOutputStream = mock(ServletOutputStream.class); when(httpServletResponse.getOutputStream()).thenReturn(servletOutputStream); downloadFileService.downloadData(httpServletResponse, array,"csv","xyz"); downloadFileService.downloadData(httpServletResponse, array,"excel","xyz1"); }
public void downloadData(final HttpServletResponse servletResponse, final JsonArray response, final String fileFormat, String methodType) throws Exception { final OutputStream output = servletResponse.getOutputStream(); byte[] byteArray=null; List<String> columnList = getColumnNames(response); if (!StringUtils.isEmpty(fileFormat) && "excel".equalsIgnoreCase(fileFormat)) { byteArray = getFileBytes(response, methodType,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".xls"); servletResponse.setContentType("application/vnd.ms-excel"); } else { byteArray = generateCsvFile(response,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".csv"); servletResponse.setContentType("text/csv"); } servletResponse.setContentLength(byteArray.length); output.write(byteArray); output.flush(); output.close(); columnList = new ArrayList<>(); }
DownloadFileService implements Constants { public void downloadData(final HttpServletResponse servletResponse, final JsonArray response, final String fileFormat, String methodType) throws Exception { final OutputStream output = servletResponse.getOutputStream(); byte[] byteArray=null; List<String> columnList = getColumnNames(response); if (!StringUtils.isEmpty(fileFormat) && "excel".equalsIgnoreCase(fileFormat)) { byteArray = getFileBytes(response, methodType,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".xls"); servletResponse.setContentType("application/vnd.ms-excel"); } else { byteArray = generateCsvFile(response,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".csv"); servletResponse.setContentType("text/csv"); } servletResponse.setContentLength(byteArray.length); output.write(byteArray); output.flush(); output.close(); columnList = new ArrayList<>(); } }
DownloadFileService implements Constants { public void downloadData(final HttpServletResponse servletResponse, final JsonArray response, final String fileFormat, String methodType) throws Exception { final OutputStream output = servletResponse.getOutputStream(); byte[] byteArray=null; List<String> columnList = getColumnNames(response); if (!StringUtils.isEmpty(fileFormat) && "excel".equalsIgnoreCase(fileFormat)) { byteArray = getFileBytes(response, methodType,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".xls"); servletResponse.setContentType("application/vnd.ms-excel"); } else { byteArray = generateCsvFile(response,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".csv"); servletResponse.setContentType("text/csv"); } servletResponse.setContentLength(byteArray.length); output.write(byteArray); output.flush(); output.close(); columnList = new ArrayList<>(); }  }
DownloadFileService implements Constants { public void downloadData(final HttpServletResponse servletResponse, final JsonArray response, final String fileFormat, String methodType) throws Exception { final OutputStream output = servletResponse.getOutputStream(); byte[] byteArray=null; List<String> columnList = getColumnNames(response); if (!StringUtils.isEmpty(fileFormat) && "excel".equalsIgnoreCase(fileFormat)) { byteArray = getFileBytes(response, methodType,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".xls"); servletResponse.setContentType("application/vnd.ms-excel"); } else { byteArray = generateCsvFile(response,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".csv"); servletResponse.setContentType("text/csv"); } servletResponse.setContentLength(byteArray.length); output.write(byteArray); output.flush(); output.close(); columnList = new ArrayList<>(); }  List<Map<String, Object>> getAllAssetGroupApis(); void downloadData(final HttpServletResponse servletResponse, final JsonArray response,
            final String fileFormat, String methodType); }
DownloadFileService implements Constants { public void downloadData(final HttpServletResponse servletResponse, final JsonArray response, final String fileFormat, String methodType) throws Exception { final OutputStream output = servletResponse.getOutputStream(); byte[] byteArray=null; List<String> columnList = getColumnNames(response); if (!StringUtils.isEmpty(fileFormat) && "excel".equalsIgnoreCase(fileFormat)) { byteArray = getFileBytes(response, methodType,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".xls"); servletResponse.setContentType("application/vnd.ms-excel"); } else { byteArray = generateCsvFile(response,columnList); servletResponse.setHeader("Content-Disposition", "attachment; filename=" + methodType + "_" + new DateTime() + ".csv"); servletResponse.setContentType("text/csv"); } servletResponse.setContentLength(byteArray.length); output.write(byteArray); output.flush(); output.close(); columnList = new ArrayList<>(); }  List<Map<String, Object>> getAllAssetGroupApis(); void downloadData(final HttpServletResponse servletResponse, final JsonArray response,
            final String fileFormat, String methodType);  }