@@ -126,6 +126,102 @@ class DocumentSpecification extends Specification {
126
126
thrown(ClassCastException )
127
127
}
128
128
129
+ def ' should return null when getting embedded value' () {
130
+ when :
131
+ Document document = Document . parse(" {a: 1, b: {x: [2, 3, 4], y: {m: 'one', len: 3}}, 'a.b': 'two'}" )
132
+
133
+ then :
134
+ document. getEmbedded(List . of(' notAKey' ), String ) == null
135
+ document. getEmbedded(List . of(' b' , ' y' , ' notAKey' ), String ) == null
136
+ document. getEmbedded(List . of(' b' , ' b' , ' m' ), String ) == null
137
+ Document . parse(' {}' ). getEmbedded(List . of(' a' , ' b' ), Integer ) == null
138
+ Document . parse(' {b: 1}' ). getEmbedded([' a' ], Integer ) == null
139
+ Document . parse(' {b: 1}' ). getEmbedded([' a' , ' b' ], Integer ) == null
140
+ Document . parse(' {a: {c: 1}}' ). getEmbedded([' a' , ' b' ], Integer ) == null
141
+ Document . parse(' {a: {c: 1}}' ). getEmbedded([' a' , ' b' , ' c' ], Integer ) == null
142
+ }
143
+
144
+ def ' should return embedded value' () {
145
+ given :
146
+ Date date = new Date ();
147
+ ObjectId objectId = new ObjectId ();
148
+
149
+ when :
150
+ Document document = Document . parse(" {a: 1, b: {x: [2, 3, 4], y: {m: 'one', len: 3}}, 'a.b': 'two'}" )
151
+ .append(' l' , new Document (' long' , 2L ))
152
+ .append(' d' , new Document (' double' , 3.0 as double ))
153
+ .append(' t' , new Document (' boolean' , true ))
154
+ .append(' o' , new Document (' objectId' , objectId))
155
+ .append(' n' , new Document (' date' , date))
156
+
157
+ then :
158
+ document. getEmbedded(List . of(' a' ), Integer ) == 1
159
+ document. getEmbedded(List . of(' b' , ' x' ), List ). get(0 ) == 2
160
+ document. getEmbedded(List . of(' b' , ' x' ), List ). get(1 ) == 3
161
+ document. getEmbedded(List . of(' b' , ' x' ), List ). get(2 ) == 4
162
+ document. getEmbedded(List . of(' b' , ' y' , ' m' ), String ) == ' one'
163
+ document. getEmbedded(List . of(' b' , ' y' , ' len' ), Integer ) == 3
164
+ document. getEmbedded(List . of(' a.b' ), String ) == ' two'
165
+ document. getEmbedded(List . of(' b' , ' y' ), Document ). getString(' m' ) == ' one'
166
+ document. getEmbedded(List . of(' b' , ' y' ), Document ). getInteger(' len' ) == 3
167
+
168
+ document. getEmbedded(Arrays . asList(' l' , ' long' ), Long ) == 2L
169
+ document. getEmbedded(Arrays . asList(' d' , ' double' ), Double ) == 3.0d
170
+ document. getEmbedded(Arrays . asList(' l' , ' long' ), Number ) == 2L
171
+ document. getEmbedded(Arrays . asList(' d' , ' double' ), Number ) == 3.0d
172
+ document. getEmbedded(Arrays . asList(' t' , ' boolean' ), Boolean ) == true
173
+ document. getEmbedded(Arrays . asList(' t' , ' x' ), false ) == false
174
+ document. getEmbedded(Arrays . asList(' o' , ' objectId' ), ObjectId ) == objectId
175
+ document. getEmbedded(Arrays . asList(' n' , ' date' ), Date ) == date
176
+ }
177
+
178
+ def ' should throw an exception getting an embedded value' () {
179
+ given :
180
+ Document document = Document . parse(" {a: 1, b: {x: [2, 3, 4], y: {m: 'one', len: 3}}, 'a.b': 'two'}" )
181
+
182
+ when :
183
+ document. getEmbedded(null , String ) == null
184
+
185
+ then :
186
+ thrown(IllegalArgumentException )
187
+
188
+ when :
189
+ document. getEmbedded(List . of(), String ) == null
190
+
191
+ then :
192
+ thrown(IllegalStateException )
193
+
194
+ when :
195
+ document. getEmbedded([' a' , ' b' ], Integer )
196
+
197
+ then :
198
+ thrown(ClassCastException )
199
+
200
+ when :
201
+ document. getEmbedded(List . of(' b' , ' y' , ' m' ), Integer )
202
+
203
+ then :
204
+ thrown(ClassCastException )
205
+
206
+ when :
207
+ document. getEmbedded(List . of(' b' , ' x' ), Document )
208
+
209
+ then :
210
+ thrown(ClassCastException )
211
+
212
+ when :
213
+ document. getEmbedded(List . of(' b' , ' x' , ' m' ), String )
214
+
215
+ then :
216
+ thrown(ClassCastException )
217
+
218
+ when :
219
+ document. getEmbedded(Arrays . asList(' b' , ' x' , ' m' ), ' invalid' )
220
+
221
+ then :
222
+ thrown(ClassCastException )
223
+ }
224
+
129
225
def ' should parse a valid JSON string to a Document' () {
130
226
when :
131
227
Document document = Document . parse(" { 'int' : 1, 'string' : 'abc' }" );
0 commit comments