Skip to content

Commit c3a9383

Browse files
committed
added tests
1 parent 580b83d commit c3a9383

File tree

2 files changed

+98
-10
lines changed

2 files changed

+98
-10
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,39 @@
22

33
import cds.gen.processorservice.Incidents;
44
import cds.gen.processorservice.ProcessorService_;
5+
import cds.gen.sap.capire.incidents.*;
6+
7+
import com.sap.cds.ql.Select;
58
import com.sap.cds.services.ErrorStatuses;
69
import com.sap.cds.services.ServiceException;
710
import com.sap.cds.services.cds.CqnService;
811
import com.sap.cds.services.handler.EventHandler;
912
import com.sap.cds.services.handler.annotations.Before;
1013
import com.sap.cds.services.handler.annotations.ServiceName;
14+
import com.sap.cds.services.persistence.PersistenceService;
15+
1116
import org.slf4j.Logger;
1217
import org.slf4j.LoggerFactory;
18+
import org.springframework.beans.factory.annotation.Autowired;
1319
import org.springframework.stereotype.Component;
1420

21+
import java.util.List;
1522
import java.util.Locale;
1623

24+
1725
@Component
1826
@ServiceName(ProcessorService_.CDS_NAME)
1927
public class ProcessorServiceHandler implements EventHandler {
2028

29+
@Autowired
30+
private PersistenceService db;
2131
/*
2232
* Change the urgency of an incident to "high" if the title contains the word "urgent"
2333
*/
2434
private static final Logger logger = LoggerFactory.getLogger(ProcessorServiceHandler.class);
2535
@Before(event = CqnService.EVENT_CREATE)
2636
public void ensureHighUrgencyForIncidentsWithUrgentInTitle(List<Incidents> incidents) {
27-
for (Incidents incident : incidents) {
28-
System.out.println(incident.getTitle());
37+
for (Incidents incident : incidents) {
2938
if (incident.getTitle().toLowerCase(Locale.ENGLISH).contains("urgent") &&
3039
incident.getUrgencyCode() == null || !incident.getUrgencyCode().equals("H")) {
3140
incident.setUrgencyCode("H");
@@ -39,11 +48,11 @@ public void ensureHighUrgencyForIncidentsWithUrgentInTitle(List<Incidents> incid
3948
*/
4049
@Before(event = CqnService.EVENT_UPDATE)
4150
public void onUpdate(Incidents incident) {
42-
43-
if(incident.getStatusCode().equals("C")){
51+
Incidents in = db.run(Select.from((Class<Incidents_>) Incidents_.class).where(i -> i.ID().eq(incident.getId()))).single(Incidents.class);
52+
if(in.getStatusCode().equals("C")){
4453
throw new ServiceException(ErrorStatuses.CONFLICT, "Can't modify a closed incident");
4554
}
46-
55+
4756
}
4857

4958

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

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import org.springframework.boot.test.context.SpringBootTest;
77
import org.springframework.security.test.context.support.WithMockUser;
88
import org.springframework.test.web.servlet.MockMvc;
9-
10-
import static org.hamcrest.Matchers.containsString;
9+
import org.springframework.test.web.servlet.MvcResult;
10+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
1111
import static org.hamcrest.Matchers.hasSize;
12+
import com.jayway.jsonpath.JsonPath;
1213
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1314
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
1415
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -18,17 +19,95 @@
1819
class IncidentsODataTests {
1920

2021
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";
2124

2225
@Autowired
2326
private MockMvc mockMvc;
2427

2528
@Test
2629
@WithMockUser(username = "alice")
2730
void incidentReturned() throws Exception {
28-
mockMvc.perform(get(incidentsURI + "?$top=1"))
31+
mockMvc.perform(get(incidentsURI))
2932
.andExpect(status().isOk())
30-
.andExpect(jsonPath("$.value", hasSize(4)))
31-
.andExpect(jsonPath("$.value[0].title").value(containsString("Solar panel broken")));
33+
.andExpect(jsonPath("$.value", hasSize(4)));
34+
35+
}
36+
@Test
37+
@WithMockUser(username = "alice")
38+
void customertReturned() throws Exception {
39+
mockMvc.perform(get(customerURI))
40+
.andExpect(status().isOk())
41+
.andExpect(jsonPath("$.value", hasSize(3)));
42+
43+
}
44+
45+
@Test
46+
@WithMockUser(username = "alice")
47+
void expandEntityEndpoint() throws Exception {
48+
mockMvc.perform(get(expandEntityURI))
49+
.andExpect(jsonPath("$.value[0].incidents[0]").isMap())
50+
.andExpect(jsonPath("$.value[0].incidents[0]").isNotEmpty());
51+
52+
}
53+
@Test
54+
@WithMockUser(username = "alice")
55+
void createIncident() throws Exception {
56+
String incidentJson = "{ \"title\": \"Urgent attention required!\", \"status_code\": \"N\", \"urgency_code\": \"M\", \"IsActiveEntity\": true }";
57+
58+
mockMvc.perform(MockMvcRequestBuilders.post("/odata/v4/ProcessorService/Incidents")
59+
.content(incidentJson)
60+
.contentType("application/json")
61+
.accept("application/json"))
62+
.andExpect(status().isCreated())
63+
.andExpect(jsonPath("$").isMap())
64+
.andExpect(jsonPath("$.title").value("Urgent attention required!"))
65+
.andExpect(jsonPath("$.status_code").value("N"))
66+
.andExpect(jsonPath("$.urgency_code").value("H"));
67+
}
68+
69+
@Test
70+
@WithMockUser(username = "alice")
71+
void updateIncident() throws Exception {
72+
String incidentCreateJson = "{ \"title\": \"Urgent attention required!\", \"status_code\": \"N\", \"IsActiveEntity\": true }";
73+
String incidentUpdateJson = "{\"status_code\": \"C\"}";
74+
String closedIncidentUpdateJson = "{\"status_code\": \"I\"}";
75+
76+
MvcResult createResult= mockMvc.perform(MockMvcRequestBuilders.post("/odata/v4/ProcessorService/Incidents")
77+
.content(incidentCreateJson)
78+
.contentType("application/json")
79+
.accept("application/json"))
80+
.andExpect(status().isCreated())
81+
.andExpect(jsonPath("$.title").value("Urgent attention required!"))
82+
.andExpect(jsonPath("$.status_code").value("N"))
83+
.andReturn();
84+
85+
String createResponseContent = createResult.getResponse().getContentAsString();
86+
String ID = JsonPath.read(createResponseContent, "$.ID");
87+
System.out.println("Incident ID : " + ID);
88+
89+
MvcResult updateResult= mockMvc.perform(MockMvcRequestBuilders.patch("/odata/v4/ProcessorService/Incidents(ID="+ID+",IsActiveEntity=true)")
90+
.content(incidentUpdateJson)
91+
.contentType("application/json")
92+
.accept("application/json"))
93+
.andExpect(status().isOk())
94+
.andExpect(jsonPath("$.title").value("Urgent attention required!"))
95+
.andExpect(jsonPath("$.status_code").value("C"))
96+
.andReturn();
97+
98+
String updateResponseContent = updateResult.getResponse().getContentAsString();
99+
String statusCode = JsonPath.read(updateResponseContent, "$.status_code");
100+
System.out.println("status code : " + statusCode);
101+
102+
103+
mockMvc.perform(MockMvcRequestBuilders.patch("/odata/v4/ProcessorService/Incidents(ID="+ID+",IsActiveEntity=true)")
104+
.content(closedIncidentUpdateJson)
105+
.contentType("application/json")
106+
.accept("application/json"))
107+
.andExpect(status().isConflict())
108+
.andExpect(jsonPath("$.error.message").value("Can't modify a closed incident"));
109+
110+
32111

33112
}
34113

0 commit comments

Comments
 (0)