From d0836a30d26c2aa4c594f8df757435e7df651e23 Mon Sep 17 00:00:00 2001 From: Julia Glaszka Date: Wed, 18 Oct 2023 15:09:03 +0200 Subject: [PATCH] changed packages to kotlinx + added test for JsonReporter --- .../build.gradle.kts | 1 - .../reporters/json/JsonReporter.kt | 9 ++-- .../reporters/json/JsonReporterTest.groovy | 51 ++++++++++++++++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/tests-execution-chart-commons/build.gradle.kts b/tests-execution-chart-commons/build.gradle.kts index 5e1db15..e1e354b 100644 --- a/tests-execution-chart-commons/build.gradle.kts +++ b/tests-execution-chart-commons/build.gradle.kts @@ -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")) diff --git a/tests-execution-chart-commons/src/main/kotlin/io/github/platan/tests_execution_chart/reporters/json/JsonReporter.kt b/tests-execution-chart-commons/src/main/kotlin/io/github/platan/tests_execution_chart/reporters/json/JsonReporter.kt index bc68078..5188b88 100644 --- a/tests-execution-chart-commons/src/main/kotlin/io/github/platan/tests_execution_chart/reporters/json/JsonReporter.kt +++ b/tests-execution-chart-commons/src/main/kotlin/io/github/platan/tests_execution_chart/reporters/json/JsonReporter.kt @@ -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() { - + 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.") } diff --git a/tests-execution-chart-commons/src/test/groovy/io/github/platan/tests_execution_chart/reporters/json/JsonReporterTest.groovy b/tests-execution-chart-commons/src/test/groovy/io/github/platan/tests_execution_chart/reporters/json/JsonReporterTest.groovy index 7be71c5..01a81f3 100644 --- a/tests-execution-chart-commons/src/test/groovy/io/github/platan/tests_execution_chart/reporters/json/JsonReporterTest.groovy +++ b/tests-execution-chart-commons/src/test/groovy/io/github/platan/tests_execution_chart/reporters/json/JsonReporterTest.groovy @@ -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() + } }