diff --git a/source/src/main/java/org/cerberus/core/api/controllers/CampaignExecutionController.java b/source/src/main/java/org/cerberus/core/api/controllers/CampaignExecutionController.java index 478d0c1c49..f92fc9c2a2 100644 --- a/source/src/main/java/org/cerberus/core/api/controllers/CampaignExecutionController.java +++ b/source/src/main/java/org/cerberus/core/api/controllers/CampaignExecutionController.java @@ -24,7 +24,9 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import java.io.File; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -51,8 +53,18 @@ import javax.servlet.http.HttpServletRequest; import java.security.Principal; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.UUID; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import org.cerberus.core.crud.entity.Tag; import org.cerberus.core.service.pdf.IPDFService; +import org.cerberus.core.util.DateUtil; import org.springframework.core.io.InputStreamResource; /** @@ -106,15 +118,15 @@ public ResponseWrapper findCampaignExecutionById( ); } - @ApiOperation(value = "Get a campaign execution report in pdf format by campaign execution id (tag)") + @ApiOperation(value = "Get Campaign execution reports in pdf format inside a zip file by campaign execution id (tag)") @ApiResponses(value = { - @ApiResponse(code = 200, message = "Campaign execution pdf report successfully returned."), + @ApiResponse(code = 200, message = "Campaign execution pdfs report successfully returned."), @ApiResponse(code = 404, message = "Campaign execution was not found."), - @ApiResponse(code = 500, message = "An error occurred when retrieving the campaign execution pdf report.") + @ApiResponse(code = 500, message = "An error occurred when retrieving the campaign execution pdfs report.") }) @JsonView(View.Public.GET.class) @ResponseStatus(HttpStatus.OK) - @GetMapping(path = "/pdf/{campaignExecutionId}", produces = MediaType.APPLICATION_PDF_VALUE) + @GetMapping(path = "/pdf/{campaignExecutionId}", produces = "application/zip") public ResponseEntity findCampaignExecutionPdfById( @PathVariable("campaignExecutionId") String campaignExecutionId, @RequestHeader(name = API_KEY, required = false) String apiKey, @@ -126,16 +138,54 @@ public ResponseEntity findCampaignExecutionPdfById( try { Tag campaignExeIdTag = this.campaignExecutionService.findByExecutionIdWithExecutions(campaignExecutionId, null); - String pdfFilenameOri = this.pdfService.generatePdf(campaignExeIdTag); - String pdfFilename = this.pdfService.addHeaderAndFooter(pdfFilenameOri, campaignExeIdTag); + Date today = Calendar.getInstance().getTime(); + DateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT_REPORT_FILE); + + String rootPath = ""; + if (System.getProperty("java.io.tmpdir") != null) { + rootPath = System.getProperty("java.io.tmpdir"); + } else { + String sep = "" + File.separatorChar; + if (sep.equalsIgnoreCase("/")) { + rootPath = "/tmp"; + } else { + rootPath = "C:"; + } + LOG.warn("Java Property for temporary folder not defined. Default to :" + rootPath); + } + UUID fileUUID = UUID.randomUUID(); + String tmpFolderPath = rootPath + File.separatorChar + fileUUID.toString().substring(0, 17) + File.separatorChar; + File folderPath = new File(tmpFolderPath); + folderPath.mkdirs(); + + String pdfFilenameOri = this.pdfService.generatePdf(campaignExeIdTag, today, tmpFolderPath); + String pdfFilename = this.pdfService.addHeaderAndFooter(pdfFilenameOri, tmpFolderPath + "Campaign Execution.pdf", campaignExeIdTag, today); Path filePath = Paths.get(pdfFilename); - logEventService.createForPublicCalls(EXECUTIONS_CAMPAIGN_PDF_PATH, "CALLRESULT", String.format("PDF calculated for campaign '%s'", campaignExecutionId), request); + String pdfFilenameOriAppendix = this.pdfService.generatePdfAppendix(campaignExeIdTag, today, tmpFolderPath); + String pdfFilenameAppendix = this.pdfService.addHeaderAndFooter(pdfFilenameOriAppendix, tmpFolderPath + "Campaign Execution - Appendix.pdf", campaignExeIdTag, today); + Path filePathAppendix = Paths.get(pdfFilenameAppendix); + + // Creating a PdfWriter + String zipPath = rootPath + File.separatorChar + "campaignExecutionReport-" + fileUUID.toString().substring(0, 17) + ".zip"; + Path zipFilePath = Paths.get(zipPath); + + List filePaths = Arrays.asList(pdfFilename, pdfFilenameAppendix); + + try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipPath))) { + for (String filePath1 : filePaths) { + File fileToZip = new File(filePath1); + zipOut.putNextEntry(new ZipEntry(fileToZip.getName())); + Files.copy(fileToZip.toPath(), zipOut); + } + } + + logEventService.createForPublicCalls(EXECUTIONS_CAMPAIGN_PDF_PATH, "CALLRESULT", String.format("PDFs calculated for campaign '%s'", campaignExecutionId), request); return ResponseEntity .status(HttpStatus.OK) - .contentLength(filePath.toFile().length()) - .header("Content-Disposition", "attachment; filename=CampaignReport-" + campaignExecutionId + ".pdf") - .body(new InputStreamResource(Files.newInputStream(filePath))); + .contentLength(zipFilePath.toFile().length()) + .header("Content-Disposition", "attachment; filename=CampaignReport-" + campaignExecutionId + "_" + String.valueOf(df.format(today)) + ".zip") + .body(new InputStreamResource(Files.newInputStream(zipFilePath))); } catch (EntityNotFoundException exception) { return ResponseEntity .status(HttpStatus.NOT_FOUND) diff --git a/source/src/main/java/org/cerberus/core/crud/service/impl/TagService.java b/source/src/main/java/org/cerberus/core/crud/service/impl/TagService.java index 22c87f22f7..da9d5804b3 100644 --- a/source/src/main/java/org/cerberus/core/crud/service/impl/TagService.java +++ b/source/src/main/java/org/cerberus/core/crud/service/impl/TagService.java @@ -29,10 +29,12 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; import org.cerberus.core.crud.dao.ITagDAO; +import org.cerberus.core.crud.entity.Campaign; import org.cerberus.core.crud.entity.EventHook; import org.cerberus.core.crud.entity.Tag; import org.cerberus.core.crud.entity.TestCaseExecution; import org.cerberus.core.crud.factory.IFactoryTag; +import org.cerberus.core.crud.service.ICampaignService; import org.cerberus.core.crud.service.ITagService; import org.cerberus.core.crud.service.ITestCaseExecutionQueueService; import org.cerberus.core.crud.service.ITestCaseExecutionService; @@ -80,6 +82,8 @@ public class TagService implements ITagService { private ITestCaseExecutionQueueService executionQueueService; @Autowired private IEventService eventService; + @Autowired + private ICampaignService campaignService; private static final Logger LOG = LogManager.getLogger("TagService"); @@ -275,8 +279,20 @@ public Answer createAuto(String tagS, String campaign, String user, JSONArray re answerTag = readByKey(tagS); Tag tag = (Tag) answerTag.getItem(); if (tag == null) { - Tag newTag = factoryTag.create(0, tagS, "", "", campaign, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", "", "", "", - reqEnvironmentList.toString(), reqCountryList.toString(), "", "", "", "", user, null, user, null); + Campaign cmp = null; + try { + cmp = campaignService.convert(campaignService.readByKey(campaign)); + } catch (CerberusException ex) { + LOG.error(ex,ex); + } + Tag newTag; + if (cmp == null) { + newTag = factoryTag.create(0, tagS, "", "", campaign, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", "", "", "", + reqEnvironmentList.toString(), reqCountryList.toString(), "", "", "", "", user, null, user, null); + } else { + newTag = factoryTag.create(0, tagS, cmp.getLongDescription(), "", campaign, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", "", "", "", + reqEnvironmentList.toString(), reqCountryList.toString(), "", "", "", "", user, null, user, null); + } Answer ans = tagDAO.create(newTag); // If campaign is not empty, we can notify the Start of campaign execution. if (!StringUtil.isEmpty(campaign)) { diff --git a/source/src/main/java/org/cerberus/core/service/pdf/IPDFService.java b/source/src/main/java/org/cerberus/core/service/pdf/IPDFService.java index 5267aa3f19..3469762a72 100644 --- a/source/src/main/java/org/cerberus/core/service/pdf/IPDFService.java +++ b/source/src/main/java/org/cerberus/core/service/pdf/IPDFService.java @@ -20,6 +20,7 @@ package org.cerberus.core.service.pdf; import java.io.FileNotFoundException; +import java.util.Date; import org.cerberus.core.crud.entity.Tag; /** @@ -29,18 +30,31 @@ public interface IPDFService { /** * @param tag + * @param today + * @param folder * @return * @throws java.io.FileNotFoundException */ - String generatePdf(Tag tag) throws FileNotFoundException; - + String generatePdf(Tag tag, Date today, String folder) throws FileNotFoundException; + + /** + * @param tag + * @param today + * @param folder + * @return + * @throws java.io.FileNotFoundException + */ + String generatePdfAppendix(Tag tag, Date today, String folder) throws FileNotFoundException; + /** * - * @param pdfFilePath + * @param pdfFilePathSrc + * @param pdfFilePathDst * @param tag + * @param today * @return * @throws FileNotFoundException */ - String addHeaderAndFooter(String pdfFilePath, Tag tag) throws FileNotFoundException; - + String addHeaderAndFooter(String pdfFilePathSrc, String pdfFilePathDst, Tag tag, Date today) throws FileNotFoundException; + } diff --git a/source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java b/source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java index d35820a002..487276af23 100644 --- a/source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java +++ b/source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java @@ -56,6 +56,7 @@ import java.util.Calendar; import java.util.Collections; import java.util.Comparator; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -93,26 +94,42 @@ public class PDFService implements IPDFService { @Autowired private IParameterService parameterService; - @Override - public String generatePdf(Tag tag) throws FileNotFoundException { - - UUID fileUUID = UUID.randomUUID(); - - String rootPath = ""; - if (System.getProperty("java.io.tmpdir") != null) { - rootPath = System.getProperty("java.io.tmpdir"); - } else { - String sep = "" + File.separatorChar; - if (sep.equalsIgnoreCase("/")) { - rootPath = "/tmp"; + private Table getTitleTable(String desc1, String desc2) { + String logoURL = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_instancelogo_url, "", "https://vm.cerberus-testing.org/img/logo.png"); + // Tittle + Table tableTitle = new Table(new float[]{100, 500}); + try { + ImageData imageDataLogo; + if (StringUtil.isNotEmptyOrNullValue(logoURL)) { + imageDataLogo = ImageDataFactory.create(logoURL); } else { - rootPath = "C:"; + imageDataLogo = ImageDataFactory.create("https://vm.cerberus-testing.org/img/logo.png"); } - LOG.warn("Java Property for temporary folder not defined. Default to :" + rootPath); + Image image = new Image(imageDataLogo).scaleToFit(100, 70); + tableTitle.addCell(new Cell().add(image.setBorder(Border.NO_BORDER).setHorizontalAlignment(HorizontalAlignment.RIGHT)).setBorder(Border.NO_BORDER)); + } catch (Exception ex) { + LOG.error("Error when trying to load content from : " + logoURL); + LOG.error(ex, ex); + tableTitle.addCell(new Cell().add(new Paragraph("")).setBorder(Border.NO_BORDER)); + } + Cell descCell = new Cell(); + descCell.add(new Paragraph(desc1).setBold().setFontSize(20).setTextAlignment(TextAlignment.CENTER)); + if (StringUtil.isNotEmptyOrNullValue(desc2)) { + descCell.add(new Paragraph(desc2).setBold().setItalic().setFontSize(20).setTextAlignment(TextAlignment.CENTER)); } + descCell.setMarginBottom(30).setBorder(Border.NO_BORDER).setVerticalAlignment(VerticalAlignment.MIDDLE); + tableTitle.addCell(descCell); + return tableTitle; + } + + @Override + public String generatePdf(Tag tag, Date today, String folder) throws FileNotFoundException { + + UUID fileUUID = UUID.randomUUID(); + DateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT_REPORT); // Creating a PdfWriter - String dest = rootPath + File.separatorChar + "campaignExecutionReport-" + fileUUID.toString().substring(0, 17) + ".pdf"; + String dest = folder + File.separatorChar + "Campaign Execution Report tmp.pdf"; LOG.info("Starting to generate PDF Report on :" + dest); PdfWriter writer = new PdfWriter(dest); @@ -125,26 +142,8 @@ public String generatePdf(Tag tag) throws FileNotFoundException { boolean displayCountryColumn = parameterService.getParameterBooleanByKey(Parameter.VALUE_cerberus_pdfcampaignreportdisplaycountry_boolean, "", true); - String logoURL = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_instancelogo_url, "", "https://vm.cerberus-testing.org/img/logo.png"); - // Tittle - Table tableTitle = new Table(new float[]{100, 500}); - try { - ImageData imageDataLogo; - if (StringUtil.isNotEmptyOrNullValue(logoURL)) { - imageDataLogo = ImageDataFactory.create(logoURL); - } else { - imageDataLogo = ImageDataFactory.create("https://vm.cerberus-testing.org/img/logo.png"); - } - Image image = new Image(imageDataLogo).scaleToFit(100, 70); - tableTitle.addCell(new Cell().add(image.setBorder(Border.NO_BORDER).setHorizontalAlignment(HorizontalAlignment.RIGHT)).setBorder(Border.NO_BORDER)); - } catch (Exception ex) { - LOG.error("Error when trying to load content from : " + logoURL); - LOG.error(ex, ex); - tableTitle.addCell(new Cell().add(new Paragraph("")).setBorder(Border.NO_BORDER)); - } - tableTitle.addCell(new Cell().add(new Paragraph("Campaign Execution Report").setBold().setFontSize(20).setTextAlignment(TextAlignment.CENTER)) - .add(new Paragraph(tag.getTag()).setBold().setFontSize(20).setTextAlignment(TextAlignment.CENTER)).setMarginBottom(30).setBorder(Border.NO_BORDER)); + Table tableTitle = getTitleTable("Campaign Execution Report", tag.getTag()); try ( // Creating a Document Document document = new Document(pdfDoc)) { @@ -154,7 +153,29 @@ public String generatePdf(Tag tag) throws FileNotFoundException { // Tittle document.add(tableTitle.setMarginLeft(0)); + document.add(new Paragraph().add(getTextFromString("", 10, false))); + document.add(new Paragraph().add(getTextFromString("", 10, false))); + document.add(new Paragraph().add(getTextFromString("", 10, false))); + + if (!StringUtil.isEmptyOrNullValue(tag.getDescription())) { + List eleList = HtmlConverter.convertToElements(tag.getDescription()); + Table tableDesc = new Table(new float[]{1000}); + + Cell myCell = new Cell(); + for (IElement element : eleList) { + myCell.add((IBlockElement) element); + } + tableDesc.addCell(myCell.setBorder(Border.NO_BORDER)); + document.add(tableDesc); + } + + document.add(new Paragraph("Main technical details").setMarginTop(30).setMarginBottom(10).setBold().setFontSize(14)); + long tagDur = (tag.getDateEndQueue().getTime() - tag.getDateCreated().getTime()) / 60000; + document.add(new Paragraph() + .add(getTextFromString("Report generated at ", 10, false)) + .add(getTextFromString(String.valueOf(df.format(today)), 12, true)) + ); document.add(new Paragraph() .add(getTextFromString("Campaign started at ", 10, false)) .add(getTextFromString(String.valueOf(tag.getDateCreated()), 12, true)) @@ -164,30 +185,6 @@ public String generatePdf(Tag tag) throws FileNotFoundException { .add(getTextFromString(String.valueOf(tagDur), 12, true)) .add(getTextFromString(" min)", 10, false)) ); - - if (StringUtil.isEmptyOrNullValue(tag.getCampaign())) { - if (!StringUtil.isEmptyOrNullValue(tag.getUsrCreated())) { - document.add(new Paragraph() - .add(getTextFromString("Triggered by ", 10, false)) - .add(getTextFromString(tag.getUsrCreated(), 12, true)) - ); - } - } else { - if (StringUtil.isEmptyOrNullValue(tag.getUsrCreated())) { - document.add(new Paragraph() - .add(getTextFromString("Triggered from campaign: ", 10, false)) - .add(getTextFromString(tag.getCampaign(), 12, true)) - ); - } else { - document.add(new Paragraph() - .add(getTextFromString("Triggered from campaign: ", 10, false)) - .add(getTextFromString(tag.getCampaign(), 12, true)) - .add(getTextFromString(" by ", 10, false)) - .add(getTextFromString(tag.getUsrCreated(), 12, true)) - ); - } - } - if (displayCountryColumn) { document.add(new Paragraph() .add(getTextFromString("Executed on Country(ies): ", 10, false)) @@ -199,42 +196,15 @@ public String generatePdf(Tag tag) throws FileNotFoundException { } else { document.add(new Paragraph() .add(getTextFromString("Executed on Environment(s): ", 10, false)) - .add(getTextFromString(StringUtil.convertToString(new JSONArray(tag.getEnvironmentList()), ","), 12, true)) + .add(getTextFromString(tag.getEnvironmentList() == null ? "" : StringUtil.convertToString(new JSONArray(tag.getEnvironmentList()), ","), 12, true)) .add(getTextFromString(" and Robot(s): ", 10, false)) - .add(getTextFromString(StringUtil.convertToString(new JSONArray(tag.getRobotDecliList()), ","), 12, true))); - } - - document.add(new Paragraph() - .add(getTextFromString("Global result for campaign is ", 10, false)) - .add(getTextFromString(tag.getCiResult(), 12, true)) - .add(getTextFromString(" (with a Score of ", 10, false)) - .add(getTextFromString(String.valueOf(tag.getCiScore()), 12, true)) - .add(getTextFromString(" vs ", 10, false)) - .add(getTextFromString(String.valueOf(tag.getCiScoreThreshold()), 12, true)) - .add(getTextFromString(")", 10, false))); - - document.add(new Paragraph() - .add(getTextFromString(String.valueOf(tag.getNbExeUsefull()), 12, true)) - .add(getTextFromString(" useful executions were performed (Over ", 10, false)) - .add(getTextFromString(String.valueOf(tag.getNbExe()), 12, true)) - .add(getTextFromString(" in total including retries)", 10, false))); - - if (!StringUtil.isEmptyOrNullValue(tag.getDescription())) { - List eleList = HtmlConverter.convertToElements(tag.getDescription()); - Table tableDesc = new Table(new float[]{1000}); - - Cell myCell = new Cell(); - for (IElement element : eleList) { - myCell.add((IBlockElement) element); - } - tableDesc.addCell(myCell); - document.add(tableDesc); + .add(getTextFromString(tag.getRobotDecliList() == null ? "" : StringUtil.convertToString(new JSONArray(tag.getRobotDecliList()), ","), 12, true))); } /** * Result information per status */ - document.add(new Paragraph("Global Status").setMarginTop(10).setBold().setFontSize(14)); + document.add(new Paragraph("Global status").setMarginTop(30).setMarginBottom(10).setBold().setFontSize(14)); // Creating a table Table tableGlobalStatus = new Table(new float[]{50, 50, 40}) .addHeaderCell(getHeaderCell("Status")) @@ -253,23 +223,26 @@ public String generatePdf(Tag tag) throws FileNotFoundException { statColorMap.put(TestCaseExecution.CONTROLSTATUS_QU, TestCaseExecution.CONTROLSTATUS_QU_COL_EXT); statColorMap.put(TestCaseExecution.CONTROLSTATUS_QE, TestCaseExecution.CONTROLSTATUS_QE_COL_EXT); statColorMap.put(TestCaseExecution.CONTROLSTATUS_CA, TestCaseExecution.CONTROLSTATUS_CA_COL_EXT); + // Map that will contain the nb of execution for global status. + List listOfExecutions = testCaseExecutionService.readLastExecutionAndExecutionInQueueByTag(tag.getTag()); + Collections.sort(listOfExecutions, new SortExecution()); + Map statNbMap = new HashMap<>(); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_OK, tag.getNbOK()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_KO, tag.getNbKO()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_FA, tag.getNbFA()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_NA, tag.getNbNA()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_NE, tag.getNbNE()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_WE, tag.getNbWE()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_PE, tag.getNbPE()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_QU, tag.getNbQU()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_QE, tag.getNbQE()); - statNbMap.put(TestCaseExecution.CONTROLSTATUS_CA, tag.getNbCA()); + for (TestCaseExecution execution : listOfExecutions) { + LOG.debug("1 " + execution.getControlStatus() + " - " + statNbMap.get(execution.getControlStatus())); + if (statNbMap.get(execution.getControlStatus()) == null) { + statNbMap.put(execution.getControlStatus(), 0); + } + statNbMap.put(execution.getControlStatus(), statNbMap.get(execution.getControlStatus()) + 1); + LOG.debug("2 " + execution.getControlStatus() + " - " + statNbMap.get(execution.getControlStatus())); + } + // Status list in the correct order. float per = 0; List statList = new ArrayList<>(Arrays.asList("OK", "KO", "FA", "NA", "NE", "WE", "PE", "QU", "QE", "CA")); for (String string : statList) { - if (statNbMap.get(string) > 0) { + if ((statNbMap.get(string) != null) && (statNbMap.get(string) > 0)) { per = statNbMap.get(string) / (float) tag.getNbExeUsefull(); per *= 100; tableGlobalStatus @@ -281,55 +254,12 @@ public String generatePdf(Tag tag) throws FileNotFoundException { } document.add(tableGlobalStatus); - /** - * Legend - */ - document.add(new Paragraph("Legend").setMarginTop(10).setBold().setFontSize(14)); - // Creating a table - Table tableLegendGlobalStatus = new Table(new float[]{50, 500}) - .addHeaderCell(getHeaderCell("Status")) - .addHeaderCell(getHeaderCell("Meaning")); - tableLegendGlobalStatus - .addCell(getStatusCell(TestCaseExecution.CONTROLSTATUS_OK, 1, 1)) - .addCell("The execution was performed correctly and all controls were OK.").setTextAlignment(TextAlignment.LEFT) - .addCell(getStatusCell(TestCaseExecution.CONTROLSTATUS_KO, 1, 1)) - .addCell("The execution was performed correcly and at least one control failed resulting a global KO. That means that a bug needs to be reported to development teams.").setTextAlignment(TextAlignment.LEFT) - .addCell(getStatusCell(TestCaseExecution.CONTROLSTATUS_FA, 1, 1)) - .addCell("The execution did not performed correctly and needs a correction from the team that is in charge of managing the testcases. It couls be a failed SQL or action during the test.").setTextAlignment(TextAlignment.LEFT) - .addCell(getStatusCell(TestCaseExecution.CONTROLSTATUS_NA, 1, 1)) - .addCell("Test could not be executed as a data could not be retreived. That probably means that the test is not possible in the current environment/status.").setTextAlignment(TextAlignment.LEFT); - - // Adding Table to document - document.add(tableLegendGlobalStatus); - - Table tableTmp; - - tableTmp = new Table(new float[]{500, 20}) - .addCell(new Cell().add(new Paragraph().add(getTextFromString("Step", 12, true).setTextAlignment(TextAlignment.LEFT))) - .setBorder(Border.NO_BORDER).setBorderLeft(new SolidBorder(ColorConstants.CYAN, 3)).setBorderRight(new SolidBorder(1)).setBorderTop(new SolidBorder(1)).setBorderBottom(new SolidBorder(1))) - .addCell(getStatusCell("OK", 1, 1).setTextAlignment(TextAlignment.RIGHT)); - document.add(tableTmp.setMarginLeft(0).setMarginTop(20)); - - tableTmp = new Table(new float[]{500, 20}) - .addCell(new Cell().add(new Paragraph().add(getTextFromString("Action", 12, true).setTextAlignment(TextAlignment.LEFT))) - .setBorder(Border.NO_BORDER).setBorderLeft(new SolidBorder(ColorConstants.BLUE, 3)).setBorderRight(new SolidBorder(1)).setBorderTop(new SolidBorder(1)).setBorderBottom(new SolidBorder(1))) - .addCell(getStatusCell("OK", 1, 1).setTextAlignment(TextAlignment.RIGHT)); - document.add(tableTmp.setMarginLeft(20)); - - tableTmp = new Table(new float[]{500, 20}) - .addCell(new Cell().add(new Paragraph().add(getTextFromString("Control", 12, true).setTextAlignment(TextAlignment.LEFT))) - .setBorder(Border.NO_BORDER).setBorderLeft(new SolidBorder(ColorConstants.GREEN, 3)).setBorderRight(new SolidBorder(1)).setBorderTop(new SolidBorder(1)).setBorderBottom(new SolidBorder(1))) - .addCell(getStatusCell("OK", 1, 1).setTextAlignment(TextAlignment.RIGHT)); - document.add(tableTmp.setMarginLeft(40)); - document.add(aB); /** * Summary result per execution */ - document.add(new Paragraph("Execution list summary").setMarginTop(10).setBold().setFontSize(14)); - List listOfExecutions = testCaseExecutionService.readLastExecutionAndExecutionInQueueByTag(tag.getTag()); - Collections.sort(listOfExecutions, new SortExecution()); + document.add(getTitleTable("Execution list summary", "").setMarginLeft(0)); // Creating a table Table tableExe; @@ -355,7 +285,6 @@ public String generatePdf(Tag tag) throws FileNotFoundException { .addHeaderCell(getHeaderCell("Ended")) .addHeaderCell(getHeaderCell("Result")); - DateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT_REPORT); DateFormat dfEnd = new SimpleDateFormat(DateUtil.DATE_FORMAT_REPORT_TIME); Calendar calStart = Calendar.getInstance(); Calendar calEnd = Calendar.getInstance(); @@ -394,6 +323,145 @@ public String generatePdf(Tag tag) throws FileNotFoundException { document.add(tableExe); document.add(aB); + document.add(getTitleTable("Other technical details", "").setMarginLeft(0)); + + if (StringUtil.isEmptyOrNullValue(tag.getCampaign())) { + if (!StringUtil.isEmptyOrNullValue(tag.getUsrCreated())) { + document.add(new Paragraph() + .add(getTextFromString("Triggered by ", 10, false)) + .add(getTextFromString(tag.getUsrCreated(), 12, true)) + ); + } + } else { + if (StringUtil.isEmptyOrNullValue(tag.getUsrCreated())) { + document.add(new Paragraph() + .add(getTextFromString("Triggered from campaign: ", 10, false)) + .add(getTextFromString(tag.getCampaign(), 12, true)) + ); + } else { + document.add(new Paragraph() + .add(getTextFromString("Triggered from campaign: ", 10, false)) + .add(getTextFromString(tag.getCampaign(), 12, true)) + .add(getTextFromString(" by ", 10, false)) + .add(getTextFromString(tag.getUsrCreated(), 12, true)) + ); + } + } + + document.add(new Paragraph() + .add(getTextFromString("Global result for campaign is ", 10, false)) + .add(getTextFromString(tag.getCiResult(), 12, true)) + .add(getTextFromString(" (with a Score of ", 10, false)) + .add(getTextFromString(String.valueOf(tag.getCiScore()), 12, true)) + .add(getTextFromString(" vs ", 10, false)) + .add(getTextFromString(String.valueOf(tag.getCiScoreThreshold()), 12, true)) + .add(getTextFromString(")", 10, false))); + + document.add(new Paragraph() + .add(getTextFromString(String.valueOf(tag.getNbExeUsefull()), 12, true)) + .add(getTextFromString(" useful executions were performed (Over ", 10, false)) + .add(getTextFromString(String.valueOf(tag.getNbExe()), 12, true)) + .add(getTextFromString(" in total including retries)", 10, false))); + + /** + * Legend + */ + document.add(new Paragraph("Execution status legend").setMarginTop(30).setMarginBottom(10).setBold().setFontSize(14)); + // Creating a table + Table tableLegendGlobalStatus = new Table(new float[]{50, 500}) + .addHeaderCell(getHeaderCell("Status")) + .addHeaderCell(getHeaderCell("Meaning")); + tableLegendGlobalStatus + .addCell(getStatusCell(TestCaseExecution.CONTROLSTATUS_OK, 1, 1)) + .addCell("The execution was performed correctly and all controls were OK.").setTextAlignment(TextAlignment.LEFT) + .addCell(getStatusCell(TestCaseExecution.CONTROLSTATUS_KO, 1, 1)) + .addCell("The execution was performed correcly and at least one control failed resulting a global KO. That means that a bug needs to be reported to development teams.").setTextAlignment(TextAlignment.LEFT) + .addCell(getStatusCell(TestCaseExecution.CONTROLSTATUS_FA, 1, 1)) + .addCell("The execution did not performed correctly and needs a correction from the team that is in charge of managing the testcases. It could be a failed SQL or action during the test.").setTextAlignment(TextAlignment.LEFT) + .addCell(getStatusCell(TestCaseExecution.CONTROLSTATUS_NA, 1, 1)) + .addCell("Test could not be executed as a data could not be retreived. That probably means that the test is not possible in the current environment/status.").setTextAlignment(TextAlignment.LEFT); + + // Adding Table to document + document.add(tableLegendGlobalStatus); + + document.add(new Paragraph("Test cases legend").setMarginTop(30).setMarginBottom(10).setBold().setFontSize(14)); + + Table tableTmp; + + tableTmp = new Table(new float[]{500, 20}) + .addCell(new Cell().add(new Paragraph().add(getTextFromString("Step", 12, true).setTextAlignment(TextAlignment.LEFT))) + .setBorder(Border.NO_BORDER).setBorderLeft(new SolidBorder(ColorConstants.CYAN, 3)).setBorderRight(new SolidBorder(1)).setBorderTop(new SolidBorder(1)).setBorderBottom(new SolidBorder(1))) + .addCell(getStatusCell("OK", 1, 1).setTextAlignment(TextAlignment.RIGHT)); + document.add(tableTmp.setMarginLeft(0)); + + tableTmp = new Table(new float[]{500, 20}) + .addCell(new Cell().add(new Paragraph().add(getTextFromString("Action", 12, true).setTextAlignment(TextAlignment.LEFT))) + .setBorder(Border.NO_BORDER).setBorderLeft(new SolidBorder(ColorConstants.BLUE, 3)).setBorderRight(new SolidBorder(1)).setBorderTop(new SolidBorder(1)).setBorderBottom(new SolidBorder(1))) + .addCell(getStatusCell("OK", 1, 1).setTextAlignment(TextAlignment.RIGHT)); + document.add(tableTmp.setMarginLeft(20)); + + tableTmp = new Table(new float[]{500, 20}) + .addCell(new Cell().add(new Paragraph().add(getTextFromString("Control", 12, true).setTextAlignment(TextAlignment.LEFT))) + .setBorder(Border.NO_BORDER).setBorderLeft(new SolidBorder(ColorConstants.GREEN, 3)).setBorderRight(new SolidBorder(1)).setBorderTop(new SolidBorder(1)).setBorderBottom(new SolidBorder(1))) + .addCell(getStatusCell("OK", 1, 1).setTextAlignment(TextAlignment.RIGHT)); + document.add(tableTmp.setMarginLeft(40)); + + // Closing the document + LOG.info("Ending to generate PDF Report on :" + dest); + return dest; + } catch (ParseException | CerberusException | JSONException ex) { + LOG.error(ex, ex); + } catch (Exception ex) { + LOG.error(ex, ex); + } + return null; + } + + @Override + public String generatePdfAppendix(Tag tag, Date today, String folder) throws FileNotFoundException { + + UUID fileUUID = UUID.randomUUID(); + + // Creating a PdfWriter + String dest = folder + File.separatorChar + "Campaign Execution Report Appendix tmp.pdf"; + LOG.info("Starting to generate PDF Report on :" + dest); + PdfWriter writer = new PdfWriter(dest); + + // Creating a PdfDocument + PdfDocument pdfDoc = new PdfDocument(writer); + + // Load parameters + String mediaPath = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_exeautomedia_path, "", ""); + mediaPath = StringUtil.addSuffixIfNotAlready(mediaPath, File.separator); + + boolean displayCountryColumn = parameterService.getParameterBooleanByKey(Parameter.VALUE_cerberus_pdfcampaignreportdisplaycountry_boolean, "", true); + + try ( // Creating a Document + Document document = new Document(pdfDoc)) { + + AreaBreak aB = new AreaBreak(); + + // Tittle + document.add(getTitleTable("", "")); + + Table tableExe, tableTmp; + + tableTmp = new Table(new float[]{600}) + .addCell(new Cell().add(new Paragraph().add(getTextFromString("APPENDIX", 20, true).setTextAlignment(TextAlignment.CENTER)).setTextAlignment(TextAlignment.CENTER)).setBorder(Border.NO_BORDER)); + document.add(tableTmp.setMarginTop(200)); + + tableTmp = new Table(new float[]{600}) + .addCell(new Cell().add(new Paragraph().add(getTextFromString("Details of Execution Campaign", 20, true).setTextAlignment(TextAlignment.CENTER)).setTextAlignment(TextAlignment.CENTER)).setBorder(Border.NO_BORDER)); + document.add(tableTmp.setMarginTop(40)); + + document.add(aB); + + /** + * Summary result per execution + */ + List listOfExecutions = testCaseExecutionService.readLastExecutionAndExecutionInQueueByTag(tag.getTag()); + Collections.sort(listOfExecutions, new SortExecution()); + /** * Detail information per execution */ @@ -523,7 +591,7 @@ public String generatePdf(Tag tag) throws FileNotFoundException { // Closing the document LOG.info("Ending to generate PDF Report on :" + dest); return dest; - } catch (ParseException | CerberusException | JSONException ex) { + } catch (ParseException | CerberusException ex) { LOG.error(ex, ex); } catch (Exception ex) { LOG.error(ex, ex); @@ -544,15 +612,16 @@ private String getElementDescription(String desc, int sort, int seq, String test } @Override - public String addHeaderAndFooter(String pdfFilePath, Tag tag) throws FileNotFoundException { - String destinationFile = pdfFilePath + "new.pdf"; + public String addHeaderAndFooter(String pdfFilePathSrc, String destinationFile, Tag tag, Date today) throws FileNotFoundException { try { - LOG.info("Starting to add Headers on PDF Report :" + pdfFilePath + " To : " + destinationFile); + LOG.info("Starting to add Headers on PDF Report :" + pdfFilePathSrc + " To : " + destinationFile); + + DateFormat df = new SimpleDateFormat(DateUtil.DATE_FORMAT_REPORT); // Adding Headers and Footers Paragraph header = new Paragraph("Campaign Execution Report - " + tag.getTag()) .setFontSize(7).setItalic(); - PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfFilePath), new PdfWriter(destinationFile)); + PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfFilePathSrc), new PdfWriter(destinationFile)); Document doc = new Document(pdfDoc); for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) { @@ -568,7 +637,7 @@ public String addHeaderAndFooter(String pdfFilePath, Tag tag) throws FileNotFoun // Footer insert Paragraph footer = new Paragraph("Page " + i + " / " + pdfDoc.getNumberOfPages()) .setFontSize(7).setItalic(); - Paragraph footerLeft = new Paragraph("(C) Cerberus Testing") + Paragraph footerLeft = new Paragraph("(C) Cerberus Testing - " + df.format(today)) .setFontSize(7).setItalic(); x = pageSize.getRight() - 60; @@ -579,7 +648,7 @@ public String addHeaderAndFooter(String pdfFilePath, Tag tag) throws FileNotFoun } doc.close(); - LOG.info("Ended to add Headers on PDF Report :" + pdfFilePath + " To : " + destinationFile); + LOG.info("Ended to add Headers on PDF Report :" + pdfFilePathSrc + " To : " + destinationFile); } catch (IOException ex) { LOG.error(ex, ex); diff --git a/source/src/main/java/org/cerberus/core/util/DateUtil.java b/source/src/main/java/org/cerberus/core/util/DateUtil.java index 1be8ce67dc..67b291fbd2 100644 --- a/source/src/main/java/org/cerberus/core/util/DateUtil.java +++ b/source/src/main/java/org/cerberus/core/util/DateUtil.java @@ -45,6 +45,7 @@ public class DateUtil { */ public static final String DATE_FORMAT_REPORT = "MM/dd/yyyy HH:mm:ss"; public static final String DATE_FORMAT_REPORT_TIME = "HH:mm:ss"; + public static final String DATE_FORMAT_REPORT_FILE = "yyMMddHHmmss"; private DateUtil() { } diff --git a/source/src/main/resources/database.sql b/source/src/main/resources/database.sql index 29eb007791..159a23a5da 100644 --- a/source/src/main/resources/database.sql +++ b/source/src/main/resources/database.sql @@ -6222,3 +6222,5 @@ INSERT INTO `parameter` (`system`, `param`, `value`, `description`) VALUES ('', 'cerberus_instancelogo_url', 'https://vm.cerberus-testing.org/img/logo.png', 'URl that point to the instance logo. Use that parameter in order to personalize some screens and pdf report.'), ('', 'cerberus_pdfcampaignreportdisplaycountry_boolean', 'true', 'Boolean in order to show or hide the country column on pdf campaign execution pdf report.'); +-- 1753 +ALTER TABLE tag MODIFY COLUMN Description TEXT NULL;