Skip to content

Commit

Permalink
Merge pull request #120 from alexandre-touret/fix/wrong_PetType_ID
Browse files Browse the repository at this point in the history
Fix "Create PetType endpoint returns wrong PetType ID"
  • Loading branch information
arey authored Aug 18, 2023
2 parents adfe18b + 8ecb3e7 commit 699abf2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.springframework.web.util.UriComponentsBuilder;

import jakarta.transaction.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin(exposedHeaders = "errors, content-type")
Expand Down Expand Up @@ -70,10 +72,14 @@ public ResponseEntity<PetTypeDto> getPetType(Integer petTypeId) {
@Override
public ResponseEntity<PetTypeDto> addPetType(PetTypeDto petTypeDto) {
HttpHeaders headers = new HttpHeaders();
final PetType type = petTypeMapper.toPetType(petTypeDto);
this.clinicService.savePetType(type);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pettypes/{id}").buildAndExpand(type.getId()).toUri());
return new ResponseEntity<>(petTypeMapper.toPetTypeDto(type), headers, HttpStatus.CREATED);
if (Objects.nonNull(petTypeDto.getId()) && !petTypeDto.getId().equals(0)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} else {
final PetType type = petTypeMapper.toPetType(petTypeDto);
this.clinicService.savePetType(type);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pettypes/{id}").buildAndExpand(type.getId()).toUri());
return new ResponseEntity<>(petTypeMapper.toPetTypeDto(type), headers, HttpStatus.CREATED);
}
}

@PreAuthorize("hasRole(@roles.VET_ADMIN)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.rest.advice.ExceptionControllerAdvice;
import org.springframework.samples.petclinic.rest.controller.PetTypeRestController;
import org.springframework.samples.petclinic.rest.dto.PetTypeDto;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.service.clinicService.ApplicationTestConfig;
import org.springframework.security.test.context.support.WithMockUser;
Expand Down Expand Up @@ -173,7 +174,7 @@ void testGetAllPetTypesNotFound() throws Exception {
@WithMockUser(roles="VET_ADMIN")
void testCreatePetTypeSuccess() throws Exception {
PetType newPetType = petTypes.get(0);
newPetType.setId(999);
newPetType.setId(null);
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeDto(newPetType));
this.mockMvc.perform(post("/api/pettypes/")
Expand All @@ -193,7 +194,17 @@ void testCreatePetTypeError() throws Exception {
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}

@Test
@WithMockUser(roles="VET_ADMIN")
void testCreatePetTypeErrorWithId() throws Exception {
PetType newPetType = petTypes.get(0);
newPetType.setId(1);
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeDto(newPetType));
this.mockMvc.perform(post("/api/pettypes/")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
@Test
@WithMockUser(roles="VET_ADMIN")
void testUpdatePetTypeSuccess() throws Exception {
Expand Down

0 comments on commit 699abf2

Please sign in to comment.