Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #145 bad request on GET /api/owners/{id}/pets/{petId} #150

Merged
merged 4 commits into from
Sep 21, 2024
Merged
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 @@ -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());
}


}
Loading