Skip to content

Commit d5d5f33

Browse files
singhabhi1999GitHub Enterprise
authored andcommitted
Merge pull request #4 from refapps/add-test
Add test
2 parents ff7516d + d7a968a commit d5d5f33

File tree

5 files changed

+171
-84
lines changed

5 files changed

+171
-84
lines changed

.DS_Store

0 Bytes
Binary file not shown.

package-lock.json

Lines changed: 0 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srv/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>spring-boot-starter-test</artifactId>
5555
<scope>test</scope>
5656
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.security</groupId>
59+
<artifactId>spring-security-test</artifactId>
60+
<scope>test</scope>
61+
</dependency>
5762
</dependencies>
5863

5964
<build>
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
package com.sap.cap.incident_management.handler;
22

3-
import java.util.List;
4-
import java.util.Locale;
5-
import java.util.logging.Level;
6-
import com.sap.cds.services.*;
7-
import org.slf4j.Logger;
8-
import org.slf4j.LoggerFactory;
9-
import org.springframework.stereotype.Component;
3+
import cds.gen.processorservice.Incidents;
4+
import cds.gen.processorservice.ProcessorService_;
5+
import cds.gen.sap.capire.incidents.*;
106

7+
import com.sap.cds.ql.Select;
8+
import com.sap.cds.services.ErrorStatuses;
9+
import com.sap.cds.services.ServiceException;
1110
import com.sap.cds.services.cds.CqnService;
1211
import com.sap.cds.services.handler.EventHandler;
13-
import com.sap.cds.services.handler.annotations.*;
12+
import com.sap.cds.services.handler.annotations.Before;
1413
import com.sap.cds.services.handler.annotations.ServiceName;
14+
import com.sap.cds.services.persistence.PersistenceService;
15+
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.stereotype.Component;
20+
21+
import java.util.List;
22+
import java.util.Locale;
1523

16-
import cds.gen.processorservice.Incidents;
17-
import cds.gen.processorservice.Urgency;
18-
import cds.gen.processorservice.ProcessorService_;
19-
import cds.gen.processorservice.Incidents_;
2024

2125
@Component
2226
@ServiceName(ProcessorService_.CDS_NAME)
2327
public class ProcessorServiceHandler implements EventHandler {
2428

29+
@Autowired
30+
private PersistenceService db;
2531
/*
2632
* Change the urgency of an incident to "high" if the title contains the word "urgent"
2733
*/
2834
private static final Logger logger = LoggerFactory.getLogger(ProcessorServiceHandler.class);
2935
@Before(event = CqnService.EVENT_CREATE)
3036
public void ensureHighUrgencyForIncidentsWithUrgentInTitle(List<Incidents> incidents) {
31-
for (Incidents incident : incidents) {
32-
System.out.println(incident.getTitle());
37+
for (Incidents incident : incidents) {
3338
if (incident.getTitle().toLowerCase(Locale.ENGLISH).contains("urgent") &&
3439
incident.getUrgencyCode() == null || !incident.getUrgencyCode().equals("H")) {
3540
incident.setUrgencyCode("H");
@@ -43,15 +48,13 @@ public void ensureHighUrgencyForIncidentsWithUrgentInTitle(List<Incidents> incid
4348
*/
4449
@Before(event = CqnService.EVENT_UPDATE)
4550
public void onUpdate(Incidents incident) {
46-
47-
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")){
4853
throw new ServiceException(ErrorStatuses.CONFLICT, "Can't modify a closed incident");
4954
}
50-
55+
5156
}
5257

5358

54-
55-
5659

5760
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)