From c6e5a750fc12884459b3c16bfd594eebf43784f3 Mon Sep 17 00:00:00 2001 From: Michael Yan Date: Wed, 7 Feb 2024 19:54:18 +0800 Subject: [PATCH] Generate all for Owner --- .../samples/OwnerController.groovy | 156 +++++++----- .../samples/OwnerService.groovy | 18 ++ app/views/owner/create.gsp | 61 +++++ app/views/owner/edit.gsp | 66 +++++ app/views/owner/index.gsp | 46 ++++ app/views/owner/show.gsp | 55 +++++ .../samples/OwnerServiceSpec.groovy | 74 ++++++ .../samples/OwnerControllerSpec.groovy | 227 ++++++++++++++++++ 8 files changed, 641 insertions(+), 62 deletions(-) create mode 100644 app/services/org/graceframework/samples/OwnerService.groovy create mode 100644 app/views/owner/create.gsp create mode 100644 app/views/owner/edit.gsp create mode 100644 app/views/owner/index.gsp create mode 100644 app/views/owner/show.gsp create mode 100644 src/integration-test/groovy/org/graceframework/samples/OwnerServiceSpec.groovy create mode 100644 src/test/groovy/org/graceframework/samples/OwnerControllerSpec.groovy diff --git a/app/controllers/org/graceframework/samples/OwnerController.groovy b/app/controllers/org/graceframework/samples/OwnerController.groovy index 394b302..88406ab 100644 --- a/app/controllers/org/graceframework/samples/OwnerController.groovy +++ b/app/controllers/org/graceframework/samples/OwnerController.groovy @@ -1,67 +1,99 @@ package org.graceframework.samples +import grails.validation.ValidationException +import static org.springframework.http.HttpStatus.* + class OwnerController { - def petclinicService - - def add() { - if (request.method == 'GET') { - return [ownerBean: new Owner()] - } - - def owner = petclinicService.createOwner(params.owner_firstName, params.owner_lastName, - params.owner_address, params.owner_city, params.owner_telephone) - - if (owner.hasErrors()) { - return [ownerBean: owner] - } - - redirect action: 'show', id: owner.id - } - - def show() { - def owner = Owner.get(params.id) - if (!owner) { - response.sendError 404 - return - } - - [ownerBean: owner] - } - - def edit() { - def owner = Owner.get(params.id) - if (request.method == 'GET') { - render view: 'add', model: [ownerBean: owner] - return - } - - petclinicService.updateOwner(Owner.get(params.id), params.owner?.firstName, params.owner?.lastName, - params.owner?.address, params.owner?.city, params.owner?.telephone) - - if (owner.hasErrors()) { - render view: 'add', model: [ownerBean: owner] - return - } - - redirect action: 'show', id: owner.id - } - - def find() { - if (!request.post) { - return - } - - def owners = Owner.findAllByLastName(params.lastName) - if (!owners) { - return [message: 'owners.not.found'] - } - - if (owners.size() > 1) { - render view: 'selection', model: [owners: owners] - } - else { - redirect action: 'show', id: owners[0].id - } - } + OwnerService ownerService + + static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"] + + def index(Integer max) { + params.max = Math.min(max ?: 10, 100) + respond ownerService.list(params), model:[ownerCount: ownerService.count()] + } + + def show(Long id) { + respond ownerService.get(id) + } + + def create() { + respond new Owner(params) + } + + def save(Owner owner) { + if (owner == null) { + notFound() + return + } + + try { + ownerService.save(owner) + } catch (ValidationException e) { + respond owner.errors, view:'create' + return + } + + request.withFormat { + form multipartForm { + flash.message = message(code: 'default.created.message', args: [message(code: 'owner.label', default: 'Owner'), owner.id]) + redirect owner + } + '*' { respond owner, [status: CREATED] } + } + } + + def edit(Long id) { + respond ownerService.get(id) + } + + def update(Owner owner) { + if (owner == null) { + notFound() + return + } + + try { + ownerService.save(owner) + } catch (ValidationException e) { + respond owner.errors, view:'edit' + return + } + + request.withFormat { + form multipartForm { + flash.message = message(code: 'default.updated.message', args: [message(code: 'owner.label', default: 'Owner'), owner.id]) + redirect owner + } + '*'{ respond owner, [status: OK] } + } + } + + def delete(Long id) { + if (id == null) { + notFound() + return + } + + ownerService.delete(id) + + request.withFormat { + form multipartForm { + flash.message = message(code: 'default.deleted.message', args: [message(code: 'owner.label', default: 'Owner'), id]) + redirect action:"index", method:"GET" + } + '*'{ render status: NO_CONTENT } + } + } + + protected void notFound() { + request.withFormat { + form multipartForm { + flash.message = message(code: 'default.not.found.message', args: [message(code: 'owner.label', default: 'Owner'), params.id]) + redirect action: "index", method: "GET" + } + '*'{ render status: NOT_FOUND } + } + } } diff --git a/app/services/org/graceframework/samples/OwnerService.groovy b/app/services/org/graceframework/samples/OwnerService.groovy new file mode 100644 index 0000000..f8e5fcc --- /dev/null +++ b/app/services/org/graceframework/samples/OwnerService.groovy @@ -0,0 +1,18 @@ +package org.graceframework.samples + +import grails.gorm.services.Service + +@Service(Owner) +interface OwnerService { + + Owner get(Serializable id) + + List list(Map args) + + Long count() + + void delete(Serializable id) + + Owner save(Owner owner) + +} \ No newline at end of file diff --git a/app/views/owner/create.gsp b/app/views/owner/create.gsp new file mode 100644 index 0000000..917a9c7 --- /dev/null +++ b/app/views/owner/create.gsp @@ -0,0 +1,61 @@ + + + + + + <g:message code="default.create.label" args="[entityName]" /> + + +
+
+
+ + +
+
+
+

+ +
${flash.message}
+
+ + + + +
+ +
+
+ +
+
+
+
+
+
+ + diff --git a/app/views/owner/edit.gsp b/app/views/owner/edit.gsp new file mode 100644 index 0000000..8aae018 --- /dev/null +++ b/app/views/owner/edit.gsp @@ -0,0 +1,66 @@ + + + + + + <g:message code="default.edit.label" args="[entityName]" /> + + +
+
+
+ + +
+
+
+

+ +
${flash.message}
+
+ + + + + +
+ +
+
+ +
+
+
+
+
+
+ + diff --git a/app/views/owner/index.gsp b/app/views/owner/index.gsp new file mode 100644 index 0000000..10220df --- /dev/null +++ b/app/views/owner/index.gsp @@ -0,0 +1,46 @@ + + + + + + <g:message code="default.list.label" args="[entityName]" /> + + +
+
+
+ + +
+
+
+

+ +
${flash.message}
+
+ + + + + +
+
+
+
+ + \ No newline at end of file diff --git a/app/views/owner/show.gsp b/app/views/owner/show.gsp new file mode 100644 index 0000000..5f0b116 --- /dev/null +++ b/app/views/owner/show.gsp @@ -0,0 +1,55 @@ + + + + + + <g:message code="default.show.label" args="[entityName]" /> + + +
+
+
+ + +
+
+
+

+ +
${flash.message}
+
+ + +
+ + + + +
+
+
+
+
+
+ + diff --git a/src/integration-test/groovy/org/graceframework/samples/OwnerServiceSpec.groovy b/src/integration-test/groovy/org/graceframework/samples/OwnerServiceSpec.groovy new file mode 100644 index 0000000..18b1230 --- /dev/null +++ b/src/integration-test/groovy/org/graceframework/samples/OwnerServiceSpec.groovy @@ -0,0 +1,74 @@ +package org.graceframework.samples + +import grails.testing.mixin.integration.Integration +import grails.gorm.transactions.Rollback +import spock.lang.Specification +import org.hibernate.SessionFactory + +@Integration +@Rollback +class OwnerServiceSpec extends Specification { + + OwnerService ownerService + SessionFactory sessionFactory + + private Long setupData() { + // TODO: Populate valid domain instances and return a valid ID + //new Owner(...).save(flush: true, failOnError: true) + //new Owner(...).save(flush: true, failOnError: true) + //Owner owner = new Owner(...).save(flush: true, failOnError: true) + //new Owner(...).save(flush: true, failOnError: true) + //new Owner(...).save(flush: true, failOnError: true) + assert false, "TODO: Provide a setupData() implementation for this generated test suite" + //owner.id + } + + void "test get"() { + setupData() + + expect: + ownerService.get(1) != null + } + + void "test list"() { + setupData() + + when: + List ownerList = ownerService.list(max: 2, offset: 2) + + then: + ownerList.size() == 2 + assert false, "TODO: Verify the correct instances are returned" + } + + void "test count"() { + setupData() + + expect: + ownerService.count() == 5 + } + + void "test delete"() { + Long ownerId = setupData() + + expect: + ownerService.count() == 5 + + when: + ownerService.delete(ownerId) + sessionFactory.currentSession.flush() + + then: + ownerService.count() == 4 + } + + void "test save"() { + when: + assert false, "TODO: Provide a valid instance to save" + Owner owner = new Owner() + ownerService.save(owner) + + then: + owner.id != null + } +} diff --git a/src/test/groovy/org/graceframework/samples/OwnerControllerSpec.groovy b/src/test/groovy/org/graceframework/samples/OwnerControllerSpec.groovy new file mode 100644 index 0000000..4ae524c --- /dev/null +++ b/src/test/groovy/org/graceframework/samples/OwnerControllerSpec.groovy @@ -0,0 +1,227 @@ +package org.graceframework.samples + +import grails.testing.gorm.DomainUnitTest +import grails.testing.web.controllers.ControllerUnitTest +import grails.validation.ValidationException +import spock.lang.* + +class OwnerControllerSpec extends Specification implements ControllerUnitTest, DomainUnitTest { + + def populateValidParams(params) { + assert params != null + + // TODO: Populate valid properties like... + //params["name"] = 'someValidName' + assert false, "TODO: Provide a populateValidParams() implementation for this generated test suite" + } + + void "Test the index action returns the correct model"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * list(_) >> [] + 1 * count() >> 0 + } + + when:"The index action is executed" + controller.index() + + then:"The model is correct" + !model.ownerList + model.ownerCount == 0 + } + + void "Test the create action returns the correct model"() { + when:"The create action is executed" + controller.create() + + then:"The model is correctly created" + model.owner!= null + } + + void "Test the save action with a null instance"() { + when:"Save is called for a domain instance that doesn't exist" + request.contentType = FORM_CONTENT_TYPE + request.method = 'POST' + controller.save(null) + + then:"A 404 error is returned" + response.redirectedUrl == '/owner/index' + flash.message != null + } + + void "Test the save action correctly persists"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * save(_ as Owner) + } + + when:"The save action is executed with a valid instance" + response.reset() + request.contentType = FORM_CONTENT_TYPE + request.method = 'POST' + populateValidParams(params) + def owner = new Owner(params) + owner.id = 1 + + controller.save(owner) + + then:"A redirect is issued to the show action" + response.redirectedUrl == '/owner/show/1' + controller.flash.message != null + } + + void "Test the save action with an invalid instance"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * save(_ as Owner) >> { Owner owner -> + throw new ValidationException("Invalid instance", owner.errors) + } + } + + when:"The save action is executed with an invalid instance" + request.contentType = FORM_CONTENT_TYPE + request.method = 'POST' + def owner = new Owner() + controller.save(owner) + + then:"The create view is rendered again with the correct model" + model.owner != null + view == 'create' + } + + void "Test the show action with a null id"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * get(null) >> null + } + + when:"The show action is executed with a null domain" + controller.show(null) + + then:"A 404 error is returned" + response.status == 404 + } + + void "Test the show action with a valid id"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * get(2) >> new Owner() + } + + when:"A domain instance is passed to the show action" + controller.show(2) + + then:"A model is populated containing the domain instance" + model.owner instanceof Owner + } + + void "Test the edit action with a null id"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * get(null) >> null + } + + when:"The show action is executed with a null domain" + controller.edit(null) + + then:"A 404 error is returned" + response.status == 404 + } + + void "Test the edit action with a valid id"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * get(2) >> new Owner() + } + + when:"A domain instance is passed to the show action" + controller.edit(2) + + then:"A model is populated containing the domain instance" + model.owner instanceof Owner + } + + + void "Test the update action with a null instance"() { + when:"Save is called for a domain instance that doesn't exist" + request.contentType = FORM_CONTENT_TYPE + request.method = 'PUT' + controller.update(null) + + then:"A 404 error is returned" + response.redirectedUrl == '/owner/index' + flash.message != null + } + + void "Test the update action correctly persists"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * save(_ as Owner) + } + + when:"The save action is executed with a valid instance" + response.reset() + request.contentType = FORM_CONTENT_TYPE + request.method = 'PUT' + populateValidParams(params) + def owner = new Owner(params) + owner.id = 1 + + controller.update(owner) + + then:"A redirect is issued to the show action" + response.redirectedUrl == '/owner/show/1' + controller.flash.message != null + } + + void "Test the update action with an invalid instance"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * save(_ as Owner) >> { Owner owner -> + throw new ValidationException("Invalid instance", owner.errors) + } + } + + when:"The save action is executed with an invalid instance" + request.contentType = FORM_CONTENT_TYPE + request.method = 'PUT' + controller.update(new Owner()) + + then:"The edit view is rendered again with the correct model" + model.owner != null + view == 'edit' + } + + void "Test the delete action with a null instance"() { + when:"The delete action is called for a null instance" + request.contentType = FORM_CONTENT_TYPE + request.method = 'DELETE' + controller.delete(null) + + then:"A 404 is returned" + response.redirectedUrl == '/owner/index' + flash.message != null + } + + void "Test the delete action with an instance"() { + given: + controller.ownerService = Mock(OwnerService) { + 1 * delete(2) + } + + when:"The domain instance is passed to the delete action" + request.contentType = FORM_CONTENT_TYPE + request.method = 'DELETE' + controller.delete(2) + + then:"The user is redirected to index" + response.redirectedUrl == '/owner/index' + flash.message != null + } +} + + + + + +