1
+ package com .iluwatar .caching .database ;
2
+
3
+ import com .iluwatar .caching .UserAccount ;
4
+ import com .iluwatar .caching .constants .CachingConstants ;
5
+ import com .mongodb .client .FindIterable ;
6
+ import com .mongodb .client .MongoCollection ;
7
+ import com .mongodb .client .MongoDatabase ;
8
+ import org .bson .Document ;
9
+ import org .junit .jupiter .api .BeforeEach ;
10
+ import org .junit .jupiter .api .Test ;
11
+ import org .mockito .Mock ;
12
+ import org .mockito .internal .util .reflection .Whitebox ;
13
+
14
+ import static com .iluwatar .caching .constants .CachingConstants .ADD_INFO ;
15
+ import static com .iluwatar .caching .constants .CachingConstants .USER_ID ;
16
+ import static com .iluwatar .caching .constants .CachingConstants .USER_NAME ;
17
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
18
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
19
+ import static org .mockito .ArgumentMatchers .any ;
20
+ import static org .mockito .Mockito .*;
21
+
22
+ class MongoDbTest {
23
+ private static final String ID = "123" ;
24
+ private static final String NAME = "Some user" ;
25
+ private static final String ADDITIONAL_INFO = "Some app Info" ;
26
+
27
+ @ Mock
28
+ MongoDatabase db ;
29
+ private MongoDb mongoDb = new MongoDb ();
30
+
31
+ private UserAccount userAccount ;
32
+
33
+ @ BeforeEach
34
+ void init () {
35
+ db = mock (MongoDatabase .class );
36
+ Whitebox .setInternalState (mongoDb , "db" , db );
37
+ userAccount = new UserAccount (ID , NAME , ADDITIONAL_INFO );
38
+
39
+
40
+ }
41
+
42
+ @ Test
43
+ void connect () {
44
+ assertDoesNotThrow (() -> mongoDb .connect ());
45
+ }
46
+
47
+ @ Test
48
+ void readFromDb () {
49
+ Document document = new Document (USER_ID , ID )
50
+ .append (USER_NAME , NAME )
51
+ .append (ADD_INFO , ADDITIONAL_INFO );
52
+ MongoCollection <Document > mongoCollection = mock (MongoCollection .class );
53
+ when (db .getCollection (CachingConstants .USER_ACCOUNT )).thenReturn (mongoCollection );
54
+
55
+ FindIterable <Document > findIterable = mock (FindIterable .class );
56
+ when (mongoCollection .find (any (Document .class ))).thenReturn (findIterable );
57
+
58
+ when (findIterable .first ()).thenReturn (document );
59
+
60
+ assertEquals (mongoDb .readFromDb (ID ),userAccount );
61
+ }
62
+
63
+ @ Test
64
+ void writeToDb () {
65
+ MongoCollection <Document > mongoCollection = mock (MongoCollection .class );
66
+ when (db .getCollection (CachingConstants .USER_ACCOUNT )).thenReturn (mongoCollection );
67
+ doNothing ().when (mongoCollection ).insertOne (any (Document .class ));
68
+ assertDoesNotThrow (()-> {mongoDb .writeToDb (userAccount );});
69
+ }
70
+
71
+ @ Test
72
+ void updateDb () {
73
+ MongoCollection <Document > mongoCollection = mock (MongoCollection .class );
74
+ when (db .getCollection (CachingConstants .USER_ACCOUNT )).thenReturn (mongoCollection );
75
+ assertDoesNotThrow (()-> {mongoDb .updateDb (userAccount );});
76
+ }
77
+
78
+ @ Test
79
+ void upsertDb () {
80
+ MongoCollection <Document > mongoCollection = mock (MongoCollection .class );
81
+ when (db .getCollection (CachingConstants .USER_ACCOUNT )).thenReturn (mongoCollection );
82
+ assertDoesNotThrow (()-> {mongoDb .upsertDb (userAccount );});
83
+ }
84
+ }
0 commit comments