Skip to content

Commit

Permalink
Merge pull request #1 from eficode/0.2
Browse files Browse the repository at this point in the history
0.2 - Adding reporting
  • Loading branch information
farthinder authored May 17, 2024
2 parents b93b63b + 1937617 commit 0247511
Show file tree
Hide file tree
Showing 5 changed files with 542 additions and 44 deletions.
28 changes: 20 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.eficode.atlassian</groupId>
<artifactId>remotespock</artifactId>
<version>0.1</version>
<version>0.2</version>


<properties>
Expand All @@ -33,7 +33,7 @@
<jirainstancemanager.version>2.1.7</jirainstancemanager.version>

<!-- Get version: https://github.com/eficode/devStack/blob/master/pom.xml-->
<devstack.version>2.3.22</devstack.version>
<devstack.version>2.3.23</devstack.version>


<allure.version>2.25.0</allure.version>
Expand Down Expand Up @@ -65,22 +65,34 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.eficode</groupId>
<artifactId>devstack</artifactId>
<version>${devstack.version}</version>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.9.0</version>
<scope>provided</scope>
<version>1.10.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-reporting</artifactId>
<version>1.9.0</version>
<scope>provided</scope>
<version>1.10.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<version>1.10.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit5</artifactId>
<scope>provided</scope>
<scope>compile</scope>
</dependency>

<!-- INSIGHT API -->
Expand Down Expand Up @@ -284,7 +296,7 @@
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.4-M1-groovy-3.0</version>
<version>2.4-M4-groovy-4.0</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.eficode.atlassian.jira.remotespock.beans.responses

/**
* Used to determine what report type is wanted when running spock tests
*/
enum SpockOutputType {

StringSummary,
LegacyXml,
OpenTestReport,
AllureReport



}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.eficode.atlassian.jira.remotespock

import com.eficode.atlassian.jira.remotespock.beans.responses.SpockOutputType


/*
To Add Xray Reporting:
Expand All @@ -18,7 +20,7 @@ xrayListener.executionFinished(testPlan)


@Grapes(
@Grab(group='io.qameta.allure', module='allure-junit5', version='2.27.0')
@Grab(group = 'io.qameta.allure', module = 'allure-junit5', version = '2.27.0')
)

/**
Expand All @@ -32,7 +34,10 @@ xrayListener.executionFinished(testPlan)


import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import groovy.transform.Field
import io.qameta.allure.junitplatform.AllureJunitPlatform
import org.apache.commons.io.FileUtils
import org.apache.commons.io.filefilter.AgeFileFilter
import org.junit.platform.launcher.TestPlan
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
Expand All @@ -44,6 +49,7 @@ import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder
import org.junit.platform.launcher.core.LauncherFactory
import org.junit.platform.launcher.listeners.SummaryGeneratingListener
import org.junit.platform.launcher.listeners.TestExecutionSummary
import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener
import org.junit.platform.reporting.open.xml.OpenTestReportGeneratingListener
import org.spockframework.runtime.model.FeatureMetadata

Expand All @@ -63,35 +69,35 @@ import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMetho
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass

@BaseScript CustomEndpointDelegate delegate
//@WithPlugin("com.riadalabs.jira.plugins.insight")

//@WithPlugin("com.riadalabs.jira.plugins.insight") //Leave to make sure automatic deployment with JIM works



Logger log = LogManager.getLogger("remoteSpec.util.jiraLocal.remoteSpoc") as Logger
Logger log = LogManager.getLogger("remoteSpec.util.jiraLocal.remoteSpocEndpoint") as Logger
Configurator.setLevel(log, Level.ALL)
log.addAppender()
ObjectMapper objectMapper = new ObjectMapper()

remoteSpock(httpMethod: "POST", groups: ["jira-administrators"]) { MultivaluedMap queryParams, String body, HttpServletRequest request ->


ObjectMapper objectMapper = new ObjectMapper()

log.info("Remote Spock triggered")
String urlSubPath = getAdditionalPath(request)
log.debug("\tGot url sub path:" + urlSubPath)
log.debug("\tGot query parameters:" + queryParams)
log.debug("\tGot body:" + body.take(15) + (body.size() > 15 ? "..." : ""))
log.debug("\tGot body:" + body.take(150) + (body.size() > 150 ? "..." : ""))


String finalOutput = ""
Map finalOutput = [:]
if (urlSubPath.startsWith("/spock/class")) {
log.info("\tRunning spock class")

Map bodyMap = objectMapper.readValue(body, Map)

finalOutput = runSpockClass(log, bodyMap.get("className", null) as String, bodyMap.get("methodName", null) as String)
finalOutput = runSpockClass(log, bodyMap.get("className", null) as String, bodyMap.get("methodName", null) as String, bodyMap.get("outputType", null) as SpockOutputType, bodyMap.get("outputDirPath", null) as String)

log.info("Finished running spock class, returning output:\n" + finalOutput)
return Response.ok(finalOutput, MediaType.TEXT_PLAIN).build()
log.info("Finished running spock class, returning output:\n" + finalOutput.take(150) + (body.size() > 150 ? "..." : ""))
return Response.ok(finalOutput, MediaType.APPLICATION_JSON).build()

} else {
return Response.status(Response.Status.BAD_REQUEST).entity("Unsupported url sub path:" + urlSubPath).build()
Expand All @@ -100,7 +106,7 @@ remoteSpock(httpMethod: "POST", groups: ["jira-administrators"]) { MultivaluedMa

}

static String runSpockClass(Logger logEndpoint, String spockClassName, String spockMethodName = "") {
static Map runSpockClass(Logger logEndpoint, String spockClassName, String spockMethodName = "", SpockOutputType outputType = null, String outputDirPath = "") {


logEndpoint.info("Starting Spock test")
Expand Down Expand Up @@ -129,28 +135,36 @@ static String runSpockClass(Logger logEndpoint, String spockClassName, String sp
logEndpoint.debug("\t\tFound method: " + spockMethod.name + " (compiled name)")
}

TestExecutionSummary spockSummary = executeSpockTest(spockClass, spockMethod)
File outputDir = outputDirPath ? new File(outputDirPath) : null

StringWriter stringWriter = new StringWriter()
PrintWriter printWriter = new PrintWriter(stringWriter)
spockSummary.printTo(printWriter)
spockSummary.printFailuresTo(printWriter)


String out = stringWriter.dump()
Map spockOut = executeSpockTest(logEndpoint, spockClass, spockMethod, outputType, outputDir)

logEndpoint.info(out)

return out
return spockOut

}


static TestExecutionSummary executeSpockTest(Class aClass, Method aMethod = null) {
static Map executeSpockTest(Logger log, Class aClass, Method aMethod = null, SpockOutputType outputType = SpockOutputType.StringSummary, File spockOutDir = null) {


System.setProperty("junit.platform.reporting.open.xml.enabled", "true")
log.info("Executing Spock Test")
log.debug("\tTest name:" + aClass.canonicalName + "#${aMethod ?: "*"}")
log.debug("\tWill create output type:" + outputType.name())
log.debug("\tWill store output in: ${spockOutDir ? spockOutDir.canonicalPath : "N/A (Output wont be persisted)"}")

Long startOfTest = System.currentTimeMillis() - 2000
LauncherDiscoveryRequest request
//Clear any previous related properties
System.clearProperty("junit.platform.reporting.open.xml.enabled")
System.clearProperty("junit.platform.reporting.output.dir")
System.clearProperty("allure.results.directory")





if (aMethod) {
request = LauncherDiscoveryRequestBuilder.request().selectors(selectMethod(aClass, aMethod)).build()
Expand All @@ -162,30 +176,128 @@ static TestExecutionSummary executeSpockTest(Class aClass, Method aMethod = null
Launcher launcher = LauncherFactory.create();


SummaryGeneratingListener sumListener = new SummaryGeneratingListener();
OpenTestReportGeneratingListener listener = new OpenTestReportGeneratingListener()
Map outputMap = [:]
File tempOutDir = new File(".tempSpockReports/" + outputType.name() + "/").canonicalFile
tempOutDir.mkdirs()



switch (outputType) {

case SpockOutputType.StringSummary:
SummaryGeneratingListener sumListener = new SummaryGeneratingListener()
launcher.registerTestExecutionListeners(sumListener)
launcher.execute(request)
TestExecutionSummary summary = sumListener.getSummary()
StringWriter stringWriter = new StringWriter()
PrintWriter printWriter = new PrintWriter(stringWriter)
summary.printTo(printWriter)
summary.printFailuresTo(printWriter)

String output = stringWriter.buffer.toString()
File sumFile = new File(tempOutDir, "summary-" + System.currentTimeMillis().toString().takeRight(5) + ".txt")
sumFile.text = output
outputMap = [(sumFile.name): sumFile.text]

break

case SpockOutputType.LegacyXml:

StringWriter stringWriter = new StringWriter()
PrintWriter printWriter = new PrintWriter(stringWriter)
LegacyXmlReportGeneratingListener listener = new LegacyXmlReportGeneratingListener(tempOutDir.toPath(), printWriter)
launcher.registerTestExecutionListeners(listener)
TestPlan testPlan = launcher.discover(request)
listener.testPlanExecutionStarted(testPlan)
launcher.execute(request)
listener.testPlanExecutionFinished(testPlan)


if (stringWriter.buffer.length()) {
//Should only happen if exception is thrown
outputMap.put("exception", stringWriter.buffer.toString())
}


tempOutDir.listFiles(new AgeFileFilter(startOfTest, false) as FileFilter).findAll { it.name.endsWith(".xml") }.each {
log.trace("\t"*2 + "Got output file: " + it.name + ", will rename it to make it unique")
String newFileName = System.currentTimeMillis().toString().takeRight(5) + ".xml"
newFileName = it.name.replace(".xml", newFileName)
File newFile = it.toPath().resolveSibling(newFileName).toFile()
it.renameTo(newFile)

log.trace("\t"*3 + "New name:" + newFile.name)

AllureJunitPlatform allureListener = new AllureJunitPlatform()
outputMap.put(newFile.name, newFile.text)
}


break

case SpockOutputType.OpenTestReport:

System.setProperty("junit.platform.reporting.open.xml.enabled", "true")
System.setProperty("junit.platform.reporting.output.dir", tempOutDir.canonicalPath)


OpenTestReportGeneratingListener listener = new OpenTestReportGeneratingListener()
launcher.registerTestExecutionListeners(listener)
TestPlan testPlan = launcher.discover(request)
listener.testPlanExecutionStarted(testPlan)
launcher.execute(request)
listener.testPlanExecutionFinished(testPlan)


tempOutDir.listFiles(new AgeFileFilter(startOfTest, false) as FileFilter).findAll { it.name.endsWith(".xml") }.each {
log.trace("\t"*2 + "Got output file: " + it.name)
outputMap.put(it.name, it.text)
}

break

case SpockOutputType.AllureReport:

launcher.registerTestExecutionListeners(listener)
System.setProperty("allure.results.directory", tempOutDir.canonicalPath)

launcher.registerTestExecutionListeners(allureListener)
launcher.registerTestExecutionListeners(sumListener)
TestPlan testPlan = launcher.discover(request)

AllureJunitPlatform listener = new AllureJunitPlatform()
launcher.registerTestExecutionListeners(listener)
TestPlan testPlan = launcher.discover(request)
listener.testPlanExecutionStarted(testPlan)
launcher.execute(request)
listener.testPlanExecutionFinished(testPlan)

allureListener.testPlanExecutionStarted(testPlan)

listener.testPlanExecutionStarted(testPlan)
launcher.execute(request)
listener.testPlanExecutionFinished(testPlan)
//listener.lifecycle.getDefaultWriter()

tempOutDir.listFiles(new AgeFileFilter(startOfTest, false) as FileFilter).findAll { it.name.endsWith(".json") }.each {
log.trace("\t"*2 + "Got output file: " + it.name)
outputMap.put(it.name, it.text)
}
break
default:
throw new InputMismatchException("Unsupported output type ${outputType}")


}


if (spockOutDir) {
tempOutDir.eachFile {src ->
log.debug("\tPersisting test file:" + src.name)
FileUtils.moveFileToDirectory(src, spockOutDir, true)
}

}

//Cleanup temp dirs and files
assert tempOutDir.deleteDir() : "Error deleting temp dir:" + tempOutDir.canonicalPath

allureListener.testPlanExecutionFinished(testPlan)

System.clearProperty("junit.platform.reporting.open.xml.enabled")

TestExecutionSummary summary = sumListener.getSummary()
return outputMap

return summary
}


Expand Down
Loading

0 comments on commit 0247511

Please sign in to comment.