Skip to content

Commit

Permalink
Merge pull request #118 from alexandre-touret/fix/create_pet_endpoint
Browse files Browse the repository at this point in the history
Add an addPet (POST /pets endpoint) implementation
  • Loading branch information
arey authored Jul 11, 2023
2 parents fdad10e + 50b69fc commit 065bba9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import org.springframework.samples.petclinic.rest.dto.PetDto;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -84,7 +85,6 @@ public ResponseEntity<PetDto> updatePet(Integer petId, PetDto petDto) {
}

@PreAuthorize("hasRole(@roles.OWNER_ADMIN)")
@Transactional
@Override
public ResponseEntity<PetDto> deletePet(Integer petId) {
Pet pet = this.clinicService.findPetById(petId);
Expand All @@ -95,5 +95,10 @@ public ResponseEntity<PetDto> deletePet(Integer petId) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}


@PreAuthorize("hasRole(@roles.OWNER_ADMIN)")
@Override
public ResponseEntity<PetDto> addPet(PetDto petDto) {
this.clinicService.savePet(petMapper.toPet(petDto));
return new ResponseEntity<>(petDto, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ void initPets() {

OwnerDto owner = new OwnerDto();
owner.id(1).firstName("Eduardo")
.lastName("Rodriquez")
.address("2693 Commerce St.")
.city("McFarland")
.telephone("6085558763");
.lastName("Rodriquez")
.address("2693 Commerce St.")
.city("McFarland")
.telephone("6085558763");

PetTypeDto petType = new PetTypeDto();
petType.id(2)
.name("dog");
.name("dog");

PetDto pet = new PetDto();
pets.add(pet.id(3)
Expand All @@ -108,7 +108,7 @@ void initPets() {
void testGetPetSuccess() throws Exception {
given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0)));
this.mockMvc.perform(get("/api/pets/3")
.accept(MediaType.APPLICATION_JSON_VALUE))
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.id").value(3))
Expand All @@ -120,7 +120,7 @@ void testGetPetSuccess() throws Exception {
void testGetPetNotFound() throws Exception {
given(petMapper.toPetDto(this.clinicService.findPetById(-1))).willReturn(null);
this.mockMvc.perform(get("/api/pets/999")
.accept(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}

Expand All @@ -132,7 +132,7 @@ void testGetAllPetsSuccess() throws Exception {
when(this.clinicService.findAllPets()).thenReturn(pets);
//given(this.clinicService.findAllPets()).willReturn(petMapper.toPets(pets));
this.mockMvc.perform(get("/api/pets/")
.accept(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.[0].id").value(3))
Expand All @@ -147,7 +147,7 @@ void testGetAllPetsNotFound() throws Exception {
pets.clear();
given(this.clinicService.findAllPets()).willReturn(petMapper.toPets(pets));
this.mockMvc.perform(get("/api/pets/")
.accept(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}

Expand All @@ -164,12 +164,12 @@ void testUpdatePetSuccess() throws Exception {

String newPetAsJSON = mapper.writeValueAsString(newPet);
this.mockMvc.perform(put("/api/pets/3")
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(content().contentType("application/json"))
.andExpect(status().isNoContent());

this.mockMvc.perform(get("/api/pets/3")
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE))
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.id").value(3))
Expand All @@ -189,7 +189,7 @@ void testUpdatePetError() throws Exception {
String newPetAsJSON = mapper.writeValueAsString(newPet);

this.mockMvc.perform(put("/api/pets/3")
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}

Expand All @@ -202,7 +202,7 @@ void testDeletePetSuccess() throws Exception {
String newPetAsJSON = mapper.writeValueAsString(newPet);
given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0)));
this.mockMvc.perform(delete("/api/pets/3")
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNoContent());
}

Expand All @@ -215,8 +215,33 @@ void testDeletePetError() throws Exception {
String newPetAsJSON = mapper.writeValueAsString(newPet);
given(this.clinicService.findPetById(999)).willReturn(null);
this.mockMvc.perform(delete("/api/pets/999")
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isNotFound());
}

@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testAddPetSuccess() throws Exception {
PetDto newPet = pets.get(0);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
String newPetAsJSON = mapper.writeValueAsString(newPet);
given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0)));
this.mockMvc.perform(post("/api/pets")
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk());
}

@Test
@WithMockUser(roles = "OWNER_ADMIN")
void testAddPetError() throws Exception {
PetDto newPet = pets.get(0);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
String newPetAsJSON = mapper.writeValueAsString(newPet);
given(this.clinicService.findPetById(999)).willReturn(null);
this.mockMvc.perform(post("/api/pets")
.content(new String()).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
}

0 comments on commit 065bba9

Please sign in to comment.