|
16 | 16 |
|
17 | 17 | package org.springframework.samples.petclinic.rest.controller; |
18 | 18 |
|
| 19 | +import org.springframework.dao.DataIntegrityViolationException; |
19 | 20 | import tools.jackson.databind.ObjectMapper; |
20 | 21 | import org.junit.jupiter.api.BeforeEach; |
21 | 22 | import org.junit.jupiter.api.Test; |
|
47 | 48 | import java.util.ArrayList; |
48 | 49 | import java.util.List; |
49 | 50 |
|
| 51 | +import static org.mockito.ArgumentMatchers.any; |
50 | 52 | import static org.mockito.BDDMockito.given; |
| 53 | +import static org.mockito.Mockito.doThrow; |
51 | 54 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
52 | 55 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
53 | 56 |
|
@@ -341,6 +344,8 @@ void testDeleteOwnerError() throws Exception { |
341 | 344 | @Test |
342 | 345 | @WithMockUser(roles = "OWNER_ADMIN") |
343 | 346 | void testCreatePetSuccess() throws Exception { |
| 347 | + final Owner owner = ownerMapper.toOwner(owners.get(0)); |
| 348 | + given(this.clinicService.findOwnerById(1)).willReturn(owner); |
344 | 349 | PetDto newPet = pets.get(0); |
345 | 350 | newPet.setId(999); |
346 | 351 | ObjectMapper mapper = JsonMapper.builder() |
@@ -368,6 +373,40 @@ void testCreatePetError() throws Exception { |
368 | 373 | .andExpect(status().isBadRequest()).andDo(MockMvcResultHandlers.print()); |
369 | 374 | } |
370 | 375 |
|
| 376 | + @Test |
| 377 | + @WithMockUser(roles = "OWNER_ADMIN") |
| 378 | + void testCreatePetShouldNotExposeTechnicalDetails() throws Exception { |
| 379 | + PetDto newPet = pets.get(0); |
| 380 | + newPet.setId(null); |
| 381 | + ObjectMapper mapper = JsonMapper.builder() |
| 382 | + .defaultDateFormat(new SimpleDateFormat("dd/MM/yyyy")) |
| 383 | + .build(); |
| 384 | + String newPetAsJSON = mapper.writeValueAsString(newPet); |
| 385 | + String technicalMessage = "could not execute statement; SQL [insert into pets ...]; constraint [fk_pet_owner]"; |
| 386 | + given(this.clinicService.findOwnerById(1)).willReturn(ownerMapper.toOwner(owners.get(0))); |
| 387 | + doThrow(new DataIntegrityViolationException(technicalMessage)).when(this.clinicService).savePet(any()); |
| 388 | + this.mockMvc.perform(post("/api/owners/1/pets") |
| 389 | + .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 390 | + .andDo(MockMvcResultHandlers.print()) |
| 391 | + .andExpect(status().isNotFound()) |
| 392 | + .andExpect(jsonPath("$.detail").value("Request could not be processed")); |
| 393 | + } |
| 394 | + |
| 395 | + @Test |
| 396 | + @WithMockUser(roles = "OWNER_ADMIN") |
| 397 | + void testCreatePetWithUnknownOwnerShouldReturnNotFound() throws Exception { |
| 398 | + PetDto newPet = pets.get(0); |
| 399 | + newPet.setId(null); |
| 400 | + ObjectMapper mapper = JsonMapper.builder() |
| 401 | + .defaultDateFormat(new SimpleDateFormat("dd/MM/yyyy")) |
| 402 | + .build(); |
| 403 | + String newPetAsJSON = mapper.writeValueAsString(newPet); |
| 404 | + given(this.clinicService.findOwnerById(1000000)).willReturn(null); |
| 405 | + this.mockMvc.perform(post("/api/owners/1000000/pets") |
| 406 | + .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 407 | + .andExpect(status().isNotFound()); |
| 408 | + } |
| 409 | + |
371 | 410 | @Test |
372 | 411 | @WithMockUser(roles = "OWNER_ADMIN") |
373 | 412 | void testCreateVisitSuccess() throws Exception { |
|
0 commit comments