Skip to content

Commit

Permalink
Fix #145 bad request on GET /api/owners/{id}/pets/{petId} (#150)
Browse files Browse the repository at this point in the history
* Fix #145 bad request on GET /api/owners/{id}/pets/{petId}

* Fix #145 404 error for both: owner and pet not found

* Fix #145 404 error for both: update the OAS description

* Fix #145 404 error for both: refactoring by introducing a Owner::getPet(Integer petId) method
  • Loading branch information
arey authored Sep 21, 2024
1 parent 9952e3f commit 072f200
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public Pet getPet(String name, boolean ignoreNew) {
return null;
}

public Pet getPet(Integer petId) {
return getPetsInternal().stream().filter(p -> p.getId().equals(petId)).findFirst().orElse(null);
}

@Override
public String toString() {
return new ToStringCreator(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,12 @@ public ResponseEntity<VisitDto> addVisitToOwner(Integer ownerId, Integer petId,
@Override
public ResponseEntity<PetDto> getOwnersPet(Integer ownerId, Integer petId) {
Owner owner = this.clinicService.findOwnerById(ownerId);
Pet pet = this.clinicService.findPetById(petId);
if (owner == null || pet == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} else {
if (!pet.getOwner().equals(owner)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} else {
if (owner != null) {
Pet pet = owner.getPet(petId);
if (pet != null) {
return new ResponseEntity<>(petMapper.toPetDto(pet), HttpStatus.OK);
}
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ paths:
schema:
$ref: '#/components/schemas/RestError'
404:
description: Pet not found.
description: Owner or pet not found.
content:
application/json:
schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
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 Down Expand Up @@ -399,9 +398,6 @@ void testCreateVisitSuccess() throws Exception {
@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testGetOwnerPetSuccess() throws Exception {
owners.remove(0);
owners.remove(1);
given(this.clinicService.findAllOwners()).willReturn(ownerMapper.toOwners(owners));
var owner = ownerMapper.toOwner(owners.get(0));
given(this.clinicService.findOwnerById(2)).willReturn(owner);
var pet = petMapper.toPet(pets.get(0));
Expand All @@ -415,13 +411,23 @@ void testGetOwnerPetSuccess() throws Exception {

@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testGetOwnersPetsNotFound() throws Exception {
void testGetOwnersPetsWithOwnerNotFound() throws Exception {
owners.clear();
given(this.clinicService.findAllOwners()).willReturn(ownerMapper.toOwners(owners));
this.mockMvc.perform(get("/api/owners/1/pets/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}

@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testGetOwnersPetsWithPetNotFound() throws Exception {
var owner1 = ownerMapper.toOwner(owners.get(0));
given(this.clinicService.findOwnerById(1)).willReturn(owner1);
this.mockMvc.perform(get("/api/owners/1/pets/2")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}


}

0 comments on commit 072f200

Please sign in to comment.