Skip to content

Commit 85366a5

Browse files
Adding missing PUT method in OwnerRestController class With Integration Tests (#221)
* fix: #220 -- adding missing PUT method in OwnerRestController class With Integration Tests * fix: optimizing data base access and reusing PetMapper
1 parent 0bae442 commit 85366a5

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ public ResponseEntity<PetDto> addPetToOwner(Integer ownerId, PetFieldsDto petFie
147147
return new ResponseEntity<>(petDto, headers, HttpStatus.CREATED);
148148
}
149149

150+
@PreAuthorize("hasRole(@roles.OWNER_ADMIN)")
151+
@Override
152+
public ResponseEntity<Void> updateOwnersPet(Integer ownerId, Integer petId, PetFieldsDto petFieldsDto) {
153+
Owner currentOwner = this.clinicService.findOwnerById(ownerId);
154+
if (currentOwner != null) {
155+
Pet currentPet = this.clinicService.findPetById(petId);
156+
if (currentPet != null) {
157+
currentPet.setBirthDate(petFieldsDto.getBirthDate());
158+
currentPet.setName(petFieldsDto.getName());
159+
currentPet.setType(petMapper.toPetType(petFieldsDto.getType()));
160+
this.clinicService.savePet(currentPet);
161+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
162+
}
163+
}
164+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
165+
}
166+
150167
@PreAuthorize("hasRole(@roles.OWNER_ADMIN)")
151168
@Override
152169
public ResponseEntity<VisitDto> addVisitToOwner(Integer ownerId, Integer petId, VisitFieldsDto visitFieldsDto) {

src/test/java/org/springframework/samples/petclinic/rest/controller/OwnerRestControllerTests.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,66 @@ void testGetOwnersPetsWithPetNotFound() throws Exception {
430430
}
431431

432432

433+
@Test
434+
@WithMockUser(roles = "OWNER_ADMIN")
435+
void testUpdateOwnersPetSuccess() throws Exception {
436+
int ownerId = owners.get(0).getId();
437+
int petId = pets.get(0).getId();
438+
given(this.clinicService.findOwnerById(ownerId)).willReturn(ownerMapper.toOwner(owners.get(0)));
439+
given(this.clinicService.findPetById(petId)).willReturn(petMapper.toPet(pets.get(0)));
440+
PetDto updatedPetDto = pets.get(0);
441+
updatedPetDto.setName("Rex");
442+
updatedPetDto.setBirthDate(LocalDate.of(2020, 1, 15));
443+
ObjectMapper mapper = new ObjectMapper();
444+
mapper.registerModule(new JavaTimeModule());
445+
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
446+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
447+
String updatedPetAsJSON = mapper.writeValueAsString(updatedPetDto);
448+
this.mockMvc.perform(put("/api/owners/" + ownerId + "/pets/" + petId)
449+
.content(updatedPetAsJSON)
450+
.accept(MediaType.APPLICATION_JSON_VALUE)
451+
.contentType(MediaType.APPLICATION_JSON_VALUE))
452+
.andExpect(status().isNoContent());
453+
}
454+
455+
@Test
456+
@WithMockUser(roles = "OWNER_ADMIN")
457+
void testUpdateOwnersPetOwnerNotFound() throws Exception {
458+
int ownerId = 0;
459+
int petId = pets.get(0).getId();
460+
given(this.clinicService.findOwnerById(ownerId)).willReturn(null);
461+
PetDto petDto = pets.get(0);
462+
petDto.setName("Thor");
463+
ObjectMapper mapper = new ObjectMapper();
464+
mapper.registerModule(new JavaTimeModule());
465+
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
466+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
467+
String updatedPetAsJSON = mapper.writeValueAsString(petDto);
468+
this.mockMvc.perform(put("/api/owners/" + ownerId + "/pets/" + petId)
469+
.contentType(MediaType.APPLICATION_JSON)
470+
.content(updatedPetAsJSON))
471+
.andExpect(status().isNotFound());
472+
}
473+
474+
@Test
475+
@WithMockUser(roles = "OWNER_ADMIN")
476+
void testUpdateOwnersPetPetNotFound() throws Exception {
477+
int ownerId = owners.get(0).getId();
478+
int petId = 0;
479+
given(this.clinicService.findOwnerById(ownerId)).willReturn(ownerMapper.toOwner(owners.get(0)));
480+
given(this.clinicService.findPetById(petId)).willReturn(null);
481+
PetDto petDto = pets.get(0);
482+
petDto.setName("Ghost");
483+
petDto.setBirthDate(LocalDate.of(2020, 1, 1));
484+
ObjectMapper mapper = new ObjectMapper();
485+
mapper.registerModule(new JavaTimeModule());
486+
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
487+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
488+
String updatedPetAsJSON = mapper.writeValueAsString(petDto);
489+
this.mockMvc.perform(put("/api/owners/" + ownerId + "/pets/" + petId)
490+
.contentType(MediaType.APPLICATION_JSON)
491+
.content(updatedPetAsJSON))
492+
.andExpect(status().isNotFound());
493+
}
494+
433495
}

0 commit comments

Comments
 (0)