Skip to content

Commit c234950

Browse files
singhabhi1999GitHub Enterprise
authored andcommitted
Merge pull request #5 from refapps/add-test
updated tests and added logs
2 parents d5d5f33 + 9c8508e commit c234950

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

srv/src/main/java/com/sap/cap/incident_management/handler/ProcessorServiceHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class ProcessorServiceHandler implements EventHandler {
3232
* Change the urgency of an incident to "high" if the title contains the word "urgent"
3333
*/
3434
private static final Logger logger = LoggerFactory.getLogger(ProcessorServiceHandler.class);
35+
3536
@Before(event = CqnService.EVENT_CREATE)
3637
public void ensureHighUrgencyForIncidentsWithUrgentInTitle(List<Incidents> incidents) {
3738
for (Incidents incident : incidents) {
@@ -50,6 +51,7 @@ public void ensureHighUrgencyForIncidentsWithUrgentInTitle(List<Incidents> incid
5051
public void onUpdate(Incidents incident) {
5152
Incidents in = db.run(Select.from((Class<Incidents_>) Incidents_.class).where(i -> i.ID().eq(incident.getId()))).single(Incidents.class);
5253
if(in.getStatusCode().equals("C")){
54+
logger.error("Incident"+ incident.getId() +"has already been closed");
5355
throw new ServiceException(ErrorStatuses.CONFLICT, "Can't modify a closed incident");
5456
}
5557

srv/src/test/java/com/sap/cap/incident_management/IncidentsODataTests.java

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class IncidentsODataTests {
3131
*/
3232
@Test
3333
@WithMockUser(username = "alice")
34-
void incidentReturned() throws Exception {
34+
void incidentReturned(@Autowired MockMvc mockMvc) throws Exception {
3535
mockMvc.perform(get(incidentsURI))
3636
.andExpect(status().isOk())
3737
.andExpect(jsonPath("$.value", hasSize(4)));
@@ -65,30 +65,61 @@ void expandEntityEndpoint() throws Exception {
6565

6666
}
6767

68+
6869
/**
69-
* Test to create an Incident.
70-
* Test custom handler ensuring High Urgency For Incidents With "Urgent" in Title
71-
* @throws Exception
70+
* Test for creating an Draft Incident
71+
* Activating the draft Incident and check Urgency code as H using custom logic
72+
* Delete the Incident
7273
*/
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 }";
7774

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-
}
75+
@Test
76+
@WithMockUser(username = "alice")
77+
void draftIncident() throws Exception {
78+
String incidentCreateJson = "{ \"title\": \"Urgent attention required!\", \"status_code\": \"N\",\"urgency_code\": \"M\"}";
79+
80+
/**
81+
* Create a draft Incident
82+
*/
83+
84+
MvcResult createResult= mockMvc.perform(MockMvcRequestBuilders.post("/odata/v4/ProcessorService/Incidents")
85+
.content(incidentCreateJson)
86+
.contentType("application/json")
87+
.accept("application/json"))
88+
.andExpect(status().isCreated())
89+
.andExpect(jsonPath("$.title").value("Urgent attention required!"))
90+
.andExpect(jsonPath("$.status_code").value("N"))
91+
.andExpect(jsonPath("$.urgency_code").value("M"))
92+
.andReturn();
93+
94+
String createResponseContent = createResult.getResponse().getContentAsString();
95+
String ID = JsonPath.read(createResponseContent, "$.ID");
96+
System.out.println("Incident ID : " + ID);
97+
98+
/**
99+
* Activating the draft Incident
100+
*/
101+
102+
mockMvc.perform(MockMvcRequestBuilders.post("/odata/v4/ProcessorService/Incidents(ID="+ID+",IsActiveEntity=false)/ProcessorService.draftActivate")
103+
.contentType("application/json")
104+
.accept("application/json"))
105+
.andExpect(status().isOk())
106+
.andExpect(jsonPath("$").isMap())
107+
.andExpect(jsonPath("$.title").value("Urgent attention required!"))
108+
.andExpect(jsonPath("$.status_code").value("N"))
109+
.andExpect(jsonPath("$.urgency_code").value("H"));
110+
111+
/**
112+
* Deleting an Incident
113+
*/
114+
115+
mockMvc.perform(MockMvcRequestBuilders.delete("/odata/v4/ProcessorService/Incidents(ID="+ID+",IsActiveEntity=true)"))
116+
.andExpect(status().is(204));
117+
}
118+
88119

89120
/**
90-
* Test for creating an Incident
91-
* Test for closing the Incident
121+
* Test for creating an Active Incident
122+
* Test for closing the Active Incident
92123
* Test for custom handler ensuing prevent users from modifying a closed Incident
93124
*/
94125

0 commit comments

Comments
 (0)