// Load dependencies
const xsltProcessor = require('xslt-processor');
const fs = require("fs");
const rp = require('request-promise').defaults({ jar: true });
const path = require("path");
const { sapProtocol, sapUserName, sapPassword, sapHost, packageToRun, host } = initialize();
const { xmlRunAbapUnit, xslt } = readXml();
/** Get CSRF Token by calling GET with x-csrf-token: fetch
* @returns Promise with the result of the call
*/
function getCSRFToken() {
const optionsGetCSRFToken = {
method: "GET",
url: host + '/sap/bc/adt/abapunit/testruns',
simple: false, // Don't handle 405 as an error
resolveWithFullResponse: true, // Read headers and not only body
auth: {
user: sapUserName,
password: sapPassword
},
headers: {
'X-CSRF-Token': 'fetch'
}
};
return rp(optionsGetCSRFToken);
}
/** Run abap unit tests
* @param csrf token needed for the call
* @returns Promise with the result
*/
function runAbapUnitTest(xCSRFToken) {
const optionsRunUnitTest = {
method: 'POST',
url: host + '/sap/bc/adt/abapunit/testruns',
auth: {
user: sapUserName,
password: sapPassword
},
headers: {
'x-csrf-token': xCSRFToken,
'Content-Type': "application/xml"
},
body: xmlRunAbapUnit
};
return rp(optionsRunUnitTest);
}
/**
* Reads XML files needed to run AUnit Tests and transform to JUnit
* @return xml file with call to run abap unit test, xsl to transform from AUnit Result to JUnit Result
*/function readXml() {
const xsltData = fs.readFileSync(path.resolve(__dirname, "./xml/aunit2junit.xsl"));
const xmlRunAbapUnitBuffer = fs.readFileSync(path.resolve(__dirname, "./xml/runAbapUnit.xml"));
const xslt = xsltProcessor.xmlParse(xsltData.toString()); // xsltString: string of xslt file contents
const xmlRunAbapUnit = xmlRunAbapUnitBuffer.toString('utf8').replace("{{package}}", packageToRun === undefined ? "ZDOMAIN" : packageToRun); // Default to ZDomain
return { xmlRunAbapUnit, xslt };
}
/** Runs the abap unit test and converts them to JUnit format
* 1) Get CSRF Token
* 2) Call Netweaver Server and get abap unit results
* 3) Transform result and save to output.xml
**/
function main() {
csrfTokenPromise = getCSRFToken();
var runAbapUnitTestPromise = csrfTokenPromise.then(function (response) {
var csrfToken = response.headers['x-csrf-token'];
return runAbapUnitTest(csrfToken);
}
).catch(function (err) {
console.error(JSON.stringify(err));
}
);
runAbapUnitTestPromise.then(function (parsedBody) {
const xml = xsltProcessor.xmlParse(parsedBody); // xsltString: string of xslt file contents
const outXmlString = xsltProcessor.xsltProcess(xml, xslt); // outXmlString: output xml string.
fs.writeFileSync("output.xml", outXmlString)
}).catch(function (err) {
console.error(JSON.stringify(err));
});
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:aunit="http://www.sap.com/adt/aunit"
xmlns:adtcore="http://www.sap.com/adt/core">
<xsl:template match="/">
<testsuites>
<xsl:attribute name="tests">
<xsl:value-of
select="count(aunit:runResult/program/testClasses/testClass/testMethods/testMethod)" />
</xsl:attribute>
<xsl:attribute name="failures">
<xsl:value-of
select="count(aunit:runResult/program/testClasses/testClass/testMethods/testMethod/alerts/alert)" />
</xsl:attribute>
<xsl:for-each select="aunit:runResult/program">
<xsl:variable name="object" select="@adtcore:name" />
<testsuite>
<xsl:attribute name="name">
<xsl:value-of select="@adtcore:packageName" />
</xsl:attribute>
<xsl:attribute name="tests">
<xsl:value-of
select="count(testClasses/testClass/testMethods/testMethod)" />
</xsl:attribute>
<xsl:attribute name="failures">
<xsl:value-of
select="count(testClasses/testClass/testMethods/testMethod/alerts/alert)" />
</xsl:attribute>
<xsl:attribute name="package">
<xsl:value-of select="@adtcore:uri" />
</xsl:attribute>
<xsl:for-each
select="testClasses/testClass/testMethods/testMethod">
<testcase>
<xsl:attribute name="name">
<xsl:value-of select="@adtcore:packageName" /> - <xsl:value-of select="$object" /> - <xsl:value-of select="@adtcore:name" />
</xsl:attribute>
<xsl:attribute name="classname">
<xsl:value-of select="@adtcore:uri" />
</xsl:attribute>
<xsl:attribute name="time">
<xsl:value-of select="@executionTime" />
</xsl:attribute>
<xsl:for-each select="alerts/alert">
<failure>
<xsl:attribute name="message">
<xsl:value-of select="title" />
</xsl:attribute>
<xsl:attribute name="type">
<xsl:value-of select="@severity" />
</xsl:attribute>
<xsl:for-each select="details/detail">
<xsl:value-of select="@text" />
<xsl:value-of select="'
'" />
<xsl:for-each select="details/detail">
<xsl:value-of select="@text" />
<xsl:value-of select="'
'" />
</xsl:for-each>
</xsl:for-each>
</failure>
</xsl:for-each>
</testcase>
</xsl:for-each>
</testsuite>
</xsl:for-each>
</testsuites>
</xsl:template>
</xsl:stylesheet>
<?xml version='1.0' encoding='UTF-8'?>
<aunit:runConfiguration xmlns:aunit='http://www.sap.com/adt/aunit'>
<external>
<coverage active='false'/>
</external>
<adtcore:objectSets xmlns:adtcore='http://www.sap.com/adt/core'>
<objectSet kind='inclusive'>
<adtcore:objectReferences>
<adtcore:objectReference adtcore:uri='/sap/bc/adt/vit/wb/object_type/devck/object_name/{{package}}'/>
</adtcore:objectReferences>
</objectSet>
</adtcore:objectSets>
</aunit:runConfiguration>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
6 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
2 | |
2 |