Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enriching export #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions gradle-test-export-plugin.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't have to commit this file

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted

<module external.linked.project.id="gradle-test-export-plugin" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="com.ullink.gradle" external.system.module.version="0.2" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
71 changes: 51 additions & 20 deletions src/main/groovy/com/ullink/testtools/elastic/TestExportTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ullink.testtools.elastic
import com.ullink.testtools.elastic.models.Result
import groovy.io.FileType
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.util.logging.Slf4j
import org.elasticsearch.action.bulk.BulkProcessor
import org.elasticsearch.action.index.IndexRequest
Expand Down Expand Up @@ -44,6 +45,14 @@ class TestExportTask extends Exec {
@Optional
def targetDirectory

@Input
@Optional
def enrichment

@Input
@Optional
def featureExport
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add another task instead of export using a closure

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in a new FeatureExportTask class inside the project that just prompts the ullink plugin to export the feature?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a new task depends on this task, exporting the feature is ok


@Input
@Optional
def type = "testcase"
Expand All @@ -54,31 +63,36 @@ class TestExportTask extends Exec {

@Input
@Optional
String indexTimestampPattern = "yyyy-MM-dd"
String indexTimestampPattern = "yyyy-MM"

@Input
@Optional
String buildTime = LocalDateTime.now().toString()

@Internal
BulkProcessor processor
@Internal
TransportClient client

def overrideDefaultProperties(Properties properties) {
if (getHost() != null) {
log.error "setting host " + getHost()
properties.setProperty('host', getHost())
static def overrideDefaultProperties(TestExportTask task, Properties properties) {
if (task.host != null) {
log.info "setting host ${task.host}" +
properties.setProperty('host', task.host)
}
if (getClusterName() != null) {
properties.setProperty('clusterName', getClusterName())
if (task.clusterName != null) {
properties.setProperty('clusterName', task.clusterName)
}
if (getPort() != null) {
properties.setProperty('port', getPort())
if (task.port != null) {
properties.setProperty('port', task.port)
}

return properties
}

@Override
void exec() {
ElasticSearchProcessor elasticSearchProcessor = new ElasticSearchProcessor()
Properties parameters = overrideDefaultProperties(elasticSearchProcessor.getParameters())
Properties parameters = overrideDefaultProperties(this, elasticSearchProcessor.getParameters())

def bulkProcessorListener = elasticSearchProcessor.buildBulkProcessorListener()
client = elasticSearchProcessor.buildTransportClient(parameters)
Expand Down Expand Up @@ -106,9 +120,14 @@ class TestExportTask extends Exec {
def list = parseTestFiles(files)
list.each {
def output = JsonOutput.toJson(it)
def timetamp = LocalDateTime.parse(it.timestamp)
String index = indexPrefix + timetamp.format(DateTimeFormatter.ofPattern(indexTimestampPattern))
output = new JsonSlurper().parseText(output)
assert output instanceof Map
output.remove("timestamp")

def timestamp = LocalDateTime.parse(it.timestamp)
String index = indexPrefix + timestamp.format(DateTimeFormatter.ofPattern(indexTimestampPattern))
index = index.replace('.', '-')

String typeFinal
switch (type) {
case GString:
Expand All @@ -123,42 +142,52 @@ class TestExportTask extends Exec {
default:
throw new IllegalArgumentException("'type' attribute of type ${type.getClass()} is not supported")
}

String id = sha1Hashed(it.getClassname() + it.getName() + it.timestamp)
IndexRequest indexObj = new IndexRequest(index, typeFinal, id)
processor.add(indexObj.source(output, XContentType.JSON))
}
}


if (featureExport) {
featureExport.exportFeature(this)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part should not be done in here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the internal one


processor.close()
}

static def sha1Hashed(String value) {
def messageDigest = MessageDigest.getInstance("SHA-1")
String hexString = messageDigest.digest(value.getBytes()).collect { String.format('%02x', it)}.join()
String hexString = messageDigest.digest(value.getBytes()).collect { String.format('%02x', it) }.join()
return hexString
}

List<Result> parseTestFiles(List<File> files) {
def list = []
files.each {
def xmlDoc = new XmlSlurper().parse(it)
String timestamp = xmlDoc.@timestamp
def fileName = (xmlDoc.@name)

xmlDoc.children().each {
if (it.name() == "testcase") {
Result result = parseTestCase(it)
result.timestamp = timestamp

if (enrichment) {
result = enrichment.enrichJson(result, it, fileName)
}

list << result
}
}
}

list
}

def parseTestCase(def p) {
String testname = p.@name
Result result = new Result(name: testname)
String testName = p.@name
Result result = new Result(name: testName)
result.timestamp = buildTime
def time = Float.parseFloat([email protected]()) * 1000
result.with {
classname = p.@classname
Expand All @@ -171,7 +200,6 @@ class TestExportTask extends Exec {
result.with {
failureMessage = node.@message
failureType = node.@type
failureText = node.text()
resultType = TestResult.ResultType.FAILURE
}
}
Expand All @@ -180,6 +208,8 @@ class TestExportTask extends Exec {
}
}
result.properties = resolveProperties(p)
result.projectName = project.getName()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, it should be done in enrichment
I can work with you to see how it can be done

result
}

Expand All @@ -192,4 +222,5 @@ class TestExportTask extends Exec {
}
return null
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Result {
float executionTime
String failureMessage
String failureType
String failureText
TestResult.ResultType resultType
String timestamp
String projectName
Map<String, ?> properties
}
}