|
6 | 6 | import org.springframework.boot.test.context.SpringBootTest; |
7 | 7 | import org.springframework.security.test.context.support.WithMockUser; |
8 | 8 | 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; |
11 | 11 | import static org.hamcrest.Matchers.hasSize; |
| 12 | +import com.jayway.jsonpath.JsonPath; |
12 | 13 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
13 | 14 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
14 | 15 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
18 | 19 | class IncidentsODataTests { |
19 | 20 |
|
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"; |
21 | 24 |
|
22 | 25 | @Autowired |
23 | 26 | private MockMvc mockMvc; |
24 | 27 |
|
25 | 28 | @Test |
26 | 29 | @WithMockUser(username = "alice") |
27 | 30 | void incidentReturned() throws Exception { |
28 | | - mockMvc.perform(get(incidentsURI + "?$top=1")) |
| 31 | + mockMvc.perform(get(incidentsURI)) |
29 | 32 | .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 | + |
32 | 111 |
|
33 | 112 | } |
34 | 113 |
|
|
0 commit comments