File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
test/unit/grails/plugin/gson/serialization Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ package grails.plugin.gson.serialization
2
+
3
+ import com.google.gson.Gson
4
+ import grails.persistence.Entity
5
+ import grails.plugin.gson.GsonFactory
6
+ import grails.test.mixin.Mock
7
+ import spock.lang.*
8
+
9
+ @Mock (Coordinate )
10
+ class CompositeIdSpec extends Specification {
11
+
12
+ Gson gson
13
+
14
+ void setup () {
15
+ gson = new GsonFactory (grailsApplication). createGson()
16
+ }
17
+
18
+ @Issue (' https://github.com/robfletcher/grails-gson/issues/7' )
19
+ void ' can deserialize an existing instance with a composite id' () {
20
+ given :
21
+ def coord1 = new Coordinate (x : ' 3.38' , y : ' -54.43' , feature : ' Bouvet Island' ). save(failOnError : true )
22
+
23
+ and :
24
+ def data = [x : ' 3.38' , y : ' -54.43' ]
25
+ def json = gson. toJson(data)
26
+
27
+ when :
28
+ def coord2 = gson. fromJson(json, Coordinate )
29
+
30
+ then :
31
+ coord2. x == coord1. x
32
+ coord2. y == coord1. y
33
+ coord2. feature == coord1. feature
34
+ }
35
+
36
+ void ' can serialize an instance with a composite id' () {
37
+ given :
38
+ def coord = new Coordinate (x : ' 3.38' , y : ' -54.43' , feature : ' Bouvet Island' ). save(failOnError : true )
39
+
40
+ expect :
41
+ def json = gson. toJsonTree(coord)
42
+ json. x. asString == coord. x
43
+ json. y. asString == coord. y
44
+ }
45
+
46
+ }
47
+
48
+ @Entity
49
+ class Coordinate {
50
+ String x
51
+ String y
52
+ String feature
53
+ static mapping = {
54
+ id composite : [' x' , ' y' ]
55
+ }
56
+ }
You can’t perform that action at this time.
0 commit comments