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

Replace groovy.json.JsonOutput with kotlinx.serialization.json in JsonReporter #74 #76

Merged
merged 4 commits into from
Oct 18, 2023
Merged
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
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
@@ -0,0 +1,53 @@
package io.github.platan.tests_execution_chart.reporters.json

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"
platan marked this conversation as resolved.
Show resolved Hide resolved
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") ==
platan marked this conversation as resolved.
Show resolved Hide resolved
"""{
"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()
}
}