This repository has been archived by the owner on May 22, 2019. It is now read-only.
/
JsonJobParserSpec.scala
87 lines (73 loc) · 2.4 KB
/
JsonJobParserSpec.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
package com.twitter.gizzard.scheduler
import org.specs.Specification
import org.specs.mock.{ClassMocker, JMocker}
import com.twitter.json.Json
class JsonJobParserSpec extends ConfiguredSpecification with JMocker with ClassMocker {
"JsonJobParser" should {
val attributes = Map("a" -> 1)
val jobMap = Map("Job" -> attributes)
val codec = mock[JsonCodec]
val job = mock[JsonJob]
val jobParser = new JsonJobParser {
def apply(json: Map[String, Any]) = {
job
}
}
"parse" in {
"simple job" in {
expect {
one(job).errorCount_=(0)
one(job).errorMessage_=(any[String])
}
jobParser.parse(attributes) mustEqual job
}
"nested job" in {
expect {
one(codec).inflate(jobMap) willReturn job
allowing(job).className willReturn "ugh"
allowing(job).toMap willReturn Map.empty[String, String]
}
val nestedAttributes = Map("tasks" -> List(jobMap))
new JsonNestedJobParser(codec).parse(nestedAttributes) mustEqual new JsonNestedJob(List(job))
}
"errors" in {
expect {
one(job).errorCount_=(23)
one(job).errorMessage_=("Good heavens!")
}
jobParser.parse(attributes ++ Map("error_count" -> 23, "error_message" -> "Good heavens!")) mustEqual job
}
}
"toJson" in {
val job = new JsonJob {
def toMap = attributes
override def className = "FakeJob"
def apply() { }
}
"JsonJob" in {
val json = job.toJson
json mustMatch "\"FakeJob\""
json mustMatch "\"a\":1"
json mustMatch "\"error_count\":0"
json mustMatch "\"error_message\":\"\\(none\\)\""
}
"JsonNestedJob" in {
val nestedJob = new JsonNestedJob(List(job))
val json = nestedJob.toJson
json mustMatch "\"com.twitter.gizzard.scheduler.JsonNestedJob\":\\{"
json mustMatch "\"error_count\":0"
json mustMatch "\"error_message\":\"\\(none\\)\""
json mustMatch "\"tasks\":\\[\\{\"FakeJob\":\\{\"a\":1\\}\\}\\]"
}
"errors" in {
job.errorCount = 23
job.errorMessage = "Good heavens!"
val json = job.toJson
json mustMatch "\\{\"FakeJob\":\\{"
json mustMatch "\"a\":1"
json mustMatch "\"error_count\":23"
json mustMatch "\"error_message\":\"Good heavens!\""
}
}
}
}