Testing Restful grails controllers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import grails.test.*
import grails.converters.XML;
import grails.converters.JSON;


class UserControllerTests extends GroovyTestCase {
   
    void testGETXMLResponse() {
        def controller = new UserController()
        def existingUser = new User(username: "fred", password: User.encrypt("letmein"), firstName: "Fred", lastName: "Flintstone", middleName: "T", phone: "555-555-5555", email: 'fred@flintstone.com', activationDate: new Date(), logonFailureCount: 0).save()
        controller.request.method = 'GET'
        controller.request.contentType = 'text/xml'
        controller.params.format = 'xml'
        controller.params.id = existingUser.id
        controller.show()
        def user = new XmlSlurper().parseText(controller.response.contentAsString)
        assert user.@id == existingUser.id //we have to use the .@id because id is stored as an attribute to the user tag
        assert user.phone.text() == existingUser.phone //we have to use phone.text() because it is stored as a value of the phone tag.
    }
   
    void testGETJSONResponse() {
        def controller = new UserController()
        def existingUser = new User(username: "wilma", password: User.encrypt("letmein"), firstName: "Wilma", lastName: "Flintstone", middleName: "T", phone: "555-555-5555", email: 'wilma@flintstone.com', activationDate: new Date(), logonFailureCount: 0).save()
        controller.request.method = 'GET'
        controller.request.contentType = 'text/json'
        controller.params.format = 'json'
        controller.params.id = existingUser.id
        controller.show()
        def user = JSON.parse(controller.response.contentAsString)
        assert user.id == existingUser.id //this is how we access json text
        assert user.phone == existingUser.phone
    }
   
    void testPUTXMLResponse() {
        def controller = new UserController()
        def existingUser = new User(username: "dino", password: User.encrypt("letmein"), firstName: "Dino", lastName: "Flintstone", middleName: "T", phone: "555-555-5555", email: 'dino@flintstone.com', activationDate: new Date(), logonFailureCount: 0).save()
        existingUser.phone = '555-555-1111'
        def xml = existingUser as XML
        controller.request.method = 'PUT'
        controller.request.contentType = 'text/xml'
        controller.params.format = 'xml'
        controller.request.content = xml.toString().getBytes()
        controller.request.getAttribute("org.codehaus.groovy.grails.WEB_REQUEST").informParameterCreationListeners()
        controller.params.id = existingUser.id
        controller.update()
        def user = XML.parse(controller.response.contentAsString)
        assert user.@id == existingUser.id
        assert user.phone.text() == existingUser.phone
    }
   
    // I have submitted a JIRA issue regarding this test not working.  http://jira.codehaus.org/browse/GRAILS-5585
    /*
     void testPUTJSONResponse() {
     def controller = new UserController()
     def pebbles = new User(username:"pebbles", password:User.encrypt("letmein"), firstName:"Pebbles", lastName:"Flintstone", middleName:"T", phone:"555-555-5555", email:'pebbles@flintstone.com', activationDate:new Date(), logonFailureCount:0, deactivationDate:null).save()
     def builder = new JSONBuilder()
     def result = builder.build { user = pebbles }
     controller.request.method = 'PUT'
     controller.request.contentType = 'application/json'
     controller.params.format = 'json'
     controller.request.content = result.toString().getBytes()
     controller.request.getAttribute("org.codehaus.groovy.grails.WEB_REQUEST").informParameterCreationListeners()
     controller.params.id = '4'
     controller.update()
     def user = JSON.parse(controller.response.contentAsString)
     assert user.id == 4
     assert user.phone == '555-555-1111'
     }
     */

   
    void testPOSTXMLResponse() {
        def controller = new UserController()
        def existingUser = new User(username: "barney", password: User.encrypt("letmein"), firstName: "Barney", lastName: "Rubble", middleName: "T", phone: "555-555-5555", email: 'barney@rubble.com', activationDate: new Date(), logonFailureCount: 0)
        def xml = existingUser as XML
        controller.request.method = 'POST'
        controller.request.contentType = 'text/xml'
        controller.params.format = 'xml'
        controller.request.content = xml.toString().getBytes()
        controller.request.getAttribute("org.codehaus.groovy.grails.WEB_REQUEST").informParameterCreationListeners()
        controller.create()
        def user = XML.parse(controller.response.contentAsString)
        assert user.username.text() == "barney"
    }
   
    // I believe this is not working due to the same bug for the PUT test. http://jira.codehaus.org/browse/GRAILS-5585
    /*
     void testPOSTJSONResponse() {
     def controller = new UserController()
     controller.request.method = 'POST'
     controller.request.contentType = 'text/json'
     controller.params.format = 'json'
     controller.request.content = '{"activationDate":"2009-12-09T15:28:37Z","activeDirectoryUsername":null,"class":"User","createdBy":null,"deactivationDate":null,"disabled":false,"email":"betty@rubble.com","firstName":"Betty","id":1,"lastAccessDate":null,"lastName":"Rubble","lastUpdatedBy":null,"lastUpdatedDate":null,"logonFailureCount":0,"middleName":"T","mustChangePassword":false,"password":"FtPJCHN","phone":"555-555-5555","useActiveDirectory":false,"username":"betty","version":null}'.getBytes()
     controller.create()
     def user = JSON.parse(controller.response.contentAsString)
     println controller.response.contentAsString
     assert user.id == 5
     assert user.username == "betty"
     }
     */

   
    void testDELETEResponse() {
        def controller = new UserController()
        def existingUser = new User(username: "fred", password: User.encrypt("letmein"), firstName: "Fred", lastName: "Flintstone", middleName: "T", phone: "555-555-5555", email: 'fred@flintstone.com', activationDate: new Date(), logonFailureCount: 0).save()
        controller.request.method = 'DELETE'
        controller.params.id = existingUser.id
        controller.delete()
        assert controller.response.status == 200
    }
   
   
}

Posted in: REST, grails, groovy



This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

addLeave a comment

You must be logged in to post a comment.