-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITAppInfoControllerSpec.groovy
More file actions
120 lines (95 loc) · 4.81 KB
/
ITAppInfoControllerSpec.groovy
File metadata and controls
120 lines (95 loc) · 4.81 KB
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
package com.showcase.backend.integration.controller
import com.showcase.backend.controller.AppInfoController
import com.showcase.backend.integration.BaseIntegrationSpec
import com.showcase.backend.service.AppInfoService
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import static com.showcase.backend.testhelper.EntityHelperSpec.APPINFO_DESCRIPTION
import static com.showcase.backend.testhelper.EntityHelperSpec.APPINFO_VERSION
import static org.hamcrest.Matchers.hasSize
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
@AutoConfigureMockMvc
class ITAppInfoControllerSpec extends BaseIntegrationSpec {
@Autowired
MockMvc mockMvc
@Autowired
AppInfoService appInfoService;
def setup() {
clearDatabase()
}
def "should create, retrieve, update and delete an AppInfo via REST API"() {
given: "a new AppInfo JSON"
def appInfoJson = JsonOutput.toJson([
version : APPINFO_VERSION,
description: APPINFO_DESCRIPTION
])
and: "the url to the AppInfo service"
def url = "${AppInfoController.ENDPOINT_URL}"
when: "send a POST request to save the AppInfo over the endpoint"
def saveResult = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(appInfoJson))
.andExpect(status().isOk())
.andExpect(jsonPath('$.id').exists())
.andExpect(jsonPath('$.version').value(APPINFO_VERSION))
.andExpect(jsonPath('$.description').value(APPINFO_DESCRIPTION))
.andReturn()
and: "retrieve the id from the response object"
Long idFromSaveResult = new JsonSlurper().parseText(saveResult.response.contentAsString).id
then: "check in database and confirm AppInfo table has one and only one entry"
appInfoService.count() == 1
when: "retrieve AppInfo from database with id from response"
def databaseEntity = appInfoService.find(idFromSaveResult)
then: "compare the database AppInfo with expected properties"
databaseEntity.version == APPINFO_VERSION
databaseEntity.description == APPINFO_DESCRIPTION
when: "send a GET request to retrieve all AppInfo entities and confirm the list size is one"
mockMvc.perform(get(url))
.andExpect(status().isOk())
.andExpect(jsonPath('$', hasSize(1)))
.andExpect(jsonPath('$[0].id').value(idFromSaveResult))
and: "construct endpoint url with id"
def urlWithId = "$url/$idFromSaveResult"
and: "send a GET request to retrieve the AppInfo over endpoint and confirm values"
mockMvc.perform(get(urlWithId))
.andExpect(status().isOk())
.andExpect(jsonPath('$.id').value(idFromSaveResult))
.andExpect(jsonPath('$.version').value(APPINFO_VERSION))
.andExpect(jsonPath('$.description').value(APPINFO_DESCRIPTION))
and: "create json for an updated version of the AppInfo"
def expectedVersion = "1234"
def expectedDesc = "Some new text"
def updatedAppInfoJson = JsonOutput.toJson([
version : expectedVersion,
description: expectedDesc
])
and: "send a PUT request with the updated AppInfo Json via endpoint"
mockMvc.perform(put(urlWithId)
.contentType(MediaType.APPLICATION_JSON)
.content(updatedAppInfoJson))
.andExpect(status().isOk())
.andExpect(jsonPath('$.version').value(expectedVersion))
.andExpect(jsonPath('$.description').value(expectedDesc))
and: "retrieve AppInfo from database"
databaseEntity = appInfoService.find(idFromSaveResult)
then: "database should still only have one entry"
appInfoService.count() == 1
and: "database object should have been updated"
databaseEntity.version == expectedVersion
databaseEntity.description == expectedDesc
when: "send a DELETE request with id via endpoint"
mockMvc.perform(delete(urlWithId))
.andExpect(status().isOk())
then: "database should be empty"
appInfoService.count() == 0
and: "the GET request for entity fails"
mockMvc.perform(get(urlWithId))
.andExpect(status().isNotFound())
}
}