-
Notifications
You must be signed in to change notification settings - Fork 17
/
SchemaTest.scala
138 lines (123 loc) 路 4.39 KB
/
SchemaTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package mutationtesting
import java.io.ByteArrayInputStream
import java.nio.file.Paths
import scala.io.Source
import io.circe.JsonObject
import io.circe.parser.decode
import mutationtesting.circe._
import org.leadpony.justify.api.JsonValidationService
class SchemaTest extends munit.FunSuite {
test("encoded json should be valid for mutation-testing-report-schema") {
val sut = MutationTestResult[JsonObject](
thresholds = Thresholds(high = 80, low = 10),
files = Map(
"src/stryker4s/Stryker4s.scala" -> FileResult(
source = "case class Stryker4s(foo: String)",
mutants = Seq(
MutantResult(
"1",
"BinaryOperator",
"-",
Location(
Position(1, 2),
Position(2, 3)
),
status = MutantStatus.Killed
)
)
)
),
projectRoot = Some("/src/stryker4s")
)
val jsonString = toJsonString(sut)
val reader = createJsonReader(jsonString)
// Actually asserts that no exception is thrown
assertEquals(reader.readValue().toString, jsonString)
}
test("decoded json report is without errors") {
val report =
Source
.fromFile(
"../elements/testResources/scala-example/mutation-report.json"
)
.mkString
decode[JsonConfigMutationTestResult](report) match {
case Left(err) => throw err
case Right(report) =>
assert(report.`$schema`.isEmpty)
assertEquals(report.schemaVersion, "1")
assertEquals(report.thresholds, Thresholds(80, 60))
assertEquals(report.projectRoot, Some("src/main/scala/stryker4s"))
report.files.foreach { case (_, result) =>
assertEquals(result.language, "scala")
assert(result.mutants.nonEmpty)
}
}
}
val validJsons = List(
"strict-report-v1",
"strict-report-v2",
"additional-properties-report",
"missing-test-files",
"missing-end-location",
"data-url"
)
validJsons.foreach { fileName =>
test(s"json $fileName should be valid according to schema") {
decodeReport(fileName) match {
case Left(value) => throw value
case Right(report) =>
val jsonString = toJsonString(report)
val reader = createJsonReader(jsonString)
// Actually asserts that no exception is thrown
assertEquals(reader.readValue().toString, jsonString)
}
}
}
val invalidJsons = Map(
"thresholds/threshold-too-high-report" -> "thresholds.high should be <= 100",
"thresholds/threshold-too-low-report" -> "thresholds.low should be > 0",
"missing-mutant-location-report" -> "end",
"missing-test-name" -> "name",
"missing-framework-name" -> "name",
"missing-tests" -> "tests",
"missing-system-ci" -> "ci",
"missing-system-cpu-logical-cores" -> "logicalCores",
"missing-system-os-platform" -> "platform",
"missing-system-ram-total" -> "total",
"missing-performance-fields" -> "setup"
)
invalidJsons.foreach { case (fileName, expectedErrorMessage) =>
test(s"json $fileName should be valid according to schema") {
decodeReport(fileName) match {
case Left(value) =>
assert(
value.getMessage().contains(expectedErrorMessage),
s"Error '${value.getMessage()}' did not contain '$expectedErrorMessage'"
)
case Right(value) =>
fail(s"Expected decoding error, but got a value '$value'")
}
}
}
def decodeReport(
fileName: String
): Either[io.circe.Error, JsonConfigMutationTestResult] = {
val reportString =
Source.fromFile(s"../report-schema/testResources/$fileName.json").mkString
decode[JsonConfigMutationTestResult](reportString)
}
def toJsonString(report: JsonConfigMutationTestResult) = {
import io.circe.syntax._
report.asJson.noSpaces
}
def createJsonReader(jsonString: String) = {
val service = JsonValidationService.newInstance();
val schema = service.readSchema(
Paths.get("../report-schema/src/mutation-testing-report-schema.json")
)
val byteStream = new ByteArrayInputStream(jsonString.getBytes())
val handler = service.createProblemPrinter(fail(_))
service.createReader(byteStream, schema, handler)
}
}