1+ package com .sap .cap .incident_management ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
6+ import org .springframework .boot .test .context .SpringBootTest ;
7+ import org .springframework .security .test .context .support .WithMockUser ;
8+ import org .springframework .test .web .servlet .MockMvc ;
9+ import org .springframework .test .web .servlet .MvcResult ;
10+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
11+ import static org .hamcrest .Matchers .hasSize ;
12+ import com .jayway .jsonpath .JsonPath ;
13+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
14+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
15+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
16+
17+ @ SpringBootTest
18+ @ AutoConfigureMockMvc
19+ class IncidentsODataTests {
20+
21+ private static final String incidentsURI = "/odata/v4/ProcessorService/Incidents" ;
22+ private static final String customerURI = "/odata/v4/ProcessorService/Customers" ;
23+ private static final String expandEntityURI = "/odata/v4/ProcessorService/Customers?$select=firstName&$expand=incidents" ;
24+
25+ @ Autowired
26+ private MockMvc mockMvc ;
27+
28+ /**
29+ * Test GET Api for Incidents
30+ * @throws Exception
31+ */
32+ @ Test
33+ @ WithMockUser (username = "alice" )
34+ void incidentReturned () throws Exception {
35+ mockMvc .perform (get (incidentsURI ))
36+ .andExpect (status ().isOk ())
37+ .andExpect (jsonPath ("$.value" , hasSize (4 )));
38+
39+ }
40+
41+ /**
42+ * Test GET Api for Customers
43+ * @throws Exception
44+ */
45+ @ Test
46+ @ WithMockUser (username = "alice" )
47+ void customertReturned () throws Exception {
48+ mockMvc .perform (get (customerURI ))
49+ .andExpect (status ().isOk ())
50+ .andExpect (jsonPath ("$.value" , hasSize (3 )));
51+
52+ }
53+
54+ /**
55+ * Test to ensure there is an Incident created by each Customer.
56+ * @throws Exception
57+ */
58+
59+ @ Test
60+ @ WithMockUser (username = "alice" )
61+ void expandEntityEndpoint () throws Exception {
62+ mockMvc .perform (get (expandEntityURI ))
63+ .andExpect (jsonPath ("$.value[0].incidents[0]" ).isMap ())
64+ .andExpect (jsonPath ("$.value[0].incidents[0]" ).isNotEmpty ());
65+
66+ }
67+
68+ /**
69+ * Test to create an Incident.
70+ * Test custom handler ensuring High Urgency For Incidents With "Urgent" in Title
71+ * @throws Exception
72+ */
73+ @ Test
74+ @ WithMockUser (username = "alice" )
75+ void createIncident () throws Exception {
76+ String incidentJson = "{ \" title\" : \" Urgent attention required!\" , \" status_code\" : \" N\" , \" urgency_code\" : \" M\" , \" IsActiveEntity\" : true }" ;
77+
78+ mockMvc .perform (MockMvcRequestBuilders .post ("/odata/v4/ProcessorService/Incidents" )
79+ .content (incidentJson )
80+ .contentType ("application/json" )
81+ .accept ("application/json" ))
82+ .andExpect (status ().isCreated ())
83+ .andExpect (jsonPath ("$" ).isMap ())
84+ .andExpect (jsonPath ("$.title" ).value ("Urgent attention required!" ))
85+ .andExpect (jsonPath ("$.status_code" ).value ("N" ))
86+ .andExpect (jsonPath ("$.urgency_code" ).value ("H" ));
87+ }
88+
89+ /**
90+ * Test for creating an Incident
91+ * Test for closing the Incident
92+ * Test for custom handler ensuing prevent users from modifying a closed Incident
93+ */
94+
95+ @ Test
96+ @ WithMockUser (username = "alice" )
97+ void updateIncident () throws Exception {
98+ String incidentCreateJson = "{ \" title\" : \" Urgent attention required!\" , \" status_code\" : \" N\" , \" IsActiveEntity\" : true }" ;
99+ String incidentUpdateJson = "{\" status_code\" : \" C\" }" ;
100+ String closedIncidentUpdateJson = "{\" status_code\" : \" I\" }" ;
101+
102+ MvcResult createResult = mockMvc .perform (MockMvcRequestBuilders .post ("/odata/v4/ProcessorService/Incidents" )
103+ .content (incidentCreateJson )
104+ .contentType ("application/json" )
105+ .accept ("application/json" ))
106+ .andExpect (status ().isCreated ())
107+ .andExpect (jsonPath ("$.title" ).value ("Urgent attention required!" ))
108+ .andExpect (jsonPath ("$.status_code" ).value ("N" ))
109+ .andReturn ();
110+
111+ String createResponseContent = createResult .getResponse ().getContentAsString ();
112+ String ID = JsonPath .read (createResponseContent , "$.ID" );
113+ System .out .println ("Incident ID : " + ID );
114+ /**
115+ * Closing an open Incident
116+ */
117+ MvcResult updateResult = mockMvc .perform (MockMvcRequestBuilders .patch ("/odata/v4/ProcessorService/Incidents(ID=" +ID +",IsActiveEntity=true)" )
118+ .content (incidentUpdateJson )
119+ .contentType ("application/json" )
120+ .accept ("application/json" ))
121+ .andExpect (status ().isOk ())
122+ .andExpect (jsonPath ("$.title" ).value ("Urgent attention required!" ))
123+ .andExpect (jsonPath ("$.status_code" ).value ("C" ))
124+ .andReturn ();
125+
126+ String updateResponseContent = updateResult .getResponse ().getContentAsString ();
127+ String statusCode = JsonPath .read (updateResponseContent , "$.status_code" );
128+ System .out .println ("status code : " + statusCode );
129+
130+ /**
131+ * Updating a Closed Incident will throw an error with error message "Can't modify a closed incident"
132+ */
133+ mockMvc .perform (MockMvcRequestBuilders .patch ("/odata/v4/ProcessorService/Incidents(ID=" +ID +",IsActiveEntity=true)" )
134+ .content (closedIncidentUpdateJson )
135+ .contentType ("application/json" )
136+ .accept ("application/json" ))
137+ .andExpect (status ().isConflict ())
138+ .andExpect (jsonPath ("$.error.message" ).value ("Can't modify a closed incident" ));
139+
140+
141+
142+ }
143+
144+ }
0 commit comments