1+ package com .booking .backend .controller ;
2+
3+ import com .booking .backend .entity .Organization ;
4+ import com .booking .backend .entity .TypeOrganization ;
5+ import com .booking .backend .repository .OrganizationRepository ;
6+ import com .fasterxml .jackson .databind .ObjectMapper ;
7+ import org .junit .jupiter .api .AfterEach ;
8+ import org .junit .jupiter .api .BeforeEach ;
9+ import org .junit .jupiter .api .Test ;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
12+ import org .springframework .boot .test .context .SpringBootTest ;
13+ import org .springframework .test .web .servlet .MockMvc ;
14+
15+ import java .util .List ;
16+
17+ import static org .hamcrest .Matchers .hasSize ;
18+ import static org .hamcrest .Matchers .is ;
19+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
20+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
21+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
22+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
23+
24+ @ SpringBootTest
25+ @ AutoConfigureMockMvc
26+ class OrganizationControllerTest {
27+
28+ @ Autowired
29+ private OrganizationRepository organizationRepository ;
30+
31+ @ Autowired
32+ private ObjectMapper objectMapper ;
33+
34+ @ Autowired
35+ private MockMvc mockMvc ;
36+
37+ @ BeforeEach
38+ void init () {
39+ organizationRepository .save (
40+ new Organization (null , "Al" , "10-22" , 1000.0 , 8.9 , TypeOrganization .BAR ));
41+ }
42+
43+ @ AfterEach
44+ void destroy () {
45+ organizationRepository .deleteAll ();
46+ }
47+
48+ @ Test
49+ void getAll () throws Exception {
50+ mockMvc .perform (get ("/organization" ))
51+ .andDo (print ())
52+ .andExpect (status ().isOk ())
53+ .andExpect (jsonPath ("$[0].name" , is ("Al" )));
54+ }
55+
56+ @ Test
57+ void getAllWithPage () throws Exception {
58+ Organization organization = new Organization (null , "Al" , "10-22" , 1000.0 , 7.7 , TypeOrganization .BAR );
59+ Organization organization1 = new Organization (null , "Al" , "10-22" , 1000.0 , 8.0 , TypeOrganization .BAR );
60+ List <Organization > list = List .of (organization , organization1 );
61+ organizationRepository .saveAll (list );
62+
63+ mockMvc .perform (get ("/organization" ).param ("pageNo" , "0" )
64+ .param ("pageSize" , "2" )
65+ .param ("sortBy" , "rate" ))
66+ .andDo (print ())
67+ .andExpect (status ().isOk ())
68+ .andExpect (jsonPath ("$" , hasSize (3 )))
69+ .andExpect (jsonPath ("$[1].rating" , is (7.7 )));
70+ }
71+
72+ @ Test
73+ void getAllOrganization () throws Exception {
74+ Organization organization = new Organization (null , "Al" , "10-22" , 1000.0 , 7.7 , TypeOrganization .BAR );
75+ Organization organization1 = new Organization (null , "Al" , "10-22" , 1000.0 , 8.0 , TypeOrganization .BAR );
76+ List <Organization > list = List .of (organization , organization1 );
77+ organizationRepository .saveAll (list );
78+
79+ mockMvc .perform (get ("/organization" ))
80+ .andDo (print ())
81+ .andExpect (status ().isOk ())
82+ .andExpect (jsonPath ("$" , hasSize (3 )));
83+ }
84+ }
0 commit comments