Skip to content

Commit

Permalink
changed packages to kotlinx + added test for JsonReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
jglaszka committed Oct 18, 2023
1 parent 5df28d2 commit d0836a3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 0 additions & 1 deletion tests-execution-chart-commons/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ plugins {
dependencies {
api("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.apache.commons:commons-text:1.10.0")
implementation(localGroovy())
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")

testImplementation(platform("org.codehaus.groovy:groovy-bom:3.0.19"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package io.github.platan.tests_execution_chart.reporters.json

import groovy.json.JsonOutput
import io.github.platan.tests_execution_chart.report.ReportConfig
import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.reporters.GanttDiagramReporter
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File

class JsonReporter : GanttDiagramReporter<JsonConfig>() {

private val json = Json {
prettyPrint = true
}

override fun report(
report: TestExecutionScheduleReport,
baseDir: File,
taskName: String
) {
val jsonReport = JsonOutput.prettyPrint(JsonOutput.toJson(report))
val jsonReport = json.encodeToString(report)
val reportFile = save(jsonReport, taskName, baseDir, config.outputLocation, "json")
logger.lifecycle("Tests execution schedule report saved to ${reportFile.absolutePath} file.")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
package io.github.platan.tests_execution_chart.reporters.json

class JsonReporterTest {
import io.github.platan.tests_execution_chart.report.data.Mark
import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.report.data.TimedTestResult
import spock.lang.Specification
import java.time.Instant

import static io.github.platan.tests_execution_chart.report.data.TimedTestResult.Type.TEST

class JsonReporterTest extends Specification {
def "should generate report in json"() {
given:
def PATHNAME = "./test"
def CONFIG_OUTPUT_LOCATION = "reports/tests-execution/json"
def report = new TestExecutionScheduleReport([
new TimedTestResult('class', 'test', toEpochMilli('2023-03-10T19:00:02Z'), toEpochMilli('2023-03-10T19:00:05Z'), 'passed', TEST)
], [new Mark('mark1', toEpochMilli('2023-03-10T19:00:05Z'))])
def reporter = new JsonReporter().tap {
it.setConfiguration(new JsonConfig(true, CONFIG_OUTPUT_LOCATION))
}

when:
reporter.report(report, new File(PATHNAME), "taskname")

then:
def file = new File("""${PATHNAME}/${CONFIG_OUTPUT_LOCATION}/taskname.json""")
// i dont know how to make it with indentation
file.getText("UTF-8") ==
"""{
"results": [
{
"className": "class",
"testName": "test",
"startTime": 1678474802000,
"endTime": 1678474805000,
"resultType": "passed",
"type": "TEST"
}
],
"marks": [
{
"name": "mark1",
"timestamp": 1678474805000
}
]
}"""
}

private static long toEpochMilli(String instant) {
Instant.parse(instant).toEpochMilli()
}
}

0 comments on commit d0836a3

Please sign in to comment.