-
Notifications
You must be signed in to change notification settings - Fork 1
/
PersonResourceSpec.groovy
102 lines (81 loc) · 2.94 KB
/
PersonResourceSpec.groovy
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
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import org.glassfish.grizzly.http.server.HttpServer
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
class PersonResourceSpec extends Specification {
@Shared static HttpServer server
RESTClient client = new RESTClient('http://localhost:1234/', ContentType.JSON)
void setupSpec() {
server = GrizzlyHttpServerFactory.createHttpServer(
'http://localhost:1234/'.toURI(), new MyApplication())
}
def 'server is running'() {
expect: server.started
}
def 'get request returns all people'() {
when:
def response = client.get(path: 'people')
then:
response.status == 200
response.contentType == 'application/json'
response.data.size() == 5
}
@Unroll
def "people/#id gives #name"() {
expect:
def response = client.get(path: "people/$id")
name == "$response.data.first $response.data.last"
response.status == 200
where:
id | name
1 | 'Jean-Luc Picard'
2 | 'Johnathan Archer'
3 | 'James Kirk'
4 | 'Benjamin Sisko'
5 | 'Kathryn Janeway'
}
def 'people/lastname/{like} searches for last names that include given string'() {
when:
def response = client.get(path: "people/lastname/a")
then:
response.data.size() == 3
response.data*.last ==~ /.*[aA].*/
}
def 'insert and delete a person'() {
given: 'A JSON object with first and last names'
def json = [first: 'Peter Quincy', last: 'Taggart']
when: 'post the JSON object'
def response = client.post(path: 'people',
contentType: ContentType.JSON, body: json)
then: 'number of stored objects goes up by one'
getAll().size() == old(getAll().size()) + 1
response.data.first == 'Peter Quincy'
response.data.last == 'Taggart'
response.status == 201
response.contentType == 'application/json'
response.headers.Location == "http://localhost:1234/people/${response.data.id}"
when: 'delete the new JSON object'
client.delete(path: "people/${response.data.id}")
then: 'number of stored objects goes down by one'
getAll().size() == old(getAll().size()) - 1
}
def 'can update an existing person'() {
given:
def kirk = client.get(path: 'people/3')
def json = [id: 3, first:'James T.', last: 'Kirk']
when:
def response = client.put(path: "people/${kirk.data.id}",
contentType: ContentType.JSON, body: json)
then:
"$response.data.first $response.data.last" == 'James T. Kirk'
}
private List getAll() {
client.get(path: 'people').data
}
void cleanupSpec() {
server?.stop()
}
}