1+ package cat .udl .eps .softarch .demo .steps ;
2+
3+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
4+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
5+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
6+
7+
8+ import java .nio .charset .StandardCharsets ;
9+ import java .time .ZonedDateTime ;
10+
11+ import org .junit .jupiter .api .Assertions ;
12+ import org .springframework .http .MediaType ;
13+
14+ import cat .udl .eps .softarch .demo .domain .Content ;
15+ import cat .udl .eps .softarch .demo .domain .Report ;
16+
17+ import cat .udl .eps .softarch .demo .repository .ContentRepository ;
18+ import cat .udl .eps .softarch .demo .repository .ReportRepository ;
19+ import io .cucumber .core .internal .com .fasterxml .jackson .databind .node .ObjectNode ;
20+ import io .cucumber .java .ParameterType ;
21+ import io .cucumber .java .en .Given ;
22+ import io .cucumber .java .en .Then ;
23+ import io .cucumber .java .en .When ;
24+
25+ public class ReportRepositoryStepsDefs {
26+
27+ private final ReportRepository reportRepository ;
28+ private final ContentRepository contentRepository ;
29+ private final StepDefs stepDefs ;
30+ private Long createdReportId ;
31+
32+ @ ParameterType (".*" )
33+ public Content content (String name ) {
34+ return this .contentRepository .findByName (name )
35+ .orElseThrow (() -> new RuntimeException ("Content not found" ));
36+ }
37+
38+ @ ParameterType ("\\ d+" )
39+ public Long Long (String value ) {
40+ return Long .valueOf (value );
41+ }
42+
43+ public ReportRepositoryStepsDefs (
44+ ReportRepository reportRepository ,
45+ ContentRepository contentRepository ,
46+ StepDefs stepDefs ) {
47+ this .reportRepository = reportRepository ;
48+ this .contentRepository = contentRepository ;
49+ this .stepDefs = stepDefs ;
50+ }
51+
52+ @ Given ("there are no Reports in the system" )
53+ public void there_are_no_Reports_in_the_system () {
54+ reportRepository .deleteAll ();
55+ Assertions .assertEquals (0 , reportRepository .count ());
56+ }
57+
58+ @ When ("^I create a Report with the last created content and reason \" ([^\" ]*)\" $" )
59+ public void iCreateAReportWithTheLastCreatedContent (String reason ) throws Exception {
60+ Content content = contentRepository .findAll ()
61+ .stream ()
62+ .reduce ((first , second ) -> second )
63+ .orElseThrow (() -> new RuntimeException ("No contents found" ));
64+
65+ String contentUri = "/contents/" + content .getContentId ();
66+
67+ com .fasterxml .jackson .databind .node .ObjectNode json = stepDefs .mapper .createObjectNode ();
68+ json .put ("reason" , reason );
69+ json .put ("createdAt" , ZonedDateTime .now ().toString ());
70+ json .put ("content" , contentUri );
71+
72+ stepDefs .result = stepDefs .mockMvc .perform (
73+ post ("/reports" )
74+ .contentType (MediaType .APPLICATION_JSON )
75+ .content (json .toString ())
76+ .characterEncoding (StandardCharsets .UTF_8 )
77+ .accept (MediaType .APPLICATION_JSON )
78+ .with (AuthenticationStepDefs .authenticate ()))
79+ .andDo (print ())
80+ .andExpect (status ().isCreated ());
81+
82+ // Leer el ID real desde el header Location
83+ String location = stepDefs .result .andReturn ().getResponse ().getHeader ("Location" );
84+ createdReportId = Long .valueOf (location .substring (location .lastIndexOf ("/" ) + 1 ));
85+ }
86+
87+ @ Then ("Report existsById should return true" )
88+ public void exists_by_id_should_return_true () {
89+ Assertions .assertNotNull (createdReportId );
90+ Assertions .assertTrue (reportRepository .existsById (createdReportId ));
91+ }
92+ }
0 commit comments