diff --git a/petclinic-ermodel.png b/petclinic-ermodel.png new file mode 100644 index 0000000000000000000000000000000000000000..6c790fb7c45f61ea9526fcf4aae5a6a0a46ec4f1 Binary files /dev/null and b/petclinic-ermodel.png differ diff --git a/pom.xml b/pom.xml index 41ee6a0f08ba3216a214cdcf08214b2146756028..ca330d719bdf85fa8d74e7b814fab383a42008d3 100644 --- a/pom.xml +++ b/pom.xml @@ -1,82 +1,82 @@ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <groupId>org.springframework.samples</groupId> - <artifactId>spring-petclinic-rest</artifactId> - <version>2.2.4</version> + <groupId>org.springframework.samples</groupId> + <artifactId>spring-petclinic-rest</artifactId> + <version>2.2.4</version> - <description>REST version of the Spring Petclinic sample application</description> + <description>REST version of the Spring Petclinic sample application</description> <url>https://spring-petclinic.github.io/</url> - <parent> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-parent</artifactId> - <version>2.2.4.RELEASE</version> - <relativePath/> <!-- lookup parent from Maven repository --> - </parent> + <parent> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-parent</artifactId> + <version>2.2.4.RELEASE</version> + <relativePath/> <!-- lookup parent from Maven repository --> + </parent> - <properties> + <properties> <spring-data-jdbc.version>1.2.1.RELEASE</spring-data-jdbc.version> - <springfox-swagger.version>2.6.1</springfox-swagger.version> - <jacoco.version>0.8.2</jacoco.version> + <springfox-swagger.version>2.9.2</springfox-swagger.version> + <jacoco.version>0.8.5</jacoco.version> <docker.jib-maven-plugin.version>1.3.0</docker.jib-maven-plugin.version> <docker.image.prefix>springcommunity</docker.image.prefix> - </properties> - - <!-- repository for springfox plugin --> - <repositories> - <repository> - <id>jcenter-snapshots</id> - <name>jcenter</name> - <url>https://jcenter.bintray.com/</url> - </repository> - </repositories> - - <dependencies> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-aop</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-cache</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-data-jpa</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-jdbc</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-web</artifactId> - </dependency> + </properties> + + <!-- repository for springfox plugin --> + <repositories> + <repository> + <id>jcenter-snapshots</id> + <name>jcenter</name> + <url>https://jcenter.bintray.com/</url> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-aop</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-cache</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-jdbc</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> - <dependency> - <groupId>org.hsqldb</groupId> - <artifactId>hsqldb</artifactId> - <scope>runtime</scope> - </dependency> - <dependency> - <groupId>mysql</groupId> - <artifactId>mysql-connector-java</artifactId> - <scope>runtime</scope> - </dependency> - <dependency> - <groupId>org.postgresql</groupId> - <artifactId>postgresql</artifactId> - <scope>runtime</scope> - </dependency> - - <dependency> + <dependency> + <groupId>org.hsqldb</groupId> + <artifactId>hsqldb</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + <scope>runtime</scope> + </dependency> + + <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jdbc-core</artifactId> <version>${spring-data-jdbc.version}</version> @@ -88,11 +88,11 @@ </exclusions> </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-test</artifactId> - <scope>test</scope> - </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> @@ -113,15 +113,15 @@ <scope>test</scope> </dependency> - <dependency> - <groupId>io.springfox</groupId> - <artifactId>springfox-swagger2</artifactId> - <version>${springfox-swagger.version}</version> - </dependency> - <dependency> - <groupId>io.springfox</groupId> - <artifactId>springfox-swagger-ui</artifactId> - <version>${springfox-swagger.version}</version> + <dependency> + <groupId>io.springfox</groupId> + <artifactId>springfox-swagger2</artifactId> + <version>${springfox-swagger.version}</version> + </dependency> + <dependency> + <groupId>io.springfox</groupId> + <artifactId>springfox-swagger-ui</artifactId> + <version>${springfox-swagger.version}</version> </dependency> <dependency> @@ -135,13 +135,13 @@ <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> - </dependencies> + </dependencies> - <build> - <plugins> - <plugin> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-maven-plugin</artifactId> + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <!-- Spring Boot Actuator displays build-related information @@ -159,7 +159,7 @@ </configuration> </execution> </executions> - </plugin> + </plugin> <plugin> <groupId>org.jacoco</groupId> @@ -171,6 +171,34 @@ <goal>prepare-agent</goal> </goals> </execution> + <execution> + <id>check</id> + <goals> + <goal>check</goal> + </goals> + <configuration> + <rules> + <rule> + <element>BUNDLE</element> + <limits> + <limit> + <counter>LINE</counter> + <value>COVEREDRATIO</value> + <minimum>0.87</minimum> + </limit> + <limit> + <counter>BRANCH</counter> + <value>COVEREDRATIO</value> + <minimum>0.74</minimum> + </limit> + </limits> + <excludes> + <!-- <exclude>com.xyz.ClassToExclude</exclude> --> + </excludes> + </rule> + </rules> + </configuration> + </execution> <execution> <id>report</id> <phase>prepare-package</phase> @@ -195,7 +223,7 @@ </to> </configuration> </plugin> - </plugins> - </build> + </plugins> + </build> </project> diff --git a/readme.md b/readme.md index 3a9243fe3d6e709d8284ce723c36979938cb6673..921034a64eb02a53f787b4fbd6db135f9f1bf7cd 100644 --- a/readme.md +++ b/readme.md @@ -7,6 +7,10 @@ The [spring-petclinic-angular project](https://github.com/spring-petclinic/sprin [See the presentation of the Spring Petclinic Framework version](http://fr.slideshare.net/AntoineRey/spring-framework-petclinic-sample-application) +### Petclinic ER Model + + + ## Running petclinic locally ### With maven command line diff --git a/src/main/java/org/springframework/samples/petclinic/rest/BindingErrorsResponse.java b/src/main/java/org/springframework/samples/petclinic/rest/BindingErrorsResponse.java index 73311e266f6065702402d0d904c297cf11f92809..a52ef8331e412ebeb51960dfe3e88ccf03d3cd39 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/BindingErrorsResponse.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/BindingErrorsResponse.java @@ -34,15 +34,35 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class BindingErrorsResponse { - private List<BindingError> bindingErrors = new ArrayList<BindingError>(); - - public List<BindingError> getBindingErrors() { - return bindingErrors; - } + public BindingErrorsResponse() { + this(null); + } + + public BindingErrorsResponse(Integer id) { + this(null, id); + } + + public BindingErrorsResponse(Integer pathId, Integer bodyId) { + boolean onlyBodyIdSpecified = pathId == null && bodyId != null; + if (onlyBodyIdSpecified) { + addBodyIdError(bodyId, "must not be specified"); + } + boolean bothIdsSpecified = pathId != null && bodyId != null; + if (bothIdsSpecified && !pathId.equals(bodyId)) { + addBodyIdError(bodyId, String.format("does not match pathId: %d", pathId)); + } + } + + private void addBodyIdError(Integer bodyId, String message) { + BindingError error = new BindingError(); + error.setObjectName("body"); + error.setFieldName("id"); + error.setFieldValue(bodyId.toString()); + error.setErrorMessage(message); + addError(error); + } - public void setBindingErrors(List<BindingError> bindingErrors) { - this.bindingErrors = bindingErrors; - } + private List<BindingError> bindingErrors = new ArrayList<BindingError>(); public void addError(BindingError bindingError) { this.bindingErrors.add(bindingError); @@ -90,34 +110,18 @@ public class BindingErrorsResponse { this.errorMessage = ""; } - protected String getObjectName() { - return objectName; - } - protected void setObjectName(String objectName) { this.objectName = objectName; } - protected String getFieldName() { - return fieldName; - } - protected void setFieldName(String fieldName) { this.fieldName = fieldName; } - protected String getFieldValue() { - return fieldValue; - } - protected void setFieldValue(String fieldValue) { this.fieldValue = fieldValue; } - protected String getErrorMessage() { - return errorMessage; - } - protected void setErrorMessage(String error_message) { this.errorMessage = error_message; } diff --git a/src/main/java/org/springframework/samples/petclinic/rest/JacksonCustomOwnerDeserializer.java b/src/main/java/org/springframework/samples/petclinic/rest/JacksonCustomOwnerDeserializer.java index b63dd422a278a37e3c6c2295b317645c87c7d278..d629b8b501135cc7aa7b92a7eebfe5f0949090a5 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/JacksonCustomOwnerDeserializer.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/JacksonCustomOwnerDeserializer.java @@ -32,7 +32,7 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer; */ public class JacksonCustomOwnerDeserializer extends StdDeserializer<Owner> { - + public JacksonCustomOwnerDeserializer(){ this(null); } @@ -45,15 +45,13 @@ public class JacksonCustomOwnerDeserializer extends StdDeserializer<Owner> { public Owner deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { JsonNode node = parser.getCodec().readTree(parser); Owner owner = new Owner(); - int id = node.get("id").asInt(); String firstName = node.get("firstName").asText(null); String lastName = node.get("lastName").asText(null); String address = node.get("address").asText(null); String city = node.get("city").asText(null); String telephone = node.get("telephone").asText(null); - - if (!(id == 0)) { - owner.setId(id); + if (node.hasNonNull("id")) { + owner.setId(node.get("id").asInt()); } owner.setFirstName(firstName); owner.setLastName(lastName); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/JacksonCustomOwnerSerializer.java b/src/main/java/org/springframework/samples/petclinic/rest/JacksonCustomOwnerSerializer.java index b106063d50be12af83d9ddc801cceb085b260554..594d82a06a9227ca8b2a87ed2deb932902320e1d 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/JacksonCustomOwnerSerializer.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/JacksonCustomOwnerSerializer.java @@ -77,7 +77,11 @@ public class JacksonCustomOwnerSerializer extends StdSerializer<Owner> { jgen.writeStringField("name", petType.getName()); jgen.writeEndObject(); // type - jgen.writeNumberField("owner", pet.getOwner().getId()); + if (pet.getOwner().getId() == null) { + jgen.writeNullField("owner"); + } else { + jgen.writeNumberField("owner", pet.getOwner().getId()); + } // write visits array jgen.writeArrayFieldStart("visits"); for (Visit visit : pet.getVisits()) { diff --git a/src/main/java/org/springframework/samples/petclinic/rest/OwnerRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/OwnerRestController.java index ff60a276910b999c0af1daf39454b113c25e4c64..4d9cb8788e4bb2e4b87a04e417c76be69dbace41 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/OwnerRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/OwnerRestController.java @@ -52,7 +52,7 @@ public class OwnerRestController { private ClinicService clinicService; @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/*/lastname/{lastName}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/*/lastname/{lastName}", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Collection<Owner>> getOwnersList(@PathVariable("lastName") String ownerLastName) { if (ownerLastName == null) { ownerLastName = ""; @@ -65,7 +65,7 @@ public class OwnerRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Collection<Owner>> getOwners() { Collection<Owner> owners = this.clinicService.findAllOwners(); if (owners.isEmpty()) { @@ -75,7 +75,7 @@ public class OwnerRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{ownerId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{ownerId}", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Owner> getOwner(@PathVariable("ownerId") int ownerId) { Owner owner = null; owner = this.clinicService.findOwnerById(ownerId); @@ -86,12 +86,12 @@ public class OwnerRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Owner> addOwner(@RequestBody @Valid Owner owner, BindingResult bindingResult, UriComponentsBuilder ucBuilder) { - BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); - if (bindingResult.hasErrors() || (owner == null)) { + if (bindingResult.hasErrors() || owner.getId() != null) { + BindingErrorsResponse errors = new BindingErrorsResponse(owner.getId()); errors.addAllErrors(bindingResult); headers.add("errors", errors.toJSON()); return new ResponseEntity<Owner>(headers, HttpStatus.BAD_REQUEST); @@ -102,13 +102,14 @@ public class OwnerRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{ownerId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{ownerId}", method = RequestMethod.PUT, produces = "application/json") public ResponseEntity<Owner> updateOwner(@PathVariable("ownerId") int ownerId, @RequestBody @Valid Owner owner, BindingResult bindingResult, UriComponentsBuilder ucBuilder) { - BindingErrorsResponse errors = new BindingErrorsResponse(); - HttpHeaders headers = new HttpHeaders(); - if (bindingResult.hasErrors() || (owner == null)) { + boolean bodyIdMatchesPathId = owner.getId() == null || ownerId == owner.getId(); + if (bindingResult.hasErrors() || !bodyIdMatchesPathId) { + BindingErrorsResponse errors = new BindingErrorsResponse(ownerId, owner.getId()); errors.addAllErrors(bindingResult); + HttpHeaders headers = new HttpHeaders(); headers.add("errors", errors.toJSON()); return new ResponseEntity<Owner>(headers, HttpStatus.BAD_REQUEST); } @@ -126,7 +127,7 @@ public class OwnerRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{ownerId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{ownerId}", method = RequestMethod.DELETE, produces = "application/json") @Transactional public ResponseEntity<Void> deleteOwner(@PathVariable("ownerId") int ownerId) { Owner owner = this.clinicService.findOwnerById(ownerId); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java index 79d971d66a4c1885133384f70e8caa451de9e5ac..875956b0afa3789448fb12ac6ba6fea718e8c46b 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/PetRestController.java @@ -53,7 +53,7 @@ public class PetRestController { private ClinicService clinicService; @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{petId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{petId}", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Pet> getPet(@PathVariable("petId") int petId){ Pet pet = this.clinicService.findPetById(petId); if(pet == null){ @@ -63,7 +63,7 @@ public class PetRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Collection<Pet>> getPets(){ Collection<Pet> pets = this.clinicService.findAllPets(); if(pets.isEmpty()){ @@ -73,13 +73,13 @@ public class PetRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/pettypes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/pettypes", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Collection<PetType>> getPetTypes(){ return new ResponseEntity<Collection<PetType>>(this.clinicService.findPetTypes(), HttpStatus.OK); } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Pet> addPet(@RequestBody @Valid Pet pet, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -94,7 +94,7 @@ public class PetRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{petId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{petId}", method = RequestMethod.PUT, produces = "application/json") public ResponseEntity<Pet> updatePet(@PathVariable("petId") int petId, @RequestBody @Valid Pet pet, BindingResult bindingResult){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -116,7 +116,7 @@ public class PetRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{petId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{petId}", method = RequestMethod.DELETE, produces = "application/json") @Transactional public ResponseEntity<Void> deletePet(@PathVariable("petId") int petId){ Pet pet = this.clinicService.findPetById(petId); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/PetTypeRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/PetTypeRestController.java index e16f51e81c5b5cac4ec27374873e2091d9af9336..ff6728769973dfffbb81a960b4cc1d9fc19d604f 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/PetTypeRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/PetTypeRestController.java @@ -48,7 +48,7 @@ public class PetTypeRestController { private ClinicService clinicService; @PreAuthorize( "hasAnyRole(@roles.OWNER_ADMIN, @roles.VET_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Collection<PetType>> getAllPetTypes(){ Collection<PetType> petTypes = new ArrayList<PetType>(); petTypes.addAll(this.clinicService.findAllPetTypes()); @@ -59,7 +59,7 @@ public class PetTypeRestController { } @PreAuthorize( "hasAnyRole(@roles.OWNER_ADMIN, @roles.VET_ADMIN)" ) - @RequestMapping(value = "/{petTypeId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{petTypeId}", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<PetType> getPetType(@PathVariable("petTypeId") int petTypeId){ PetType petType = this.clinicService.findPetTypeById(petTypeId); if(petType == null){ @@ -69,7 +69,7 @@ public class PetTypeRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<PetType> addPetType(@RequestBody @Valid PetType petType, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -84,7 +84,7 @@ public class PetTypeRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "/{petTypeId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{petTypeId}", method = RequestMethod.PUT, produces = "application/json") public ResponseEntity<PetType> updatePetType(@PathVariable("petTypeId") int petTypeId, @RequestBody @Valid PetType petType, BindingResult bindingResult){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -103,7 +103,7 @@ public class PetTypeRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "/{petTypeId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{petTypeId}", method = RequestMethod.DELETE, produces = "application/json") @Transactional public ResponseEntity<Void> deletePetType(@PathVariable("petTypeId") int petTypeId){ PetType petType = this.clinicService.findPetTypeById(petTypeId); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/SpecialtyRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/SpecialtyRestController.java index eb63b7064b43140a50c90d513e3275aa264a1147..6680e72952f846e05c01d26f9dcea4fb3c152543 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/SpecialtyRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/SpecialtyRestController.java @@ -53,7 +53,7 @@ public class SpecialtyRestController { private ClinicService clinicService; @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Collection<Specialty>> getAllSpecialtys(){ Collection<Specialty> specialties = new ArrayList<Specialty>(); specialties.addAll(this.clinicService.findAllSpecialties()); @@ -64,7 +64,7 @@ public class SpecialtyRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "/{specialtyId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{specialtyId}", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Specialty> getSpecialty(@PathVariable("specialtyId") int specialtyId){ Specialty specialty = this.clinicService.findSpecialtyById(specialtyId); if(specialty == null){ @@ -74,7 +74,7 @@ public class SpecialtyRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Specialty> addSpecialty(@RequestBody @Valid Specialty specialty, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -89,7 +89,7 @@ public class SpecialtyRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "/{specialtyId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{specialtyId}", method = RequestMethod.PUT, produces = "application/json") public ResponseEntity<Specialty> updateSpecialty(@PathVariable("specialtyId") int specialtyId, @RequestBody @Valid Specialty specialty, BindingResult bindingResult){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -108,7 +108,7 @@ public class SpecialtyRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "/{specialtyId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{specialtyId}", method = RequestMethod.DELETE, produces = "application/json") @Transactional public ResponseEntity<Void> deleteSpecialty(@PathVariable("specialtyId") int specialtyId){ Specialty specialty = this.clinicService.findSpecialtyById(specialtyId); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/UserRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/UserRestController.java index 7abb07cb959a3fa0904ec1c85b0636513c9b47f5..47ad14b73ab32fd9469f79d59073421a1717934a 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/UserRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/UserRestController.java @@ -42,7 +42,7 @@ public class UserRestController { private UserService userService; @PreAuthorize( "hasRole(@roles.ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<User> addOwner(@RequestBody @Valid User user, BindingResult bindingResult) throws Exception { BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/VetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/VetRestController.java index c666976e56a477fad5f868af7cecef04b2dfc15c..7570173a9118b01e5faa93690fffc9599762bf19 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/VetRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/VetRestController.java @@ -53,7 +53,7 @@ public class VetRestController { private ClinicService clinicService; @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Collection<Vet>> getAllVets(){ Collection<Vet> vets = new ArrayList<Vet>(); vets.addAll(this.clinicService.findAllVets()); @@ -64,7 +64,7 @@ public class VetRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "/{vetId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{vetId}", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Vet> getVet(@PathVariable("vetId") int vetId){ Vet vet = this.clinicService.findVetById(vetId); if(vet == null){ @@ -74,7 +74,7 @@ public class VetRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Vet> addVet(@RequestBody @Valid Vet vet, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -89,7 +89,7 @@ public class VetRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "/{vetId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{vetId}", method = RequestMethod.PUT, produces = "application/json") public ResponseEntity<Vet> updateVet(@PathVariable("vetId") int vetId, @RequestBody @Valid Vet vet, BindingResult bindingResult){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -113,7 +113,7 @@ public class VetRestController { } @PreAuthorize( "hasRole(@roles.VET_ADMIN)" ) - @RequestMapping(value = "/{vetId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{vetId}", method = RequestMethod.DELETE, produces = "application/json") @Transactional public ResponseEntity<Void> deleteVet(@PathVariable("vetId") int vetId){ Vet vet = this.clinicService.findVetById(vetId); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/VisitRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/VisitRestController.java index ba1638e6f86bae1958e59d9e201b4ed7e0287ef9..92fe1531a39b34b292a81b9d5a0b11cbd921efc2 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/VisitRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/VisitRestController.java @@ -53,7 +53,7 @@ public class VisitRestController { private ClinicService clinicService; @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Collection<Visit>> getAllVisits(){ Collection<Visit> visits = new ArrayList<Visit>(); visits.addAll(this.clinicService.findAllVisits()); @@ -64,7 +64,7 @@ public class VisitRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{visitId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{visitId}", method = RequestMethod.GET, produces = "application/json") public ResponseEntity<Visit> getVisit(@PathVariable("visitId") int visitId){ Visit visit = this.clinicService.findVisitById(visitId); if(visit == null){ @@ -74,7 +74,7 @@ public class VisitRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") public ResponseEntity<Visit> addVisit(@RequestBody @Valid Visit visit, BindingResult bindingResult, UriComponentsBuilder ucBuilder){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -89,7 +89,7 @@ public class VisitRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{visitId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{visitId}", method = RequestMethod.PUT, produces = "application/json") public ResponseEntity<Visit> updateVisit(@PathVariable("visitId") int visitId, @RequestBody @Valid Visit visit, BindingResult bindingResult){ BindingErrorsResponse errors = new BindingErrorsResponse(); HttpHeaders headers = new HttpHeaders(); @@ -110,7 +110,7 @@ public class VisitRestController { } @PreAuthorize( "hasRole(@roles.OWNER_ADMIN)" ) - @RequestMapping(value = "/{visitId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + @RequestMapping(value = "/{visitId}", method = RequestMethod.DELETE, produces = "application/json") @Transactional public ResponseEntity<Void> deleteVisit(@PathVariable("visitId") int visitId){ Visit visit = this.clinicService.findVisitById(visitId); diff --git a/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java b/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java index 0cc4ba68cf417f3da6d505addbda95c0194a08b1..18df3bd717387d148750811bea8d35b1734b278b 100644 --- a/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java +++ b/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java @@ -29,9 +29,11 @@ import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; +import java.util.Collections; + /** * Java config for Springfox swagger documentation plugin - * + * * @author Vitaliy Fedoriv * */ @@ -62,7 +64,7 @@ public class ApplicationSwaggerConfig { "https://github.com/spring-petclinic/spring-petclinic-rest", "vitaliy.fedoriv@gmail.com"), "Apache 2.0", - "http://www.apache.org/licenses/LICENSE-2.0"); + "http://www.apache.org/licenses/LICENSE-2.0", Collections.emptyList()); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java index 75d8f5f8b149feee863993e5b111c8b20ddb56f3..57147a55211a1a2db7e7bd96d09addecc32a411a 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java @@ -21,11 +21,10 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.ArrayList; +import java.util.Date; import java.util.List; import org.junit.Before; @@ -36,6 +35,9 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; 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.service.clinicService.ApplicationTestConfig; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.security.test.context.support.WithMockUser; @@ -76,16 +78,17 @@ public class OwnerRestControllerTests { .build(); owners = new ArrayList<Owner>(); - Owner owner = new Owner(); - owner.setId(1); - owner.setFirstName("George"); - owner.setLastName("Franklin"); - owner.setAddress("110 W. Liberty St."); - owner.setCity("Madison"); - owner.setTelephone("6085551023"); - owners.add(owner); - - owner = new Owner(); + Owner ownerWithPet = new Owner(); + ownerWithPet.setId(1); + ownerWithPet.setFirstName("George"); + ownerWithPet.setLastName("Franklin"); + ownerWithPet.setAddress("110 W. Liberty St."); + ownerWithPet.setCity("Madison"); + ownerWithPet.setTelephone("6085551023"); + ownerWithPet.addPet(getTestPetWithIdAndName(ownerWithPet, 1, "Rosy")); + owners.add(ownerWithPet); + + Owner owner = new Owner(); owner.setId(2); owner.setFirstName("Betty"); owner.setLastName("Davis"); @@ -111,8 +114,29 @@ public class OwnerRestControllerTests { owner.setCity("Windsor"); owner.setTelephone("6085553198"); owners.add(owner); + } + private Pet getTestPetWithIdAndName(final Owner owner, final int id, final String name) { + PetType petType = new PetType(); + petType.setId(2); + petType.setName("dog"); + Pet pet = new Pet(); + pet.setId(id); + pet.setName(name); + pet.setBirthDate(new Date()); + pet.setOwner(owner); + pet.setType(petType); + pet.addVisit(getTestVisitForPet(pet, 1)); + return pet; + } + private Visit getTestVisitForPet(final Pet pet, final int id) { + Visit visit = new Visit(); + visit.setId(id); + visit.setPet(pet); + visit.setDate(new Date()); + visit.setDescription("test" + id); + return visit; } @Test @@ -122,7 +146,7 @@ public class OwnerRestControllerTests { this.mockMvc.perform(get("/api/owners/1") .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(1)) .andExpect(jsonPath("$.firstName").value("George")); } @@ -145,7 +169,7 @@ public class OwnerRestControllerTests { this.mockMvc.perform(get("/api/owners/*/lastname/Davis") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(2)) .andExpect(jsonPath("$.[0].firstName").value("Betty")) .andExpect(jsonPath("$.[1].id").value(4)) @@ -171,7 +195,7 @@ public class OwnerRestControllerTests { this.mockMvc.perform(get("/api/owners/") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(2)) .andExpect(jsonPath("$.[0].firstName").value("Betty")) .andExpect(jsonPath("$.[1].id").value(4)) @@ -192,7 +216,7 @@ public class OwnerRestControllerTests { @WithMockUser(roles="OWNER_ADMIN") public void testCreateOwnerSuccess() throws Exception { Owner newOwner = owners.get(0); - newOwner.setId(999); + newOwner.setId(null); ObjectMapper mapper = new ObjectMapper(); String newOwnerAsJSON = mapper.writeValueAsString(newOwner); this.mockMvc.perform(post("/api/owners/") @@ -200,6 +224,20 @@ public class OwnerRestControllerTests { .andExpect(status().isCreated()); } + @Test + @WithMockUser(roles="OWNER_ADMIN") + public void testCreateOwnerErrorIdSpecified() throws Exception { + Owner newOwner = owners.get(0); + newOwner.setId(999); + ObjectMapper mapper = new ObjectMapper(); + String newOwnerAsJSON = mapper.writeValueAsString(newOwner); + this.mockMvc.perform(post("/api/owners/") + .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isBadRequest()) + .andExpect(header().string("errors", + "[{\"objectName\":\"body\",\"fieldName\":\"id\",\"fieldValue\":\"999\",\"errorMessage\":\"must not be specified\"}]")); + } + @Test @WithMockUser(roles="OWNER_ADMIN") public void testCreateOwnerError() throws Exception { @@ -216,25 +254,79 @@ public class OwnerRestControllerTests { @Test @WithMockUser(roles="OWNER_ADMIN") public void testUpdateOwnerSuccess() throws Exception { + given(this.clinicService.findOwnerById(1)).willReturn(owners.get(0)); + int ownerId = owners.get(0).getId(); + Owner updatedOwner = new Owner(); + // body.id = ownerId which is used in url path + updatedOwner.setId(ownerId); + updatedOwner.setFirstName("George I"); + updatedOwner.setLastName("Franklin"); + updatedOwner.setAddress("110 W. Liberty St."); + updatedOwner.setCity("Madison"); + updatedOwner.setTelephone("6085551023"); + ObjectMapper mapper = new ObjectMapper(); + String newOwnerAsJSON = mapper.writeValueAsString(updatedOwner); + this.mockMvc.perform(put("/api/owners/" + ownerId) + .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(content().contentType("application/json")) + .andExpect(status().isNoContent()); + + this.mockMvc.perform(get("/api/owners/" + ownerId) + .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json")) + .andExpect(jsonPath("$.id").value(ownerId)) + .andExpect(jsonPath("$.firstName").value("George I")); + + } + @Test + @WithMockUser(roles="OWNER_ADMIN") + public void testUpdateOwnerSuccessNoBodyId() throws Exception { given(this.clinicService.findOwnerById(1)).willReturn(owners.get(0)); - Owner newOwner = owners.get(0); - newOwner.setFirstName("George I"); + int ownerId = owners.get(0).getId(); + Owner updatedOwner = new Owner(); + updatedOwner.setFirstName("George I"); + updatedOwner.setLastName("Franklin"); + updatedOwner.setAddress("110 W. Liberty St."); + updatedOwner.setCity("Madison"); + updatedOwner.setTelephone("6085551023"); ObjectMapper mapper = new ObjectMapper(); - String newOwnerAsJSON = mapper.writeValueAsString(newOwner); - this.mockMvc.perform(put("/api/owners/1") + String newOwnerAsJSON = mapper.writeValueAsString(updatedOwner); + this.mockMvc.perform(put("/api/owners/" + ownerId) .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(status().isNoContent()); - this.mockMvc.perform(get("/api/owners/1") + this.mockMvc.perform(get("/api/owners/" + ownerId) .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) - .andExpect(jsonPath("$.id").value(1)) + .andExpect(content().contentType("application/json")) + .andExpect(jsonPath("$.id").value(ownerId)) .andExpect(jsonPath("$.firstName").value("George I")); } + @Test + @WithMockUser(roles="OWNER_ADMIN") + public void testUpdateOwnerErrorBodyIdMismatchWithPathId() throws Exception { + int ownerId = owners.get(0).getId(); + Owner updatedOwner = new Owner(); + // body.id != ownerId + updatedOwner.setId(-1); + updatedOwner.setFirstName("George I"); + updatedOwner.setLastName("Franklin"); + updatedOwner.setAddress("110 W. Liberty St."); + updatedOwner.setCity("Madison"); + updatedOwner.setTelephone("6085551023"); + ObjectMapper mapper = new ObjectMapper(); + String newOwnerAsJSON = mapper.writeValueAsString(updatedOwner); + this.mockMvc.perform(put("/api/owners/" + ownerId) + .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(status().isBadRequest()) + .andExpect(header().string("errors", + "[{\"objectName\":\"body\",\"fieldName\":\"id\",\"fieldValue\":\"-1\",\"errorMessage\":\"does not match pathId: 1\"}]")); + } + @Test @WithMockUser(roles="OWNER_ADMIN") public void testUpdateOwnerError() throws Exception { diff --git a/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java index 1a421170ab6cf182dd8b601551853bba1c8c7d5f..9513f34b461e84b2e98d5f997130db43245fe424 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java @@ -46,6 +46,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import com.fasterxml.jackson.databind.ObjectMapper; @@ -116,7 +117,7 @@ public class PetRestControllerTests { this.mockMvc.perform(get("/api/pets/3") .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(3)) .andExpect(jsonPath("$.name").value("Rosy")); } @@ -137,7 +138,7 @@ public class PetRestControllerTests { this.mockMvc.perform(get("/api/pets/") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(3)) .andExpect(jsonPath("$.[0].name").value("Rosy")) .andExpect(jsonPath("$.[1].id").value(4)) @@ -176,7 +177,7 @@ public class PetRestControllerTests { String newPetAsJSON = mapper.writeValueAsString(newPet); this.mockMvc.perform(post("/api/pets/") .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(status().isBadRequest()); + .andExpect(status().isBadRequest()).andDo(MockMvcResultHandlers.print()); } @Test @@ -189,13 +190,13 @@ public class PetRestControllerTests { String newPetAsJSON = mapper.writeValueAsString(newPet); this.mockMvc.perform(put("/api/pets/3") .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(status().isNoContent()); this.mockMvc.perform(get("/api/pets/3") .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(3)) .andExpect(jsonPath("$.name").value("Rosy I")); diff --git a/src/test/java/org/springframework/samples/petclinic/rest/PetTypeRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/PetTypeRestControllerTests.java index e0a3c7cb0d2865c0f7ca85a4a05ea2b4a25274e6..089cebfbbc1fa19c4b6ddac5ee9d83054dcaa285 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/PetTypeRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/PetTypeRestControllerTests.java @@ -104,7 +104,7 @@ public class PetTypeRestControllerTests { this.mockMvc.perform(get("/api/pettypes/1") .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(1)) .andExpect(jsonPath("$.name").value("cat")); } @@ -116,7 +116,7 @@ public class PetTypeRestControllerTests { this.mockMvc.perform(get("/api/pettypes/1") .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(1)) .andExpect(jsonPath("$.name").value("cat")); } @@ -139,7 +139,7 @@ public class PetTypeRestControllerTests { this.mockMvc.perform(get("/api/pettypes/") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(2)) .andExpect(jsonPath("$.[0].name").value("dog")) .andExpect(jsonPath("$.[1].id").value(4)) @@ -155,7 +155,7 @@ public class PetTypeRestControllerTests { this.mockMvc.perform(get("/api/pettypes/") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(2)) .andExpect(jsonPath("$.[0].name").value("dog")) .andExpect(jsonPath("$.[1].id").value(4)) @@ -207,13 +207,13 @@ public class PetTypeRestControllerTests { String newPetTypeAsJSON = mapper.writeValueAsString(newPetType); this.mockMvc.perform(put("/api/pettypes/2") .content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(status().isNoContent()); this.mockMvc.perform(get("/api/pettypes/2") .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(2)) .andExpect(jsonPath("$.name").value("dog I")); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/SpecialtyRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/SpecialtyRestControllerTests.java index 1f99c1cccda0fe6dc39c216b4e469098a701e833..c4592101d597836257dc449e53a9cefe65a91d2b 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/SpecialtyRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/SpecialtyRestControllerTests.java @@ -99,7 +99,7 @@ public class SpecialtyRestControllerTests { this.mockMvc.perform(get("/api/specialties/1") .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(1)) .andExpect(jsonPath("$.name").value("radiology")); } @@ -121,7 +121,7 @@ public class SpecialtyRestControllerTests { this.mockMvc.perform(get("/api/specialties/") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(2)) .andExpect(jsonPath("$.[0].name").value("surgery")) .andExpect(jsonPath("$.[1].id").value(3)) @@ -173,13 +173,13 @@ public class SpecialtyRestControllerTests { String newSpecialtyAsJSON = mapper.writeValueAsString(newSpecialty); this.mockMvc.perform(put("/api/specialties/2") .content(newSpecialtyAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(status().isNoContent()); this.mockMvc.perform(get("/api/specialties/2") .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(2)) .andExpect(jsonPath("$.name").value("surgery I")); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/VetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/VetRestControllerTests.java index 9358b6daf6aded04b881854361a5195723952c00..a6ab35ced9b47b6317a34b9a69c8904ce7df8cbe 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/VetRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/VetRestControllerTests.java @@ -102,7 +102,7 @@ public class VetRestControllerTests { this.mockMvc.perform(get("/api/vets/1") .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(1)) .andExpect(jsonPath("$.firstName").value("James")); } @@ -123,7 +123,7 @@ public class VetRestControllerTests { this.mockMvc.perform(get("/api/vets/") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(1)) .andExpect(jsonPath("$.[0].firstName").value("James")) .andExpect(jsonPath("$.[1].id").value(2)) @@ -175,13 +175,13 @@ public class VetRestControllerTests { String newVetAsJSON = mapper.writeValueAsString(newVet); this.mockMvc.perform(put("/api/vets/1") .content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(status().isNoContent()); this.mockMvc.perform(get("/api/vets/1") .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(1)) .andExpect(jsonPath("$.firstName").value("James")); diff --git a/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java index 1d0c1b2aac444dde05e1d6dd2aa14077353a3793..e318080e28887a10f4bb1d98a163320d078141d2 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java @@ -125,7 +125,7 @@ public class VisitRestControllerTests { this.mockMvc.perform(get("/api/visits/2") .accept(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(2)) .andExpect(jsonPath("$.description").value("rabies shot")); } @@ -146,7 +146,7 @@ public class VisitRestControllerTests { this.mockMvc.perform(get("/api/visits/") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.[0].id").value(2)) .andExpect(jsonPath("$.[0].description").value("rabies shot")) .andExpect(jsonPath("$.[1].id").value(3)) @@ -199,13 +199,13 @@ public class VisitRestControllerTests { String newVisitAsJSON = mapper.writeValueAsString(newVisit); this.mockMvc.perform(put("/api/visits/2") .content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(status().isNoContent()); this.mockMvc.perform(get("/api/visits/2") .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()) - .andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(content().contentType("application/json")) .andExpect(jsonPath("$.id").value(2)) .andExpect(jsonPath("$.description").value("rabies shot test")); }