11package org .jooby .mongodb ;
22
3- import static org .easymock .EasyMock .expect ;
4- import static org .junit .Assert .assertEquals ;
5-
6- import java .util .Date ;
7- import java .util .LinkedHashMap ;
8- import java .util .Map ;
9- import java .util .concurrent .TimeUnit ;
10-
11- import org .bson .Document ;
12- import org .bson .conversions .Bson ;
13- import org .jooby .Session ;
14- import org .jooby .test .MockUnit ;
15- import org .jooby .test .MockUnit .Block ;
16- import org .junit .Test ;
17- import org .junit .runner .RunWith ;
18- import org .powermock .core .classloader .annotations .PrepareForTest ;
19- import org .powermock .modules .junit4 .PowerMockRunner ;
20-
213import com .google .common .collect .ImmutableMap ;
224import com .mongodb .DBObject ;
235import com .mongodb .client .FindIterable ;
2911import com .mongodb .client .model .IndexOptions ;
3012import com .mongodb .client .model .UpdateOptions ;
3113import com .mongodb .client .result .UpdateResult ;
14+ import org .bson .Document ;
15+ import org .bson .conversions .Bson ;
16+ import org .jooby .Session ;
17+ import org .jooby .test .MockUnit ;
18+ import org .jooby .test .MockUnit .Block ;
19+ import org .junit .Test ;
20+ import org .junit .runner .RunWith ;
21+ import org .powermock .core .classloader .annotations .PrepareForTest ;
22+ import org .powermock .modules .junit4 .PowerMockRunner ;
23+
24+ import java .util .Date ;
25+ import java .util .LinkedHashMap ;
26+ import java .util .Map ;
27+ import java .util .concurrent .TimeUnit ;
28+ import java .util .function .BiConsumer ;
29+
30+ import static org .easymock .EasyMock .expect ;
31+ import static org .junit .Assert .assertEquals ;
3232
3333@ RunWith (PowerMockRunner .class )
3434@ PrepareForTest ({MongoSessionStore .class , IndexOptions .class , UpdateOptions .class , Filters .class ,
35- LinkedHashMap .class })
35+ LinkedHashMap .class })
3636public class MongodbSessionStoreTest {
3737
38- @ SuppressWarnings ({"unchecked" , "rawtypes" })
38+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
3939 MockUnit .Block boot = unit -> {
4040 MongoCollection collection = unit .get (MongoCollection .class );
4141
@@ -45,7 +45,7 @@ public class MongodbSessionStoreTest {
4545
4646 long now = System .currentTimeMillis ();
4747
48- Map <String , String > attrs = ImmutableMap .<String , String > of ("k" , "v" );
48+ Map <String , String > attrs = ImmutableMap .<String , String >of ("k.v " , "v" , "$d" , "d " );
4949
5050 @ SuppressWarnings ("rawtypes" )
5151 MockUnit .Block saveSession = unit -> {
@@ -65,7 +65,8 @@ public class MongodbSessionStoreTest {
6565 .append ("_accessedAt" , new Date (now ))
6666 .append ("_createdAt" , new Date (now ))
6767 .append ("_savedAt" , new Date (now ))
68- .append ("k" , "v" );
68+ .append ("k\uFF0E v" , "v" )
69+ .append ("\uFF04 d" , "d" );
6970
7071 UpdateOptions options = unit .constructor (UpdateOptions .class )
7172 .build ();
@@ -225,47 +226,98 @@ public void saveSyncTtl() throws Exception {
225226 });
226227 }
227228
228- @ SuppressWarnings ({"unchecked" , "rawtypes" })
229+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
229230 @ Test
230231 public void get () throws Exception {
231232 long now = System .currentTimeMillis ();
232233 new MockUnit (Session .class , Session .Builder .class , MongoDatabase .class , MongoCollection .class ,
233234 DBObject .class )
234- .expect (boot )
235- .expect (unit -> {
236- Document doc = unit .mock (Document .class );
237-
238- Map sessionMap = unit .constructor (LinkedHashMap .class )
239- .args (Map .class )
240- .build (doc );
241- expect (sessionMap .remove ("_accessedAt" )).andReturn (new Date (now ));
242- expect (sessionMap .remove ("_createdAt" )).andReturn (new Date (now ));
243- expect (sessionMap .remove ("_savedAt" )).andReturn (new Date (now ));
244- expect (sessionMap .remove ("_id" )).andReturn ("1234" );
245-
246- FindIterable result = unit .mock (FindIterable .class );
247- expect (result .first ()).andReturn (doc );
248-
249- Session .Builder sb = unit .get (Session .Builder .class );
250- expect (sb .sessionId ()).andReturn ("1234" );
251- expect (sb .accessedAt (now )).andReturn (sb );
252- expect (sb .createdAt (now )).andReturn (sb );
253- expect (sb .savedAt (now )).andReturn (sb );
254- expect (sb .set (sessionMap )).andReturn (sb );
255- expect (sb .build ()).andReturn (unit .get (Session .class ));
256-
257- Bson eq = unit .mock (Bson .class );
258- unit .mockStatic (Filters .class );
259- expect (Filters .eq ("_id" , "1234" )).andReturn (eq );
260-
261- MongoCollection collection = unit .get (MongoCollection .class );
262- expect (collection .find (eq )).andReturn (result );
263- })
264- .run (unit -> {
265- MongoSessionStore mss = new MongoSessionStore (unit .get (MongoDatabase .class ), "sess" ,
266- "60" );
267- assertEquals (unit .get (Session .class ), mss .get (unit .get (Session .Builder .class )));
268- });
235+ .expect (boot )
236+ .expect (unit -> {
237+ Document doc = unit .mock (Document .class );
238+
239+ Map sessionMap = unit .constructor (LinkedHashMap .class )
240+ .args (Map .class )
241+ .build (doc );
242+ expect (sessionMap .remove ("_accessedAt" )).andReturn (new Date (now ));
243+ expect (sessionMap .remove ("_createdAt" )).andReturn (new Date (now ));
244+ expect (sessionMap .remove ("_savedAt" )).andReturn (new Date (now ));
245+ expect (sessionMap .remove ("_id" )).andReturn ("1234" );
246+ sessionMap .forEach (unit .capture (BiConsumer .class ));
247+
248+ FindIterable result = unit .mock (FindIterable .class );
249+ expect (result .first ()).andReturn (doc );
250+
251+ Session .Builder sb = unit .get (Session .Builder .class );
252+ expect (sb .sessionId ()).andReturn ("1234" );
253+ expect (sb .accessedAt (now )).andReturn (sb );
254+ expect (sb .createdAt (now )).andReturn (sb );
255+ expect (sb .savedAt (now )).andReturn (sb );
256+ expect (sb .set ("a.b" , "c" )).andReturn (sb );
257+ expect (sb .build ()).andReturn (unit .get (Session .class ));
258+
259+ Bson eq = unit .mock (Bson .class );
260+ unit .mockStatic (Filters .class );
261+ expect (Filters .eq ("_id" , "1234" )).andReturn (eq );
262+
263+ MongoCollection collection = unit .get (MongoCollection .class );
264+ expect (collection .find (eq )).andReturn (result );
265+ })
266+ .run (unit -> {
267+ MongoSessionStore mss = new MongoSessionStore (unit .get (MongoDatabase .class ), "sess" ,
268+ "60" );
269+ assertEquals (unit .get (Session .class ), mss .get (unit .get (Session .Builder .class )));
270+ }, unit -> {
271+ BiConsumer <String , String > setter = unit .captured (BiConsumer .class ).get (0 );
272+ setter .accept ("a\uFF0E b" , "c" );
273+ });
274+ }
275+
276+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
277+ @ Test
278+ public void getDollar () throws Exception {
279+ long now = System .currentTimeMillis ();
280+ new MockUnit (Session .class , Session .Builder .class , MongoDatabase .class , MongoCollection .class ,
281+ DBObject .class )
282+ .expect (boot )
283+ .expect (unit -> {
284+ Document doc = unit .mock (Document .class );
285+
286+ Map sessionMap = unit .constructor (LinkedHashMap .class )
287+ .args (Map .class )
288+ .build (doc );
289+ expect (sessionMap .remove ("_accessedAt" )).andReturn (new Date (now ));
290+ expect (sessionMap .remove ("_createdAt" )).andReturn (new Date (now ));
291+ expect (sessionMap .remove ("_savedAt" )).andReturn (new Date (now ));
292+ expect (sessionMap .remove ("_id" )).andReturn ("1234" );
293+ sessionMap .forEach (unit .capture (BiConsumer .class ));
294+
295+ FindIterable result = unit .mock (FindIterable .class );
296+ expect (result .first ()).andReturn (doc );
297+
298+ Session .Builder sb = unit .get (Session .Builder .class );
299+ expect (sb .sessionId ()).andReturn ("1234" );
300+ expect (sb .accessedAt (now )).andReturn (sb );
301+ expect (sb .createdAt (now )).andReturn (sb );
302+ expect (sb .savedAt (now )).andReturn (sb );
303+ expect (sb .set ("$ab" , "c" )).andReturn (sb );
304+ expect (sb .build ()).andReturn (unit .get (Session .class ));
305+
306+ Bson eq = unit .mock (Bson .class );
307+ unit .mockStatic (Filters .class );
308+ expect (Filters .eq ("_id" , "1234" )).andReturn (eq );
309+
310+ MongoCollection collection = unit .get (MongoCollection .class );
311+ expect (collection .find (eq )).andReturn (result );
312+ })
313+ .run (unit -> {
314+ MongoSessionStore mss = new MongoSessionStore (unit .get (MongoDatabase .class ), "sess" ,
315+ "60" );
316+ assertEquals (unit .get (Session .class ), mss .get (unit .get (Session .Builder .class )));
317+ }, unit -> {
318+ BiConsumer <String , String > setter = unit .captured (BiConsumer .class ).get (0 );
319+ setter .accept ("\uFF04 ab" , "c" );
320+ });
269321 }
270322
271323 @ SuppressWarnings ("rawtypes" )
0 commit comments