From fb1a9b21fd9fc57b6737dfd61ce7eac721918533 Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Sun, 31 Dec 2023 12:02:19 +0100 Subject: [PATCH] Remove the readOnly property to PetType #125 --- .../petclinic/mapper/PetTypeMapper.java | 1 + .../rest/controller/OwnerRestController.java | 3 --- .../rest/controller/PetTypeRestController.java | 16 ++++++---------- .../petclinic/service/ClinicService.java | 2 -- .../petclinic/service/ClinicServiceImpl.java | 13 ------------- src/main/resources/openapi.yml | 3 +-- .../controller/PetTypeRestControllerTests.java | 18 +++--------------- .../AbstractClinicServiceTests.java | 7 ------- 8 files changed, 11 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/PetTypeMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/PetTypeMapper.java index 486e5cbaf..05617161a 100644 --- a/src/main/java/org/springframework/samples/petclinic/mapper/PetTypeMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/PetTypeMapper.java @@ -19,6 +19,7 @@ public interface PetTypeMapper { PetType toPetType(PetTypeFieldsDto petTypeFieldsDto); PetTypeDto toPetTypeDto(PetType petType); + PetTypeFieldsDto toPetTypeFieldsDto(PetType petType); List toPetTypeDtos(Collection petTypes); } diff --git a/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java index cb2d0bad0..2b0107335 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java @@ -24,7 +24,6 @@ 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.model.PetType; import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.rest.api.OwnersApi; import org.springframework.samples.petclinic.rest.dto.*; @@ -140,8 +139,6 @@ public ResponseEntity addPetToOwner(Integer ownerId, PetFieldsDto petFie Owner owner = new Owner(); owner.setId(ownerId); pet.setOwner(owner); - PetType petType = this.clinicService.findPetTypeByName(pet.getType().getName()); - pet.setType(petType); this.clinicService.savePet(pet); PetDto petDto = petMapper.toPetDto(pet); headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pets/{id}") diff --git a/src/main/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestController.java index 870cd6618..3d0f42338 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestController.java @@ -23,6 +23,7 @@ import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.rest.api.PettypesApi; import org.springframework.samples.petclinic.rest.dto.PetTypeDto; +import org.springframework.samples.petclinic.rest.dto.PetTypeFieldsDto; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; @@ -32,7 +33,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Objects; @RestController @CrossOrigin(exposedHeaders = "errors, content-type") @@ -70,16 +70,12 @@ public ResponseEntity getPetType(Integer petTypeId) { @PreAuthorize("hasRole(@roles.VET_ADMIN)") @Override - public ResponseEntity addPetType(PetTypeDto petTypeDto) { + public ResponseEntity addPetType(PetTypeFieldsDto petTypeFieldsDto) { HttpHeaders headers = new HttpHeaders(); - 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); - } + final PetType type = petTypeMapper.toPetType(petTypeFieldsDto); + 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)") diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java index 6b01f1a92..32d0975eb 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java @@ -67,6 +67,4 @@ public interface ClinicService { void deleteSpecialty(Specialty specialty) throws DataAccessException; List findSpecialtiesByNameIn(Set names) throws DataAccessException; - - PetType findPetTypeByName(String name) throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java index 001fb4aed..e1f456d8b 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -296,17 +296,4 @@ public List findSpecialtiesByNameIn(Set names){ } return specialties; } - - @Override - @Transactional(readOnly = true) - public PetType findPetTypeByName(String name){ - PetType petType; - try { - petType = petTypeRepository.findByName(name); - } catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) { - // just ignore not found exceptions for Jdbc/Jpa realization - return null; - } - return petType; - } } diff --git a/src/main/resources/openapi.yml b/src/main/resources/openapi.yml index c49fe4968..162ef2754 100755 --- a/src/main/resources/openapi.yml +++ b/src/main/resources/openapi.yml @@ -557,7 +557,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/PetType' + $ref: '#/components/schemas/PetTypeFields' required: true responses: 200: @@ -2170,7 +2170,6 @@ components: format: int32 minimum: 0 example: 1 - readOnly: true required: - id User: diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestControllerTests.java index 72cb078e8..2e21d73ae 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestControllerTests.java @@ -26,8 +26,6 @@ import org.springframework.samples.petclinic.mapper.PetTypeMapper; 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; @@ -72,7 +70,7 @@ void initPetTypes(){ this.mockMvc = MockMvcBuilders.standaloneSetup(petTypeRestController) .setControllerAdvice(new ExceptionControllerAdvice()) .build(); - petTypes = new ArrayList(); + petTypes = new ArrayList<>(); PetType petType = new PetType(); petType.setId(1); @@ -176,7 +174,7 @@ void testCreatePetTypeSuccess() throws Exception { PetType newPetType = petTypes.get(0); newPetType.setId(null); ObjectMapper mapper = new ObjectMapper(); - String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeDto(newPetType)); + String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeFieldsDto(newPetType)); this.mockMvc.perform(post("/api/pettypes/") .content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isCreated()); @@ -194,17 +192,7 @@ 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 { diff --git a/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java index 55a4b4231..aff2ec156 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java @@ -499,11 +499,4 @@ void shouldFindSpecialtiesByNameIn() { && actual.getId().equals(expected.getId()))).isTrue(); } } - - @Test - @Transactional - void shouldFindPetTypeByName(){ - PetType petType = this.clinicService.findPetTypeByName("cat"); - assertThat(petType.getId()).isEqualTo(1); - } }