16
16
17
17
package com .josdem .vetlog .controller ;
18
18
19
+ import static com .josdem .vetlog .controller .PetControllerTest .PET_UUID ;
20
+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
21
+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
19
22
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
23
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
20
24
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .model ;
21
25
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
22
26
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .view ;
23
27
28
+ import com .josdem .vetlog .enums .PetStatus ;
29
+ import com .josdem .vetlog .enums .PetType ;
30
+ import jakarta .transaction .Transactional ;
24
31
import lombok .extern .slf4j .Slf4j ;
32
+ import org .junit .jupiter .api .BeforeEach ;
25
33
import org .junit .jupiter .api .DisplayName ;
26
34
import org .junit .jupiter .api .Test ;
27
35
import org .junit .jupiter .api .TestInfo ;
28
36
import org .springframework .beans .factory .annotation .Autowired ;
29
37
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
30
38
import org .springframework .boot .test .context .SpringBootTest ;
39
+ import org .springframework .mock .web .MockMultipartFile ;
31
40
import org .springframework .security .test .context .support .WithMockUser ;
32
41
import org .springframework .test .web .servlet .MockMvc ;
42
+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
43
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
44
+ import org .springframework .web .context .WebApplicationContext ;
33
45
34
46
@ Slf4j
35
47
@ SpringBootTest
@@ -39,6 +51,19 @@ class VetControllerTest {
39
51
@ Autowired
40
52
private MockMvc mockMvc ;
41
53
54
+ @ Autowired
55
+ private WebApplicationContext webApplicationContext ;
56
+
57
+ private final MockMultipartFile image =
58
+ new MockMultipartFile ("mockImage" , "image.jpg" , "image/jpeg" , "image" .getBytes ());
59
+
60
+ @ BeforeEach
61
+ public void setUp () {
62
+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext )
63
+ .apply (springSecurity ())
64
+ .build ();
65
+ }
66
+
42
67
@ Test
43
68
@ DisplayName ("showing create vet form" )
44
69
@ WithMockUser (username = "josdem" , password = "12345678" , roles = "USER" )
@@ -49,4 +74,39 @@ void shouldShowCreateVetForm(TestInfo testInfo) throws Exception {
49
74
.andExpect (model ().attributeExists ("usernameCommand" ))
50
75
.andExpect (view ().name ("vet/form" ));
51
76
}
77
+
78
+ @ Test
79
+ @ Transactional
80
+ @ DisplayName ("searching by user" )
81
+ @ WithMockUser (username = "admin" , password = "12345678" , roles = "ADMIN" )
82
+ void shouldSearchPetsByUser (TestInfo testInfo ) throws Exception {
83
+ log .info ("Running: {}" , testInfo .getDisplayName ());
84
+
85
+ registerPet ();
86
+
87
+ mockMvc .perform (post ("/vet/search" ).with (csrf ()).param ("username" , "josdem" ))
88
+ .andExpect (status ().isOk ())
89
+ .andExpect (model ().attributeExists ("pets" ))
90
+ .andExpect (model ().attributeExists ("defaultImage" ))
91
+ .andExpect (view ().name ("vet/list" ));
92
+ }
93
+
94
+ private void registerPet () throws Exception {
95
+
96
+ mockMvc .perform (MockMvcRequestBuilders .multipart ("/pet/save" )
97
+ .file (image )
98
+ .with (csrf ())
99
+ .param ("name" , "Cremita" )
100
+ .param ("uuid" , PET_UUID )
101
+ .param ("birthDate" , "2024-08-22T09:28:00" )
102
+ .param ("dewormed" , "true" )
103
+ .param ("vaccinated" , "true" )
104
+ .param ("sterilized" , "true" )
105
+ .param ("breed" , "11" )
106
+ .param ("user" , "1" )
107
+ .param ("status" , PetStatus .OWNED .toString ())
108
+ .param ("type" , PetType .DOG .toString ()))
109
+ .andExpect (status ().isOk ())
110
+ .andExpect (view ().name ("pet/create" ));
111
+ }
52
112
}
0 commit comments