1+ package cat .udl .eps .softarch .fll .steps ;
2+
3+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
4+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
5+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
6+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .user ;
7+ import static org .hamcrest .Matchers .hasSize ;
8+ import java .nio .charset .StandardCharsets ;
9+ import java .util .HashMap ;
10+ import java .util .Map ;
11+ import org .springframework .http .MediaType ;
12+ import io .cucumber .java .en .Given ;
13+ import io .cucumber .java .en .Then ;
14+ import io .cucumber .java .en .When ;
15+
16+ public class SearchVenueStepDefs {
17+
18+ private final StepDefs stepDefs ;
19+
20+ public SearchVenueStepDefs (StepDefs stepDefs ) {
21+ this .stepDefs = stepDefs ;
22+ }
23+
24+ @ Given ("a venue exists with name {string} and city {string}" )
25+ public void a_venue_exists_with_name_and_city (String name , String city ) throws Exception {
26+ Map <String , String > body = new HashMap <>();
27+ body .put ("name" , name );
28+ body .put ("city" , city );
29+
30+ stepDefs .mockMvc .perform (post ("/venues" )
31+ .contentType (MediaType .APPLICATION_JSON )
32+ .content (stepDefs .mapper .writeValueAsString (body ))
33+ .characterEncoding (StandardCharsets .UTF_8 )
34+ .with (user ("user" ).roles ("USER" )))
35+ .andExpect (status ().isCreated ());
36+ }
37+
38+ @ When ("I search for a venue by partial name {string}" )
39+ public void i_search_for_a_venue_by_partial_name (String searchName ) throws Exception {
40+ stepDefs .result = stepDefs .mockMvc .perform (get ("/venues/search/findByNameContaining" )
41+ .param ("name" , searchName )
42+ .with (user ("user" ).roles ("USER" )));
43+ }
44+
45+ @ Then ("the venue search API response status should be {int}" )
46+ public void the_venue_search_api_response_status_should_be (int expectedStatus ) throws Exception {
47+ stepDefs .result .andExpect (status ().is (expectedStatus ));
48+ }
49+
50+ @ Then ("the search response should contain a venue with name {string}" )
51+ public void the_search_response_should_contain_a_venue_with_name (String expectedName ) throws Exception {
52+ stepDefs .result .andExpect (jsonPath ("$._embedded.venues[0].name" ).value (expectedName ));
53+ }
54+
55+ @ Then ("the search response should be empty" )
56+ public void the_search_response_should_be_empty () throws Exception {
57+ stepDefs .result .andExpect (jsonPath ("$._embedded.venues" , hasSize (0 )));
58+ }
59+ }
0 commit comments