Skip to content

Commit

Permalink
Added support for formatting XML and JSON request and response
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Sharma committed Oct 20, 2023
1 parent a6ac456 commit 4e6087c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/com/soapuiextentter/reporter/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,12 +80,12 @@ public Report(String reportPath, String reportName, HashMap<String, String> 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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -257,7 +259,7 @@ public void logPass(TestStepResult testStepContext, String testSuiteId, String t
ResponseAsXML = testStepContext.getTestStep().getProperty("ResponseAsXml");
getTestNode(testSuiteId, testCaseId).pass(logText + " <b>- JDBC Response as below</b>");
actualRespAsXml = ResponseAsXML.getValue();
Markup mSqlResponse = MarkupHelper.createCodeBlock(actualRespAsXml);
Markup mSqlResponse = MarkupHelper.createCodeBlock(formatXmlOrJson(actualRespAsXml));
getTestNode(testSuiteId, testCaseId).info(mSqlResponse);
assignCategory(testSuiteId, "JDBC");
} else {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
41 changes: 41 additions & 0 deletions src/com/soapuiextentter/utilities/StringUtils.java
Original file line number Diff line number Diff line change
@@ -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("<?xml"));
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);
preetyXML = writer.writeToString(document);
} catch (Exception e) {
return preetyXML;
}
return preetyXML;
}

public static String prettyPrintJson(String jsonString) {
String preetyJson = jsonString;
try {
preetyJson = (new JSONObject(jsonString)).toString(4);
} catch (Exception e) {
return preetyJson;
}
return preetyJson;
}
}

0 comments on commit 4e6087c

Please sign in to comment.