Skip to content

Commit

Permalink
Layout improvements on pdf campaign execution report pdf. #2475
Browse files Browse the repository at this point in the history
  • Loading branch information
vertigo17 committed Aug 19, 2023
1 parent 49c2ebc commit 814343b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 44 deletions.
160 changes: 116 additions & 44 deletions source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -56,6 +59,7 @@
import java.util.UUID;
import org.cerberus.core.crud.entity.Parameter;
import org.cerberus.core.crud.entity.Tag;
import org.cerberus.core.crud.entity.Test;
import org.cerberus.core.crud.entity.TestCaseExecution;
import org.cerberus.core.crud.entity.TestCaseExecutionFile;
import org.cerberus.core.crud.entity.TestCaseStepActionControlExecution;
Expand All @@ -67,6 +71,7 @@
import org.springframework.stereotype.Service;

import org.cerberus.core.service.pdf.IPDFService;
import org.cerberus.core.util.DateUtil;
import org.cerberus.core.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -95,7 +100,6 @@ public String generatePdf(Tag tag) throws FileNotFoundException {
rootPath = System.getProperty("java.io.tmpdir");
} else {
String sep = "" + File.separatorChar;
LOG.info(sep);
if (sep.equalsIgnoreCase("/")) {
rootPath = "/tmp";
} else {
Expand Down Expand Up @@ -212,8 +216,49 @@ public String generatePdf(Tag tag) throws FileNotFoundException {

}
}
// Adding Table to document
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);

/**
Expand All @@ -224,30 +269,42 @@ public String generatePdf(Tag tag) throws FileNotFoundException {
Collections.sort(listOfExecutions, new SortExecution());

// Creating a table
Table tableExe = new Table(new float[]{25, 90, 40, 80, 20, 20, 40, 20, 50})
Table tableExe = new Table(new float[]{40, 160, 80, 80, 20, 20, 50, 50, 50, 30})
.addHeaderCell(getHeaderCell("Exe ID"))
.addHeaderCell(getHeaderCell("Prio"))
// .addHeaderCell(getHeaderCell("Prio"))
.addHeaderCell(getHeaderCell("Test Folder"))
.addHeaderCell(getHeaderCell("Test ID"))
.addHeaderCell(getHeaderCell("Application"))
.addHeaderCell(getHeaderCell("Country"))
.addHeaderCell(getHeaderCell("Environment"))
.addHeaderCell(getHeaderCell("Robot"))
.addHeaderCell(getHeaderCell("Started"))
.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();

for (TestCaseExecution execution : listOfExecutions) {
Cell cellID = new Cell(2, 1).add(new Paragraph(String.valueOf(execution.getId()))).setVerticalAlignment(VerticalAlignment.MIDDLE).setTextAlignment(TextAlignment.CENTER);
Cell cellID = new Cell(2, 1).add(new Paragraph(String.valueOf(execution.getId()))).setFontSize(6).setVerticalAlignment(VerticalAlignment.MIDDLE).setTextAlignment(TextAlignment.CENTER);
Cell cellRes = getStatusCell(execution.getControlStatus(), 2, 1);
Cell cellTCDesc = new Cell(1, 7).add(new Paragraph(execution.getDescription())).setFontSize(7);
Cell cellTCDesc = new Cell(1, 8).add(new Paragraph(execution.getDescription())).setFontSize(7);
calStart.setTimeInMillis(execution.getStart());
calEnd.setTimeInMillis(execution.getEnd());

tableExe
.addCell(cellID.setAction(PdfAction.createGoTo(String.valueOf(execution.getId()))))
.addCell(String.valueOf(execution.getTestCasePriority()))
.addCell(execution.getTest())
.addCell(execution.getTestCase())
.addCell(execution.getApplication())
.addCell(execution.getCountry())
.addCell(execution.getEnvironment())
.addCell(execution.getRobot())
// .addCell(String.valueOf(execution.getTestCasePriority()))
.addCell(new Cell().add(new Paragraph(execution.getTest())).setFontSize(7))
.addCell(new Cell().add(new Paragraph(execution.getTestCase())).setFontSize(7))
.addCell(new Cell().add(new Paragraph(execution.getApplication())).setFontSize(7))
.addCell(new Cell().add(new Paragraph(execution.getCountry())).setFontSize(7))
.addCell(new Cell().add(new Paragraph(execution.getEnvironment())).setFontSize(7))
.addCell(new Cell().add(new Paragraph(execution.getRobot())).setFontSize(7))
.addCell(new Cell().add(new Paragraph(df.format(calStart.getTime()))).setFontSize(7))
.addCell(new Cell().add(new Paragraph(df.format(calEnd.getTime()))).setFontSize(7))
.addCell(cellRes);
tableExe
.addCell(cellTCDesc);
Expand All @@ -268,18 +325,18 @@ public String generatePdf(Tag tag) throws FileNotFoundException {
document.add(new Paragraph()
.add(getTextFromString(String.valueOf(execution.getControlMessage()), 12, true)));

tableExe = new Table(new float[]{50, 90, 40, 80, 20, 20, 40, 20, 50})
tableExe = new Table(new float[]{200, 90, 70, 80, 20, 20, 40, 20, 50})
.addHeaderCell(getHeaderCell("Test Folder"))
.addHeaderCell(getHeaderCell("Test ID"))
.addHeaderCell(getHeaderCell("Prio"))
// .addHeaderCell(getHeaderCell("Prio"))
.addHeaderCell(getHeaderCell("Application"))
.addHeaderCell(getHeaderCell("Country"))
.addHeaderCell(getHeaderCell("Environment"))
.addHeaderCell(getHeaderCell("Robot"));
tableExe
.addCell(execution.getTest())
.addCell(execution.getTestCase())
.addCell(String.valueOf(execution.getTestCasePriority()))
// .addCell(String.valueOf(execution.getTestCasePriority()))
.addCell(execution.getApplication())
.addCell(execution.getCountry())
.addCell(execution.getEnvironment())
Expand All @@ -288,14 +345,14 @@ public String generatePdf(Tag tag) throws FileNotFoundException {

@SuppressWarnings("unchecked")
TestCaseExecution exec = testCaseExecutionService.convert(testCaseExecutionService.readByKeyWithDependency(execution.getId()));

Table tableTmp;
String desc = "";

for (TestCaseStepExecution step : exec.getTestCaseStepExecutionList()) {
if (!TestCaseExecution.CONTROLSTATUS_NE.equals(step.getReturnCode())) {

// Creating a table
tableTmp = new Table(new float[]{500, 20})
.addCell(new Cell().add(new Paragraph().add(getTextFromString(step.getDescription(), 12, true).setTextAlignment(TextAlignment.LEFT)))
.addCell(new Cell().add(new Paragraph().add(getTextFromString(getElementDescription(step.getDescription(), step.getSort(), step.getIndex(), step.getTest()), 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(step.getReturnCode(), 1, 1).setTextAlignment(TextAlignment.RIGHT));
document.add(tableTmp.setMarginLeft(0).setMarginTop(20));
Expand All @@ -316,7 +373,7 @@ public String generatePdf(Tag tag) throws FileNotFoundException {
for (TestCaseStepActionExecution action : step.getTestCaseStepActionExecutionList()) {
if (!TestCaseExecution.CONTROLSTATUS_NE.equals(action.getReturnCode())) {
tableTmp = new Table(new float[]{500, 20})
.addCell(new Cell().add(new Paragraph().add(getTextFromString(action.getDescription(), 12, true).setTextAlignment(TextAlignment.LEFT)))
.addCell(new Cell().add(new Paragraph().add(getTextFromString(getElementDescription(action.getDescription(), action.getSort(), 0, action.getTest()), 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(action.getReturnCode(), 1, 1).setTextAlignment(TextAlignment.RIGHT));
document.add(tableTmp.setMarginLeft(20));
Expand All @@ -338,7 +395,7 @@ public String generatePdf(Tag tag) throws FileNotFoundException {

if (!TestCaseExecution.CONTROLSTATUS_NE.equals(control.getReturnCode())) {
tableTmp = new Table(new float[]{500, 20})
.addCell(new Cell().add(new Paragraph().add(getTextFromString(control.getDescription(), 12, true).setTextAlignment(TextAlignment.LEFT)))
.addCell(new Cell().add(new Paragraph().add(getTextFromString(getElementDescription(control.getDescription(), control.getSort(), 0, control.getTest()), 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(control.getReturnCode(), 1, 1).setTextAlignment(TextAlignment.RIGHT));
document.add(tableTmp.setMarginLeft(40));
Expand Down Expand Up @@ -368,7 +425,7 @@ public String generatePdf(Tag tag) throws FileNotFoundException {
}

// Closing the document
LOG.info("Starting to generate PDF Report on :" + dest);
LOG.info("Ending to generate PDF Report on :" + dest);
return dest;
} catch (ParseException | CerberusException | JSONException ex) {
LOG.error(ex, ex);
Expand All @@ -378,6 +435,18 @@ public String generatePdf(Tag tag) throws FileNotFoundException {
return null;
}

private String getElementDescription(String desc, int sort, int seq, String test) {
if (Test.TEST_PRETESTING.equals(test)) {
return "[PRE] " + desc;
} else if (Test.TEST_POSTTESTING.equals(test)) {
return "[POST] " + desc;
} else if (seq > 0) {
return "[" + sort + "." + seq + "] " + desc;
} else {
return "[" + sort + "] " + desc;
}
}

@Override
public String addHeaderAndFooter(String pdfFilePath, Tag tag) throws FileNotFoundException {
String destinationFile = pdfFilePath + "new.pdf";
Expand All @@ -403,11 +472,14 @@ 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")
.setFontSize(7).setItalic();

x = pageSize.getRight() - 60;
// x = 20;
y = pageSize.getBottom() + 20;
doc.showTextAligned(footer, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
doc.showTextAligned(footerLeft, 20, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);

}
doc.close();
Expand Down Expand Up @@ -482,36 +554,36 @@ class SortExecution implements Comparator<TestCaseExecution> {
@Override
public int compare(TestCaseExecution a, TestCaseExecution b) {
if (a != null && b != null) {
int aPrio = a.getTestCasePriority();
if (a.getTestCasePriority() < 1 || a.getTestCasePriority() > 5) {
aPrio = 999 + a.getTestCasePriority();
}
int bPrio = b.getTestCasePriority();
if (b.getTestCasePriority() < 1 || b.getTestCasePriority() > 5) {
bPrio = 999 + b.getTestCasePriority();
}

if (aPrio == bPrio) {
if (a.getTest().equals(b.getTest())) {
if (a.getTestCase().equals(b.getTestCase())) {
if (a.getEnvironment().equals(b.getEnvironment())) {
if (a.getCountry().equals(b.getCountry())) {
return a.getRobotDecli().compareToIgnoreCase(b.getRobotDecli());
} else {
return a.getCountry().compareToIgnoreCase(b.getCountry());
}
// int aPrio = a.getTestCasePriority();
// if (a.getTestCasePriority() < 1 || a.getTestCasePriority() > 5) {
// aPrio = 999 + a.getTestCasePriority();
// }
// int bPrio = b.getTestCasePriority();
// if (b.getTestCasePriority() < 1 || b.getTestCasePriority() > 5) {
// bPrio = 999 + b.getTestCasePriority();
// }

// if (aPrio == bPrio) {
if (a.getTest().equals(b.getTest())) {
if (a.getTestCase().equals(b.getTestCase())) {
if (a.getEnvironment().equals(b.getEnvironment())) {
if (a.getCountry().equals(b.getCountry())) {
return a.getRobotDecli().compareToIgnoreCase(b.getRobotDecli());
} else {
return a.getEnvironment().compareToIgnoreCase(b.getEnvironment());
return a.getCountry().compareToIgnoreCase(b.getCountry());
}
} else {
return a.getTestCase().compareToIgnoreCase(b.getTestCase());
return a.getEnvironment().compareToIgnoreCase(b.getEnvironment());
}
} else {
return a.getTest().compareToIgnoreCase(b.getTest());
return a.getTestCase().compareToIgnoreCase(b.getTestCase());
}
} else {
return aPrio - bPrio;
return a.getTest().compareToIgnoreCase(b.getTest());
}
// } else {
// return aPrio - bPrio;
// }
} else {
return 1;
}
Expand Down
6 changes: 6 additions & 0 deletions source/src/main/java/org/cerberus/core/util/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public class DateUtil {
*/
public static final String DATE_FORMAT_TIMESTAMP = "yyyyMMddHHmmssSSS";

/**
* SimpleDateFormat use to display inside reports
*/
public static final String DATE_FORMAT_REPORT = "MM/dd/yyyy HH:mm:ss";
public static final String DATE_FORMAT_REPORT_TIME = "HH:mm:ss";

private DateUtil() {
}

Expand Down

0 comments on commit 814343b

Please sign in to comment.