From 4e6087cf21571f070b6706b3aed270bde6776041 Mon Sep 17 00:00:00 2001 From: Akshay Sharma Date: Fri, 20 Oct 2023 18:02:29 +0530 Subject: [PATCH 1/2] Added support for formatting XML and JSON request and response --- src/com/soapuiextentter/reporter/Report.java | 27 ++++++++---- .../utilities/StringUtils.java | 41 +++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 src/com/soapuiextentter/utilities/StringUtils.java diff --git a/src/com/soapuiextentter/reporter/Report.java b/src/com/soapuiextentter/reporter/Report.java index dd23932..e44d53d 100644 --- a/src/com/soapuiextentter/reporter/Report.java +++ b/src/com/soapuiextentter/reporter/Report.java @@ -19,6 +19,7 @@ import com.eviware.soapui.SoapUI; import com.eviware.soapui.model.testsuite.TestProperty; import com.eviware.soapui.model.testsuite.TestStepResult; +import com.soapuiextentter.utilities.StringUtils; /* * Author : Akshay Sharma @@ -79,12 +80,12 @@ public Report(String reportPath, String reportName, HashMap klov } } - public static String getReportName(String reportName) { + private static String getReportName(String reportName) { String fileName = "AutomationReport_" + reportName + "_" + java.time.LocalDate.now() + ".html"; return fileName; } - public void assignCategory(String testSuiteId, String categoryName) { + private void assignCategory(String testSuiteId, String categoryName) { try { getTest(testSuiteId).assignCategory(categoryName); } catch (Exception e) { @@ -93,7 +94,7 @@ public void assignCategory(String testSuiteId, String categoryName) { } } - public void assignAuthor(String testSuiteId, String authorName) { + private void assignAuthor(String testSuiteId, String authorName) { try { getTest(testSuiteId).assignAuthor(authorName); } catch (Exception e) { @@ -209,9 +210,10 @@ public void logPass(TestStepResult testStepContext, String testSuiteId, String t if (GroovyScript == null) { actualReq = Request.getValue(); actualRes = Response.getValue(); - Markup mReqRes = MarkupHelper.createCodeBlock(actualReq, actualRes); + Markup mReqRes = MarkupHelper.createCodeBlock(formatXmlOrJson(actualReq), + formatXmlOrJson(actualRes)); endPoint = testStepContext.getTestStep().getProperty("Endpoint").getValue(); - + if (AuthType == null) { assignCategory(testSuiteId, "REST"); } else { @@ -257,7 +259,7 @@ public void logPass(TestStepResult testStepContext, String testSuiteId, String t ResponseAsXML = testStepContext.getTestStep().getProperty("ResponseAsXml"); getTestNode(testSuiteId, testCaseId).pass(logText + " - JDBC Response as below"); actualRespAsXml = ResponseAsXML.getValue(); - Markup mSqlResponse = MarkupHelper.createCodeBlock(actualRespAsXml); + Markup mSqlResponse = MarkupHelper.createCodeBlock(formatXmlOrJson(actualRespAsXml)); getTestNode(testSuiteId, testCaseId).info(mSqlResponse); assignCategory(testSuiteId, "JDBC"); } else { @@ -318,7 +320,8 @@ public void logFail(TestStepResult testStepContext, String testSuiteId, String t if (GroovyScript == null) { actualReq = Request.getValue(); actualRes = Response.getValue(); - Markup mReqRes = MarkupHelper.createCodeBlock(actualReq, actualRes); + Markup mReqRes = MarkupHelper.createCodeBlock(formatXmlOrJson(actualReq), + formatXmlOrJson(actualRes)); endPoint = testStepContext.getTestStep().getProperty("Endpoint").getValue(); if (AuthType == null) { @@ -497,4 +500,14 @@ private String getEndpointRef(String endPoint) { } return actualEndRef; } + + private String formatXmlOrJson(String unformattedStr) { + String formattedXMl = unformattedStr; + if (unformattedStr.startsWith("[") || unformattedStr.startsWith("{")) { + formattedXMl = StringUtils.prettyPrintJson(unformattedStr); + } else if (unformattedStr.startsWith("<")) { + formattedXMl = StringUtils.prettyPrintXML(unformattedStr); + } + return formattedXMl; + } } \ No newline at end of file diff --git a/src/com/soapuiextentter/utilities/StringUtils.java b/src/com/soapuiextentter/utilities/StringUtils.java new file mode 100644 index 0000000..6c9ed86 --- /dev/null +++ b/src/com/soapuiextentter/utilities/StringUtils.java @@ -0,0 +1,41 @@ +package com.soapuiextentter.utilities; + +import javax.xml.parsers.DocumentBuilderFactory; + +import org.json.JSONObject; +import org.w3c.dom.bootstrap.DOMImplementationRegistry; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSSerializer; +import org.xml.sax.InputSource; + +public class StringUtils { + + public static String prettyPrintXML(String xmlString) { + String preetyXML = xmlString; + try { + InputSource src = new InputSource(new String(xmlString)); + org.w3c.dom.Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) + .getDocumentElement(); + Boolean keepDeclaration = Boolean.valueOf(xmlString.startsWith(" Date: Thu, 26 Oct 2023 18:35:13 +0530 Subject: [PATCH 2/2] Fixed #19 --- Listener-XML/soapuiextentter-listeners.xml | 21 +++++++++++--------- src/com/soapuiextentter/reporter/Report.java | 3 ++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Listener-XML/soapuiextentter-listeners.xml b/Listener-XML/soapuiextentter-listeners.xml index ef23f25..d4cbc48 100644 --- a/Listener-XML/soapuiextentter-listeners.xml +++ b/Listener-XML/soapuiextentter-listeners.xml @@ -1,12 +1,15 @@ - - - - - - + + + + + + \ No newline at end of file diff --git a/src/com/soapuiextentter/reporter/Report.java b/src/com/soapuiextentter/reporter/Report.java index e44d53d..31928d4 100644 --- a/src/com/soapuiextentter/reporter/Report.java +++ b/src/com/soapuiextentter/reporter/Report.java @@ -30,7 +30,6 @@ public class Report { private ExtentReports reports; - private Object object; private static Map extentNodeMap; private static Map extentTestMap; private String finalReportPath; @@ -448,6 +447,8 @@ private boolean envLogCheck(List properties) { "Invalid value specified for 'AddDataToReport' in project properties. Please check and correct."); return propValue; } + } else { + propValue = true; } } }