/
PetControllerAutoTest.groovy
101 lines (90 loc) · 3.22 KB
/
PetControllerAutoTest.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
package ru.spring.auto.rest.docs.demo.controllers
import org.junit.Test
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders
import ru.ryabov.swagger_library.rest.model.Category
import ru.ryabov.swagger_library.rest.model.Pet
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
class PetControllerAutoTest extends MockMvcDocumentSpec {
@Test
void addPetTest() {
mockMvc.perform(
RestDocumentationRequestBuilders
.post("/pet")
.content(objectMapper.writeValueAsString(buildPet()))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
).andExpect(status().isOk())
}
@Test
void getPetTest() {
//given
def petId = 1L
//when
def resultActions = mockMvc
.perform(RestDocumentationRequestBuilders
.get("/pet/{petId}", petId)
.header("Accept", "application/json"))
//then
resultActions
.andExpect(status().isOk())
.andExpect(content().string(objectMapper.writeValueAsString(buildReturnPet())))
}
@Test
void deletePetTest() {
def petId = 200L
mockMvc.perform(
RestDocumentationRequestBuilders
.post("/pet")
.content(objectMapper.writeValueAsString(buildPet(petId)))
.header("Accept", "application/json")
.header("Content-Type", "application/json"))
mockMvc.perform(
RestDocumentationRequestBuilders
.delete("/pet/{petId}", petId)
.header("Accept", "application/json")
).andExpect(status().isOk())
}
@Test
void deletePetNegativeTest() {
def petId = 100L
mockMvc.perform(
RestDocumentationRequestBuilders
.delete("/pet/{petId}", petId)
.header("Accept", "application/json")
).andExpect(status().isNotFound())
}
private static Pet buildPet() {
Pet pet = new Pet()
pet.setId(2L)
pet.setName("Pussy")
Category category = new Category()
category.setId(2L)
category.setName("Cat")
pet.setCategory(category)
pet.setStatus(Pet.StatusEnum.AVAILABLE)
return pet
}
private static Pet buildPet(Long id) {
Pet pet = new Pet()
pet.setId(id)
pet.setName("Pussy")
Category category = new Category()
category.setId(2L)
category.setName("Cat")
pet.setCategory(category)
pet.setStatus(Pet.StatusEnum.AVAILABLE)
return pet
}
private static Pet buildReturnPet() {
Pet pet = new Pet();
pet.setId(1L);
pet.setName("Fluffy");
Category category = new Category();
category.setId(1L);
category.setName("Dog");
pet.setCategory(category);
pet.setStatus(Pet.StatusEnum.AVAILABLE);
return pet
}
}