19
19
import com .couchbase .client .CouchbaseClient ;
20
20
import com .couchbase .client .protocol .views .Query ;
21
21
import com .couchbase .client .protocol .views .Stale ;
22
+ import com .fasterxml .jackson .annotation .JsonFormat ;
22
23
import org .junit .Test ;
23
24
import org .junit .runner .RunWith ;
24
25
import org .springframework .beans .factory .annotation .Autowired ;
@@ -68,26 +69,26 @@ public void saveSimpleEntityCorrectly() throws Exception {
68
69
String result = (String ) client .get (id );
69
70
70
71
String expected = "{\" _class\" :\" org.springframework.data.couchbase.core.Beer\" "
71
- + ",\" is_active\" :false,\" name\" :\" The Awesome Stout\" }" ;
72
+ + ",\" is_active\" :false,\" name\" :\" The Awesome Stout\" }" ;
72
73
assertNotNull (result );
73
74
assertEquals (expected , result );
74
75
}
75
76
76
77
@ Test
77
78
public void saveDocumentWithExpiry () throws Exception {
78
- String id = "simple-doc-with-expiry" ;
79
- DocumentWithExpiry doc = new DocumentWithExpiry (id );
80
- template .save (doc );
81
- assertNotNull (client .get (id ));
82
- Thread .sleep (3000 );
83
- assertNull (client .get (id ));
79
+ String id = "simple-doc-with-expiry" ;
80
+ DocumentWithExpiry doc = new DocumentWithExpiry (id );
81
+ template .save (doc );
82
+ assertNotNull (client .get (id ));
83
+ Thread .sleep (3000 );
84
+ assertNull (client .get (id ));
84
85
}
85
86
86
87
@ Test
87
88
public void insertDoesNotOverride () {
88
- String id ="double-insert-test" ;
89
+ String id = "double-insert-test" ;
89
90
String expected = "{\" _class\" :\" org.springframework.data.couchbase.core."
90
- + "CouchbaseTemplateTests$SimplePerson\" ,\" name\" :\" Mr. A\" }" ;
91
+ + "CouchbaseTemplateTests$SimplePerson\" ,\" name\" :\" Mr. A\" }" ;
91
92
92
93
SimplePerson doc = new SimplePerson (id , "Mr. A" );
93
94
template .insert (doc );
@@ -103,10 +104,10 @@ public void insertDoesNotOverride() {
103
104
104
105
@ Test
105
106
public void updateDoesNotInsert () {
106
- String id ="update-does-not-insert" ;
107
- SimplePerson doc = new SimplePerson (id , "Nice Guy" );
108
- template .update (doc );
109
- assertNull (client .get (id ));
107
+ String id = "update-does-not-insert" ;
108
+ SimplePerson doc = new SimplePerson (id , "Nice Guy" );
109
+ template .update (doc );
110
+ assertNull (client .get (id ));
110
111
}
111
112
112
113
@@ -127,7 +128,7 @@ public void removeDocument() {
127
128
128
129
@ Test
129
130
public void storeListsAndMaps () {
130
- String id ="persons:lots-of-names" ;
131
+ String id = "persons:lots-of-names" ;
131
132
List <String > names = new ArrayList <String >();
132
133
names .add ("Michael" );
133
134
names .add ("Thomas" );
@@ -142,9 +143,9 @@ public void storeListsAndMaps() {
142
143
template .save (complex );
143
144
144
145
String expected = "{\" _class\" :\" org.springframework.data.couchbase.core."
145
- + "CouchbaseTemplateTests$ComplexPerson\" ,\" info1\" :{\" foo\" :true,\" bar\" "
146
- + ":false},\" votes\" :[],\" firstnames\" :[\" Michael\" ,\" Thomas\" ],\" info2\" :"
147
- + "{}}" ;
146
+ + "CouchbaseTemplateTests$ComplexPerson\" ,\" info1\" :{\" foo\" :true,\" bar\" "
147
+ + ":false},\" votes\" :[],\" firstnames\" :[\" Michael\" ,\" Thomas\" ],\" info2\" :"
148
+ + "{}}" ;
148
149
assertEquals (expected , client .get (id ));
149
150
150
151
ComplexPerson response = template .findById (id , ComplexPerson .class );
@@ -180,7 +181,7 @@ public void shouldLoadAndMapViewDocs() {
180
181
final List <Beer > beers = template .findByView ("test_beers" , "by_name" , query , Beer .class );
181
182
assertTrue (beers .size () > 0 );
182
183
183
- for (Beer beer : beers ) {
184
+ for (Beer beer : beers ) {
184
185
assertNotNull (beer .getId ());
185
186
assertNotNull (beer .getName ());
186
187
assertNotNull (beer .getActive ());
@@ -194,7 +195,7 @@ public void shouldNotSaveNull() {
194
195
try {
195
196
template .save (things );
196
197
fail ("We should not be able to store a NULL!" );
197
- } catch (final IllegalArgumentException e ) {
198
+ } catch (final IllegalArgumentException e ) {
198
199
assertTrue (true );
199
200
}
200
201
}
@@ -208,38 +209,50 @@ public void shouldDeserialiseLongs() {
208
209
assertNotNull (simpleWithLong );
209
210
assertEquals (time , simpleWithLong .getValue ());
210
211
}
211
-
212
+
213
+ @ Test
214
+ public void shouldDeserialiseEnums () {
215
+ SimpleWithEnum simpleWithEnum = new SimpleWithEnum ("simpleWithEnum:enum" , SimpleWithEnum .Type .BIG );
216
+ template .save (simpleWithEnum );
217
+ simpleWithEnum = template .findById ("simpleWithEnum:enum" , SimpleWithEnum .class );
218
+ assertNotNull (simpleWithEnum );
219
+ assertEquals (simpleWithEnum .getType (), SimpleWithEnum .Type .BIG );
220
+ }
221
+
212
222
/**
213
223
* A sample document with just an id and property.
214
224
*/
215
225
@ Document
216
226
static class SimplePerson {
227
+
217
228
@ Id
218
229
private final String id ;
219
230
@ Field
220
231
private final String name ;
221
232
222
233
public SimplePerson (String id , String name ) {
223
- this .id = id ;
224
- this .name = name ;
225
- }
234
+ this .id = id ;
235
+ this .name = name ;
236
+ }
226
237
}
227
-
238
+
228
239
/**
229
240
* A sample document that expires in 2 seconds.
230
241
*/
231
- @ Document (expiry = 2 )
242
+ @ Document (expiry = 2 )
232
243
static class DocumentWithExpiry {
244
+
233
245
@ Id
234
246
private final String id ;
235
-
247
+
236
248
public DocumentWithExpiry (String id ) {
237
- this .id = id ;
249
+ this .id = id ;
238
250
}
239
251
}
240
252
241
253
@ Document
242
254
static class ComplexPerson {
255
+
243
256
@ Id
244
257
private final String id ;
245
258
@ Field
@@ -253,8 +266,8 @@ static class ComplexPerson {
253
266
private final Map <String , Integer > info2 ;
254
267
255
268
public ComplexPerson (String id , List <String > firstnames ,
256
- List <Integer > votes , Map <String , Boolean > info1 ,
257
- Map <String , Integer > info2 ) {
269
+ List <Integer > votes , Map <String , Boolean > info1 ,
270
+ Map <String , Integer > info2 ) {
258
271
this .id = id ;
259
272
this .firstnames = firstnames ;
260
273
this .votes = votes ;
@@ -308,4 +321,38 @@ void setValue(final long value) {
308
321
this .value = value ;
309
322
}
310
323
}
324
+
325
+ static class SimpleWithEnum {
326
+
327
+ @ Id
328
+ private String id ;
329
+
330
+ private enum Type {
331
+ BIG
332
+ }
333
+
334
+ private Type type ;
335
+
336
+ SimpleWithEnum (final String id , final Type type ) {
337
+ this .id = id ;
338
+ this .type = type ;
339
+ }
340
+
341
+ String getId () {
342
+ return id ;
343
+ }
344
+
345
+ void setId (final String id ) {
346
+ this .id = id ;
347
+ }
348
+
349
+ Type getType () {
350
+ return type ;
351
+ }
352
+
353
+ void setType (final Type type ) {
354
+ this .type = type ;
355
+ }
356
+ }
357
+
311
358
}
0 commit comments