Permalink
Browse files

#11 make REST controller in test dynamically scaffolded

  • Loading branch information...
1 parent 351a701 commit f4a7ea86412d32fbbeaa7dade1dda203a9bf97b5 Rob Fletcher committed Feb 21, 2013
@@ -1,149 +1,7 @@
package grails.plugin.gson.test
-import grails.plugin.gson.converters.GSON
-import org.springframework.dao.DataIntegrityViolationException
-import static javax.servlet.http.HttpServletResponse.*
-
class AlbumController {
- public static final String X_PAGINATION_TOTAL = 'X-Pagination-Total'
- public static final int SC_UNPROCESSABLE_ENTITY = 422
-
- def beforeInterceptor = [action: this.&checkRequestIsJson, only: ['save', 'update']]
-
- def list(Integer max) {
- params.max = Math.min(max ?: 10, 100)
- response.addIntHeader X_PAGINATION_TOTAL, Album.count()
- render Album.list(params) as GSON
- }
-
- def save() {
- def albumInstance = new Album(request.GSON)
- log.error albumInstance
- if (albumInstance.save(flush: true)) {
- respondCreated albumInstance
- } else {
- respondUnprocessableEntity albumInstance
- }
- }
-
- def show(Long id) {
- def albumInstance = Album.get(id)
- if (albumInstance) {
- respondFound albumInstance
- } else {
- respondNotFound id
- }
- }
-
- def update(Long id, Long version) {
- def albumInstance = Album.get(id)
- if (!albumInstance) {
- respondNotFound id
- return
- }
-
- if (version != null) {
- if (albumInstance.version > version) {
- respondConflict(albumInstance)
- return
- }
- }
-
- albumInstance.properties = request.GSON
- log.error albumInstance
-
- if (albumInstance.save(flush: true)) {
- respondUpdated albumInstance
- } else {
- respondUnprocessableEntity albumInstance
- }
- }
-
- def delete(Long id) {
- def albumInstance = Album.get(id)
- if (!albumInstance) {
- respondNotFound id
- return
- }
-
- try {
- albumInstance.delete(flush: true)
- respondDeleted id
- } catch (DataIntegrityViolationException e) {
- respondNotDeleted id
- }
- }
-
- private checkRequestIsJson() {
- if (request.contentType != 'application/json') {
- respondNotAcceptable()
- return false
- }
- }
-
- private void respondFound(Album albumInstance) {
- response.status = SC_OK
- render albumInstance as GSON
- }
-
- private void respondCreated(Album albumInstance) {
- response.status = SC_CREATED
- render albumInstance as GSON
- }
-
- private void respondUpdated(Album albumInstance) {
- response.status = SC_OK
- render albumInstance as GSON
- }
-
- private void respondUnprocessableEntity(Album albumInstance) {
- def responseBody = [:]
- responseBody.errors = albumInstance.errors.allErrors.collect {
- message(error: it)
- }
- response.status = SC_UNPROCESSABLE_ENTITY
- render responseBody as GSON
- }
-
- private void respondNotFound(long id) {
- def responseBody = [:]
- responseBody.message = message(code: 'default.not.found.message', args: [message(code: 'item.label', default: 'Album'), id])
- response.status = SC_NOT_FOUND
- render responseBody as GSON
- }
-
- private void respondConflict(Album albumInstance) {
- albumInstance.errors.rejectValue('version', 'default.optimistic.locking.failure',
- [message(code: 'item.label', default: 'Album')] as Object[],
- 'Another user has updated this Album while you were editing')
- def responseBody = [:]
- responseBody.errors = albumInstance.errors.allErrors.collect {
- message(error: it)
- }
- response.status = SC_CONFLICT
- render responseBody as GSON
- }
-
- private void respondDeleted(long id) {
- def responseBody = [:]
- responseBody.message = message(code: 'default.deleted.message', args: [message(code: 'item.label', default: 'Album'), id])
- response.status = SC_OK
- render responseBody as GSON
- }
-
- private void respondNotDeleted(long id) {
- def responseBody = [:]
- responseBody.message = message(code: 'default.not.deleted.message', args: [message(code: 'item.label', default: 'Album'), id])
- response.status = SC_INTERNAL_SERVER_ERROR
- render responseBody as GSON
- }
-
- private void respondNotAcceptable() {
- response.status = SC_NOT_ACCEPTABLE
- response.contentLength = 0
- response.outputStream.flush()
- response.outputStream.close()
- }
+ static scaffold = true
}
@@ -0,0 +1,6 @@
+package grails.plugin.gson.http
+
+class HttpConstants {
+ public static final String X_PAGINATION_TOTAL = 'X-Pagination-Total'
+ public static final int SC_UNPROCESSABLE_ENTITY = 422
+}
@@ -0,0 +1,148 @@
+<%=packageName ? "package ${packageName}\n\n" : ''%>import grails.plugin.gson.converters.GSON
+import org.springframework.dao.DataIntegrityViolationException
+import static javax.servlet.http.HttpServletResponse.*
+import static grails.plugin.gson.http.HttpConstants.*
+
+class ${className}Controller {
+
+ def list(Integer max) {
+ params.max = Math.min(max ?: 10, 100)
+ response.addIntHeader X_PAGINATION_TOTAL, ${className}.count()
+ render ${className}.list(params) as GSON
+ }
+
+ def save() {
+ if (!requestIsJson()) {
+ respondNotAcceptable()
+ return
+ }
+
+ def ${propertyName} = new ${className}(request.GSON)
+ if (${propertyName}.save(flush: true)) {
+ respondCreated ${propertyName}
+ } else {
+ respondUnprocessableEntity ${propertyName}
+ }
+ }
+
+ def show(Long id) {
+ def ${propertyName} = ${className}.get(id)
+ if (${propertyName}) {
+ respondFound ${propertyName}
+ } else {
+ respondNotFound id
+ }
+ }
+
+ def update(Long id, Long version) {
+ if (!requestIsJson()) {
+ respondNotAcceptable()
+ return
+ }
+
+ def ${propertyName} = ${className}.get(id)
+ if (!${propertyName}) {
+ respondNotFound id
+ return
+ }
+
+ if (version != null) {
+ if (${propertyName}.version > version) {
+ respondConflict(${propertyName})
+ return
+ }
+ }
+
+ ${propertyName}.properties = request.GSON
+
+ if (${propertyName}.save(flush: true)) {
+ respondUpdated ${propertyName}
+ } else {
+ respondUnprocessableEntity ${propertyName}
+ }
+ }
+
+ def delete(Long id) {
+ def ${propertyName} = ${className}.get(id)
+ if (!${propertyName}) {
+ respondNotFound id
+ return
+ }
+
+ try {
+ ${propertyName}.delete(flush: true)
+ respondDeleted id
+ } catch (DataIntegrityViolationException e) {
+ respondNotDeleted id
+ }
+ }
+
+ private boolean requestIsJson() {
+ request.contentType == 'application/json'
+ }
+
+ private void respondFound(${className} ${propertyName}) {
+ response.status = SC_OK
+ render ${propertyName} as GSON
+ }
+
+ private void respondCreated(${className} ${propertyName}) {
+ response.status = SC_CREATED
+ render ${propertyName} as GSON
+ }
+
+ private void respondUpdated(${className} ${propertyName}) {
+ response.status = SC_OK
+ render ${propertyName} as GSON
+ }
+
+ private void respondUnprocessableEntity(${className} ${propertyName}) {
+ def responseBody = [:]
+ responseBody.errors = ${propertyName}.errors.allErrors.collect {
+ message(error: it)
+ }
+ response.status = SC_UNPROCESSABLE_ENTITY
+ render responseBody as GSON
+ }
+
+ private void respondNotFound(long id) {
+ def responseBody = [:]
+ responseBody.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
+ response.status = SC_NOT_FOUND
+ render responseBody as GSON
+ }
+
+ private void respondConflict(${className} ${propertyName}) {
+ ${propertyName}.errors.rejectValue('version', 'default.optimistic.locking.failure',
+ [message(code: '${domainClass.propertyName}.label', default: '${className}')] as Object[],
+ 'Another user has updated this ${className} while you were editing')
+ def responseBody = [:]
+ responseBody.errors = ${propertyName}.errors.allErrors.collect {
+ message(error: it)
+ }
+ response.status = SC_CONFLICT
+ render responseBody as GSON
+ }
+
+ private void respondDeleted(long id) {
+ def responseBody = [:]
+ responseBody.message = message(code: 'default.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
+ response.status = SC_OK
+ render responseBody as GSON
+ }
+
+ private void respondNotDeleted(long id) {
+ def responseBody = [:]
+ responseBody.message = message(code: 'default.not.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
+ response.status = SC_INTERNAL_SERVER_ERROR
+ render responseBody as GSON
+ }
+
+ private void respondNotAcceptable() {
+ response.status = SC_NOT_ACCEPTABLE
+ response.contentLength = 0
+ response.outputStream.flush()
+ response.outputStream.close()
+ }
+
+}
@@ -6,6 +6,7 @@ import static grails.plugin.gson.test.AlbumController.SC_UNPROCESSABLE_ENTITY
import static groovyx.net.http.ContentType.JSON
import static javax.servlet.http.HttpServletResponse.SC_NOT_ACCEPTABLE
import static org.apache.http.entity.ContentType.APPLICATION_JSON
+import static grails.plugin.gson.http.HttpConstants.*
@Unroll
class InvalidRequestSpec extends RestEndpointSpec {
@@ -6,6 +6,7 @@ import static grails.plugin.gson.test.AlbumController.X_PAGINATION_TOTAL
import static groovyx.net.http.ContentType.JSON
import static javax.servlet.http.HttpServletResponse.*
import static org.apache.http.entity.ContentType.APPLICATION_JSON
+import static grails.plugin.gson.http.HttpConstants.*
@Unroll
class NoDataSpec extends RestEndpointSpec {
@@ -4,6 +4,7 @@ import groovyx.net.http.HttpResponseDecorator
import static grails.plugin.gson.test.AlbumController.X_PAGINATION_TOTAL
import static javax.servlet.http.HttpServletResponse.SC_OK
import static org.apache.http.entity.ContentType.APPLICATION_JSON
+import static grails.plugin.gson.http.HttpConstants.*
class QueryEntitySpec extends RestEndpointSpec {

0 comments on commit f4a7ea8

Please sign in to comment.