Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.springframework.samples.petclinic.rest.advice;

import jakarta.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
Expand All @@ -42,6 +45,9 @@
@ControllerAdvice
public class ExceptionControllerAdvice {

private static final Logger log =
LoggerFactory.getLogger(ExceptionControllerAdvice.class);

/**
* Private method for constructing the {@link ProblemDetail} object passing the name and details of the exception
* class.
Expand All @@ -53,9 +59,12 @@ public class ExceptionControllerAdvice {
private ProblemDetail detailBuild(Exception ex, HttpStatus status, StringBuffer url) {
ProblemDetail detail = ProblemDetail.forStatus(status);
detail.setType(URI.create(url.toString()));
detail.setTitle(ex.getClass().getSimpleName());
detail.setDetail(ex.getLocalizedMessage());
detail.setTitle(status.getReasonPhrase());
detail.setDetail("An unexpected error occurred.");
detail.setProperty("timestamp", Instant.now());

log.error("Request failed. status={}, url={}, ex={}", status, url, ex.getLocalizedMessage(), ex);

return detail;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public class OwnerRestController implements OwnersApi {
private final VisitMapper visitMapper;

public OwnerRestController(ClinicService clinicService,
OwnerMapper ownerMapper,
PetMapper petMapper,
VisitMapper visitMapper) {
OwnerMapper ownerMapper,
PetMapper petMapper,
VisitMapper visitMapper) {
this.clinicService = clinicService;
this.ownerMapper = ownerMapper;
this.petMapper = petMapper;
Expand Down Expand Up @@ -99,7 +99,7 @@ public ResponseEntity<OwnerDto> addOwner(OwnerFieldsDto ownerFieldsDto) {
this.clinicService.saveOwner(owner);
OwnerDto ownerDto = ownerMapper.toOwnerDto(owner);
headers.setLocation(UriComponentsBuilder.newInstance()
.path("/api/owners/{id}").buildAndExpand(owner.getId()).toUri());
.path("/api/owners/{id}").buildAndExpand(owner.getId()).toUri());
return new ResponseEntity<>(ownerDto, headers, HttpStatus.CREATED);
}

Expand Down Expand Up @@ -136,8 +136,12 @@ public ResponseEntity<OwnerDto> deleteOwner(Integer ownerId) {
public ResponseEntity<PetDto> addPetToOwner(Integer ownerId, PetFieldsDto petFieldsDto) {
HttpHeaders headers = new HttpHeaders();
Pet pet = petMapper.toPet(petFieldsDto);
Owner owner = new Owner();
owner.setId(ownerId);
Owner owner = clinicService.findOwnerById(ownerId);

if (owner == null){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

pet.setOwner(owner);
pet.getType().setName(null);
this.clinicService.savePet(pet);
Expand Down Expand Up @@ -175,11 +179,10 @@ public ResponseEntity<VisitDto> addVisitToOwner(Integer ownerId, Integer petId,
this.clinicService.saveVisit(visit);
VisitDto visitDto = visitMapper.toVisitDto(visit);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/visits/{id}")
.buildAndExpand(visit.getId()).toUri());
.buildAndExpand(visit.getId()).toUri());
return new ResponseEntity<>(visitDto, headers, HttpStatus.CREATED);
}


@PreAuthorize("hasRole(@roles.OWNER_ADMIN)")
@Override
public ResponseEntity<PetDto> getOwnersPet(Integer ownerId, Integer petId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.samples.petclinic.mapper.PetMapper;
import org.springframework.samples.petclinic.mapper.VisitMapper;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.rest.advice.ExceptionControllerAdvice;
import org.springframework.samples.petclinic.rest.dto.OwnerDto;
import org.springframework.samples.petclinic.rest.dto.PetDto;
Expand All @@ -47,7 +48,9 @@
import java.util.ArrayList;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.never;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

Expand Down Expand Up @@ -368,6 +371,26 @@ void testCreatePetError() throws Exception {
.andExpect(status().isBadRequest()).andDo(MockMvcResultHandlers.print());
}

@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testCreatePetOwnerNotFound() throws Exception {
PetDto newPet = pets.get(0);
newPet.setId(null);
ObjectMapper mapper = JsonMapper.builder()
.defaultDateFormat(new SimpleDateFormat("dd/MM/yyyy"))
.build();

String newPetAsJSON = mapper.writeValueAsString(newPet);
given(this.clinicService.findOwnerById(999)).willReturn(null);

this.mockMvc.perform(post("/api/owners/999/pets")
.content(newPetAsJSON)
.accept(MediaType.APPLICATION_JSON_VALUE)
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNotFound())
.andDo(MockMvcResultHandlers.print());
}

@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testCreateVisitSuccess() throws Exception {
Expand Down
Loading