diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/model/Pet.java index 593798033c6c93ddcdb6d9b5fc41adeb17fd1f7a..d618f08bbb87798b429c9f074ddd99138da5a3a4 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java @@ -34,7 +34,6 @@ import org.hibernate.annotations.Type; import org.joda.time.DateTime; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; -import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -51,7 +50,6 @@ public class Pet extends NamedEntity { @Column(name = "birth_date") @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") - @DateTimeFormat(pattern = "yyyy/MM/dd") private DateTime birthDate; @ManyToOne diff --git a/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java b/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java index 315cb7011804fa4caa2c6d9a65f521471a40e11c..851a3ff757b07e62e809445b0b362f5448f11bb7 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerResource.java @@ -18,7 +18,7 @@ package org.springframework.samples.petclinic.web; import java.util.Collection; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; +import org.springframework.http.HttpStatus; import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.web.bind.WebDataBinder; @@ -28,6 +28,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; /** @@ -56,26 +57,27 @@ public class OwnerResource { return this.clinicService.findOwnerById(ownerId); } - @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.PUT) - public Owner updateOwner(@PathVariable("ownerId") int ownerId, @RequestBody Owner ownerRequest) { - Owner ownerModel = retrieveOwner(ownerId); - // This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. - ownerModel.setFirstName(ownerRequest.getFirstName()); - ownerModel.setLastName(ownerRequest.getLastName()); - ownerModel.setCity(ownerRequest.getCity()); - ownerModel.setAddress(ownerRequest.getAddress()); - ownerModel.setTelephone(ownerRequest.getTelephone()); - this.clinicService.saveOwner(ownerModel); - return ownerModel; - // TODO: need to handle failure + /** + * Create Owner + */ + @RequestMapping(value = "/owner", method = RequestMethod.POST) + @ResponseStatus(HttpStatus.CREATED) + public void createOwner(@RequestBody Owner owner) { + this.clinicService.saveOwner(owner); + // TODO: need to handle failure } - - + + /** + * Read single Owner + */ @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.GET) public Owner findOwner(@PathVariable("ownerId") int ownerId) { return retrieveOwner(ownerId); } + /** + * Read List of Owners + */ @RequestMapping(value = "/owner/list", method = RequestMethod.GET) public Collection<Owner> findOwnerCollection(@RequestParam("lastName") String ownerLastName) { @@ -91,5 +93,24 @@ public class OwnerResource { return results; } } + + /** + * Update Owner + */ + @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.PUT) + public Owner updateOwner(@PathVariable("ownerId") int ownerId, @RequestBody Owner ownerRequest) { + Owner ownerModel = retrieveOwner(ownerId); + // This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. + ownerModel.setFirstName(ownerRequest.getFirstName()); + ownerModel.setLastName(ownerRequest.getLastName()); + ownerModel.setCity(ownerRequest.getCity()); + ownerModel.setAddress(ownerRequest.getAddress()); + ownerModel.setTelephone(ownerRequest.getTelephone()); + this.clinicService.saveOwner(ownerModel); + return ownerModel; + // TODO: need to handle failure + } + + } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetController.java b/src/main/java/org/springframework/samples/petclinic/web/PetResource.java similarity index 86% rename from src/main/java/org/springframework/samples/petclinic/web/PetController.java rename to src/main/java/org/springframework/samples/petclinic/web/PetResource.java index ea8aeaaa86bf90149bc20f93e803cccc63b78465..3651968bb41618ab0c269abe087092e83ec0762f 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetResource.java @@ -23,7 +23,6 @@ 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.service.ClinicService; -import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; @@ -31,7 +30,7 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.support.SessionStatus; /** @@ -39,15 +38,14 @@ import org.springframework.web.bind.support.SessionStatus; * @author Ken Krebs * @author Arjen Poutsma */ -@Controller -@SessionAttributes("pet") -public class PetController { +@RestController +public class PetResource { private final ClinicService clinicService; @Autowired - public PetController(ClinicService clinicService) { + public PetResource(ClinicService clinicService) { this.clinicService = clinicService; } @@ -78,15 +76,14 @@ public class PetController { } else { this.clinicService.savePet(pet); status.setComplete(); - return "redirect:/owners/{ownerId}"; + return "redirect:/owner/{ownerId}"; } } - @RequestMapping(value = "/owners/*/pets/{petId}/edit", method = RequestMethod.GET) - public String initUpdateForm(@PathVariable("petId") int petId, Map<String, Object> model) { + @RequestMapping(value = "/owner/*/pet/{petId}", method = RequestMethod.GET) + public Pet findPet(@PathVariable("petId") int petId) { Pet pet = this.clinicService.findPetById(petId); - model.put("pet", pet); - return "pets/createOrUpdatePetForm"; + return pet; } @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST}) diff --git a/src/main/resources/spring/mvc-core-config.xml b/src/main/resources/spring/mvc-core-config.xml index ceb7d99ca62d29f67acf9e4e39f18613cd3c367d..51d2ce8280d0d0a6fd3dbf3d2cc3ff4359d38d68 100644 --- a/src/main/resources/spring/mvc-core-config.xml +++ b/src/main/resources/spring/mvc-core-config.xml @@ -21,7 +21,7 @@ --> <context:component-scan base-package="org.springframework.samples.petclinic.web"/> - + <mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> @@ -31,7 +31,7 @@ </bean> </mvc:message-converters> </mvc:annotation-driven> - + <!-- all resources inside folder src/main/webapp/resources are mapped so they can be refered to inside JSP files (see header.jsp for more details) --> <mvc:resources mapping="/resources/**" location="/resources/"/> diff --git a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp b/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp deleted file mode 100644 index c41f02ddf7c40b75909639aee4b14bd38c7ea757..0000000000000000000000000000000000000000 --- a/src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp +++ /dev/null @@ -1,64 +0,0 @@ -<!DOCTYPE html> - -<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> -<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> -<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %> - - -<html lang="en"> - -<jsp:include page="../fragments/staticFiles.jsp"/> -<body> - -<script> - $(function () { - $("#birthDate").datepicker({ dateFormat: 'yy/mm/dd'}); - }); -</script> -<div class="container"> - <jsp:include page="../fragments/bodyHeader.jsp"/> - <c:choose> - <c:when test="${pet['new']}"> - <c:set var="method" value="post"/> - </c:when> - <c:otherwise> - <c:set var="method" value="put"/> - </c:otherwise> - </c:choose> - - <h2> - <c:if test="${pet['new']}">New </c:if> - Pet - </h2> - - <form:form modelAttribute="pet" method="${method}" - class="form-horizontal"> - <div class="control-group" id="owner"> - <label class="control-label">Owner </label> - - <c:out value="${pet.owner.firstName} ${pet.owner.lastName}"/> - </div> - <petclinic:inputField label="Name" name="name"/> - <petclinic:inputField label="Birth Date" name="birthDate"/> - <div class="control-group"> - <petclinic:selectField name="type" label="Type " names="${types}" size="5"/> - </div> - <div class="form-actions"> - <c:choose> - <c:when test="${pet['new']}"> - <button type="submit">Add Pet</button> - </c:when> - <c:otherwise> - <button type="submit">Update Pet</button> - </c:otherwise> - </c:choose> - </div> - </form:form> - <c:if test="${!pet['new']}"> - </c:if> - <jsp:include page="../fragments/footer.jsp"/> -</div> -</body> - -</html> diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 7a0d87412bc007278cba86d50dbf5035b4e52f94..44123baf83dc837b1326302230559fd3c83103bb 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -15,6 +15,7 @@ <script src="scripts/app/main/MainController.js"></script> <script src="scripts/app/vet/VetController.js"></script> <script src="scripts/app/owner/OwnerController.js"></script> + <script src="scripts/app/pet/PetController.js"></script> </head> <body class="container"> diff --git a/src/main/webapp/scripts/app/app.js b/src/main/webapp/scripts/app/app.js index 5510889e345f876020b8b09d0471869fb944a154..671bb041c3b74d8fca08dfac540daf28ce0561da 100644 --- a/src/main/webapp/scripts/app/app.js +++ b/src/main/webapp/scripts/app/app.js @@ -29,8 +29,8 @@ petClinicApp.config(['$stateProvider', '$urlRouterProvider', function($stateProv url: 'owner/search', views: { 'content@': { - templateUrl: 'scripts/app/owner/ownerSearchForm.html', - controller: 'ownerSearchController' + controller: 'ownerSearchController', + templateUrl: 'scripts/app/owner/ownerSearchForm.html' } } @@ -39,8 +39,8 @@ petClinicApp.config(['$stateProvider', '$urlRouterProvider', function($stateProv url: 'owner/list?lastName', views: { 'content@': { - templateUrl: 'scripts/app/owner/ownerList.html', - controller: 'ownerListController' + controller: 'ownerListController', + templateUrl: 'scripts/app/owner/ownerList.html' } } @@ -49,8 +49,18 @@ petClinicApp.config(['$stateProvider', '$urlRouterProvider', function($stateProv url: 'owner/:id', views: { 'content@': { - templateUrl: 'scripts/app/owner/ownerDetail.html', - controller: 'ownerDetailController' + controller: 'ownerDetailController', + templateUrl: 'scripts/app/owner/ownerDetail.html' + } + } + + }). + state('app.ownercreate', { + url: 'owner', + views: { + 'content@': { + controller: 'ownerFormController', + templateUrl: 'scripts/app/owner/ownerForm.html' } } @@ -59,8 +69,8 @@ petClinicApp.config(['$stateProvider', '$urlRouterProvider', function($stateProv url: 'owner/:id/edit', views: { 'content@': { - templateUrl: 'scripts/app/owner/ownerForm.html', - controller: 'ownerFormController' + controller: 'ownerFormController', + templateUrl: 'scripts/app/owner/ownerForm.html' } } @@ -69,12 +79,32 @@ petClinicApp.config(['$stateProvider', '$urlRouterProvider', function($stateProv url: 'vets', views: { 'content@': { - templateUrl: 'scripts/app/vet/vetList.html', - controller: 'vetController' + controller: 'vetController', + templateUrl: 'scripts/app/vet/vetList.html' } } - }); + }). + state('app.petedit', { + url: 'owner/:ownerid/pet/:petid', + views: { + 'content@': { + controller: 'petFormController', + templateUrl: 'scripts/app/pet/petForm.html' + } + } + + }). + state('app.petcreate', { + url: 'owner/:ownerid/pet', + views: { + 'content@': { + controller: 'petFormController', + templateUrl: 'scripts/app/pet/petForm.html' + } + } + + }); }]); diff --git a/src/main/webapp/scripts/app/owner/OwnerController.js b/src/main/webapp/scripts/app/owner/OwnerController.js index 07acfc0e8ea70e146d5a591ab3d6b500645198b8..b03f7a2d1419f8a2c428339e1e3cb2f711a2474c 100644 --- a/src/main/webapp/scripts/app/owner/OwnerController.js +++ b/src/main/webapp/scripts/app/owner/OwnerController.js @@ -1,5 +1,12 @@ 'use strict'; +function loadOwner($scope, $resource, $stateParams) { + var ownerResource = $resource('/petclinic/owner/' + $stateParams.id); + $scope.owner = ownerResource.get(); +} + + + /* * Owner Search */ @@ -36,16 +43,13 @@ angular.module('controllers').controller('ownerDetailController', ['$scope', '$r loadOwner ]); -function loadOwner($scope, $resource, $stateParams) { - var ownerResource = $resource('/petclinic/owner/' + $stateParams.id); - $scope.owner = ownerResource.get(); -} + /* - * Owner Edit Form + * Form used to create and edit owners */ -angular.module('controllers').controller('ownerFormController', ['$scope', '$resource', '$http', '$stateParams', '$state', -function($scope, $resource, $http, $stateParams, $state) { +angular.module('controllers').controller('ownerFormController', ['$scope', '$http', '$resource', '$stateParams', '$state', +function($scope, $http, $resource, $stateParams, $state) { $scope.submitOwnerForm = {}; @@ -60,12 +64,22 @@ function($scope, $resource, $http, $stateParams, $state) { city: form.city, telephone: form.telephone }; - var restUrl = "/petclinic/owner/" + $stateParams.id; - $http.put(restUrl, data); - $state.go('app.ownerlist'); + + if ($state.current.name == 'app.owneredit') { + var restUrl = "/petclinic/owner/" + $stateParams.id; + $http.put(restUrl, data); + $state.go('app.ownerlist'); + } + else { // in case of owner creation + var restUrl = "/petclinic/owner"; + $http.post(restUrl, data); + $state.go('app.ownerlist'); + } } - loadOwner($scope, $resource, $stateParams); + if ($state.current.name == 'app.owneredit') { + loadOwner($scope, $resource, $stateParams); + } }]); diff --git a/src/main/webapp/scripts/app/owner/ownerDetail.html b/src/main/webapp/scripts/app/owner/ownerDetail.html index c3ba84aef0226f7952b49a046a531e93257473eb..45a7b9fddadf11f8f9df5e67cb51bd14e992db94 100644 --- a/src/main/webapp/scripts/app/owner/ownerDetail.html +++ b/src/main/webapp/scripts/app/owner/ownerDetail.html @@ -23,10 +23,8 @@ <td> <a class="btn btn-info" ui-sref="app.owneredit({id: owner.id})">Edit Owner</a></td> <td> - <spring:url value="{ownerId}/pets/new.html" var="addUrl"> - <spring:param name="ownerId" value="${owner.id}"/> - </spring:url> - <a href="${fn:escapeXml(addUrl)}" class="btn btn-success">Add New Pet</a></td> + <a ui-sref="app.petcreate({ownerid: owner.id})" class="btn btn-success">Add New Pet</a> + </td> </tr> </table> @@ -58,11 +56,7 @@ </tr> <tr> <td> - <spring:url value="/owners/{ownerId}/pets/{petId}/edit" var="petUrl"> - <spring:param name="ownerId" value="${owner.id}"/> - <spring:param name="petId" value="${pet.id}"/> - </spring:url> - <a href="${fn:escapeXml(petUrl)}">Edit Pet</a> + <a ui-sref="app.petedit( {ownerid: owner.id, petid: pet.id})">Edit Pet</a> </td> <td> <spring:url value="/owners/{ownerId}/pets/{petId}/visits/new" var="visitUrl"> diff --git a/src/main/webapp/scripts/app/owner/ownerSearchForm.html b/src/main/webapp/scripts/app/owner/ownerSearchForm.html index 7b9fe5c489dde87328020596dbe7fa49799ca39b..548c452a8d399d9aa023e0453a1d07f10b14844d 100644 --- a/src/main/webapp/scripts/app/owner/ownerSearchForm.html +++ b/src/main/webapp/scripts/app/owner/ownerSearchForm.html @@ -12,4 +12,4 @@ </form> <br/> - <a href="/owners/new">Add Owners</a> + <a ui-sref="app.ownercreate">Add Owners</a> diff --git a/src/main/webapp/scripts/app/pet/PetController.js b/src/main/webapp/scripts/app/pet/PetController.js new file mode 100644 index 0000000000000000000000000000000000000000..a766a281c0279b809b08400dcf8df39750dc7b69 --- /dev/null +++ b/src/main/webapp/scripts/app/pet/PetController.js @@ -0,0 +1,64 @@ +'use strict'; + +function loadPet($scope, $resource, $stateParams) { + var petResource = $resource('/petclinic/owner/' + $stateParams.ownerid +"/pet/" +$stateParams.petid); + $scope.pet = petResource.get(); +} + + + + +/* + * Form used to create and edit pets + */ +angular.module('controllers').controller('petFormController', ['$scope', '$http', '$resource', '$stateParams', '$state', +function($scope, $http, $resource, $stateParams, $state) { + + $scope.submitPetForm = {}; + + $scope.submitPetForm = function() { + var form = $scope.pet; + + // Creating a Javascript object + var data = { + name: form.name, + birthDate: form.birthDate, + type: form.type + }; + + if ($state.current.name == 'app.petedit') { + var restUrl = "/petclinic/owner/" + $stateParams.ownerid +"/pet/" +$stateParams.petid; + $http.put(restUrl, data); + $state.go('app.ownerdetail'); + } + else { // in case of pet creation + var restUrl = "/petclinic/owner/" + $stateParams.ownerid +"/pet"; + $http.post(restUrl, data); + $state.go('app.ownerdetail'); + } + } + + if ($state.current.name == 'app.petedit') { + loadPet($scope, $resource, $stateParams); + } + +}]); + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/scripts/app/pet/petForm.html b/src/main/webapp/scripts/app/pet/petForm.html new file mode 100644 index 0000000000000000000000000000000000000000..d8aa79bdcadbf238962da46202205b89372a4cbb --- /dev/null +++ b/src/main/webapp/scripts/app/pet/petForm.html @@ -0,0 +1,44 @@ +<!DOCTYPE html> + +<script> + $(function () { + $("#birthDate").datepicker({ dateFormat: 'yy/mm/dd'}); + }); +</script> +<div class="container"> + + <h2>Pet (work in progress) </h2> + <form class="form-horizontal" name="petForm" data-ng-controller="petFormController"> + <fieldset> + <div class="control-group" id="owner"> + <label class="control-label"> Owner </label> + {{pet.owner.firstName}} {{pet.owner.lastName}} + </div> + <div class="control-group" id="name"> + <label class="control-label">Name </label> + <input ng-model="pet.name" name="name" required/> + <span ng-show="petForm.name.$error.required" class="help-inline"> Name is required.</span> + </div> + <div class="control-group" id="birthDate"> + <label class="control-label">Birth date </label> + <input ng-model="pet.birthDate" value="{{pet.birthDate | date:'MM/dd/yyyy'}} name="birthDate" required/> + <span ng-show="petForm.name.$error.required" class="help-inline"> birth date is required.</span> + </div> + + + <div class="control-group"> + <petclinic:selectField name="type" label="Type " names="${types}" size="5"/> + </div> + <div class="form-actions"> + <c:choose> + <c:when test="${pet['new']}"> + <button type="submit">Add Pet</button> + </c:when> + <c:otherwise> + <button type="submit">Update Pet</button> + </c:otherwise> + </c:choose> + </div> + </fieldset> + </form> +</div> diff --git a/src/test/java/org/springframework/samples/petclinic/web/AbstractWebResourceTests.java b/src/test/java/org/springframework/samples/petclinic/web/AbstractWebResourceTests.java new file mode 100644 index 0000000000000000000000000000000000000000..9c28da2baffeb3e89f9ea81202410427453601cc --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/web/AbstractWebResourceTests.java @@ -0,0 +1,28 @@ +package org.springframework.samples.petclinic.web; + +import org.junit.runner.RunWith; +import org.springframework.test.context.ActiveProfiles; +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.setup.MockMvcBuilders; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) +@WebAppConfiguration +@ActiveProfiles("spring-data-jpa") + +public abstract class AbstractWebResourceTests { + + protected MockMvc mockMvc; + + public void runMockSpringMVC(Object resource) { + this.mockMvc = MockMvcBuilders.standaloneSetup(resource).build(); + } + + public AbstractWebResourceTests() { + super(); + } + +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java b/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java new file mode 100644 index 0000000000000000000000000000000000000000..0bd74cb397e26222cd461e368219cf08f4ed5b43 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/web/PetResourceTests.java @@ -0,0 +1,53 @@ +package org.springframework.samples.petclinic.web; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +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 org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.ResultActions; + +/** + * Test class for the UserResource REST controller. + * + * @see UserResource + */ +public class PetResourceTests extends AbstractWebResourceTests { + + @Autowired + private PetResource petResource; + + @Before + public void setup() { + runMockSpringMVC(petResource); + } + + /** + * Expected JSon result: + * { + "id":2, + "name":"Basil", + "birthDate":1344211200000, + "type":{ + "id":6, + "name":"hamster", + "new":false + }, + "visits":[], + "new":false + } + */ + @Test + public void shouldGetAPetInJSonFormat() throws Exception { + ResultActions actions = mockMvc.perform(get("/owner/2/pet/2.json").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + actions.andExpect(content().contentType("application/json;charset=UTF-8")) + .andExpect(jsonPath("$.id").value(2)) + .andExpect(jsonPath("$.name").value("Basil")) + .andExpect(jsonPath("$.type.id").value(6)); + } +} diff --git a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java b/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java similarity index 55% rename from src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java rename to src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java index c8b2d08bd8e59134ee156a720cb6efa14e8d7542..4e6283e639cd4648790158b592089e4e73699711 100644 --- a/src/test/java/org/springframework/samples/petclinic/web/VetControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/web/VetResourceTests.java @@ -7,44 +7,27 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; -import org.springframework.test.context.ActiveProfiles; -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.ResultActions; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; /** * Test class for the UserResource REST controller. * * @see UserResource */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"}) -@WebAppConfiguration -@ActiveProfiles("spring-data-jpa") -public class VetControllerTests { +public class VetResourceTests extends AbstractWebResourceTests { @Autowired private VetResource vetResource; - @Autowired - private WebApplicationContext ctx; - - private MockMvc mockMvc; - @Before public void setup() { - this.mockMvc = MockMvcBuilders.standaloneSetup(vetResource).build(); + runMockSpringMVC(vetResource); } @Test - public void testGetExistingUser() throws Exception { + public void shouldGetAListOfVetsInJSonFormat() throws Exception { ResultActions actions = mockMvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); actions.andExpect(content().contentType("application/json"))