Skip to content

Commit

Permalink
Merge pull request #119 from alexandre-touret/fix/get_owner_pets_impl…
Browse files Browse the repository at this point in the history
…ementation

add getOwnersPet implementation
  • Loading branch information
arey committed Jul 14, 2023
2 parents 065bba9 + 6011644 commit adfe18b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import org.springframework.web.util.UriComponentsBuilder;

import jakarta.transaction.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
* @author Vitaliy Fedoriv
Expand Down Expand Up @@ -160,4 +162,20 @@ public ResponseEntity<VisitDto> addVisitToOwner(Integer ownerId, Integer petId,
return new ResponseEntity<>(visitDto, headers, HttpStatus.CREATED);
}


@PreAuthorize("hasRole(@roles.OWNER_ADMIN)")
@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 {
return new ResponseEntity<>(petMapper.toPetDto(pet), HttpStatus.OK);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.samples.petclinic.mapper.OwnerMapper;
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 @@ -68,6 +70,9 @@ class OwnerRestControllerTests {
@Autowired
private OwnerMapper ownerMapper;

@Autowired
private PetMapper petMapper;

@Autowired
private VisitMapper visitMapper;

Expand Down Expand Up @@ -377,7 +382,7 @@ void testCreatePetError() throws Exception {
}

@Test
@WithMockUser(roles="OWNER_ADMIN")
@WithMockUser(roles = "OWNER_ADMIN")
void testCreateVisitSuccess() throws Exception {
VisitDto newVisit = visits.get(0);
newVisit.setId(999);
Expand All @@ -391,4 +396,32 @@ void testCreateVisitSuccess() throws Exception {
.andExpect(status().isCreated());
}

@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));
pet.setOwner(owner);
given(this.clinicService.findPetById(1)).willReturn(pet);
this.mockMvc.perform(get("/api/owners/2/pets/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"));
}

@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testGetOwnersPetsNotFound() 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());
}


}

0 comments on commit adfe18b

Please sign in to comment.