1+ package cat .udl .eps .softarch .demo .steps ;
2+
3+ import cat .udl .eps .softarch .demo .domain .Project ;
4+ import cat .udl .eps .softarch .demo .domain .Visibility ;
5+ import cat .udl .eps .softarch .demo .repository .ProjectRepository ;
6+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
7+ import io .cucumber .java .en .And ;
8+ import io .cucumber .java .en .Given ;
9+ import io .cucumber .java .en .Then ;
10+ import io .cucumber .java .en .When ;
11+ import org .springframework .http .MediaType ;
12+
13+ import java .nio .charset .StandardCharsets ;
14+
15+ import static org .hamcrest .Matchers .hasItem ;
16+ import static org .hamcrest .Matchers .is ;
17+ import static org .hamcrest .Matchers .empty ;
18+ import static org .hamcrest .Matchers .notNullValue ;
19+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
20+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
21+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
22+
23+ public class ManageProjectStepDefs {
24+
25+ private final StepDefs stepDefs ;
26+ private final ProjectRepository projectRepository ;
27+ private Project preparedProject ;
28+
29+ public ManageProjectStepDefs (StepDefs stepDefs , ProjectRepository projectRepository ) {
30+ this .stepDefs = stepDefs ;
31+ this .projectRepository = projectRepository ;
32+ this .stepDefs .mapper .registerModule (new JavaTimeModule ());
33+ }
34+
35+ // ===================== GIVEN =====================
36+
37+ @ Given ("I prepare a project with name {string} and description {string} and visibility {string}" )
38+ public void iPrepareAProject (String name , String description , String visibility ) {
39+ preparedProject = new Project ();
40+ preparedProject .setName (name );
41+ preparedProject .setDescription (description );
42+ preparedProject .setVisibility (Visibility .valueOf (visibility ));
43+ }
44+
45+ @ And ("There is a project with name {string} and description {string} and visibility {string}" )
46+ public void thereIsAProject (String name , String description , String visibility ) {
47+ Project p = new Project ();
48+ p .setName (name );
49+ p .setDescription (description );
50+ p .setVisibility (Visibility .valueOf (visibility ));
51+ projectRepository .save (p );
52+ }
53+
54+
55+ // ===================== WHEN =====================
56+
57+ @ When ("I send the create project request" )
58+ public void iSendTheCreateProjectRequest () throws Exception {
59+ stepDefs .result = stepDefs .mockMvc .perform (
60+ post ("/projects" )
61+ .contentType (MediaType .APPLICATION_JSON )
62+ .content (stepDefs .mapper .writeValueAsString (preparedProject ))
63+ .characterEncoding (StandardCharsets .UTF_8 )
64+ .accept (MediaType .APPLICATION_JSON )
65+ .with (AuthenticationStepDefs .authenticate ()))
66+ .andDo (print ());
67+
68+ if (stepDefs .result .andReturn ().getResponse ().getStatus () == 201 ) {
69+ String location = stepDefs .result .andReturn ().getResponse ().getHeader ("Location" );
70+ stepDefs .result = stepDefs .mockMvc .perform (
71+ get (location )
72+ .accept (MediaType .APPLICATION_JSON )
73+ .with (AuthenticationStepDefs .authenticate ()))
74+ .andDo (print ());
75+ }
76+ }
77+
78+ @ When ("I retrieve the project named {string}" )
79+ public void iRetrieveTheProjectNamed (String name ) throws Exception {
80+ Project found = projectRepository .findByNameContaining (name )
81+ .stream ().filter (p -> p .getName ().equals (name ))
82+ .findFirst ().orElseThrow ();
83+
84+ stepDefs .result = stepDefs .mockMvc .perform (
85+ get ("/projects/{id}" , found .getId ())
86+ .accept (MediaType .APPLICATION_JSON )
87+ .with (AuthenticationStepDefs .authenticate ()))
88+ .andDo (print ());
89+ }
90+
91+ @ When ("I retrieve the project with id {long}" )
92+ public void iRetrieveTheProjectWithId (Long id ) throws Exception {
93+ stepDefs .result = stepDefs .mockMvc .perform (
94+ get ("/projects/{id}" , id )
95+ .accept (MediaType .APPLICATION_JSON )
96+ .with (AuthenticationStepDefs .authenticate ()))
97+ .andDo (print ());
98+ }
99+
100+ @ When ("I update the project with id {long} setting name to {string}" )
101+ public void iUpdateTheProjectWithIdSettingName (Long id , String newName ) throws Exception {
102+ Project project = new Project ();
103+ project .setName (newName );
104+
105+ stepDefs .result = stepDefs .mockMvc .perform (
106+ patch ("/projects/{id}" , id )
107+ .contentType (MediaType .APPLICATION_JSON )
108+ .content (stepDefs .mapper .writeValueAsString (project ))
109+ .characterEncoding (StandardCharsets .UTF_8 )
110+ .accept (MediaType .APPLICATION_JSON )
111+ .with (AuthenticationStepDefs .authenticate ()))
112+ .andDo (print ());
113+ }
114+
115+ @ When ("I update the project with id {long} setting visibility to {string}" )
116+ public void iUpdateTheProjectWithIdSettingVisibility (Long id , String visibility ) throws Exception {
117+ Project project = new Project ();
118+ project .setVisibility (Visibility .valueOf (visibility ));
119+
120+ stepDefs .result = stepDefs .mockMvc .perform (
121+ patch ("/projects/{id}" , id )
122+ .contentType (MediaType .APPLICATION_JSON )
123+ .content (stepDefs .mapper .writeValueAsString (project ))
124+ .characterEncoding (StandardCharsets .UTF_8 )
125+ .accept (MediaType .APPLICATION_JSON )
126+ .with (AuthenticationStepDefs .authenticate ()))
127+ .andDo (print ());
128+ }
129+
130+ @ When ("I update the project named {string} with new name {string}" )
131+ public void iUpdateTheProjectNamed (String oldName , String newName ) throws Exception {
132+ Project found = projectRepository .findByNameContaining (oldName )
133+ .stream ().filter (p -> p .getName ().equals (oldName ))
134+ .findFirst ().orElseThrow ();
135+
136+ found .setName (newName );
137+
138+ stepDefs .result = stepDefs .mockMvc .perform (
139+ put ("/projects/{id}" , found .getId ())
140+ .contentType (MediaType .APPLICATION_JSON )
141+ .content (stepDefs .mapper .writeValueAsString (found ))
142+ .characterEncoding (StandardCharsets .UTF_8 )
143+ .accept (MediaType .APPLICATION_JSON )
144+ .with (AuthenticationStepDefs .authenticate ()))
145+ .andDo (print ());
146+ }
147+
148+
149+ @ When ("I update the project named {string} setting visibility to {string}" )
150+ public void iUpdateTheProjectVisibility (String name , String visibility ) throws Exception {
151+ Project found = projectRepository .findByNameContaining (name )
152+ .stream ().filter (p -> p .getName ().equals (name ))
153+ .findFirst ().orElseThrow ();
154+
155+ found .setVisibility (Visibility .valueOf (visibility ));
156+
157+ stepDefs .result = stepDefs .mockMvc .perform (
158+ put ("/projects/{id}" , found .getId ())
159+ .contentType (MediaType .APPLICATION_JSON )
160+ .content (stepDefs .mapper .writeValueAsString (found ))
161+ .characterEncoding (StandardCharsets .UTF_8 )
162+ .accept (MediaType .APPLICATION_JSON )
163+ .with (AuthenticationStepDefs .authenticate ()))
164+ .andDo (print ());
165+ }
166+
167+
168+ @ When ("I retrieve the list of projects" )
169+ public void iRetrieveTheListOfProjects () throws Exception {
170+ stepDefs .result = stepDefs .mockMvc .perform (
171+ get ("/projects" )
172+ .accept (MediaType .APPLICATION_JSON )
173+ .with (AuthenticationStepDefs .authenticate ()))
174+ .andDo (print ());
175+ }
176+
177+ @ When ("I delete the project named {string}" )
178+ public void iDeleteTheProjectNamed (String name ) throws Exception {
179+ Project found = projectRepository .findByNameContaining (name )
180+ .stream ().filter (p -> p .getName ().equals (name ))
181+ .findFirst ().orElseThrow ();
182+
183+ stepDefs .result = stepDefs .mockMvc .perform (
184+ delete ("/projects/{id}" , found .getId ())
185+ .with (AuthenticationStepDefs .authenticate ()))
186+ .andDo (print ());
187+ }
188+
189+ @ When ("I delete the project with id {long}" )
190+ public void iDeleteTheProjectWithId (Long id ) throws Exception {
191+ stepDefs .result = stepDefs .mockMvc .perform (
192+ delete ("/projects/{id}" , id )
193+ .with (AuthenticationStepDefs .authenticate ()))
194+ .andDo (print ());
195+ }
196+
197+ // ===================== THEN / AND =====================
198+
199+ @ Then ("The project name is {string}" )
200+ public void theProjectNameIs (String name ) throws Exception {
201+ stepDefs .result .andExpect (jsonPath ("$.name" , is (name )));
202+ }
203+
204+ @ Then ("The project creation date should be set" )
205+ public void theProjectCreationDateShouldBeSet () throws Exception {
206+ stepDefs .result .andExpect (jsonPath ("$.created" , notNullValue ()));
207+ }
208+
209+
210+ @ Then ("The project modification date should be set" )
211+ public void theProjectModificationDateShouldBeSet () throws Exception {
212+ stepDefs .result .andExpect (jsonPath ("$.modified" , notNullValue ()));
213+ }
214+
215+ @ Then ("The project list contains a project named {string}" )
216+ public void theProjectListContainsAProjectNamed (String name ) throws Exception {
217+ stepDefs .result .andExpect (
218+ jsonPath ("$._embedded.projects[*].name" , hasItem (is (name )))
219+ );
220+ }
221+
222+ @ Then ("The project visibility should be {string}" )
223+ public void theProjectVisibilityShouldBe (String visibility ) throws Exception {
224+ stepDefs .result .andExpect (jsonPath ("$.visibility" , is (visibility )));
225+ }
226+
227+ @ Then ("The project list is empty" )
228+ public void theProjectListIsEmpty () throws Exception {
229+ stepDefs .result .andExpect (jsonPath ("$._embedded.projects" , empty ()));
230+ }
231+
232+
233+ }
0 commit comments