11package com.opendatamask.connector
22
3+ import com.mongodb.client.FindIterable
34import com.mongodb.client.MongoClient
45import com.mongodb.client.MongoCollection
56import com.mongodb.client.MongoDatabase
67import org.bson.Document
78import org.junit.jupiter.api.Test
89import org.junit.jupiter.api.Assertions.*
10+ import org.mockito.Mockito
911import org.mockito.kotlin.*
1012
1113class MongoDBConnectorTest {
@@ -16,6 +18,70 @@ class MongoDBConnectorTest {
1618 }
1719 }
1820
21+ /* *
22+ * Returns a FindIterable mock that uses RETURNS_DEEP_STUBS so that the
23+ * MongoIterable.map(...).toList() call chain in fetchData() works without NPE.
24+ * fetchData() will return an empty list, but the filter/limit arguments
25+ * passed to find()/limit() can still be verified.
26+ */
27+ private fun mockFindIterable (): FindIterable <Document > {
28+ val iterable = mock<FindIterable <Document >>(defaultAnswer = Mockito .RETURNS_DEEP_STUBS )
29+ whenever(iterable.limit(any<Int >())).thenReturn(iterable)
30+ return iterable
31+ }
32+
33+ @Test
34+ fun `fetchData uses empty filter when no whereClause provided` () {
35+ val mockCollection = mock<MongoCollection <Document >>()
36+ val mockDb = mock<MongoDatabase >()
37+ val mockClient = mock<MongoClient >()
38+ val findIterable = mockFindIterable()
39+
40+ whenever(mockClient.getDatabase(" testdb" )).thenReturn(mockDb)
41+ whenever(mockDb.getCollection(" users" )).thenReturn(mockCollection)
42+ whenever(mockCollection.find(any<Document >())).thenReturn(findIterable)
43+
44+ val connector = createConnector(mockClient)
45+ connector.fetchData(" users" )
46+
47+ verify(mockCollection).find(Document ())
48+ }
49+
50+ @Test
51+ fun `fetchData applies JSON filter when whereClause provided` () {
52+ val mockCollection = mock<MongoCollection <Document >>()
53+ val mockDb = mock<MongoDatabase >()
54+ val mockClient = mock<MongoClient >()
55+ val findIterable = mockFindIterable()
56+ val expectedFilter = Document .parse(""" {"age": 18}""" )
57+
58+ whenever(mockClient.getDatabase(" testdb" )).thenReturn(mockDb)
59+ whenever(mockDb.getCollection(" users" )).thenReturn(mockCollection)
60+ whenever(mockCollection.find(any<Document >())).thenReturn(findIterable)
61+
62+ val connector = createConnector(mockClient)
63+ connector.fetchData(" users" , whereClause = """ {"age": 18}""" )
64+
65+ verify(mockCollection).find(expectedFilter)
66+ }
67+
68+ @Test
69+ fun `fetchData applies limit when specified` () {
70+ val mockCollection = mock<MongoCollection <Document >>()
71+ val mockDb = mock<MongoDatabase >()
72+ val mockClient = mock<MongoClient >()
73+ val findIterable = mockFindIterable()
74+
75+ whenever(mockClient.getDatabase(" testdb" )).thenReturn(mockDb)
76+ whenever(mockDb.getCollection(" users" )).thenReturn(mockCollection)
77+ whenever(mockCollection.find(any<Document >())).thenReturn(findIterable)
78+
79+ val connector = createConnector(mockClient)
80+ connector.fetchData(" users" , limit = 5 )
81+
82+ verify(findIterable).limit(5 )
83+ }
84+
1985 @Test
2086 fun `writeData calls insertMany and returns row count` () {
2187 val mockCollection = mock<MongoCollection <Document >>()
0 commit comments