|
19 | 19 | import static org.junit.Assert.assertEquals; |
20 | 20 | import static org.junit.Assert.assertFalse; |
21 | 21 | import static org.junit.Assert.assertTrue; |
22 | | -import static org.mockito.Mockito.doThrow; |
23 | 22 | import static org.mockito.Mockito.mock; |
24 | 23 | import static org.mockito.Mockito.when; |
25 | 24 |
|
26 | 25 | import java.util.ArrayList; |
27 | 26 |
|
28 | | -import org.bson.Document; |
29 | 27 | import org.junit.Before; |
30 | 28 | import org.junit.Test; |
| 29 | +import org.mockito.Mockito; |
31 | 30 |
|
32 | 31 | import com.ericsson.ei.test.utils.TestConfigs; |
33 | | -import com.mongodb.ServerAddress; |
34 | | -import com.mongodb.client.ListDatabasesIterable; |
35 | 32 | import com.mongodb.client.MongoClient; |
36 | | -import com.mongodb.client.MongoCursor; |
| 33 | +import com.mongodb.client.MongoDatabase; |
37 | 34 |
|
38 | 35 | public class MongoDBHandlerTest { |
39 | 36 |
|
@@ -123,21 +120,19 @@ public void dropCollection() { |
123 | 120 |
|
124 | 121 | @Test |
125 | 122 | public void testIsMongoDBServerUp() { |
126 | | - MongoClient client = mock(MongoClient.class); |
127 | | - Document document = mock(Document.class); |
128 | | - ListDatabasesIterable documents = mock(ListDatabasesIterable.class);; |
129 | | - ListDatabasesIterable<Document> list = client.listDatabases(); |
130 | | - MongoCursor cursor= mock(MongoCursor.class); |
131 | | - |
132 | | - when(client.listDatabases()).thenReturn(documents); |
133 | | - when(cursor.getServerAddress()).thenReturn(new ServerAddress()); |
134 | | - assertTrue(mongoDBHandler.isMongoDBServerUp()); |
135 | | - |
136 | | - doThrow(Exception.class).when(documents); |
137 | | - mongoDBHandler.setMongoClient(null); |
138 | | - assertFalse(mongoDBHandler.isMongoDBServerUp()); |
139 | | - |
140 | | - //Need to set a working client to enable cleanup |
141 | | - mongoDBHandler.setMongoClient(TestConfigs.getMongoClient()); |
| 123 | + MongoDBHandler mongoDbHandler=mock(MongoDBHandler.class); |
| 124 | + when(mongoDbHandler.isMongoDBServerUp()).thenReturn(true); |
| 125 | + assertTrue(mongoDbHandler.isMongoDBServerUp()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testIsMongoDBServerDown() { |
| 130 | + MongoClient client = mock(MongoClient.class); |
| 131 | + MongoDatabase database=mock(MongoDatabase.class); |
| 132 | + when(client.getDatabase(Mockito.anyString())).thenReturn(database); |
| 133 | + mongoDBHandler.setMongoClient(null); |
| 134 | + assertFalse(mongoDBHandler.isMongoDBServerUp()); |
| 135 | + //Need to set a working client to enable cleanup |
| 136 | + mongoDBHandler.setMongoClient(TestConfigs.getMongoClient()); |
142 | 137 | } |
143 | 138 | } |
0 commit comments