Skip to content

Commit

Permalink
Fix #145 404 error for both: owner and pet not found
Browse files Browse the repository at this point in the history
  • Loading branch information
arey committed Sep 15, 2024
1 parent 29a3295 commit fcb05f1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,12 @@ public ResponseEntity<VisitDto> addVisitToOwner(Integer ownerId, Integer petId,
@Override
public ResponseEntity<PetDto> getOwnersPet(Integer ownerId, Integer petId) {
Owner owner = this.clinicService.findOwnerById(ownerId);
if (owner == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
Optional<Pet> pet = owner.getPets().stream().filter(p -> p.getId().equals(petId)).findFirst();
if (pet.isPresent()) {
return new ResponseEntity<>(petMapper.toPetDto(pet.get()), HttpStatus.OK);
if (owner != null) {
Optional<Pet> pet = owner.getPets().stream().filter(p -> p.getId().equals(petId)).findFirst();
if (pet.isPresent()) {
return new ResponseEntity<>(petMapper.toPetDto(pet.get()), HttpStatus.OK);
}
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ 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")
Expand All @@ -421,12 +421,12 @@ void testGetOwnersPetsNotFound() throws Exception {

@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testGetOwnersPetsBadRequest() throws Exception {
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().isBadRequest());
.andExpect(status().isNotFound());
}


Expand Down

0 comments on commit fcb05f1

Please sign in to comment.